OSDN Git Service

PR bootstrap/48148
[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_comp_dir_attribute (dw_die_ref);
6503 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6504 static void add_subscript_info (dw_die_ref, tree, bool);
6505 static void add_byte_size_attribute (dw_die_ref, tree);
6506 static void add_bit_offset_attribute (dw_die_ref, tree);
6507 static void add_bit_size_attribute (dw_die_ref, tree);
6508 static void add_prototyped_attribute (dw_die_ref, tree);
6509 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6510 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6511 static void add_src_coords_attributes (dw_die_ref, tree);
6512 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6513 static void push_decl_scope (tree);
6514 static void pop_decl_scope (void);
6515 static dw_die_ref scope_die_for (tree, dw_die_ref);
6516 static inline int local_scope_p (dw_die_ref);
6517 static inline int class_scope_p (dw_die_ref);
6518 static inline int class_or_namespace_scope_p (dw_die_ref);
6519 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6520 static void add_calling_convention_attribute (dw_die_ref, tree);
6521 static const char *type_tag (const_tree);
6522 static tree member_declared_type (const_tree);
6523 #if 0
6524 static const char *decl_start_label (tree);
6525 #endif
6526 static void gen_array_type_die (tree, dw_die_ref);
6527 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6528 #if 0
6529 static void gen_entry_point_die (tree, dw_die_ref);
6530 #endif
6531 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6532 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6533 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6534 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6535 static void gen_formal_types_die (tree, dw_die_ref);
6536 static void gen_subprogram_die (tree, dw_die_ref);
6537 static void gen_variable_die (tree, tree, dw_die_ref);
6538 static void gen_const_die (tree, dw_die_ref);
6539 static void gen_label_die (tree, dw_die_ref);
6540 static void gen_lexical_block_die (tree, dw_die_ref, int);
6541 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6542 static void gen_field_die (tree, dw_die_ref);
6543 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6544 static dw_die_ref gen_compile_unit_die (const char *);
6545 static void gen_inheritance_die (tree, tree, dw_die_ref);
6546 static void gen_member_die (tree, dw_die_ref);
6547 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6548                                                 enum debug_info_usage);
6549 static void gen_subroutine_type_die (tree, dw_die_ref);
6550 static void gen_typedef_die (tree, dw_die_ref);
6551 static void gen_type_die (tree, dw_die_ref);
6552 static void gen_block_die (tree, dw_die_ref, int);
6553 static void decls_for_scope (tree, dw_die_ref, int);
6554 static int is_redundant_typedef (const_tree);
6555 static bool is_naming_typedef_decl (const_tree);
6556 static inline dw_die_ref get_context_die (tree);
6557 static void gen_namespace_die (tree, dw_die_ref);
6558 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6559 static dw_die_ref force_decl_die (tree);
6560 static dw_die_ref force_type_die (tree);
6561 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6562 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6563 static struct dwarf_file_data * lookup_filename (const char *);
6564 static void retry_incomplete_types (void);
6565 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6566 static void gen_generic_params_dies (tree);
6567 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6568 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6569 static void splice_child_die (dw_die_ref, dw_die_ref);
6570 static int file_info_cmp (const void *, const void *);
6571 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6572                                      const char *, const char *);
6573 static void output_loc_list (dw_loc_list_ref);
6574 static char *gen_internal_sym (const char *);
6575
6576 static void prune_unmark_dies (dw_die_ref);
6577 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6578 static void prune_unused_types_mark (dw_die_ref, int);
6579 static void prune_unused_types_walk (dw_die_ref);
6580 static void prune_unused_types_walk_attribs (dw_die_ref);
6581 static void prune_unused_types_prune (dw_die_ref);
6582 static void prune_unused_types (void);
6583 static int maybe_emit_file (struct dwarf_file_data *fd);
6584 static inline const char *AT_vms_delta1 (dw_attr_ref);
6585 static inline const char *AT_vms_delta2 (dw_attr_ref);
6586 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6587                                      const char *, const char *);
6588 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6589 static void gen_remaining_tmpl_value_param_die_attribute (void);
6590 static bool generic_type_p (tree);
6591 static void schedule_generic_params_dies_gen (tree t);
6592 static void gen_scheduled_generic_parms_dies (void);
6593
6594 /* Section names used to hold DWARF debugging information.  */
6595 #ifndef DEBUG_INFO_SECTION
6596 #define DEBUG_INFO_SECTION      ".debug_info"
6597 #endif
6598 #ifndef DEBUG_ABBREV_SECTION
6599 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6600 #endif
6601 #ifndef DEBUG_ARANGES_SECTION
6602 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6603 #endif
6604 #ifndef DEBUG_MACINFO_SECTION
6605 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6606 #endif
6607 #ifndef DEBUG_LINE_SECTION
6608 #define DEBUG_LINE_SECTION      ".debug_line"
6609 #endif
6610 #ifndef DEBUG_LOC_SECTION
6611 #define DEBUG_LOC_SECTION       ".debug_loc"
6612 #endif
6613 #ifndef DEBUG_PUBNAMES_SECTION
6614 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6615 #endif
6616 #ifndef DEBUG_PUBTYPES_SECTION
6617 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6618 #endif
6619 #ifndef DEBUG_STR_SECTION
6620 #define DEBUG_STR_SECTION       ".debug_str"
6621 #endif
6622 #ifndef DEBUG_RANGES_SECTION
6623 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6624 #endif
6625
6626 /* Standard ELF section names for compiled code and data.  */
6627 #ifndef TEXT_SECTION_NAME
6628 #define TEXT_SECTION_NAME       ".text"
6629 #endif
6630
6631 /* Section flags for .debug_str section.  */
6632 #define DEBUG_STR_SECTION_FLAGS \
6633   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6634    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6635    : SECTION_DEBUG)
6636
6637 /* Labels we insert at beginning sections we can reference instead of
6638    the section names themselves.  */
6639
6640 #ifndef TEXT_SECTION_LABEL
6641 #define TEXT_SECTION_LABEL              "Ltext"
6642 #endif
6643 #ifndef COLD_TEXT_SECTION_LABEL
6644 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6645 #endif
6646 #ifndef DEBUG_LINE_SECTION_LABEL
6647 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6648 #endif
6649 #ifndef DEBUG_INFO_SECTION_LABEL
6650 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6651 #endif
6652 #ifndef DEBUG_ABBREV_SECTION_LABEL
6653 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6654 #endif
6655 #ifndef DEBUG_LOC_SECTION_LABEL
6656 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6657 #endif
6658 #ifndef DEBUG_RANGES_SECTION_LABEL
6659 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6660 #endif
6661 #ifndef DEBUG_MACINFO_SECTION_LABEL
6662 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6663 #endif
6664
6665
6666 /* Definitions of defaults for formats and names of various special
6667    (artificial) labels which may be generated within this file (when the -g
6668    options is used and DWARF2_DEBUGGING_INFO is in effect.
6669    If necessary, these may be overridden from within the tm.h file, but
6670    typically, overriding these defaults is unnecessary.  */
6671
6672 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6673 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6674 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6675 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6676 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6677 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6678 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6679 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6680 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6681 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6682
6683 #ifndef TEXT_END_LABEL
6684 #define TEXT_END_LABEL          "Letext"
6685 #endif
6686 #ifndef COLD_END_LABEL
6687 #define COLD_END_LABEL          "Letext_cold"
6688 #endif
6689 #ifndef BLOCK_BEGIN_LABEL
6690 #define BLOCK_BEGIN_LABEL       "LBB"
6691 #endif
6692 #ifndef BLOCK_END_LABEL
6693 #define BLOCK_END_LABEL         "LBE"
6694 #endif
6695 #ifndef LINE_CODE_LABEL
6696 #define LINE_CODE_LABEL         "LM"
6697 #endif
6698
6699 \f
6700 /* Return the root of the DIE's built for the current compilation unit.  */
6701 static dw_die_ref
6702 comp_unit_die (void)
6703 {
6704   if (!single_comp_unit_die)
6705     single_comp_unit_die = gen_compile_unit_die (NULL);
6706   return single_comp_unit_die;
6707 }
6708
6709 /* We allow a language front-end to designate a function that is to be
6710    called to "demangle" any name before it is put into a DIE.  */
6711
6712 static const char *(*demangle_name_func) (const char *);
6713
6714 void
6715 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6716 {
6717   demangle_name_func = func;
6718 }
6719
6720 /* Test if rtl node points to a pseudo register.  */
6721
6722 static inline int
6723 is_pseudo_reg (const_rtx rtl)
6724 {
6725   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6726           || (GET_CODE (rtl) == SUBREG
6727               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6728 }
6729
6730 /* Return a reference to a type, with its const and volatile qualifiers
6731    removed.  */
6732
6733 static inline tree
6734 type_main_variant (tree type)
6735 {
6736   type = TYPE_MAIN_VARIANT (type);
6737
6738   /* ??? There really should be only one main variant among any group of
6739      variants of a given type (and all of the MAIN_VARIANT values for all
6740      members of the group should point to that one type) but sometimes the C
6741      front-end messes this up for array types, so we work around that bug
6742      here.  */
6743   if (TREE_CODE (type) == ARRAY_TYPE)
6744     while (type != TYPE_MAIN_VARIANT (type))
6745       type = TYPE_MAIN_VARIANT (type);
6746
6747   return type;
6748 }
6749
6750 /* Return nonzero if the given type node represents a tagged type.  */
6751
6752 static inline int
6753 is_tagged_type (const_tree type)
6754 {
6755   enum tree_code code = TREE_CODE (type);
6756
6757   return (code == RECORD_TYPE || code == UNION_TYPE
6758           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6759 }
6760
6761 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6762
6763 static void
6764 get_ref_die_offset_label (char *label, dw_die_ref ref)
6765 {
6766   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6767 }
6768
6769 /* Convert a DIE tag into its string name.  */
6770
6771 static const char *
6772 dwarf_tag_name (unsigned int tag)
6773 {
6774   switch (tag)
6775     {
6776     case DW_TAG_padding:
6777       return "DW_TAG_padding";
6778     case DW_TAG_array_type:
6779       return "DW_TAG_array_type";
6780     case DW_TAG_class_type:
6781       return "DW_TAG_class_type";
6782     case DW_TAG_entry_point:
6783       return "DW_TAG_entry_point";
6784     case DW_TAG_enumeration_type:
6785       return "DW_TAG_enumeration_type";
6786     case DW_TAG_formal_parameter:
6787       return "DW_TAG_formal_parameter";
6788     case DW_TAG_imported_declaration:
6789       return "DW_TAG_imported_declaration";
6790     case DW_TAG_label:
6791       return "DW_TAG_label";
6792     case DW_TAG_lexical_block:
6793       return "DW_TAG_lexical_block";
6794     case DW_TAG_member:
6795       return "DW_TAG_member";
6796     case DW_TAG_pointer_type:
6797       return "DW_TAG_pointer_type";
6798     case DW_TAG_reference_type:
6799       return "DW_TAG_reference_type";
6800     case DW_TAG_compile_unit:
6801       return "DW_TAG_compile_unit";
6802     case DW_TAG_string_type:
6803       return "DW_TAG_string_type";
6804     case DW_TAG_structure_type:
6805       return "DW_TAG_structure_type";
6806     case DW_TAG_subroutine_type:
6807       return "DW_TAG_subroutine_type";
6808     case DW_TAG_typedef:
6809       return "DW_TAG_typedef";
6810     case DW_TAG_union_type:
6811       return "DW_TAG_union_type";
6812     case DW_TAG_unspecified_parameters:
6813       return "DW_TAG_unspecified_parameters";
6814     case DW_TAG_variant:
6815       return "DW_TAG_variant";
6816     case DW_TAG_common_block:
6817       return "DW_TAG_common_block";
6818     case DW_TAG_common_inclusion:
6819       return "DW_TAG_common_inclusion";
6820     case DW_TAG_inheritance:
6821       return "DW_TAG_inheritance";
6822     case DW_TAG_inlined_subroutine:
6823       return "DW_TAG_inlined_subroutine";
6824     case DW_TAG_module:
6825       return "DW_TAG_module";
6826     case DW_TAG_ptr_to_member_type:
6827       return "DW_TAG_ptr_to_member_type";
6828     case DW_TAG_set_type:
6829       return "DW_TAG_set_type";
6830     case DW_TAG_subrange_type:
6831       return "DW_TAG_subrange_type";
6832     case DW_TAG_with_stmt:
6833       return "DW_TAG_with_stmt";
6834     case DW_TAG_access_declaration:
6835       return "DW_TAG_access_declaration";
6836     case DW_TAG_base_type:
6837       return "DW_TAG_base_type";
6838     case DW_TAG_catch_block:
6839       return "DW_TAG_catch_block";
6840     case DW_TAG_const_type:
6841       return "DW_TAG_const_type";
6842     case DW_TAG_constant:
6843       return "DW_TAG_constant";
6844     case DW_TAG_enumerator:
6845       return "DW_TAG_enumerator";
6846     case DW_TAG_file_type:
6847       return "DW_TAG_file_type";
6848     case DW_TAG_friend:
6849       return "DW_TAG_friend";
6850     case DW_TAG_namelist:
6851       return "DW_TAG_namelist";
6852     case DW_TAG_namelist_item:
6853       return "DW_TAG_namelist_item";
6854     case DW_TAG_packed_type:
6855       return "DW_TAG_packed_type";
6856     case DW_TAG_subprogram:
6857       return "DW_TAG_subprogram";
6858     case DW_TAG_template_type_param:
6859       return "DW_TAG_template_type_param";
6860     case DW_TAG_template_value_param:
6861       return "DW_TAG_template_value_param";
6862     case DW_TAG_thrown_type:
6863       return "DW_TAG_thrown_type";
6864     case DW_TAG_try_block:
6865       return "DW_TAG_try_block";
6866     case DW_TAG_variant_part:
6867       return "DW_TAG_variant_part";
6868     case DW_TAG_variable:
6869       return "DW_TAG_variable";
6870     case DW_TAG_volatile_type:
6871       return "DW_TAG_volatile_type";
6872     case DW_TAG_dwarf_procedure:
6873       return "DW_TAG_dwarf_procedure";
6874     case DW_TAG_restrict_type:
6875       return "DW_TAG_restrict_type";
6876     case DW_TAG_interface_type:
6877       return "DW_TAG_interface_type";
6878     case DW_TAG_namespace:
6879       return "DW_TAG_namespace";
6880     case DW_TAG_imported_module:
6881       return "DW_TAG_imported_module";
6882     case DW_TAG_unspecified_type:
6883       return "DW_TAG_unspecified_type";
6884     case DW_TAG_partial_unit:
6885       return "DW_TAG_partial_unit";
6886     case DW_TAG_imported_unit:
6887       return "DW_TAG_imported_unit";
6888     case DW_TAG_condition:
6889       return "DW_TAG_condition";
6890     case DW_TAG_shared_type:
6891       return "DW_TAG_shared_type";
6892     case DW_TAG_type_unit:
6893       return "DW_TAG_type_unit";
6894     case DW_TAG_rvalue_reference_type:
6895       return "DW_TAG_rvalue_reference_type";
6896     case DW_TAG_template_alias:
6897       return "DW_TAG_template_alias";
6898     case DW_TAG_GNU_template_parameter_pack:
6899       return "DW_TAG_GNU_template_parameter_pack";
6900     case DW_TAG_GNU_formal_parameter_pack:
6901       return "DW_TAG_GNU_formal_parameter_pack";
6902     case DW_TAG_MIPS_loop:
6903       return "DW_TAG_MIPS_loop";
6904     case DW_TAG_format_label:
6905       return "DW_TAG_format_label";
6906     case DW_TAG_function_template:
6907       return "DW_TAG_function_template";
6908     case DW_TAG_class_template:
6909       return "DW_TAG_class_template";
6910     case DW_TAG_GNU_BINCL:
6911       return "DW_TAG_GNU_BINCL";
6912     case DW_TAG_GNU_EINCL:
6913       return "DW_TAG_GNU_EINCL";
6914     case DW_TAG_GNU_template_template_param:
6915       return "DW_TAG_GNU_template_template_param";
6916     case DW_TAG_GNU_call_site:
6917       return "DW_TAG_GNU_call_site";
6918     case DW_TAG_GNU_call_site_parameter:
6919       return "DW_TAG_GNU_call_site_parameter";
6920     default:
6921       return "DW_TAG_<unknown>";
6922     }
6923 }
6924
6925 /* Convert a DWARF attribute code into its string name.  */
6926
6927 static const char *
6928 dwarf_attr_name (unsigned int attr)
6929 {
6930   switch (attr)
6931     {
6932     case DW_AT_sibling:
6933       return "DW_AT_sibling";
6934     case DW_AT_location:
6935       return "DW_AT_location";
6936     case DW_AT_name:
6937       return "DW_AT_name";
6938     case DW_AT_ordering:
6939       return "DW_AT_ordering";
6940     case DW_AT_subscr_data:
6941       return "DW_AT_subscr_data";
6942     case DW_AT_byte_size:
6943       return "DW_AT_byte_size";
6944     case DW_AT_bit_offset:
6945       return "DW_AT_bit_offset";
6946     case DW_AT_bit_size:
6947       return "DW_AT_bit_size";
6948     case DW_AT_element_list:
6949       return "DW_AT_element_list";
6950     case DW_AT_stmt_list:
6951       return "DW_AT_stmt_list";
6952     case DW_AT_low_pc:
6953       return "DW_AT_low_pc";
6954     case DW_AT_high_pc:
6955       return "DW_AT_high_pc";
6956     case DW_AT_language:
6957       return "DW_AT_language";
6958     case DW_AT_member:
6959       return "DW_AT_member";
6960     case DW_AT_discr:
6961       return "DW_AT_discr";
6962     case DW_AT_discr_value:
6963       return "DW_AT_discr_value";
6964     case DW_AT_visibility:
6965       return "DW_AT_visibility";
6966     case DW_AT_import:
6967       return "DW_AT_import";
6968     case DW_AT_string_length:
6969       return "DW_AT_string_length";
6970     case DW_AT_common_reference:
6971       return "DW_AT_common_reference";
6972     case DW_AT_comp_dir:
6973       return "DW_AT_comp_dir";
6974     case DW_AT_const_value:
6975       return "DW_AT_const_value";
6976     case DW_AT_containing_type:
6977       return "DW_AT_containing_type";
6978     case DW_AT_default_value:
6979       return "DW_AT_default_value";
6980     case DW_AT_inline:
6981       return "DW_AT_inline";
6982     case DW_AT_is_optional:
6983       return "DW_AT_is_optional";
6984     case DW_AT_lower_bound:
6985       return "DW_AT_lower_bound";
6986     case DW_AT_producer:
6987       return "DW_AT_producer";
6988     case DW_AT_prototyped:
6989       return "DW_AT_prototyped";
6990     case DW_AT_return_addr:
6991       return "DW_AT_return_addr";
6992     case DW_AT_start_scope:
6993       return "DW_AT_start_scope";
6994     case DW_AT_bit_stride:
6995       return "DW_AT_bit_stride";
6996     case DW_AT_upper_bound:
6997       return "DW_AT_upper_bound";
6998     case DW_AT_abstract_origin:
6999       return "DW_AT_abstract_origin";
7000     case DW_AT_accessibility:
7001       return "DW_AT_accessibility";
7002     case DW_AT_address_class:
7003       return "DW_AT_address_class";
7004     case DW_AT_artificial:
7005       return "DW_AT_artificial";
7006     case DW_AT_base_types:
7007       return "DW_AT_base_types";
7008     case DW_AT_calling_convention:
7009       return "DW_AT_calling_convention";
7010     case DW_AT_count:
7011       return "DW_AT_count";
7012     case DW_AT_data_member_location:
7013       return "DW_AT_data_member_location";
7014     case DW_AT_decl_column:
7015       return "DW_AT_decl_column";
7016     case DW_AT_decl_file:
7017       return "DW_AT_decl_file";
7018     case DW_AT_decl_line:
7019       return "DW_AT_decl_line";
7020     case DW_AT_declaration:
7021       return "DW_AT_declaration";
7022     case DW_AT_discr_list:
7023       return "DW_AT_discr_list";
7024     case DW_AT_encoding:
7025       return "DW_AT_encoding";
7026     case DW_AT_external:
7027       return "DW_AT_external";
7028     case DW_AT_explicit:
7029       return "DW_AT_explicit";
7030     case DW_AT_frame_base:
7031       return "DW_AT_frame_base";
7032     case DW_AT_friend:
7033       return "DW_AT_friend";
7034     case DW_AT_identifier_case:
7035       return "DW_AT_identifier_case";
7036     case DW_AT_macro_info:
7037       return "DW_AT_macro_info";
7038     case DW_AT_namelist_items:
7039       return "DW_AT_namelist_items";
7040     case DW_AT_priority:
7041       return "DW_AT_priority";
7042     case DW_AT_segment:
7043       return "DW_AT_segment";
7044     case DW_AT_specification:
7045       return "DW_AT_specification";
7046     case DW_AT_static_link:
7047       return "DW_AT_static_link";
7048     case DW_AT_type:
7049       return "DW_AT_type";
7050     case DW_AT_use_location:
7051       return "DW_AT_use_location";
7052     case DW_AT_variable_parameter:
7053       return "DW_AT_variable_parameter";
7054     case DW_AT_virtuality:
7055       return "DW_AT_virtuality";
7056     case DW_AT_vtable_elem_location:
7057       return "DW_AT_vtable_elem_location";
7058
7059     case DW_AT_allocated:
7060       return "DW_AT_allocated";
7061     case DW_AT_associated:
7062       return "DW_AT_associated";
7063     case DW_AT_data_location:
7064       return "DW_AT_data_location";
7065     case DW_AT_byte_stride:
7066       return "DW_AT_byte_stride";
7067     case DW_AT_entry_pc:
7068       return "DW_AT_entry_pc";
7069     case DW_AT_use_UTF8:
7070       return "DW_AT_use_UTF8";
7071     case DW_AT_extension:
7072       return "DW_AT_extension";
7073     case DW_AT_ranges:
7074       return "DW_AT_ranges";
7075     case DW_AT_trampoline:
7076       return "DW_AT_trampoline";
7077     case DW_AT_call_column:
7078       return "DW_AT_call_column";
7079     case DW_AT_call_file:
7080       return "DW_AT_call_file";
7081     case DW_AT_call_line:
7082       return "DW_AT_call_line";
7083     case DW_AT_object_pointer:
7084       return "DW_AT_object_pointer";
7085
7086     case DW_AT_signature:
7087       return "DW_AT_signature";
7088     case DW_AT_main_subprogram:
7089       return "DW_AT_main_subprogram";
7090     case DW_AT_data_bit_offset:
7091       return "DW_AT_data_bit_offset";
7092     case DW_AT_const_expr:
7093       return "DW_AT_const_expr";
7094     case DW_AT_enum_class:
7095       return "DW_AT_enum_class";
7096     case DW_AT_linkage_name:
7097       return "DW_AT_linkage_name";
7098
7099     case DW_AT_MIPS_fde:
7100       return "DW_AT_MIPS_fde";
7101     case DW_AT_MIPS_loop_begin:
7102       return "DW_AT_MIPS_loop_begin";
7103     case DW_AT_MIPS_tail_loop_begin:
7104       return "DW_AT_MIPS_tail_loop_begin";
7105     case DW_AT_MIPS_epilog_begin:
7106       return "DW_AT_MIPS_epilog_begin";
7107 #if VMS_DEBUGGING_INFO
7108     case DW_AT_HP_prologue:
7109       return "DW_AT_HP_prologue";
7110 #else
7111     case DW_AT_MIPS_loop_unroll_factor:
7112       return "DW_AT_MIPS_loop_unroll_factor";
7113 #endif
7114     case DW_AT_MIPS_software_pipeline_depth:
7115       return "DW_AT_MIPS_software_pipeline_depth";
7116     case DW_AT_MIPS_linkage_name:
7117       return "DW_AT_MIPS_linkage_name";
7118 #if VMS_DEBUGGING_INFO
7119     case DW_AT_HP_epilogue:
7120       return "DW_AT_HP_epilogue";
7121 #else
7122     case DW_AT_MIPS_stride:
7123       return "DW_AT_MIPS_stride";
7124 #endif
7125     case DW_AT_MIPS_abstract_name:
7126       return "DW_AT_MIPS_abstract_name";
7127     case DW_AT_MIPS_clone_origin:
7128       return "DW_AT_MIPS_clone_origin";
7129     case DW_AT_MIPS_has_inlines:
7130       return "DW_AT_MIPS_has_inlines";
7131
7132     case DW_AT_sf_names:
7133       return "DW_AT_sf_names";
7134     case DW_AT_src_info:
7135       return "DW_AT_src_info";
7136     case DW_AT_mac_info:
7137       return "DW_AT_mac_info";
7138     case DW_AT_src_coords:
7139       return "DW_AT_src_coords";
7140     case DW_AT_body_begin:
7141       return "DW_AT_body_begin";
7142     case DW_AT_body_end:
7143       return "DW_AT_body_end";
7144     case DW_AT_GNU_vector:
7145       return "DW_AT_GNU_vector";
7146     case DW_AT_GNU_guarded_by:
7147       return "DW_AT_GNU_guarded_by";
7148     case DW_AT_GNU_pt_guarded_by:
7149       return "DW_AT_GNU_pt_guarded_by";
7150     case DW_AT_GNU_guarded:
7151       return "DW_AT_GNU_guarded";
7152     case DW_AT_GNU_pt_guarded:
7153       return "DW_AT_GNU_pt_guarded";
7154     case DW_AT_GNU_locks_excluded:
7155       return "DW_AT_GNU_locks_excluded";
7156     case DW_AT_GNU_exclusive_locks_required:
7157       return "DW_AT_GNU_exclusive_locks_required";
7158     case DW_AT_GNU_shared_locks_required:
7159       return "DW_AT_GNU_shared_locks_required";
7160     case DW_AT_GNU_odr_signature:
7161       return "DW_AT_GNU_odr_signature";
7162     case DW_AT_GNU_template_name:
7163       return "DW_AT_GNU_template_name";
7164     case DW_AT_GNU_call_site_value:
7165       return "DW_AT_GNU_call_site_value";
7166     case DW_AT_GNU_call_site_data_value:
7167       return "DW_AT_GNU_call_site_data_value";
7168     case DW_AT_GNU_call_site_target:
7169       return "DW_AT_GNU_call_site_target";
7170     case DW_AT_GNU_call_site_target_clobbered:
7171       return "DW_AT_GNU_call_site_target_clobbered";
7172     case DW_AT_GNU_tail_call:
7173       return "DW_AT_GNU_tail_call";
7174     case DW_AT_GNU_all_tail_call_sites:
7175       return "DW_AT_GNU_all_tail_call_sites";
7176     case DW_AT_GNU_all_call_sites:
7177       return "DW_AT_GNU_all_call_sites";
7178     case DW_AT_GNU_all_source_call_sites:
7179       return "DW_AT_GNU_all_source_call_sites";
7180
7181     case DW_AT_VMS_rtnbeg_pd_address:
7182       return "DW_AT_VMS_rtnbeg_pd_address";
7183
7184     default:
7185       return "DW_AT_<unknown>";
7186     }
7187 }
7188
7189 /* Convert a DWARF value form code into its string name.  */
7190
7191 static const char *
7192 dwarf_form_name (unsigned int form)
7193 {
7194   switch (form)
7195     {
7196     case DW_FORM_addr:
7197       return "DW_FORM_addr";
7198     case DW_FORM_block2:
7199       return "DW_FORM_block2";
7200     case DW_FORM_block4:
7201       return "DW_FORM_block4";
7202     case DW_FORM_data2:
7203       return "DW_FORM_data2";
7204     case DW_FORM_data4:
7205       return "DW_FORM_data4";
7206     case DW_FORM_data8:
7207       return "DW_FORM_data8";
7208     case DW_FORM_string:
7209       return "DW_FORM_string";
7210     case DW_FORM_block:
7211       return "DW_FORM_block";
7212     case DW_FORM_block1:
7213       return "DW_FORM_block1";
7214     case DW_FORM_data1:
7215       return "DW_FORM_data1";
7216     case DW_FORM_flag:
7217       return "DW_FORM_flag";
7218     case DW_FORM_sdata:
7219       return "DW_FORM_sdata";
7220     case DW_FORM_strp:
7221       return "DW_FORM_strp";
7222     case DW_FORM_udata:
7223       return "DW_FORM_udata";
7224     case DW_FORM_ref_addr:
7225       return "DW_FORM_ref_addr";
7226     case DW_FORM_ref1:
7227       return "DW_FORM_ref1";
7228     case DW_FORM_ref2:
7229       return "DW_FORM_ref2";
7230     case DW_FORM_ref4:
7231       return "DW_FORM_ref4";
7232     case DW_FORM_ref8:
7233       return "DW_FORM_ref8";
7234     case DW_FORM_ref_udata:
7235       return "DW_FORM_ref_udata";
7236     case DW_FORM_indirect:
7237       return "DW_FORM_indirect";
7238     case DW_FORM_sec_offset:
7239       return "DW_FORM_sec_offset";
7240     case DW_FORM_exprloc:
7241       return "DW_FORM_exprloc";
7242     case DW_FORM_flag_present:
7243       return "DW_FORM_flag_present";
7244     case DW_FORM_ref_sig8:
7245       return "DW_FORM_ref_sig8";
7246     default:
7247       return "DW_FORM_<unknown>";
7248     }
7249 }
7250 \f
7251 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7252    instance of an inlined instance of a decl which is local to an inline
7253    function, so we have to trace all of the way back through the origin chain
7254    to find out what sort of node actually served as the original seed for the
7255    given block.  */
7256
7257 static tree
7258 decl_ultimate_origin (const_tree decl)
7259 {
7260   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7261     return NULL_TREE;
7262
7263   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7264      nodes in the function to point to themselves; ignore that if
7265      we're trying to output the abstract instance of this function.  */
7266   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7267     return NULL_TREE;
7268
7269   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7270      most distant ancestor, this should never happen.  */
7271   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7272
7273   return DECL_ABSTRACT_ORIGIN (decl);
7274 }
7275
7276 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7277    of a virtual function may refer to a base class, so we check the 'this'
7278    parameter.  */
7279
7280 static tree
7281 decl_class_context (tree decl)
7282 {
7283   tree context = NULL_TREE;
7284
7285   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7286     context = DECL_CONTEXT (decl);
7287   else
7288     context = TYPE_MAIN_VARIANT
7289       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7290
7291   if (context && !TYPE_P (context))
7292     context = NULL_TREE;
7293
7294   return context;
7295 }
7296 \f
7297 /* Add an attribute/value pair to a DIE.  */
7298
7299 static inline void
7300 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7301 {
7302   /* Maybe this should be an assert?  */
7303   if (die == NULL)
7304     return;
7305
7306   if (die->die_attr == NULL)
7307     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7308   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7309 }
7310
7311 static inline enum dw_val_class
7312 AT_class (dw_attr_ref a)
7313 {
7314   return a->dw_attr_val.val_class;
7315 }
7316
7317 /* Add a flag value attribute to a DIE.  */
7318
7319 static inline void
7320 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7321 {
7322   dw_attr_node attr;
7323
7324   attr.dw_attr = attr_kind;
7325   attr.dw_attr_val.val_class = dw_val_class_flag;
7326   attr.dw_attr_val.v.val_flag = flag;
7327   add_dwarf_attr (die, &attr);
7328 }
7329
7330 static inline unsigned
7331 AT_flag (dw_attr_ref a)
7332 {
7333   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7334   return a->dw_attr_val.v.val_flag;
7335 }
7336
7337 /* Add a signed integer attribute value to a DIE.  */
7338
7339 static inline void
7340 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7341 {
7342   dw_attr_node attr;
7343
7344   attr.dw_attr = attr_kind;
7345   attr.dw_attr_val.val_class = dw_val_class_const;
7346   attr.dw_attr_val.v.val_int = int_val;
7347   add_dwarf_attr (die, &attr);
7348 }
7349
7350 static inline HOST_WIDE_INT
7351 AT_int (dw_attr_ref a)
7352 {
7353   gcc_assert (a && AT_class (a) == dw_val_class_const);
7354   return a->dw_attr_val.v.val_int;
7355 }
7356
7357 /* Add an unsigned integer attribute value to a DIE.  */
7358
7359 static inline void
7360 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7361                  unsigned HOST_WIDE_INT unsigned_val)
7362 {
7363   dw_attr_node attr;
7364
7365   attr.dw_attr = attr_kind;
7366   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7367   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7368   add_dwarf_attr (die, &attr);
7369 }
7370
7371 static inline unsigned HOST_WIDE_INT
7372 AT_unsigned (dw_attr_ref a)
7373 {
7374   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7375   return a->dw_attr_val.v.val_unsigned;
7376 }
7377
7378 /* Add an unsigned double integer attribute value to a DIE.  */
7379
7380 static inline void
7381 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7382                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7383 {
7384   dw_attr_node attr;
7385
7386   attr.dw_attr = attr_kind;
7387   attr.dw_attr_val.val_class = dw_val_class_const_double;
7388   attr.dw_attr_val.v.val_double.high = high;
7389   attr.dw_attr_val.v.val_double.low = low;
7390   add_dwarf_attr (die, &attr);
7391 }
7392
7393 /* Add a floating point attribute value to a DIE and return it.  */
7394
7395 static inline void
7396 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7397             unsigned int length, unsigned int elt_size, unsigned char *array)
7398 {
7399   dw_attr_node attr;
7400
7401   attr.dw_attr = attr_kind;
7402   attr.dw_attr_val.val_class = dw_val_class_vec;
7403   attr.dw_attr_val.v.val_vec.length = length;
7404   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7405   attr.dw_attr_val.v.val_vec.array = array;
7406   add_dwarf_attr (die, &attr);
7407 }
7408
7409 /* Add an 8-byte data attribute value to a DIE.  */
7410
7411 static inline void
7412 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7413               unsigned char data8[8])
7414 {
7415   dw_attr_node attr;
7416
7417   attr.dw_attr = attr_kind;
7418   attr.dw_attr_val.val_class = dw_val_class_data8;
7419   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7420   add_dwarf_attr (die, &attr);
7421 }
7422
7423 /* Hash and equality functions for debug_str_hash.  */
7424
7425 static hashval_t
7426 debug_str_do_hash (const void *x)
7427 {
7428   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7429 }
7430
7431 static int
7432 debug_str_eq (const void *x1, const void *x2)
7433 {
7434   return strcmp ((((const struct indirect_string_node *)x1)->str),
7435                  (const char *)x2) == 0;
7436 }
7437
7438 /* Add STR to the indirect string hash table.  */
7439
7440 static struct indirect_string_node *
7441 find_AT_string (const char *str)
7442 {
7443   struct indirect_string_node *node;
7444   void **slot;
7445
7446   if (! debug_str_hash)
7447     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7448                                       debug_str_eq, NULL);
7449
7450   slot = htab_find_slot_with_hash (debug_str_hash, str,
7451                                    htab_hash_string (str), INSERT);
7452   if (*slot == NULL)
7453     {
7454       node = ggc_alloc_cleared_indirect_string_node ();
7455       node->str = ggc_strdup (str);
7456       *slot = node;
7457     }
7458   else
7459     node = (struct indirect_string_node *) *slot;
7460
7461   node->refcount++;
7462   return node;
7463 }
7464
7465 /* Add a string attribute value to a DIE.  */
7466
7467 static inline void
7468 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7469 {
7470   dw_attr_node attr;
7471   struct indirect_string_node *node;
7472
7473   node = find_AT_string (str);
7474
7475   attr.dw_attr = attr_kind;
7476   attr.dw_attr_val.val_class = dw_val_class_str;
7477   attr.dw_attr_val.v.val_str = node;
7478   add_dwarf_attr (die, &attr);
7479 }
7480
7481 /* Create a label for an indirect string node, ensuring it is going to
7482    be output, unless its reference count goes down to zero.  */
7483
7484 static inline void
7485 gen_label_for_indirect_string (struct indirect_string_node *node)
7486 {
7487   char label[32];
7488
7489   if (node->label)
7490     return;
7491
7492   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7493   ++dw2_string_counter;
7494   node->label = xstrdup (label);
7495 }
7496
7497 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7498    debug string STR.  */
7499
7500 static inline rtx
7501 get_debug_string_label (const char *str)
7502 {
7503   struct indirect_string_node *node = find_AT_string (str);
7504
7505   debug_str_hash_forced = true;
7506
7507   gen_label_for_indirect_string (node);
7508
7509   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7510 }
7511
7512 static inline const char *
7513 AT_string (dw_attr_ref a)
7514 {
7515   gcc_assert (a && AT_class (a) == dw_val_class_str);
7516   return a->dw_attr_val.v.val_str->str;
7517 }
7518
7519 /* Find out whether a string should be output inline in DIE
7520    or out-of-line in .debug_str section.  */
7521
7522 static enum dwarf_form
7523 AT_string_form (dw_attr_ref a)
7524 {
7525   struct indirect_string_node *node;
7526   unsigned int len;
7527
7528   gcc_assert (a && AT_class (a) == dw_val_class_str);
7529
7530   node = a->dw_attr_val.v.val_str;
7531   if (node->form)
7532     return node->form;
7533
7534   len = strlen (node->str) + 1;
7535
7536   /* If the string is shorter or equal to the size of the reference, it is
7537      always better to put it inline.  */
7538   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7539     return node->form = DW_FORM_string;
7540
7541   /* If we cannot expect the linker to merge strings in .debug_str
7542      section, only put it into .debug_str if it is worth even in this
7543      single module.  */
7544   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7545       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7546       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7547     return node->form = DW_FORM_string;
7548
7549   gen_label_for_indirect_string (node);
7550
7551   return node->form = DW_FORM_strp;
7552 }
7553
7554 /* Add a DIE reference attribute value to a DIE.  */
7555
7556 static inline void
7557 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7558 {
7559   dw_attr_node attr;
7560
7561 #ifdef ENABLE_CHECKING
7562   gcc_assert (targ_die != NULL);
7563 #else
7564   /* With LTO we can end up trying to reference something we didn't create
7565      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7566   if (targ_die == NULL)
7567     return;
7568 #endif
7569
7570   attr.dw_attr = attr_kind;
7571   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7572   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7573   attr.dw_attr_val.v.val_die_ref.external = 0;
7574   add_dwarf_attr (die, &attr);
7575 }
7576
7577 /* Add an AT_specification attribute to a DIE, and also make the back
7578    pointer from the specification to the definition.  */
7579
7580 static inline void
7581 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7582 {
7583   add_AT_die_ref (die, DW_AT_specification, targ_die);
7584   gcc_assert (!targ_die->die_definition);
7585   targ_die->die_definition = die;
7586 }
7587
7588 static inline dw_die_ref
7589 AT_ref (dw_attr_ref a)
7590 {
7591   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7592   return a->dw_attr_val.v.val_die_ref.die;
7593 }
7594
7595 static inline int
7596 AT_ref_external (dw_attr_ref a)
7597 {
7598   if (a && AT_class (a) == dw_val_class_die_ref)
7599     return a->dw_attr_val.v.val_die_ref.external;
7600
7601   return 0;
7602 }
7603
7604 static inline void
7605 set_AT_ref_external (dw_attr_ref a, int i)
7606 {
7607   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7608   a->dw_attr_val.v.val_die_ref.external = i;
7609 }
7610
7611 /* Add an FDE reference attribute value to a DIE.  */
7612
7613 static inline void
7614 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7615 {
7616   dw_attr_node attr;
7617
7618   attr.dw_attr = attr_kind;
7619   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7620   attr.dw_attr_val.v.val_fde_index = targ_fde;
7621   add_dwarf_attr (die, &attr);
7622 }
7623
7624 /* Add a location description attribute value to a DIE.  */
7625
7626 static inline void
7627 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7628 {
7629   dw_attr_node attr;
7630
7631   attr.dw_attr = attr_kind;
7632   attr.dw_attr_val.val_class = dw_val_class_loc;
7633   attr.dw_attr_val.v.val_loc = loc;
7634   add_dwarf_attr (die, &attr);
7635 }
7636
7637 static inline dw_loc_descr_ref
7638 AT_loc (dw_attr_ref a)
7639 {
7640   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7641   return a->dw_attr_val.v.val_loc;
7642 }
7643
7644 static inline void
7645 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7646 {
7647   dw_attr_node attr;
7648
7649   attr.dw_attr = attr_kind;
7650   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7651   attr.dw_attr_val.v.val_loc_list = loc_list;
7652   add_dwarf_attr (die, &attr);
7653   have_location_lists = true;
7654 }
7655
7656 static inline dw_loc_list_ref
7657 AT_loc_list (dw_attr_ref a)
7658 {
7659   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7660   return a->dw_attr_val.v.val_loc_list;
7661 }
7662
7663 static inline dw_loc_list_ref *
7664 AT_loc_list_ptr (dw_attr_ref a)
7665 {
7666   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7667   return &a->dw_attr_val.v.val_loc_list;
7668 }
7669
7670 /* Add an address constant attribute value to a DIE.  */
7671
7672 static inline void
7673 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7674 {
7675   dw_attr_node attr;
7676
7677   attr.dw_attr = attr_kind;
7678   attr.dw_attr_val.val_class = dw_val_class_addr;
7679   attr.dw_attr_val.v.val_addr = addr;
7680   add_dwarf_attr (die, &attr);
7681 }
7682
7683 /* Get the RTX from to an address DIE attribute.  */
7684
7685 static inline rtx
7686 AT_addr (dw_attr_ref a)
7687 {
7688   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7689   return a->dw_attr_val.v.val_addr;
7690 }
7691
7692 /* Add a file attribute value to a DIE.  */
7693
7694 static inline void
7695 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7696              struct dwarf_file_data *fd)
7697 {
7698   dw_attr_node attr;
7699
7700   attr.dw_attr = attr_kind;
7701   attr.dw_attr_val.val_class = dw_val_class_file;
7702   attr.dw_attr_val.v.val_file = fd;
7703   add_dwarf_attr (die, &attr);
7704 }
7705
7706 /* Get the dwarf_file_data from a file DIE attribute.  */
7707
7708 static inline struct dwarf_file_data *
7709 AT_file (dw_attr_ref a)
7710 {
7711   gcc_assert (a && AT_class (a) == dw_val_class_file);
7712   return a->dw_attr_val.v.val_file;
7713 }
7714
7715 /* Add a vms delta attribute value to a DIE.  */
7716
7717 static inline void
7718 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7719                   const char *lbl1, const char *lbl2)
7720 {
7721   dw_attr_node attr;
7722
7723   attr.dw_attr = attr_kind;
7724   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7725   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7726   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7727   add_dwarf_attr (die, &attr);
7728 }
7729
7730 /* Add a label identifier attribute value to a DIE.  */
7731
7732 static inline void
7733 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7734 {
7735   dw_attr_node attr;
7736
7737   attr.dw_attr = attr_kind;
7738   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7739   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7740   add_dwarf_attr (die, &attr);
7741 }
7742
7743 /* Add a section offset attribute value to a DIE, an offset into the
7744    debug_line section.  */
7745
7746 static inline void
7747 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7748                 const char *label)
7749 {
7750   dw_attr_node attr;
7751
7752   attr.dw_attr = attr_kind;
7753   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7754   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7755   add_dwarf_attr (die, &attr);
7756 }
7757
7758 /* Add a section offset attribute value to a DIE, an offset into the
7759    debug_macinfo section.  */
7760
7761 static inline void
7762 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7763                const char *label)
7764 {
7765   dw_attr_node attr;
7766
7767   attr.dw_attr = attr_kind;
7768   attr.dw_attr_val.val_class = dw_val_class_macptr;
7769   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7770   add_dwarf_attr (die, &attr);
7771 }
7772
7773 /* Add an offset attribute value to a DIE.  */
7774
7775 static inline void
7776 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7777                unsigned HOST_WIDE_INT offset)
7778 {
7779   dw_attr_node attr;
7780
7781   attr.dw_attr = attr_kind;
7782   attr.dw_attr_val.val_class = dw_val_class_offset;
7783   attr.dw_attr_val.v.val_offset = offset;
7784   add_dwarf_attr (die, &attr);
7785 }
7786
7787 /* Add an range_list attribute value to a DIE.  */
7788
7789 static void
7790 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7791                    long unsigned int offset)
7792 {
7793   dw_attr_node attr;
7794
7795   attr.dw_attr = attr_kind;
7796   attr.dw_attr_val.val_class = dw_val_class_range_list;
7797   attr.dw_attr_val.v.val_offset = offset;
7798   add_dwarf_attr (die, &attr);
7799 }
7800
7801 /* Return the start label of a delta attribute.  */
7802
7803 static inline const char *
7804 AT_vms_delta1 (dw_attr_ref a)
7805 {
7806   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7807   return a->dw_attr_val.v.val_vms_delta.lbl1;
7808 }
7809
7810 /* Return the end label of a delta attribute.  */
7811
7812 static inline const char *
7813 AT_vms_delta2 (dw_attr_ref a)
7814 {
7815   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7816   return a->dw_attr_val.v.val_vms_delta.lbl2;
7817 }
7818
7819 static inline const char *
7820 AT_lbl (dw_attr_ref a)
7821 {
7822   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7823                     || AT_class (a) == dw_val_class_lineptr
7824                     || AT_class (a) == dw_val_class_macptr));
7825   return a->dw_attr_val.v.val_lbl_id;
7826 }
7827
7828 /* Get the attribute of type attr_kind.  */
7829
7830 static dw_attr_ref
7831 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7832 {
7833   dw_attr_ref a;
7834   unsigned ix;
7835   dw_die_ref spec = NULL;
7836
7837   if (! die)
7838     return NULL;
7839
7840   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7841     if (a->dw_attr == attr_kind)
7842       return a;
7843     else if (a->dw_attr == DW_AT_specification
7844              || a->dw_attr == DW_AT_abstract_origin)
7845       spec = AT_ref (a);
7846
7847   if (spec)
7848     return get_AT (spec, attr_kind);
7849
7850   return NULL;
7851 }
7852
7853 /* Return the "low pc" attribute value, typically associated with a subprogram
7854    DIE.  Return null if the "low pc" attribute is either not present, or if it
7855    cannot be represented as an assembler label identifier.  */
7856
7857 static inline const char *
7858 get_AT_low_pc (dw_die_ref die)
7859 {
7860   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7861
7862   return a ? AT_lbl (a) : NULL;
7863 }
7864
7865 /* Return the "high pc" attribute value, typically associated with a subprogram
7866    DIE.  Return null if the "high pc" attribute is either not present, or if it
7867    cannot be represented as an assembler label identifier.  */
7868
7869 static inline const char *
7870 get_AT_hi_pc (dw_die_ref die)
7871 {
7872   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7873
7874   return a ? AT_lbl (a) : NULL;
7875 }
7876
7877 /* Return the value of the string attribute designated by ATTR_KIND, or
7878    NULL if it is not present.  */
7879
7880 static inline const char *
7881 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7882 {
7883   dw_attr_ref a = get_AT (die, attr_kind);
7884
7885   return a ? AT_string (a) : NULL;
7886 }
7887
7888 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7889    if it is not present.  */
7890
7891 static inline int
7892 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7893 {
7894   dw_attr_ref a = get_AT (die, attr_kind);
7895
7896   return a ? AT_flag (a) : 0;
7897 }
7898
7899 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7900    if it is not present.  */
7901
7902 static inline unsigned
7903 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7904 {
7905   dw_attr_ref a = get_AT (die, attr_kind);
7906
7907   return a ? AT_unsigned (a) : 0;
7908 }
7909
7910 static inline dw_die_ref
7911 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7912 {
7913   dw_attr_ref a = get_AT (die, attr_kind);
7914
7915   return a ? AT_ref (a) : NULL;
7916 }
7917
7918 static inline struct dwarf_file_data *
7919 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7920 {
7921   dw_attr_ref a = get_AT (die, attr_kind);
7922
7923   return a ? AT_file (a) : NULL;
7924 }
7925
7926 /* Return TRUE if the language is C++.  */
7927
7928 static inline bool
7929 is_cxx (void)
7930 {
7931   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7932
7933   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7934 }
7935
7936 /* Return TRUE if the language is Fortran.  */
7937
7938 static inline bool
7939 is_fortran (void)
7940 {
7941   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7942
7943   return (lang == DW_LANG_Fortran77
7944           || lang == DW_LANG_Fortran90
7945           || lang == DW_LANG_Fortran95);
7946 }
7947
7948 /* Return TRUE if the language is Ada.  */
7949
7950 static inline bool
7951 is_ada (void)
7952 {
7953   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7954
7955   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7956 }
7957
7958 /* Remove the specified attribute if present.  */
7959
7960 static void
7961 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7962 {
7963   dw_attr_ref a;
7964   unsigned ix;
7965
7966   if (! die)
7967     return;
7968
7969   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7970     if (a->dw_attr == attr_kind)
7971       {
7972         if (AT_class (a) == dw_val_class_str)
7973           if (a->dw_attr_val.v.val_str->refcount)
7974             a->dw_attr_val.v.val_str->refcount--;
7975
7976         /* VEC_ordered_remove should help reduce the number of abbrevs
7977            that are needed.  */
7978         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7979         return;
7980       }
7981 }
7982
7983 /* Remove CHILD from its parent.  PREV must have the property that
7984    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7985
7986 static void
7987 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7988 {
7989   gcc_assert (child->die_parent == prev->die_parent);
7990   gcc_assert (prev->die_sib == child);
7991   if (prev == child)
7992     {
7993       gcc_assert (child->die_parent->die_child == child);
7994       prev = NULL;
7995     }
7996   else
7997     prev->die_sib = child->die_sib;
7998   if (child->die_parent->die_child == child)
7999     child->die_parent->die_child = prev;
8000 }
8001
8002 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
8003    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
8004
8005 static void
8006 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8007 {
8008   dw_die_ref parent = old_child->die_parent;
8009
8010   gcc_assert (parent == prev->die_parent);
8011   gcc_assert (prev->die_sib == old_child);
8012
8013   new_child->die_parent = parent;
8014   if (prev == old_child)
8015     {
8016       gcc_assert (parent->die_child == old_child);
8017       new_child->die_sib = new_child;
8018     }
8019   else
8020     {
8021       prev->die_sib = new_child;
8022       new_child->die_sib = old_child->die_sib;
8023     }
8024   if (old_child->die_parent->die_child == old_child)
8025     old_child->die_parent->die_child = new_child;
8026 }
8027
8028 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8029
8030 static void
8031 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8032 {
8033   dw_die_ref c;
8034   new_parent->die_child = old_parent->die_child;
8035   old_parent->die_child = NULL;
8036   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8037 }
8038
8039 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8040    matches TAG.  */
8041
8042 static void
8043 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8044 {
8045   dw_die_ref c;
8046
8047   c = die->die_child;
8048   if (c) do {
8049     dw_die_ref prev = c;
8050     c = c->die_sib;
8051     while (c->die_tag == tag)
8052       {
8053         remove_child_with_prev (c, prev);
8054         /* Might have removed every child.  */
8055         if (c == c->die_sib)
8056           return;
8057         c = c->die_sib;
8058       }
8059   } while (c != die->die_child);
8060 }
8061
8062 /* Add a CHILD_DIE as the last child of DIE.  */
8063
8064 static void
8065 add_child_die (dw_die_ref die, dw_die_ref child_die)
8066 {
8067   /* FIXME this should probably be an assert.  */
8068   if (! die || ! child_die)
8069     return;
8070   gcc_assert (die != child_die);
8071
8072   child_die->die_parent = die;
8073   if (die->die_child)
8074     {
8075       child_die->die_sib = die->die_child->die_sib;
8076       die->die_child->die_sib = child_die;
8077     }
8078   else
8079     child_die->die_sib = child_die;
8080   die->die_child = child_die;
8081 }
8082
8083 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8084    is the specification, to the end of PARENT's list of children.
8085    This is done by removing and re-adding it.  */
8086
8087 static void
8088 splice_child_die (dw_die_ref parent, dw_die_ref child)
8089 {
8090   dw_die_ref p;
8091
8092   /* We want the declaration DIE from inside the class, not the
8093      specification DIE at toplevel.  */
8094   if (child->die_parent != parent)
8095     {
8096       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8097
8098       if (tmp)
8099         child = tmp;
8100     }
8101
8102   gcc_assert (child->die_parent == parent
8103               || (child->die_parent
8104                   == get_AT_ref (parent, DW_AT_specification)));
8105
8106   for (p = child->die_parent->die_child; ; p = p->die_sib)
8107     if (p->die_sib == child)
8108       {
8109         remove_child_with_prev (child, p);
8110         break;
8111       }
8112
8113   add_child_die (parent, child);
8114 }
8115
8116 /* Return a pointer to a newly created DIE node.  */
8117
8118 static inline dw_die_ref
8119 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8120 {
8121   dw_die_ref die = ggc_alloc_cleared_die_node ();
8122
8123   die->die_tag = tag_value;
8124
8125   if (parent_die != NULL)
8126     add_child_die (parent_die, die);
8127   else
8128     {
8129       limbo_die_node *limbo_node;
8130
8131       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8132       limbo_node->die = die;
8133       limbo_node->created_for = t;
8134       limbo_node->next = limbo_die_list;
8135       limbo_die_list = limbo_node;
8136     }
8137
8138   return die;
8139 }
8140
8141 /* Return the DIE associated with the given type specifier.  */
8142
8143 static inline dw_die_ref
8144 lookup_type_die (tree type)
8145 {
8146   return TYPE_SYMTAB_DIE (type);
8147 }
8148
8149 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8150    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8151    anonymous type instead the one of the naming typedef.  */
8152
8153 static inline dw_die_ref
8154 strip_naming_typedef (tree type, dw_die_ref type_die)
8155 {
8156   if (type
8157       && TREE_CODE (type) == RECORD_TYPE
8158       && type_die
8159       && type_die->die_tag == DW_TAG_typedef
8160       && is_naming_typedef_decl (TYPE_NAME (type)))
8161     type_die = get_AT_ref (type_die, DW_AT_type);
8162   return type_die;
8163 }
8164
8165 /* Like lookup_type_die, but if type is an anonymous type named by a
8166    typedef[1], return the DIE of the anonymous type instead the one of
8167    the naming typedef.  This is because in gen_typedef_die, we did
8168    equate the anonymous struct named by the typedef with the DIE of
8169    the naming typedef. So by default, lookup_type_die on an anonymous
8170    struct yields the DIE of the naming typedef.
8171
8172    [1]: Read the comment of is_naming_typedef_decl to learn about what
8173    a naming typedef is.  */
8174
8175 static inline dw_die_ref
8176 lookup_type_die_strip_naming_typedef (tree type)
8177 {
8178   dw_die_ref die = lookup_type_die (type);
8179   return strip_naming_typedef (type, die);
8180 }
8181
8182 /* Equate a DIE to a given type specifier.  */
8183
8184 static inline void
8185 equate_type_number_to_die (tree type, dw_die_ref type_die)
8186 {
8187   TYPE_SYMTAB_DIE (type) = type_die;
8188 }
8189
8190 /* Returns a hash value for X (which really is a die_struct).  */
8191
8192 static hashval_t
8193 decl_die_table_hash (const void *x)
8194 {
8195   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8196 }
8197
8198 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8199
8200 static int
8201 decl_die_table_eq (const void *x, const void *y)
8202 {
8203   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8204 }
8205
8206 /* Return the DIE associated with a given declaration.  */
8207
8208 static inline dw_die_ref
8209 lookup_decl_die (tree decl)
8210 {
8211   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8212 }
8213
8214 /* Returns a hash value for X (which really is a var_loc_list).  */
8215
8216 static hashval_t
8217 decl_loc_table_hash (const void *x)
8218 {
8219   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8220 }
8221
8222 /* Return nonzero if decl_id of var_loc_list X is the same as
8223    UID of decl *Y.  */
8224
8225 static int
8226 decl_loc_table_eq (const void *x, const void *y)
8227 {
8228   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8229 }
8230
8231 /* Return the var_loc list associated with a given declaration.  */
8232
8233 static inline var_loc_list *
8234 lookup_decl_loc (const_tree decl)
8235 {
8236   if (!decl_loc_table)
8237     return NULL;
8238   return (var_loc_list *)
8239     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8240 }
8241
8242 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8243
8244 static hashval_t
8245 cached_dw_loc_list_table_hash (const void *x)
8246 {
8247   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8248 }
8249
8250 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8251    UID of decl *Y.  */
8252
8253 static int
8254 cached_dw_loc_list_table_eq (const void *x, const void *y)
8255 {
8256   return (((const cached_dw_loc_list *) x)->decl_id
8257           == DECL_UID ((const_tree) y));
8258 }
8259
8260 /* Equate a DIE to a particular declaration.  */
8261
8262 static void
8263 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8264 {
8265   unsigned int decl_id = DECL_UID (decl);
8266   void **slot;
8267
8268   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8269   *slot = decl_die;
8270   decl_die->decl_id = decl_id;
8271 }
8272
8273 /* Return how many bits covers PIECE EXPR_LIST.  */
8274
8275 static int
8276 decl_piece_bitsize (rtx piece)
8277 {
8278   int ret = (int) GET_MODE (piece);
8279   if (ret)
8280     return ret;
8281   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8282               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8283   return INTVAL (XEXP (XEXP (piece, 0), 0));
8284 }
8285
8286 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8287
8288 static rtx *
8289 decl_piece_varloc_ptr (rtx piece)
8290 {
8291   if ((int) GET_MODE (piece))
8292     return &XEXP (piece, 0);
8293   else
8294     return &XEXP (XEXP (piece, 0), 1);
8295 }
8296
8297 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8298    Next is the chain of following piece nodes.  */
8299
8300 static rtx
8301 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8302 {
8303   if (bitsize <= (int) MAX_MACHINE_MODE)
8304     return alloc_EXPR_LIST (bitsize, loc_note, next);
8305   else
8306     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8307                                                GEN_INT (bitsize),
8308                                                loc_note), next);
8309 }
8310
8311 /* Return rtx that should be stored into loc field for
8312    LOC_NOTE and BITPOS/BITSIZE.  */
8313
8314 static rtx
8315 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8316                       HOST_WIDE_INT bitsize)
8317 {
8318   if (bitsize != -1)
8319     {
8320       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8321       if (bitpos != 0)
8322         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8323     }
8324   return loc_note;
8325 }
8326
8327 /* This function either modifies location piece list *DEST in
8328    place (if SRC and INNER is NULL), or copies location piece list
8329    *SRC to *DEST while modifying it.  Location BITPOS is modified
8330    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8331    not copied and if needed some padding around it is added.
8332    When modifying in place, DEST should point to EXPR_LIST where
8333    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8334    to the start of the whole list and INNER points to the EXPR_LIST
8335    where earlier pieces cover PIECE_BITPOS bits.  */
8336
8337 static void
8338 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8339                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8340                    HOST_WIDE_INT bitsize, rtx loc_note)
8341 {
8342   int diff;
8343   bool copy = inner != NULL;
8344
8345   if (copy)
8346     {
8347       /* First copy all nodes preceeding the current bitpos.  */
8348       while (src != inner)
8349         {
8350           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8351                                    decl_piece_bitsize (*src), NULL_RTX);
8352           dest = &XEXP (*dest, 1);
8353           src = &XEXP (*src, 1);
8354         }
8355     }
8356   /* Add padding if needed.  */
8357   if (bitpos != piece_bitpos)
8358     {
8359       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8360                                copy ? NULL_RTX : *dest);
8361       dest = &XEXP (*dest, 1);
8362     }
8363   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8364     {
8365       gcc_assert (!copy);
8366       /* A piece with correct bitpos and bitsize already exist,
8367          just update the location for it and return.  */
8368       *decl_piece_varloc_ptr (*dest) = loc_note;
8369       return;
8370     }
8371   /* Add the piece that changed.  */
8372   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8373   dest = &XEXP (*dest, 1);
8374   /* Skip over pieces that overlap it.  */
8375   diff = bitpos - piece_bitpos + bitsize;
8376   if (!copy)
8377     src = dest;
8378   while (diff > 0 && *src)
8379     {
8380       rtx piece = *src;
8381       diff -= decl_piece_bitsize (piece);
8382       if (copy)
8383         src = &XEXP (piece, 1);
8384       else
8385         {
8386           *src = XEXP (piece, 1);
8387           free_EXPR_LIST_node (piece);
8388         }
8389     }
8390   /* Add padding if needed.  */
8391   if (diff < 0 && *src)
8392     {
8393       if (!copy)
8394         dest = src;
8395       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8396       dest = &XEXP (*dest, 1);
8397     }
8398   if (!copy)
8399     return;
8400   /* Finally copy all nodes following it.  */
8401   while (*src)
8402     {
8403       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8404                                decl_piece_bitsize (*src), NULL_RTX);
8405       dest = &XEXP (*dest, 1);
8406       src = &XEXP (*src, 1);
8407     }
8408 }
8409
8410 /* Add a variable location node to the linked list for DECL.  */
8411
8412 static struct var_loc_node *
8413 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8414 {
8415   unsigned int decl_id;
8416   var_loc_list *temp;
8417   void **slot;
8418   struct var_loc_node *loc = NULL;
8419   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8420
8421   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8422     {
8423       tree realdecl = DECL_DEBUG_EXPR (decl);
8424       if (realdecl && handled_component_p (realdecl))
8425         {
8426           HOST_WIDE_INT maxsize;
8427           tree innerdecl;
8428           innerdecl
8429             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8430           if (!DECL_P (innerdecl)
8431               || DECL_IGNORED_P (innerdecl)
8432               || TREE_STATIC (innerdecl)
8433               || bitsize <= 0
8434               || bitpos + bitsize > 256
8435               || bitsize != maxsize)
8436             return NULL;
8437           decl = innerdecl;
8438         }
8439     }
8440
8441   decl_id = DECL_UID (decl);
8442   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8443   if (*slot == NULL)
8444     {
8445       temp = ggc_alloc_cleared_var_loc_list ();
8446       temp->decl_id = decl_id;
8447       *slot = temp;
8448     }
8449   else
8450     temp = (var_loc_list *) *slot;
8451
8452   if (temp->last)
8453     {
8454       struct var_loc_node *last = temp->last, *unused = NULL;
8455       rtx *piece_loc = NULL, last_loc_note;
8456       int piece_bitpos = 0;
8457       if (last->next)
8458         {
8459           last = last->next;
8460           gcc_assert (last->next == NULL);
8461         }
8462       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8463         {
8464           piece_loc = &last->loc;
8465           do
8466             {
8467               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8468               if (piece_bitpos + cur_bitsize > bitpos)
8469                 break;
8470               piece_bitpos += cur_bitsize;
8471               piece_loc = &XEXP (*piece_loc, 1);
8472             }
8473           while (*piece_loc);
8474         }
8475       /* TEMP->LAST here is either pointer to the last but one or
8476          last element in the chained list, LAST is pointer to the
8477          last element.  */
8478       if (label && strcmp (last->label, label) == 0)
8479         {
8480           /* For SRA optimized variables if there weren't any real
8481              insns since last note, just modify the last node.  */
8482           if (piece_loc != NULL)
8483             {
8484               adjust_piece_list (piece_loc, NULL, NULL,
8485                                  bitpos, piece_bitpos, bitsize, loc_note);
8486               return NULL;
8487             }
8488           /* If the last note doesn't cover any instructions, remove it.  */
8489           if (temp->last != last)
8490             {
8491               temp->last->next = NULL;
8492               unused = last;
8493               last = temp->last;
8494               gcc_assert (strcmp (last->label, label) != 0);
8495             }
8496           else
8497             {
8498               gcc_assert (temp->first == temp->last);
8499               memset (temp->last, '\0', sizeof (*temp->last));
8500               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8501               return temp->last;
8502             }
8503         }
8504       if (bitsize == -1 && NOTE_P (last->loc))
8505         last_loc_note = last->loc;
8506       else if (piece_loc != NULL
8507                && *piece_loc != NULL_RTX
8508                && piece_bitpos == bitpos
8509                && decl_piece_bitsize (*piece_loc) == bitsize)
8510         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8511       else
8512         last_loc_note = NULL_RTX;
8513       /* If the current location is the same as the end of the list,
8514          and either both or neither of the locations is uninitialized,
8515          we have nothing to do.  */
8516       if (last_loc_note == NULL_RTX
8517           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8518                             NOTE_VAR_LOCATION_LOC (loc_note)))
8519           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8520                != NOTE_VAR_LOCATION_STATUS (loc_note))
8521               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8522                    == VAR_INIT_STATUS_UNINITIALIZED)
8523                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8524                       == VAR_INIT_STATUS_UNINITIALIZED))))
8525         {
8526           /* Add LOC to the end of list and update LAST.  If the last
8527              element of the list has been removed above, reuse its
8528              memory for the new node, otherwise allocate a new one.  */
8529           if (unused)
8530             {
8531               loc = unused;
8532               memset (loc, '\0', sizeof (*loc));
8533             }
8534           else
8535             loc = ggc_alloc_cleared_var_loc_node ();
8536           if (bitsize == -1 || piece_loc == NULL)
8537             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8538           else
8539             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8540                                bitpos, piece_bitpos, bitsize, loc_note);
8541           last->next = loc;
8542           /* Ensure TEMP->LAST will point either to the new last but one
8543              element of the chain, or to the last element in it.  */
8544           if (last != temp->last)
8545             temp->last = last;
8546         }
8547       else if (unused)
8548         ggc_free (unused);
8549     }
8550   else
8551     {
8552       loc = ggc_alloc_cleared_var_loc_node ();
8553       temp->first = loc;
8554       temp->last = loc;
8555       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8556     }
8557   return loc;
8558 }
8559 \f
8560 /* Keep track of the number of spaces used to indent the
8561    output of the debugging routines that print the structure of
8562    the DIE internal representation.  */
8563 static int print_indent;
8564
8565 /* Indent the line the number of spaces given by print_indent.  */
8566
8567 static inline void
8568 print_spaces (FILE *outfile)
8569 {
8570   fprintf (outfile, "%*s", print_indent, "");
8571 }
8572
8573 /* Print a type signature in hex.  */
8574
8575 static inline void
8576 print_signature (FILE *outfile, char *sig)
8577 {
8578   int i;
8579
8580   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8581     fprintf (outfile, "%02x", sig[i] & 0xff);
8582 }
8583
8584 /* Print the information associated with a given DIE, and its children.
8585    This routine is a debugging aid only.  */
8586
8587 static void
8588 print_die (dw_die_ref die, FILE *outfile)
8589 {
8590   dw_attr_ref a;
8591   dw_die_ref c;
8592   unsigned ix;
8593
8594   print_spaces (outfile);
8595   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8596            die->die_offset, dwarf_tag_name (die->die_tag),
8597            (void*) die);
8598   print_spaces (outfile);
8599   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8600   fprintf (outfile, " offset: %ld", die->die_offset);
8601   fprintf (outfile, " mark: %d\n", die->die_mark);
8602
8603   if (use_debug_types && die->die_id.die_type_node)
8604     {
8605       print_spaces (outfile);
8606       fprintf (outfile, "  signature: ");
8607       print_signature (outfile, die->die_id.die_type_node->signature);
8608       fprintf (outfile, "\n");
8609     }
8610
8611   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8612     {
8613       print_spaces (outfile);
8614       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8615
8616       switch (AT_class (a))
8617         {
8618         case dw_val_class_addr:
8619           fprintf (outfile, "address");
8620           break;
8621         case dw_val_class_offset:
8622           fprintf (outfile, "offset");
8623           break;
8624         case dw_val_class_loc:
8625           fprintf (outfile, "location descriptor");
8626           break;
8627         case dw_val_class_loc_list:
8628           fprintf (outfile, "location list -> label:%s",
8629                    AT_loc_list (a)->ll_symbol);
8630           break;
8631         case dw_val_class_range_list:
8632           fprintf (outfile, "range list");
8633           break;
8634         case dw_val_class_const:
8635           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8636           break;
8637         case dw_val_class_unsigned_const:
8638           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8639           break;
8640         case dw_val_class_const_double:
8641           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8642                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8643                    a->dw_attr_val.v.val_double.high,
8644                    a->dw_attr_val.v.val_double.low);
8645           break;
8646         case dw_val_class_vec:
8647           fprintf (outfile, "floating-point or vector constant");
8648           break;
8649         case dw_val_class_flag:
8650           fprintf (outfile, "%u", AT_flag (a));
8651           break;
8652         case dw_val_class_die_ref:
8653           if (AT_ref (a) != NULL)
8654             {
8655               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8656                 {
8657                   fprintf (outfile, "die -> signature: ");
8658                   print_signature (outfile,
8659                                    AT_ref (a)->die_id.die_type_node->signature);
8660                 }
8661               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8662                 fprintf (outfile, "die -> label: %s",
8663                          AT_ref (a)->die_id.die_symbol);
8664               else
8665                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8666               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8667             }
8668           else
8669             fprintf (outfile, "die -> <null>");
8670           break;
8671         case dw_val_class_vms_delta:
8672           fprintf (outfile, "delta: @slotcount(%s-%s)",
8673                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8674           break;
8675         case dw_val_class_lbl_id:
8676         case dw_val_class_lineptr:
8677         case dw_val_class_macptr:
8678           fprintf (outfile, "label: %s", AT_lbl (a));
8679           break;
8680         case dw_val_class_str:
8681           if (AT_string (a) != NULL)
8682             fprintf (outfile, "\"%s\"", AT_string (a));
8683           else
8684             fprintf (outfile, "<null>");
8685           break;
8686         case dw_val_class_file:
8687           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8688                    AT_file (a)->emitted_number);
8689           break;
8690         case dw_val_class_data8:
8691           {
8692             int i;
8693
8694             for (i = 0; i < 8; i++)
8695               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8696             break;
8697           }
8698         default:
8699           break;
8700         }
8701
8702       fprintf (outfile, "\n");
8703     }
8704
8705   if (die->die_child != NULL)
8706     {
8707       print_indent += 4;
8708       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8709       print_indent -= 4;
8710     }
8711   if (print_indent == 0)
8712     fprintf (outfile, "\n");
8713 }
8714
8715 /* Print the information collected for a given DIE.  */
8716
8717 DEBUG_FUNCTION void
8718 debug_dwarf_die (dw_die_ref die)
8719 {
8720   print_die (die, stderr);
8721 }
8722
8723 /* Print all DWARF information collected for the compilation unit.
8724    This routine is a debugging aid only.  */
8725
8726 DEBUG_FUNCTION void
8727 debug_dwarf (void)
8728 {
8729   print_indent = 0;
8730   print_die (comp_unit_die (), stderr);
8731 }
8732 \f
8733 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8734    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8735    DIE that marks the start of the DIEs for this include file.  */
8736
8737 static dw_die_ref
8738 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8739 {
8740   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8741   dw_die_ref new_unit = gen_compile_unit_die (filename);
8742
8743   new_unit->die_sib = old_unit;
8744   return new_unit;
8745 }
8746
8747 /* Close an include-file CU and reopen the enclosing one.  */
8748
8749 static dw_die_ref
8750 pop_compile_unit (dw_die_ref old_unit)
8751 {
8752   dw_die_ref new_unit = old_unit->die_sib;
8753
8754   old_unit->die_sib = NULL;
8755   return new_unit;
8756 }
8757
8758 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8759 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8760
8761 /* Calculate the checksum of a location expression.  */
8762
8763 static inline void
8764 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8765 {
8766   int tem;
8767
8768   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8769   CHECKSUM (tem);
8770   CHECKSUM (loc->dw_loc_oprnd1);
8771   CHECKSUM (loc->dw_loc_oprnd2);
8772 }
8773
8774 /* Calculate the checksum of an attribute.  */
8775
8776 static void
8777 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8778 {
8779   dw_loc_descr_ref loc;
8780   rtx r;
8781
8782   CHECKSUM (at->dw_attr);
8783
8784   /* We don't care that this was compiled with a different compiler
8785      snapshot; if the output is the same, that's what matters.  */
8786   if (at->dw_attr == DW_AT_producer)
8787     return;
8788
8789   switch (AT_class (at))
8790     {
8791     case dw_val_class_const:
8792       CHECKSUM (at->dw_attr_val.v.val_int);
8793       break;
8794     case dw_val_class_unsigned_const:
8795       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8796       break;
8797     case dw_val_class_const_double:
8798       CHECKSUM (at->dw_attr_val.v.val_double);
8799       break;
8800     case dw_val_class_vec:
8801       CHECKSUM (at->dw_attr_val.v.val_vec);
8802       break;
8803     case dw_val_class_flag:
8804       CHECKSUM (at->dw_attr_val.v.val_flag);
8805       break;
8806     case dw_val_class_str:
8807       CHECKSUM_STRING (AT_string (at));
8808       break;
8809
8810     case dw_val_class_addr:
8811       r = AT_addr (at);
8812       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8813       CHECKSUM_STRING (XSTR (r, 0));
8814       break;
8815
8816     case dw_val_class_offset:
8817       CHECKSUM (at->dw_attr_val.v.val_offset);
8818       break;
8819
8820     case dw_val_class_loc:
8821       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8822         loc_checksum (loc, ctx);
8823       break;
8824
8825     case dw_val_class_die_ref:
8826       die_checksum (AT_ref (at), ctx, mark);
8827       break;
8828
8829     case dw_val_class_fde_ref:
8830     case dw_val_class_vms_delta:
8831     case dw_val_class_lbl_id:
8832     case dw_val_class_lineptr:
8833     case dw_val_class_macptr:
8834       break;
8835
8836     case dw_val_class_file:
8837       CHECKSUM_STRING (AT_file (at)->filename);
8838       break;
8839
8840     case dw_val_class_data8:
8841       CHECKSUM (at->dw_attr_val.v.val_data8);
8842       break;
8843
8844     default:
8845       break;
8846     }
8847 }
8848
8849 /* Calculate the checksum of a DIE.  */
8850
8851 static void
8852 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8853 {
8854   dw_die_ref c;
8855   dw_attr_ref a;
8856   unsigned ix;
8857
8858   /* To avoid infinite recursion.  */
8859   if (die->die_mark)
8860     {
8861       CHECKSUM (die->die_mark);
8862       return;
8863     }
8864   die->die_mark = ++(*mark);
8865
8866   CHECKSUM (die->die_tag);
8867
8868   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8869     attr_checksum (a, ctx, mark);
8870
8871   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8872 }
8873
8874 #undef CHECKSUM
8875 #undef CHECKSUM_STRING
8876
8877 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8878 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8879 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8880 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8881 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8882 #define CHECKSUM_ATTR(FOO) \
8883   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8884
8885 /* Calculate the checksum of a number in signed LEB128 format.  */
8886
8887 static void
8888 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8889 {
8890   unsigned char byte;
8891   bool more;
8892
8893   while (1)
8894     {
8895       byte = (value & 0x7f);
8896       value >>= 7;
8897       more = !((value == 0 && (byte & 0x40) == 0)
8898                 || (value == -1 && (byte & 0x40) != 0));
8899       if (more)
8900         byte |= 0x80;
8901       CHECKSUM (byte);
8902       if (!more)
8903         break;
8904     }
8905 }
8906
8907 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8908
8909 static void
8910 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8911 {
8912   while (1)
8913     {
8914       unsigned char byte = (value & 0x7f);
8915       value >>= 7;
8916       if (value != 0)
8917         /* More bytes to follow.  */
8918         byte |= 0x80;
8919       CHECKSUM (byte);
8920       if (value == 0)
8921         break;
8922     }
8923 }
8924
8925 /* Checksum the context of the DIE.  This adds the names of any
8926    surrounding namespaces or structures to the checksum.  */
8927
8928 static void
8929 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8930 {
8931   const char *name;
8932   dw_die_ref spec;
8933   int tag = die->die_tag;
8934
8935   if (tag != DW_TAG_namespace
8936       && tag != DW_TAG_structure_type
8937       && tag != DW_TAG_class_type)
8938     return;
8939
8940   name = get_AT_string (die, DW_AT_name);
8941
8942   spec = get_AT_ref (die, DW_AT_specification);
8943   if (spec != NULL)
8944     die = spec;
8945
8946   if (die->die_parent != NULL)
8947     checksum_die_context (die->die_parent, ctx);
8948
8949   CHECKSUM_ULEB128 ('C');
8950   CHECKSUM_ULEB128 (tag);
8951   if (name != NULL)
8952     CHECKSUM_STRING (name);
8953 }
8954
8955 /* Calculate the checksum of a location expression.  */
8956
8957 static inline void
8958 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8959 {
8960   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8961      were emitted as a DW_FORM_sdata instead of a location expression.  */
8962   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8963     {
8964       CHECKSUM_ULEB128 (DW_FORM_sdata);
8965       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8966       return;
8967     }
8968
8969   /* Otherwise, just checksum the raw location expression.  */
8970   while (loc != NULL)
8971     {
8972       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8973       CHECKSUM (loc->dw_loc_oprnd1);
8974       CHECKSUM (loc->dw_loc_oprnd2);
8975       loc = loc->dw_loc_next;
8976     }
8977 }
8978
8979 /* Calculate the checksum of an attribute.  */
8980
8981 static void
8982 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8983                        struct md5_ctx *ctx, int *mark)
8984 {
8985   dw_loc_descr_ref loc;
8986   rtx r;
8987
8988   if (AT_class (at) == dw_val_class_die_ref)
8989     {
8990       dw_die_ref target_die = AT_ref (at);
8991
8992       /* For pointer and reference types, we checksum only the (qualified)
8993          name of the target type (if there is a name).  For friend entries,
8994          we checksum only the (qualified) name of the target type or function.
8995          This allows the checksum to remain the same whether the target type
8996          is complete or not.  */
8997       if ((at->dw_attr == DW_AT_type
8998            && (tag == DW_TAG_pointer_type
8999                || tag == DW_TAG_reference_type
9000                || tag == DW_TAG_rvalue_reference_type
9001                || tag == DW_TAG_ptr_to_member_type))
9002           || (at->dw_attr == DW_AT_friend
9003               && tag == DW_TAG_friend))
9004         {
9005           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9006
9007           if (name_attr != NULL)
9008             {
9009               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9010
9011               if (decl == NULL)
9012                 decl = target_die;
9013               CHECKSUM_ULEB128 ('N');
9014               CHECKSUM_ULEB128 (at->dw_attr);
9015               if (decl->die_parent != NULL)
9016                 checksum_die_context (decl->die_parent, ctx);
9017               CHECKSUM_ULEB128 ('E');
9018               CHECKSUM_STRING (AT_string (name_attr));
9019               return;
9020             }
9021         }
9022
9023       /* For all other references to another DIE, we check to see if the
9024          target DIE has already been visited.  If it has, we emit a
9025          backward reference; if not, we descend recursively.  */
9026       if (target_die->die_mark > 0)
9027         {
9028           CHECKSUM_ULEB128 ('R');
9029           CHECKSUM_ULEB128 (at->dw_attr);
9030           CHECKSUM_ULEB128 (target_die->die_mark);
9031         }
9032       else
9033         {
9034           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9035
9036           if (decl == NULL)
9037             decl = target_die;
9038           target_die->die_mark = ++(*mark);
9039           CHECKSUM_ULEB128 ('T');
9040           CHECKSUM_ULEB128 (at->dw_attr);
9041           if (decl->die_parent != NULL)
9042             checksum_die_context (decl->die_parent, ctx);
9043           die_checksum_ordered (target_die, ctx, mark);
9044         }
9045       return;
9046     }
9047
9048   CHECKSUM_ULEB128 ('A');
9049   CHECKSUM_ULEB128 (at->dw_attr);
9050
9051   switch (AT_class (at))
9052     {
9053     case dw_val_class_const:
9054       CHECKSUM_ULEB128 (DW_FORM_sdata);
9055       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9056       break;
9057
9058     case dw_val_class_unsigned_const:
9059       CHECKSUM_ULEB128 (DW_FORM_sdata);
9060       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9061       break;
9062
9063     case dw_val_class_const_double:
9064       CHECKSUM_ULEB128 (DW_FORM_block);
9065       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9066       CHECKSUM (at->dw_attr_val.v.val_double);
9067       break;
9068
9069     case dw_val_class_vec:
9070       CHECKSUM_ULEB128 (DW_FORM_block);
9071       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9072       CHECKSUM (at->dw_attr_val.v.val_vec);
9073       break;
9074
9075     case dw_val_class_flag:
9076       CHECKSUM_ULEB128 (DW_FORM_flag);
9077       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9078       break;
9079
9080     case dw_val_class_str:
9081       CHECKSUM_ULEB128 (DW_FORM_string);
9082       CHECKSUM_STRING (AT_string (at));
9083       break;
9084
9085     case dw_val_class_addr:
9086       r = AT_addr (at);
9087       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9088       CHECKSUM_ULEB128 (DW_FORM_string);
9089       CHECKSUM_STRING (XSTR (r, 0));
9090       break;
9091
9092     case dw_val_class_offset:
9093       CHECKSUM_ULEB128 (DW_FORM_sdata);
9094       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9095       break;
9096
9097     case dw_val_class_loc:
9098       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9099         loc_checksum_ordered (loc, ctx);
9100       break;
9101
9102     case dw_val_class_fde_ref:
9103     case dw_val_class_lbl_id:
9104     case dw_val_class_lineptr:
9105     case dw_val_class_macptr:
9106       break;
9107
9108     case dw_val_class_file:
9109       CHECKSUM_ULEB128 (DW_FORM_string);
9110       CHECKSUM_STRING (AT_file (at)->filename);
9111       break;
9112
9113     case dw_val_class_data8:
9114       CHECKSUM (at->dw_attr_val.v.val_data8);
9115       break;
9116
9117     default:
9118       break;
9119     }
9120 }
9121
9122 struct checksum_attributes
9123 {
9124   dw_attr_ref at_name;
9125   dw_attr_ref at_type;
9126   dw_attr_ref at_friend;
9127   dw_attr_ref at_accessibility;
9128   dw_attr_ref at_address_class;
9129   dw_attr_ref at_allocated;
9130   dw_attr_ref at_artificial;
9131   dw_attr_ref at_associated;
9132   dw_attr_ref at_binary_scale;
9133   dw_attr_ref at_bit_offset;
9134   dw_attr_ref at_bit_size;
9135   dw_attr_ref at_bit_stride;
9136   dw_attr_ref at_byte_size;
9137   dw_attr_ref at_byte_stride;
9138   dw_attr_ref at_const_value;
9139   dw_attr_ref at_containing_type;
9140   dw_attr_ref at_count;
9141   dw_attr_ref at_data_location;
9142   dw_attr_ref at_data_member_location;
9143   dw_attr_ref at_decimal_scale;
9144   dw_attr_ref at_decimal_sign;
9145   dw_attr_ref at_default_value;
9146   dw_attr_ref at_digit_count;
9147   dw_attr_ref at_discr;
9148   dw_attr_ref at_discr_list;
9149   dw_attr_ref at_discr_value;
9150   dw_attr_ref at_encoding;
9151   dw_attr_ref at_endianity;
9152   dw_attr_ref at_explicit;
9153   dw_attr_ref at_is_optional;
9154   dw_attr_ref at_location;
9155   dw_attr_ref at_lower_bound;
9156   dw_attr_ref at_mutable;
9157   dw_attr_ref at_ordering;
9158   dw_attr_ref at_picture_string;
9159   dw_attr_ref at_prototyped;
9160   dw_attr_ref at_small;
9161   dw_attr_ref at_segment;
9162   dw_attr_ref at_string_length;
9163   dw_attr_ref at_threads_scaled;
9164   dw_attr_ref at_upper_bound;
9165   dw_attr_ref at_use_location;
9166   dw_attr_ref at_use_UTF8;
9167   dw_attr_ref at_variable_parameter;
9168   dw_attr_ref at_virtuality;
9169   dw_attr_ref at_visibility;
9170   dw_attr_ref at_vtable_elem_location;
9171 };
9172
9173 /* Collect the attributes that we will want to use for the checksum.  */
9174
9175 static void
9176 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9177 {
9178   dw_attr_ref a;
9179   unsigned ix;
9180
9181   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9182     {
9183       switch (a->dw_attr)
9184         {
9185         case DW_AT_name:
9186           attrs->at_name = a;
9187           break;
9188         case DW_AT_type:
9189           attrs->at_type = a;
9190           break;
9191         case DW_AT_friend:
9192           attrs->at_friend = a;
9193           break;
9194         case DW_AT_accessibility:
9195           attrs->at_accessibility = a;
9196           break;
9197         case DW_AT_address_class:
9198           attrs->at_address_class = a;
9199           break;
9200         case DW_AT_allocated:
9201           attrs->at_allocated = a;
9202           break;
9203         case DW_AT_artificial:
9204           attrs->at_artificial = a;
9205           break;
9206         case DW_AT_associated:
9207           attrs->at_associated = a;
9208           break;
9209         case DW_AT_binary_scale:
9210           attrs->at_binary_scale = a;
9211           break;
9212         case DW_AT_bit_offset:
9213           attrs->at_bit_offset = a;
9214           break;
9215         case DW_AT_bit_size:
9216           attrs->at_bit_size = a;
9217           break;
9218         case DW_AT_bit_stride:
9219           attrs->at_bit_stride = a;
9220           break;
9221         case DW_AT_byte_size:
9222           attrs->at_byte_size = a;
9223           break;
9224         case DW_AT_byte_stride:
9225           attrs->at_byte_stride = a;
9226           break;
9227         case DW_AT_const_value:
9228           attrs->at_const_value = a;
9229           break;
9230         case DW_AT_containing_type:
9231           attrs->at_containing_type = a;
9232           break;
9233         case DW_AT_count:
9234           attrs->at_count = a;
9235           break;
9236         case DW_AT_data_location:
9237           attrs->at_data_location = a;
9238           break;
9239         case DW_AT_data_member_location:
9240           attrs->at_data_member_location = a;
9241           break;
9242         case DW_AT_decimal_scale:
9243           attrs->at_decimal_scale = a;
9244           break;
9245         case DW_AT_decimal_sign:
9246           attrs->at_decimal_sign = a;
9247           break;
9248         case DW_AT_default_value:
9249           attrs->at_default_value = a;
9250           break;
9251         case DW_AT_digit_count:
9252           attrs->at_digit_count = a;
9253           break;
9254         case DW_AT_discr:
9255           attrs->at_discr = a;
9256           break;
9257         case DW_AT_discr_list:
9258           attrs->at_discr_list = a;
9259           break;
9260         case DW_AT_discr_value:
9261           attrs->at_discr_value = a;
9262           break;
9263         case DW_AT_encoding:
9264           attrs->at_encoding = a;
9265           break;
9266         case DW_AT_endianity:
9267           attrs->at_endianity = a;
9268           break;
9269         case DW_AT_explicit:
9270           attrs->at_explicit = a;
9271           break;
9272         case DW_AT_is_optional:
9273           attrs->at_is_optional = a;
9274           break;
9275         case DW_AT_location:
9276           attrs->at_location = a;
9277           break;
9278         case DW_AT_lower_bound:
9279           attrs->at_lower_bound = a;
9280           break;
9281         case DW_AT_mutable:
9282           attrs->at_mutable = a;
9283           break;
9284         case DW_AT_ordering:
9285           attrs->at_ordering = a;
9286           break;
9287         case DW_AT_picture_string:
9288           attrs->at_picture_string = a;
9289           break;
9290         case DW_AT_prototyped:
9291           attrs->at_prototyped = a;
9292           break;
9293         case DW_AT_small:
9294           attrs->at_small = a;
9295           break;
9296         case DW_AT_segment:
9297           attrs->at_segment = a;
9298           break;
9299         case DW_AT_string_length:
9300           attrs->at_string_length = a;
9301           break;
9302         case DW_AT_threads_scaled:
9303           attrs->at_threads_scaled = a;
9304           break;
9305         case DW_AT_upper_bound:
9306           attrs->at_upper_bound = a;
9307           break;
9308         case DW_AT_use_location:
9309           attrs->at_use_location = a;
9310           break;
9311         case DW_AT_use_UTF8:
9312           attrs->at_use_UTF8 = a;
9313           break;
9314         case DW_AT_variable_parameter:
9315           attrs->at_variable_parameter = a;
9316           break;
9317         case DW_AT_virtuality:
9318           attrs->at_virtuality = a;
9319           break;
9320         case DW_AT_visibility:
9321           attrs->at_visibility = a;
9322           break;
9323         case DW_AT_vtable_elem_location:
9324           attrs->at_vtable_elem_location = a;
9325           break;
9326         default:
9327           break;
9328         }
9329     }
9330 }
9331
9332 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9333
9334 static void
9335 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9336 {
9337   dw_die_ref c;
9338   dw_die_ref decl;
9339   struct checksum_attributes attrs;
9340
9341   CHECKSUM_ULEB128 ('D');
9342   CHECKSUM_ULEB128 (die->die_tag);
9343
9344   memset (&attrs, 0, sizeof (attrs));
9345
9346   decl = get_AT_ref (die, DW_AT_specification);
9347   if (decl != NULL)
9348     collect_checksum_attributes (&attrs, decl);
9349   collect_checksum_attributes (&attrs, die);
9350
9351   CHECKSUM_ATTR (attrs.at_name);
9352   CHECKSUM_ATTR (attrs.at_accessibility);
9353   CHECKSUM_ATTR (attrs.at_address_class);
9354   CHECKSUM_ATTR (attrs.at_allocated);
9355   CHECKSUM_ATTR (attrs.at_artificial);
9356   CHECKSUM_ATTR (attrs.at_associated);
9357   CHECKSUM_ATTR (attrs.at_binary_scale);
9358   CHECKSUM_ATTR (attrs.at_bit_offset);
9359   CHECKSUM_ATTR (attrs.at_bit_size);
9360   CHECKSUM_ATTR (attrs.at_bit_stride);
9361   CHECKSUM_ATTR (attrs.at_byte_size);
9362   CHECKSUM_ATTR (attrs.at_byte_stride);
9363   CHECKSUM_ATTR (attrs.at_const_value);
9364   CHECKSUM_ATTR (attrs.at_containing_type);
9365   CHECKSUM_ATTR (attrs.at_count);
9366   CHECKSUM_ATTR (attrs.at_data_location);
9367   CHECKSUM_ATTR (attrs.at_data_member_location);
9368   CHECKSUM_ATTR (attrs.at_decimal_scale);
9369   CHECKSUM_ATTR (attrs.at_decimal_sign);
9370   CHECKSUM_ATTR (attrs.at_default_value);
9371   CHECKSUM_ATTR (attrs.at_digit_count);
9372   CHECKSUM_ATTR (attrs.at_discr);
9373   CHECKSUM_ATTR (attrs.at_discr_list);
9374   CHECKSUM_ATTR (attrs.at_discr_value);
9375   CHECKSUM_ATTR (attrs.at_encoding);
9376   CHECKSUM_ATTR (attrs.at_endianity);
9377   CHECKSUM_ATTR (attrs.at_explicit);
9378   CHECKSUM_ATTR (attrs.at_is_optional);
9379   CHECKSUM_ATTR (attrs.at_location);
9380   CHECKSUM_ATTR (attrs.at_lower_bound);
9381   CHECKSUM_ATTR (attrs.at_mutable);
9382   CHECKSUM_ATTR (attrs.at_ordering);
9383   CHECKSUM_ATTR (attrs.at_picture_string);
9384   CHECKSUM_ATTR (attrs.at_prototyped);
9385   CHECKSUM_ATTR (attrs.at_small);
9386   CHECKSUM_ATTR (attrs.at_segment);
9387   CHECKSUM_ATTR (attrs.at_string_length);
9388   CHECKSUM_ATTR (attrs.at_threads_scaled);
9389   CHECKSUM_ATTR (attrs.at_upper_bound);
9390   CHECKSUM_ATTR (attrs.at_use_location);
9391   CHECKSUM_ATTR (attrs.at_use_UTF8);
9392   CHECKSUM_ATTR (attrs.at_variable_parameter);
9393   CHECKSUM_ATTR (attrs.at_virtuality);
9394   CHECKSUM_ATTR (attrs.at_visibility);
9395   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9396   CHECKSUM_ATTR (attrs.at_type);
9397   CHECKSUM_ATTR (attrs.at_friend);
9398
9399   /* Checksum the child DIEs, except for nested types and member functions.  */
9400   c = die->die_child;
9401   if (c) do {
9402     dw_attr_ref name_attr;
9403
9404     c = c->die_sib;
9405     name_attr = get_AT (c, DW_AT_name);
9406     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9407         && name_attr != NULL)
9408       {
9409         CHECKSUM_ULEB128 ('S');
9410         CHECKSUM_ULEB128 (c->die_tag);
9411         CHECKSUM_STRING (AT_string (name_attr));
9412       }
9413     else
9414       {
9415         /* Mark this DIE so it gets processed when unmarking.  */
9416         if (c->die_mark == 0)
9417           c->die_mark = -1;
9418         die_checksum_ordered (c, ctx, mark);
9419       }
9420   } while (c != die->die_child);
9421
9422   CHECKSUM_ULEB128 (0);
9423 }
9424
9425 #undef CHECKSUM
9426 #undef CHECKSUM_STRING
9427 #undef CHECKSUM_ATTR
9428 #undef CHECKSUM_LEB128
9429 #undef CHECKSUM_ULEB128
9430
9431 /* Generate the type signature for DIE.  This is computed by generating an
9432    MD5 checksum over the DIE's tag, its relevant attributes, and its
9433    children.  Attributes that are references to other DIEs are processed
9434    by recursion, using the MARK field to prevent infinite recursion.
9435    If the DIE is nested inside a namespace or another type, we also
9436    need to include that context in the signature.  The lower 64 bits
9437    of the resulting MD5 checksum comprise the signature.  */
9438
9439 static void
9440 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9441 {
9442   int mark;
9443   const char *name;
9444   unsigned char checksum[16];
9445   struct md5_ctx ctx;
9446   dw_die_ref decl;
9447
9448   name = get_AT_string (die, DW_AT_name);
9449   decl = get_AT_ref (die, DW_AT_specification);
9450
9451   /* First, compute a signature for just the type name (and its surrounding
9452      context, if any.  This is stored in the type unit DIE for link-time
9453      ODR (one-definition rule) checking.  */
9454
9455   if (is_cxx() && name != NULL)
9456     {
9457       md5_init_ctx (&ctx);
9458
9459       /* Checksum the names of surrounding namespaces and structures.  */
9460       if (decl != NULL && decl->die_parent != NULL)
9461         checksum_die_context (decl->die_parent, &ctx);
9462
9463       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9464       md5_process_bytes (name, strlen (name) + 1, &ctx);
9465       md5_finish_ctx (&ctx, checksum);
9466
9467       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9468     }
9469
9470   /* Next, compute the complete type signature.  */
9471
9472   md5_init_ctx (&ctx);
9473   mark = 1;
9474   die->die_mark = mark;
9475
9476   /* Checksum the names of surrounding namespaces and structures.  */
9477   if (decl != NULL && decl->die_parent != NULL)
9478     checksum_die_context (decl->die_parent, &ctx);
9479
9480   /* Checksum the DIE and its children.  */
9481   die_checksum_ordered (die, &ctx, &mark);
9482   unmark_all_dies (die);
9483   md5_finish_ctx (&ctx, checksum);
9484
9485   /* Store the signature in the type node and link the type DIE and the
9486      type node together.  */
9487   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9488           DWARF_TYPE_SIGNATURE_SIZE);
9489   die->die_id.die_type_node = type_node;
9490   type_node->type_die = die;
9491
9492   /* If the DIE is a specification, link its declaration to the type node
9493      as well.  */
9494   if (decl != NULL)
9495     decl->die_id.die_type_node = type_node;
9496 }
9497
9498 /* Do the location expressions look same?  */
9499 static inline int
9500 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9501 {
9502   return loc1->dw_loc_opc == loc2->dw_loc_opc
9503          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9504          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9505 }
9506
9507 /* Do the values look the same?  */
9508 static int
9509 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9510 {
9511   dw_loc_descr_ref loc1, loc2;
9512   rtx r1, r2;
9513
9514   if (v1->val_class != v2->val_class)
9515     return 0;
9516
9517   switch (v1->val_class)
9518     {
9519     case dw_val_class_const:
9520       return v1->v.val_int == v2->v.val_int;
9521     case dw_val_class_unsigned_const:
9522       return v1->v.val_unsigned == v2->v.val_unsigned;
9523     case dw_val_class_const_double:
9524       return v1->v.val_double.high == v2->v.val_double.high
9525              && v1->v.val_double.low == v2->v.val_double.low;
9526     case dw_val_class_vec:
9527       if (v1->v.val_vec.length != v2->v.val_vec.length
9528           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9529         return 0;
9530       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9531                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9532         return 0;
9533       return 1;
9534     case dw_val_class_flag:
9535       return v1->v.val_flag == v2->v.val_flag;
9536     case dw_val_class_str:
9537       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9538
9539     case dw_val_class_addr:
9540       r1 = v1->v.val_addr;
9541       r2 = v2->v.val_addr;
9542       if (GET_CODE (r1) != GET_CODE (r2))
9543         return 0;
9544       return !rtx_equal_p (r1, r2);
9545
9546     case dw_val_class_offset:
9547       return v1->v.val_offset == v2->v.val_offset;
9548
9549     case dw_val_class_loc:
9550       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9551            loc1 && loc2;
9552            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9553         if (!same_loc_p (loc1, loc2, mark))
9554           return 0;
9555       return !loc1 && !loc2;
9556
9557     case dw_val_class_die_ref:
9558       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9559
9560     case dw_val_class_fde_ref:
9561     case dw_val_class_vms_delta:
9562     case dw_val_class_lbl_id:
9563     case dw_val_class_lineptr:
9564     case dw_val_class_macptr:
9565       return 1;
9566
9567     case dw_val_class_file:
9568       return v1->v.val_file == v2->v.val_file;
9569
9570     case dw_val_class_data8:
9571       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9572
9573     default:
9574       return 1;
9575     }
9576 }
9577
9578 /* Do the attributes look the same?  */
9579
9580 static int
9581 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9582 {
9583   if (at1->dw_attr != at2->dw_attr)
9584     return 0;
9585
9586   /* We don't care that this was compiled with a different compiler
9587      snapshot; if the output is the same, that's what matters. */
9588   if (at1->dw_attr == DW_AT_producer)
9589     return 1;
9590
9591   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9592 }
9593
9594 /* Do the dies look the same?  */
9595
9596 static int
9597 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9598 {
9599   dw_die_ref c1, c2;
9600   dw_attr_ref a1;
9601   unsigned ix;
9602
9603   /* To avoid infinite recursion.  */
9604   if (die1->die_mark)
9605     return die1->die_mark == die2->die_mark;
9606   die1->die_mark = die2->die_mark = ++(*mark);
9607
9608   if (die1->die_tag != die2->die_tag)
9609     return 0;
9610
9611   if (VEC_length (dw_attr_node, die1->die_attr)
9612       != VEC_length (dw_attr_node, die2->die_attr))
9613     return 0;
9614
9615   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9616     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9617       return 0;
9618
9619   c1 = die1->die_child;
9620   c2 = die2->die_child;
9621   if (! c1)
9622     {
9623       if (c2)
9624         return 0;
9625     }
9626   else
9627     for (;;)
9628       {
9629         if (!same_die_p (c1, c2, mark))
9630           return 0;
9631         c1 = c1->die_sib;
9632         c2 = c2->die_sib;
9633         if (c1 == die1->die_child)
9634           {
9635             if (c2 == die2->die_child)
9636               break;
9637             else
9638               return 0;
9639           }
9640     }
9641
9642   return 1;
9643 }
9644
9645 /* Do the dies look the same?  Wrapper around same_die_p.  */
9646
9647 static int
9648 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9649 {
9650   int mark = 0;
9651   int ret = same_die_p (die1, die2, &mark);
9652
9653   unmark_all_dies (die1);
9654   unmark_all_dies (die2);
9655
9656   return ret;
9657 }
9658
9659 /* The prefix to attach to symbols on DIEs in the current comdat debug
9660    info section.  */
9661 static char *comdat_symbol_id;
9662
9663 /* The index of the current symbol within the current comdat CU.  */
9664 static unsigned int comdat_symbol_number;
9665
9666 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9667    children, and set comdat_symbol_id accordingly.  */
9668
9669 static void
9670 compute_section_prefix (dw_die_ref unit_die)
9671 {
9672   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9673   const char *base = die_name ? lbasename (die_name) : "anonymous";
9674   char *name = XALLOCAVEC (char, strlen (base) + 64);
9675   char *p;
9676   int i, mark;
9677   unsigned char checksum[16];
9678   struct md5_ctx ctx;
9679
9680   /* Compute the checksum of the DIE, then append part of it as hex digits to
9681      the name filename of the unit.  */
9682
9683   md5_init_ctx (&ctx);
9684   mark = 0;
9685   die_checksum (unit_die, &ctx, &mark);
9686   unmark_all_dies (unit_die);
9687   md5_finish_ctx (&ctx, checksum);
9688
9689   sprintf (name, "%s.", base);
9690   clean_symbol_name (name);
9691
9692   p = name + strlen (name);
9693   for (i = 0; i < 4; i++)
9694     {
9695       sprintf (p, "%.2x", checksum[i]);
9696       p += 2;
9697     }
9698
9699   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9700   comdat_symbol_number = 0;
9701 }
9702
9703 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9704
9705 static int
9706 is_type_die (dw_die_ref die)
9707 {
9708   switch (die->die_tag)
9709     {
9710     case DW_TAG_array_type:
9711     case DW_TAG_class_type:
9712     case DW_TAG_interface_type:
9713     case DW_TAG_enumeration_type:
9714     case DW_TAG_pointer_type:
9715     case DW_TAG_reference_type:
9716     case DW_TAG_rvalue_reference_type:
9717     case DW_TAG_string_type:
9718     case DW_TAG_structure_type:
9719     case DW_TAG_subroutine_type:
9720     case DW_TAG_union_type:
9721     case DW_TAG_ptr_to_member_type:
9722     case DW_TAG_set_type:
9723     case DW_TAG_subrange_type:
9724     case DW_TAG_base_type:
9725     case DW_TAG_const_type:
9726     case DW_TAG_file_type:
9727     case DW_TAG_packed_type:
9728     case DW_TAG_volatile_type:
9729     case DW_TAG_typedef:
9730       return 1;
9731     default:
9732       return 0;
9733     }
9734 }
9735
9736 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9737    Basically, we want to choose the bits that are likely to be shared between
9738    compilations (types) and leave out the bits that are specific to individual
9739    compilations (functions).  */
9740
9741 static int
9742 is_comdat_die (dw_die_ref c)
9743 {
9744   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9745      we do for stabs.  The advantage is a greater likelihood of sharing between
9746      objects that don't include headers in the same order (and therefore would
9747      put the base types in a different comdat).  jason 8/28/00 */
9748
9749   if (c->die_tag == DW_TAG_base_type)
9750     return 0;
9751
9752   if (c->die_tag == DW_TAG_pointer_type
9753       || c->die_tag == DW_TAG_reference_type
9754       || c->die_tag == DW_TAG_rvalue_reference_type
9755       || c->die_tag == DW_TAG_const_type
9756       || c->die_tag == DW_TAG_volatile_type)
9757     {
9758       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9759
9760       return t ? is_comdat_die (t) : 0;
9761     }
9762
9763   return is_type_die (c);
9764 }
9765
9766 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9767    compilation unit.  */
9768
9769 static int
9770 is_symbol_die (dw_die_ref c)
9771 {
9772   return (is_type_die (c)
9773           || is_declaration_die (c)
9774           || c->die_tag == DW_TAG_namespace
9775           || c->die_tag == DW_TAG_module);
9776 }
9777
9778 /* Returns true iff C is a compile-unit DIE.  */
9779
9780 static inline bool
9781 is_cu_die (dw_die_ref c)
9782 {
9783   return c && c->die_tag == DW_TAG_compile_unit;
9784 }
9785
9786 static char *
9787 gen_internal_sym (const char *prefix)
9788 {
9789   char buf[256];
9790
9791   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9792   return xstrdup (buf);
9793 }
9794
9795 /* Assign symbols to all worthy DIEs under DIE.  */
9796
9797 static void
9798 assign_symbol_names (dw_die_ref die)
9799 {
9800   dw_die_ref c;
9801
9802   if (is_symbol_die (die))
9803     {
9804       if (comdat_symbol_id)
9805         {
9806           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9807
9808           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9809                    comdat_symbol_id, comdat_symbol_number++);
9810           die->die_id.die_symbol = xstrdup (p);
9811         }
9812       else
9813         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9814     }
9815
9816   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9817 }
9818
9819 struct cu_hash_table_entry
9820 {
9821   dw_die_ref cu;
9822   unsigned min_comdat_num, max_comdat_num;
9823   struct cu_hash_table_entry *next;
9824 };
9825
9826 /* Routines to manipulate hash table of CUs.  */
9827 static hashval_t
9828 htab_cu_hash (const void *of)
9829 {
9830   const struct cu_hash_table_entry *const entry =
9831     (const struct cu_hash_table_entry *) of;
9832
9833   return htab_hash_string (entry->cu->die_id.die_symbol);
9834 }
9835
9836 static int
9837 htab_cu_eq (const void *of1, const void *of2)
9838 {
9839   const struct cu_hash_table_entry *const entry1 =
9840     (const struct cu_hash_table_entry *) of1;
9841   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9842
9843   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9844 }
9845
9846 static void
9847 htab_cu_del (void *what)
9848 {
9849   struct cu_hash_table_entry *next,
9850     *entry = (struct cu_hash_table_entry *) what;
9851
9852   while (entry)
9853     {
9854       next = entry->next;
9855       free (entry);
9856       entry = next;
9857     }
9858 }
9859
9860 /* Check whether we have already seen this CU and set up SYM_NUM
9861    accordingly.  */
9862 static int
9863 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9864 {
9865   struct cu_hash_table_entry dummy;
9866   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9867
9868   dummy.max_comdat_num = 0;
9869
9870   slot = (struct cu_hash_table_entry **)
9871     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9872         INSERT);
9873   entry = *slot;
9874
9875   for (; entry; last = entry, entry = entry->next)
9876     {
9877       if (same_die_p_wrap (cu, entry->cu))
9878         break;
9879     }
9880
9881   if (entry)
9882     {
9883       *sym_num = entry->min_comdat_num;
9884       return 1;
9885     }
9886
9887   entry = XCNEW (struct cu_hash_table_entry);
9888   entry->cu = cu;
9889   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9890   entry->next = *slot;
9891   *slot = entry;
9892
9893   return 0;
9894 }
9895
9896 /* Record SYM_NUM to record of CU in HTABLE.  */
9897 static void
9898 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9899 {
9900   struct cu_hash_table_entry **slot, *entry;
9901
9902   slot = (struct cu_hash_table_entry **)
9903     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9904         NO_INSERT);
9905   entry = *slot;
9906
9907   entry->max_comdat_num = sym_num;
9908 }
9909
9910 /* Traverse the DIE (which is always comp_unit_die), and set up
9911    additional compilation units for each of the include files we see
9912    bracketed by BINCL/EINCL.  */
9913
9914 static void
9915 break_out_includes (dw_die_ref die)
9916 {
9917   dw_die_ref c;
9918   dw_die_ref unit = NULL;
9919   limbo_die_node *node, **pnode;
9920   htab_t cu_hash_table;
9921
9922   c = die->die_child;
9923   if (c) do {
9924     dw_die_ref prev = c;
9925     c = c->die_sib;
9926     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9927            || (unit && is_comdat_die (c)))
9928       {
9929         dw_die_ref next = c->die_sib;
9930
9931         /* This DIE is for a secondary CU; remove it from the main one.  */
9932         remove_child_with_prev (c, prev);
9933
9934         if (c->die_tag == DW_TAG_GNU_BINCL)
9935           unit = push_new_compile_unit (unit, c);
9936         else if (c->die_tag == DW_TAG_GNU_EINCL)
9937           unit = pop_compile_unit (unit);
9938         else
9939           add_child_die (unit, c);
9940         c = next;
9941         if (c == die->die_child)
9942           break;
9943       }
9944   } while (c != die->die_child);
9945
9946 #if 0
9947   /* We can only use this in debugging, since the frontend doesn't check
9948      to make sure that we leave every include file we enter.  */
9949   gcc_assert (!unit);
9950 #endif
9951
9952   assign_symbol_names (die);
9953   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9954   for (node = limbo_die_list, pnode = &limbo_die_list;
9955        node;
9956        node = node->next)
9957     {
9958       int is_dupl;
9959
9960       compute_section_prefix (node->die);
9961       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9962                         &comdat_symbol_number);
9963       assign_symbol_names (node->die);
9964       if (is_dupl)
9965         *pnode = node->next;
9966       else
9967         {
9968           pnode = &node->next;
9969           record_comdat_symbol_number (node->die, cu_hash_table,
9970                 comdat_symbol_number);
9971         }
9972     }
9973   htab_delete (cu_hash_table);
9974 }
9975
9976 /* Return non-zero if this DIE is a declaration.  */
9977
9978 static int
9979 is_declaration_die (dw_die_ref die)
9980 {
9981   dw_attr_ref a;
9982   unsigned ix;
9983
9984   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9985     if (a->dw_attr == DW_AT_declaration)
9986       return 1;
9987
9988   return 0;
9989 }
9990
9991 /* Return non-zero if this DIE is nested inside a subprogram.  */
9992
9993 static int
9994 is_nested_in_subprogram (dw_die_ref die)
9995 {
9996   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9997
9998   if (decl == NULL)
9999     decl = die;
10000   return local_scope_p (decl);
10001 }
10002
10003 /* Return non-zero if this is a type DIE that should be moved to a
10004    COMDAT .debug_types section.  */
10005
10006 static int
10007 should_move_die_to_comdat (dw_die_ref die)
10008 {
10009   switch (die->die_tag)
10010     {
10011     case DW_TAG_class_type:
10012     case DW_TAG_structure_type:
10013     case DW_TAG_enumeration_type:
10014     case DW_TAG_union_type:
10015       /* Don't move declarations, inlined instances, or types nested in a
10016          subprogram.  */
10017       if (is_declaration_die (die)
10018           || get_AT (die, DW_AT_abstract_origin)
10019           || is_nested_in_subprogram (die))
10020         return 0;
10021       return 1;
10022     case DW_TAG_array_type:
10023     case DW_TAG_interface_type:
10024     case DW_TAG_pointer_type:
10025     case DW_TAG_reference_type:
10026     case DW_TAG_rvalue_reference_type:
10027     case DW_TAG_string_type:
10028     case DW_TAG_subroutine_type:
10029     case DW_TAG_ptr_to_member_type:
10030     case DW_TAG_set_type:
10031     case DW_TAG_subrange_type:
10032     case DW_TAG_base_type:
10033     case DW_TAG_const_type:
10034     case DW_TAG_file_type:
10035     case DW_TAG_packed_type:
10036     case DW_TAG_volatile_type:
10037     case DW_TAG_typedef:
10038     default:
10039       return 0;
10040     }
10041 }
10042
10043 /* Make a clone of DIE.  */
10044
10045 static dw_die_ref
10046 clone_die (dw_die_ref die)
10047 {
10048   dw_die_ref clone;
10049   dw_attr_ref a;
10050   unsigned ix;
10051
10052   clone = ggc_alloc_cleared_die_node ();
10053   clone->die_tag = die->die_tag;
10054
10055   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10056     add_dwarf_attr (clone, a);
10057
10058   return clone;
10059 }
10060
10061 /* Make a clone of the tree rooted at DIE.  */
10062
10063 static dw_die_ref
10064 clone_tree (dw_die_ref die)
10065 {
10066   dw_die_ref c;
10067   dw_die_ref clone = clone_die (die);
10068
10069   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10070
10071   return clone;
10072 }
10073
10074 /* Make a clone of DIE as a declaration.  */
10075
10076 static dw_die_ref
10077 clone_as_declaration (dw_die_ref die)
10078 {
10079   dw_die_ref clone;
10080   dw_die_ref decl;
10081   dw_attr_ref a;
10082   unsigned ix;
10083
10084   /* If the DIE is already a declaration, just clone it.  */
10085   if (is_declaration_die (die))
10086     return clone_die (die);
10087
10088   /* If the DIE is a specification, just clone its declaration DIE.  */
10089   decl = get_AT_ref (die, DW_AT_specification);
10090   if (decl != NULL)
10091     return clone_die (decl);
10092
10093   clone = ggc_alloc_cleared_die_node ();
10094   clone->die_tag = die->die_tag;
10095
10096   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10097     {
10098       /* We don't want to copy over all attributes.
10099          For example we don't want DW_AT_byte_size because otherwise we will no
10100          longer have a declaration and GDB will treat it as a definition.  */
10101
10102       switch (a->dw_attr)
10103         {
10104         case DW_AT_artificial:
10105         case DW_AT_containing_type:
10106         case DW_AT_external:
10107         case DW_AT_name:
10108         case DW_AT_type:
10109         case DW_AT_virtuality:
10110         case DW_AT_linkage_name:
10111         case DW_AT_MIPS_linkage_name:
10112           add_dwarf_attr (clone, a);
10113           break;
10114         case DW_AT_byte_size:
10115         default:
10116           break;
10117         }
10118     }
10119
10120   if (die->die_id.die_type_node)
10121     add_AT_die_ref (clone, DW_AT_signature, die);
10122
10123   add_AT_flag (clone, DW_AT_declaration, 1);
10124   return clone;
10125 }
10126
10127 /* Copy the declaration context to the new compile unit DIE.  This includes
10128    any surrounding namespace or type declarations.  If the DIE has an
10129    AT_specification attribute, it also includes attributes and children
10130    attached to the specification.  */
10131
10132 static void
10133 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10134 {
10135   dw_die_ref decl;
10136   dw_die_ref new_decl;
10137
10138   decl = get_AT_ref (die, DW_AT_specification);
10139   if (decl == NULL)
10140     decl = die;
10141   else
10142     {
10143       unsigned ix;
10144       dw_die_ref c;
10145       dw_attr_ref a;
10146
10147       /* Copy the type node pointer from the new DIE to the original
10148          declaration DIE so we can forward references later.  */
10149       decl->die_id.die_type_node = die->die_id.die_type_node;
10150
10151       remove_AT (die, DW_AT_specification);
10152
10153       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10154         {
10155           if (a->dw_attr != DW_AT_name
10156               && a->dw_attr != DW_AT_declaration
10157               && a->dw_attr != DW_AT_external)
10158             add_dwarf_attr (die, a);
10159         }
10160
10161       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10162     }
10163
10164   if (decl->die_parent != NULL
10165       && decl->die_parent->die_tag != DW_TAG_compile_unit
10166       && decl->die_parent->die_tag != DW_TAG_type_unit)
10167     {
10168       new_decl = copy_ancestor_tree (unit, decl, NULL);
10169       if (new_decl != NULL)
10170         {
10171           remove_AT (new_decl, DW_AT_signature);
10172           add_AT_specification (die, new_decl);
10173         }
10174     }
10175 }
10176
10177 /* Generate the skeleton ancestor tree for the given NODE, then clone
10178    the DIE and add the clone into the tree.  */
10179
10180 static void
10181 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10182 {
10183   if (node->new_die != NULL)
10184     return;
10185
10186   node->new_die = clone_as_declaration (node->old_die);
10187
10188   if (node->parent != NULL)
10189     {
10190       generate_skeleton_ancestor_tree (node->parent);
10191       add_child_die (node->parent->new_die, node->new_die);
10192     }
10193 }
10194
10195 /* Generate a skeleton tree of DIEs containing any declarations that are
10196    found in the original tree.  We traverse the tree looking for declaration
10197    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10198
10199 static void
10200 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10201 {
10202   skeleton_chain_node node;
10203   dw_die_ref c;
10204   dw_die_ref first;
10205   dw_die_ref prev = NULL;
10206   dw_die_ref next = NULL;
10207
10208   node.parent = parent;
10209
10210   first = c = parent->old_die->die_child;
10211   if (c)
10212     next = c->die_sib;
10213   if (c) do {
10214     if (prev == NULL || prev->die_sib == c)
10215       prev = c;
10216     c = next;
10217     next = (c == first ? NULL : c->die_sib);
10218     node.old_die = c;
10219     node.new_die = NULL;
10220     if (is_declaration_die (c))
10221       {
10222         /* Clone the existing DIE, move the original to the skeleton
10223            tree (which is in the main CU), and put the clone, with
10224            all the original's children, where the original came from.  */
10225         dw_die_ref clone = clone_die (c);
10226         move_all_children (c, clone);
10227
10228         replace_child (c, clone, prev);
10229         generate_skeleton_ancestor_tree (parent);
10230         add_child_die (parent->new_die, c);
10231         node.new_die = c;
10232         c = clone;
10233       }
10234     generate_skeleton_bottom_up (&node);
10235   } while (next != NULL);
10236 }
10237
10238 /* Wrapper function for generate_skeleton_bottom_up.  */
10239
10240 static dw_die_ref
10241 generate_skeleton (dw_die_ref die)
10242 {
10243   skeleton_chain_node node;
10244
10245   node.old_die = die;
10246   node.new_die = NULL;
10247   node.parent = NULL;
10248
10249   /* If this type definition is nested inside another type,
10250      always leave at least a declaration in its place.  */
10251   if (die->die_parent != NULL && is_type_die (die->die_parent))
10252     node.new_die = clone_as_declaration (die);
10253
10254   generate_skeleton_bottom_up (&node);
10255   return node.new_die;
10256 }
10257
10258 /* Remove the DIE from its parent, possibly replacing it with a cloned
10259    declaration.  The original DIE will be moved to a new compile unit
10260    so that existing references to it follow it to the new location.  If
10261    any of the original DIE's descendants is a declaration, we need to
10262    replace the original DIE with a skeleton tree and move the
10263    declarations back into the skeleton tree.  */
10264
10265 static dw_die_ref
10266 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10267 {
10268   dw_die_ref skeleton;
10269
10270   skeleton = generate_skeleton (child);
10271   if (skeleton == NULL)
10272     remove_child_with_prev (child, prev);
10273   else
10274     {
10275       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10276       replace_child (child, skeleton, prev);
10277     }
10278
10279   return skeleton;
10280 }
10281
10282 /* Traverse the DIE and set up additional .debug_types sections for each
10283    type worthy of being placed in a COMDAT section.  */
10284
10285 static void
10286 break_out_comdat_types (dw_die_ref die)
10287 {
10288   dw_die_ref c;
10289   dw_die_ref first;
10290   dw_die_ref prev = NULL;
10291   dw_die_ref next = NULL;
10292   dw_die_ref unit = NULL;
10293
10294   first = c = die->die_child;
10295   if (c)
10296     next = c->die_sib;
10297   if (c) do {
10298     if (prev == NULL || prev->die_sib == c)
10299       prev = c;
10300     c = next;
10301     next = (c == first ? NULL : c->die_sib);
10302     if (should_move_die_to_comdat (c))
10303       {
10304         dw_die_ref replacement;
10305         comdat_type_node_ref type_node;
10306
10307         /* Create a new type unit DIE as the root for the new tree, and
10308            add it to the list of comdat types.  */
10309         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10310         add_AT_unsigned (unit, DW_AT_language,
10311                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10312         type_node = ggc_alloc_cleared_comdat_type_node ();
10313         type_node->root_die = unit;
10314         type_node->next = comdat_type_list;
10315         comdat_type_list = type_node;
10316
10317         /* Generate the type signature.  */
10318         generate_type_signature (c, type_node);
10319
10320         /* Copy the declaration context, attributes, and children of the
10321            declaration into the new compile unit DIE.  */
10322         copy_declaration_context (unit, c);
10323
10324         /* Remove this DIE from the main CU.  */
10325         replacement = remove_child_or_replace_with_skeleton (c, prev);
10326
10327         /* Break out nested types into their own type units.  */
10328         break_out_comdat_types (c);
10329
10330         /* Add the DIE to the new compunit.  */
10331         add_child_die (unit, c);
10332
10333         if (replacement != NULL)
10334           c = replacement;
10335       }
10336     else if (c->die_tag == DW_TAG_namespace
10337              || c->die_tag == DW_TAG_class_type
10338              || c->die_tag == DW_TAG_structure_type
10339              || c->die_tag == DW_TAG_union_type)
10340       {
10341         /* Look for nested types that can be broken out.  */
10342         break_out_comdat_types (c);
10343       }
10344   } while (next != NULL);
10345 }
10346
10347 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10348
10349 struct decl_table_entry
10350 {
10351   dw_die_ref orig;
10352   dw_die_ref copy;
10353 };
10354
10355 /* Routines to manipulate hash table of copied declarations.  */
10356
10357 static hashval_t
10358 htab_decl_hash (const void *of)
10359 {
10360   const struct decl_table_entry *const entry =
10361     (const struct decl_table_entry *) of;
10362
10363   return htab_hash_pointer (entry->orig);
10364 }
10365
10366 static int
10367 htab_decl_eq (const void *of1, const void *of2)
10368 {
10369   const struct decl_table_entry *const entry1 =
10370     (const struct decl_table_entry *) of1;
10371   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10372
10373   return entry1->orig == entry2;
10374 }
10375
10376 static void
10377 htab_decl_del (void *what)
10378 {
10379   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10380
10381   free (entry);
10382 }
10383
10384 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10385    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10386    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10387    to check if the ancestor has already been copied into UNIT.  */
10388
10389 static dw_die_ref
10390 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10391 {
10392   dw_die_ref parent = die->die_parent;
10393   dw_die_ref new_parent = unit;
10394   dw_die_ref copy;
10395   void **slot = NULL;
10396   struct decl_table_entry *entry = NULL;
10397
10398   if (decl_table)
10399     {
10400       /* Check if the entry has already been copied to UNIT.  */
10401       slot = htab_find_slot_with_hash (decl_table, die,
10402                                        htab_hash_pointer (die), INSERT);
10403       if (*slot != HTAB_EMPTY_ENTRY)
10404         {
10405           entry = (struct decl_table_entry *) *slot;
10406           return entry->copy;
10407         }
10408
10409       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10410       entry = XCNEW (struct decl_table_entry);
10411       entry->orig = die;
10412       entry->copy = NULL;
10413       *slot = entry;
10414     }
10415
10416   if (parent != NULL)
10417     {
10418       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10419       if (spec != NULL)
10420         parent = spec;
10421       if (parent->die_tag != DW_TAG_compile_unit
10422           && parent->die_tag != DW_TAG_type_unit)
10423         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10424     }
10425
10426   copy = clone_as_declaration (die);
10427   add_child_die (new_parent, copy);
10428
10429   if (decl_table != NULL)
10430     {
10431       /* Record the pointer to the copy.  */
10432       entry->copy = copy;
10433     }
10434
10435   return copy;
10436 }
10437
10438 /* Walk the DIE and its children, looking for references to incomplete
10439    or trivial types that are unmarked (i.e., that are not in the current
10440    type_unit).  */
10441
10442 static void
10443 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10444 {
10445   dw_die_ref c;
10446   dw_attr_ref a;
10447   unsigned ix;
10448
10449   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10450     {
10451       if (AT_class (a) == dw_val_class_die_ref)
10452         {
10453           dw_die_ref targ = AT_ref (a);
10454           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10455           void **slot;
10456           struct decl_table_entry *entry;
10457
10458           if (targ->die_mark != 0 || type_node != NULL)
10459             continue;
10460
10461           slot = htab_find_slot_with_hash (decl_table, targ,
10462                                            htab_hash_pointer (targ), INSERT);
10463
10464           if (*slot != HTAB_EMPTY_ENTRY)
10465             {
10466               /* TARG has already been copied, so we just need to
10467                  modify the reference to point to the copy.  */
10468               entry = (struct decl_table_entry *) *slot;
10469               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10470             }
10471           else
10472             {
10473               dw_die_ref parent = unit;
10474               dw_die_ref copy = clone_tree (targ);
10475
10476               /* Make sure the cloned tree is marked as part of the
10477                  type unit.  */
10478               mark_dies (copy);
10479
10480               /* Record in DECL_TABLE that TARG has been copied.
10481                  Need to do this now, before the recursive call,
10482                  because DECL_TABLE may be expanded and SLOT
10483                  would no longer be a valid pointer.  */
10484               entry = XCNEW (struct decl_table_entry);
10485               entry->orig = targ;
10486               entry->copy = copy;
10487               *slot = entry;
10488
10489               /* If TARG has surrounding context, copy its ancestor tree
10490                  into the new type unit.  */
10491               if (targ->die_parent != NULL
10492                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10493                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10494                 parent = copy_ancestor_tree (unit, targ->die_parent,
10495                                              decl_table);
10496
10497               add_child_die (parent, copy);
10498               a->dw_attr_val.v.val_die_ref.die = copy;
10499
10500               /* Make sure the newly-copied DIE is walked.  If it was
10501                  installed in a previously-added context, it won't
10502                  get visited otherwise.  */
10503               if (parent != unit)
10504                 {
10505                   /* Find the highest point of the newly-added tree,
10506                      mark each node along the way, and walk from there.  */
10507                   parent->die_mark = 1;
10508                   while (parent->die_parent
10509                          && parent->die_parent->die_mark == 0)
10510                     {
10511                       parent = parent->die_parent;
10512                       parent->die_mark = 1;
10513                     }
10514                   copy_decls_walk (unit, parent, decl_table);
10515                 }
10516             }
10517         }
10518     }
10519
10520   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10521 }
10522
10523 /* Copy declarations for "unworthy" types into the new comdat section.
10524    Incomplete types, modified types, and certain other types aren't broken
10525    out into comdat sections of their own, so they don't have a signature,
10526    and we need to copy the declaration into the same section so that we
10527    don't have an external reference.  */
10528
10529 static void
10530 copy_decls_for_unworthy_types (dw_die_ref unit)
10531 {
10532   htab_t decl_table;
10533
10534   mark_dies (unit);
10535   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10536   copy_decls_walk (unit, unit, decl_table);
10537   htab_delete (decl_table);
10538   unmark_dies (unit);
10539 }
10540
10541 /* Traverse the DIE and add a sibling attribute if it may have the
10542    effect of speeding up access to siblings.  To save some space,
10543    avoid generating sibling attributes for DIE's without children.  */
10544
10545 static void
10546 add_sibling_attributes (dw_die_ref die)
10547 {
10548   dw_die_ref c;
10549
10550   if (! die->die_child)
10551     return;
10552
10553   if (die->die_parent && die != die->die_parent->die_child)
10554     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10555
10556   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10557 }
10558
10559 /* Output all location lists for the DIE and its children.  */
10560
10561 static void
10562 output_location_lists (dw_die_ref die)
10563 {
10564   dw_die_ref c;
10565   dw_attr_ref a;
10566   unsigned ix;
10567
10568   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10569     if (AT_class (a) == dw_val_class_loc_list)
10570       output_loc_list (AT_loc_list (a));
10571
10572   FOR_EACH_CHILD (die, c, output_location_lists (c));
10573 }
10574
10575 /* The format of each DIE (and its attribute value pairs) is encoded in an
10576    abbreviation table.  This routine builds the abbreviation table and assigns
10577    a unique abbreviation id for each abbreviation entry.  The children of each
10578    die are visited recursively.  */
10579
10580 static void
10581 build_abbrev_table (dw_die_ref die)
10582 {
10583   unsigned long abbrev_id;
10584   unsigned int n_alloc;
10585   dw_die_ref c;
10586   dw_attr_ref a;
10587   unsigned ix;
10588
10589   /* Scan the DIE references, and mark as external any that refer to
10590      DIEs from other CUs (i.e. those which are not marked).  */
10591   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10592     if (AT_class (a) == dw_val_class_die_ref
10593         && AT_ref (a)->die_mark == 0)
10594       {
10595         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10596         set_AT_ref_external (a, 1);
10597       }
10598
10599   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10600     {
10601       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10602       dw_attr_ref die_a, abbrev_a;
10603       unsigned ix;
10604       bool ok = true;
10605
10606       if (abbrev->die_tag != die->die_tag)
10607         continue;
10608       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10609         continue;
10610
10611       if (VEC_length (dw_attr_node, abbrev->die_attr)
10612           != VEC_length (dw_attr_node, die->die_attr))
10613         continue;
10614
10615       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10616         {
10617           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10618           if ((abbrev_a->dw_attr != die_a->dw_attr)
10619               || (value_format (abbrev_a) != value_format (die_a)))
10620             {
10621               ok = false;
10622               break;
10623             }
10624         }
10625       if (ok)
10626         break;
10627     }
10628
10629   if (abbrev_id >= abbrev_die_table_in_use)
10630     {
10631       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10632         {
10633           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10634           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10635                                             n_alloc);
10636
10637           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10638                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10639           abbrev_die_table_allocated = n_alloc;
10640         }
10641
10642       ++abbrev_die_table_in_use;
10643       abbrev_die_table[abbrev_id] = die;
10644     }
10645
10646   die->die_abbrev = abbrev_id;
10647   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10648 }
10649 \f
10650 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10651
10652 static int
10653 constant_size (unsigned HOST_WIDE_INT value)
10654 {
10655   int log;
10656
10657   if (value == 0)
10658     log = 0;
10659   else
10660     log = floor_log2 (value);
10661
10662   log = log / 8;
10663   log = 1 << (floor_log2 (log) + 1);
10664
10665   return log;
10666 }
10667
10668 /* Return the size of a DIE as it is represented in the
10669    .debug_info section.  */
10670
10671 static unsigned long
10672 size_of_die (dw_die_ref die)
10673 {
10674   unsigned long size = 0;
10675   dw_attr_ref a;
10676   unsigned ix;
10677
10678   size += size_of_uleb128 (die->die_abbrev);
10679   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10680     {
10681       switch (AT_class (a))
10682         {
10683         case dw_val_class_addr:
10684           size += DWARF2_ADDR_SIZE;
10685           break;
10686         case dw_val_class_offset:
10687           size += DWARF_OFFSET_SIZE;
10688           break;
10689         case dw_val_class_loc:
10690           {
10691             unsigned long lsize = size_of_locs (AT_loc (a));
10692
10693             /* Block length.  */
10694             if (dwarf_version >= 4)
10695               size += size_of_uleb128 (lsize);
10696             else
10697               size += constant_size (lsize);
10698             size += lsize;
10699           }
10700           break;
10701         case dw_val_class_loc_list:
10702           size += DWARF_OFFSET_SIZE;
10703           break;
10704         case dw_val_class_range_list:
10705           size += DWARF_OFFSET_SIZE;
10706           break;
10707         case dw_val_class_const:
10708           size += size_of_sleb128 (AT_int (a));
10709           break;
10710         case dw_val_class_unsigned_const:
10711           size += constant_size (AT_unsigned (a));
10712           break;
10713         case dw_val_class_const_double:
10714           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10715           if (HOST_BITS_PER_WIDE_INT >= 64)
10716             size++; /* block */
10717           break;
10718         case dw_val_class_vec:
10719           size += constant_size (a->dw_attr_val.v.val_vec.length
10720                                  * a->dw_attr_val.v.val_vec.elt_size)
10721                   + a->dw_attr_val.v.val_vec.length
10722                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10723           break;
10724         case dw_val_class_flag:
10725           if (dwarf_version >= 4)
10726             /* Currently all add_AT_flag calls pass in 1 as last argument,
10727                so DW_FORM_flag_present can be used.  If that ever changes,
10728                we'll need to use DW_FORM_flag and have some optimization
10729                in build_abbrev_table that will change those to
10730                DW_FORM_flag_present if it is set to 1 in all DIEs using
10731                the same abbrev entry.  */
10732             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10733           else
10734             size += 1;
10735           break;
10736         case dw_val_class_die_ref:
10737           if (AT_ref_external (a))
10738             {
10739               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10740                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10741                  is sized by target address length, whereas in DWARF3
10742                  it's always sized as an offset.  */
10743               if (use_debug_types)
10744                 size += DWARF_TYPE_SIGNATURE_SIZE;
10745               else if (dwarf_version == 2)
10746                 size += DWARF2_ADDR_SIZE;
10747               else
10748                 size += DWARF_OFFSET_SIZE;
10749             }
10750           else
10751             size += DWARF_OFFSET_SIZE;
10752           break;
10753         case dw_val_class_fde_ref:
10754           size += DWARF_OFFSET_SIZE;
10755           break;
10756         case dw_val_class_lbl_id:
10757           size += DWARF2_ADDR_SIZE;
10758           break;
10759         case dw_val_class_lineptr:
10760         case dw_val_class_macptr:
10761           size += DWARF_OFFSET_SIZE;
10762           break;
10763         case dw_val_class_str:
10764           if (AT_string_form (a) == DW_FORM_strp)
10765             size += DWARF_OFFSET_SIZE;
10766           else
10767             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10768           break;
10769         case dw_val_class_file:
10770           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10771           break;
10772         case dw_val_class_data8:
10773           size += 8;
10774           break;
10775         case dw_val_class_vms_delta:
10776           size += DWARF_OFFSET_SIZE;
10777           break;
10778         default:
10779           gcc_unreachable ();
10780         }
10781     }
10782
10783   return size;
10784 }
10785
10786 /* Size the debugging information associated with a given DIE.  Visits the
10787    DIE's children recursively.  Updates the global variable next_die_offset, on
10788    each time through.  Uses the current value of next_die_offset to update the
10789    die_offset field in each DIE.  */
10790
10791 static void
10792 calc_die_sizes (dw_die_ref die)
10793 {
10794   dw_die_ref c;
10795
10796   die->die_offset = next_die_offset;
10797   next_die_offset += size_of_die (die);
10798
10799   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10800
10801   if (die->die_child != NULL)
10802     /* Count the null byte used to terminate sibling lists.  */
10803     next_die_offset += 1;
10804 }
10805
10806 /* Set the marks for a die and its children.  We do this so
10807    that we know whether or not a reference needs to use FORM_ref_addr; only
10808    DIEs in the same CU will be marked.  We used to clear out the offset
10809    and use that as the flag, but ran into ordering problems.  */
10810
10811 static void
10812 mark_dies (dw_die_ref die)
10813 {
10814   dw_die_ref c;
10815
10816   gcc_assert (!die->die_mark);
10817
10818   die->die_mark = 1;
10819   FOR_EACH_CHILD (die, c, mark_dies (c));
10820 }
10821
10822 /* Clear the marks for a die and its children.  */
10823
10824 static void
10825 unmark_dies (dw_die_ref die)
10826 {
10827   dw_die_ref c;
10828
10829   if (! use_debug_types)
10830     gcc_assert (die->die_mark);
10831
10832   die->die_mark = 0;
10833   FOR_EACH_CHILD (die, c, unmark_dies (c));
10834 }
10835
10836 /* Clear the marks for a die, its children and referred dies.  */
10837
10838 static void
10839 unmark_all_dies (dw_die_ref die)
10840 {
10841   dw_die_ref c;
10842   dw_attr_ref a;
10843   unsigned ix;
10844
10845   if (!die->die_mark)
10846     return;
10847   die->die_mark = 0;
10848
10849   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10850
10851   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10852     if (AT_class (a) == dw_val_class_die_ref)
10853       unmark_all_dies (AT_ref (a));
10854 }
10855
10856 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10857    generated for the compilation unit.  */
10858
10859 static unsigned long
10860 size_of_pubnames (VEC (pubname_entry, gc) * names)
10861 {
10862   unsigned long size;
10863   unsigned i;
10864   pubname_ref p;
10865
10866   size = DWARF_PUBNAMES_HEADER_SIZE;
10867   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10868     if (names != pubtype_table
10869         || p->die->die_offset != 0
10870         || !flag_eliminate_unused_debug_types)
10871       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10872
10873   size += DWARF_OFFSET_SIZE;
10874   return size;
10875 }
10876
10877 /* Return the size of the information in the .debug_aranges section.  */
10878
10879 static unsigned long
10880 size_of_aranges (void)
10881 {
10882   unsigned long size;
10883
10884   size = DWARF_ARANGES_HEADER_SIZE;
10885
10886   /* Count the address/length pair for this compilation unit.  */
10887   if (text_section_used)
10888     size += 2 * DWARF2_ADDR_SIZE;
10889   if (cold_text_section_used)
10890     size += 2 * DWARF2_ADDR_SIZE;
10891   if (have_multiple_function_sections)
10892     {
10893       unsigned fde_idx = 0;
10894
10895       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
10896         {
10897           dw_fde_ref fde = &fde_table[fde_idx];
10898
10899           if (!fde->in_std_section)
10900             size += 2 * DWARF2_ADDR_SIZE;
10901           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10902             size += 2 * DWARF2_ADDR_SIZE;
10903         }
10904     }
10905
10906   /* Count the two zero words used to terminated the address range table.  */
10907   size += 2 * DWARF2_ADDR_SIZE;
10908   return size;
10909 }
10910 \f
10911 /* Select the encoding of an attribute value.  */
10912
10913 static enum dwarf_form
10914 value_format (dw_attr_ref a)
10915 {
10916   switch (a->dw_attr_val.val_class)
10917     {
10918     case dw_val_class_addr:
10919       /* Only very few attributes allow DW_FORM_addr.  */
10920       switch (a->dw_attr)
10921         {
10922         case DW_AT_low_pc:
10923         case DW_AT_high_pc:
10924         case DW_AT_entry_pc:
10925         case DW_AT_trampoline:
10926           return DW_FORM_addr;
10927         default:
10928           break;
10929         }
10930       switch (DWARF2_ADDR_SIZE)
10931         {
10932         case 1:
10933           return DW_FORM_data1;
10934         case 2:
10935           return DW_FORM_data2;
10936         case 4:
10937           return DW_FORM_data4;
10938         case 8:
10939           return DW_FORM_data8;
10940         default:
10941           gcc_unreachable ();
10942         }
10943     case dw_val_class_range_list:
10944     case dw_val_class_loc_list:
10945       if (dwarf_version >= 4)
10946         return DW_FORM_sec_offset;
10947       /* FALLTHRU */
10948     case dw_val_class_vms_delta:
10949     case dw_val_class_offset:
10950       switch (DWARF_OFFSET_SIZE)
10951         {
10952         case 4:
10953           return DW_FORM_data4;
10954         case 8:
10955           return DW_FORM_data8;
10956         default:
10957           gcc_unreachable ();
10958         }
10959     case dw_val_class_loc:
10960       if (dwarf_version >= 4)
10961         return DW_FORM_exprloc;
10962       switch (constant_size (size_of_locs (AT_loc (a))))
10963         {
10964         case 1:
10965           return DW_FORM_block1;
10966         case 2:
10967           return DW_FORM_block2;
10968         default:
10969           gcc_unreachable ();
10970         }
10971     case dw_val_class_const:
10972       return DW_FORM_sdata;
10973     case dw_val_class_unsigned_const:
10974       switch (constant_size (AT_unsigned (a)))
10975         {
10976         case 1:
10977           return DW_FORM_data1;
10978         case 2:
10979           return DW_FORM_data2;
10980         case 4:
10981           return DW_FORM_data4;
10982         case 8:
10983           return DW_FORM_data8;
10984         default:
10985           gcc_unreachable ();
10986         }
10987     case dw_val_class_const_double:
10988       switch (HOST_BITS_PER_WIDE_INT)
10989         {
10990         case 8:
10991           return DW_FORM_data2;
10992         case 16:
10993           return DW_FORM_data4;
10994         case 32:
10995           return DW_FORM_data8;
10996         case 64:
10997         default:
10998           return DW_FORM_block1;
10999         }
11000     case dw_val_class_vec:
11001       switch (constant_size (a->dw_attr_val.v.val_vec.length
11002                              * a->dw_attr_val.v.val_vec.elt_size))
11003         {
11004         case 1:
11005           return DW_FORM_block1;
11006         case 2:
11007           return DW_FORM_block2;
11008         case 4:
11009           return DW_FORM_block4;
11010         default:
11011           gcc_unreachable ();
11012         }
11013     case dw_val_class_flag:
11014       if (dwarf_version >= 4)
11015         {
11016           /* Currently all add_AT_flag calls pass in 1 as last argument,
11017              so DW_FORM_flag_present can be used.  If that ever changes,
11018              we'll need to use DW_FORM_flag and have some optimization
11019              in build_abbrev_table that will change those to
11020              DW_FORM_flag_present if it is set to 1 in all DIEs using
11021              the same abbrev entry.  */
11022           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11023           return DW_FORM_flag_present;
11024         }
11025       return DW_FORM_flag;
11026     case dw_val_class_die_ref:
11027       if (AT_ref_external (a))
11028         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11029       else
11030         return DW_FORM_ref;
11031     case dw_val_class_fde_ref:
11032       return DW_FORM_data;
11033     case dw_val_class_lbl_id:
11034       return DW_FORM_addr;
11035     case dw_val_class_lineptr:
11036     case dw_val_class_macptr:
11037       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11038     case dw_val_class_str:
11039       return AT_string_form (a);
11040     case dw_val_class_file:
11041       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11042         {
11043         case 1:
11044           return DW_FORM_data1;
11045         case 2:
11046           return DW_FORM_data2;
11047         case 4:
11048           return DW_FORM_data4;
11049         default:
11050           gcc_unreachable ();
11051         }
11052
11053     case dw_val_class_data8:
11054       return DW_FORM_data8;
11055
11056     default:
11057       gcc_unreachable ();
11058     }
11059 }
11060
11061 /* Output the encoding of an attribute value.  */
11062
11063 static void
11064 output_value_format (dw_attr_ref a)
11065 {
11066   enum dwarf_form form = value_format (a);
11067
11068   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11069 }
11070
11071 /* Output the .debug_abbrev section which defines the DIE abbreviation
11072    table.  */
11073
11074 static void
11075 output_abbrev_section (void)
11076 {
11077   unsigned long abbrev_id;
11078
11079   if (abbrev_die_table_in_use == 1)
11080     return;
11081
11082   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11083     {
11084       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11085       unsigned ix;
11086       dw_attr_ref a_attr;
11087
11088       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11089       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11090                                    dwarf_tag_name (abbrev->die_tag));
11091
11092       if (abbrev->die_child != NULL)
11093         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11094       else
11095         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11096
11097       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11098            ix++)
11099         {
11100           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11101                                        dwarf_attr_name (a_attr->dw_attr));
11102           output_value_format (a_attr);
11103         }
11104
11105       dw2_asm_output_data (1, 0, NULL);
11106       dw2_asm_output_data (1, 0, NULL);
11107     }
11108
11109   /* Terminate the table.  */
11110   dw2_asm_output_data (1, 0, NULL);
11111 }
11112
11113 /* Output a symbol we can use to refer to this DIE from another CU.  */
11114
11115 static inline void
11116 output_die_symbol (dw_die_ref die)
11117 {
11118   char *sym = die->die_id.die_symbol;
11119
11120   if (sym == 0)
11121     return;
11122
11123   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11124     /* We make these global, not weak; if the target doesn't support
11125        .linkonce, it doesn't support combining the sections, so debugging
11126        will break.  */
11127     targetm.asm_out.globalize_label (asm_out_file, sym);
11128
11129   ASM_OUTPUT_LABEL (asm_out_file, sym);
11130 }
11131
11132 /* Return a new location list, given the begin and end range, and the
11133    expression.  */
11134
11135 static inline dw_loc_list_ref
11136 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11137               const char *section)
11138 {
11139   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11140
11141   retlist->begin = begin;
11142   retlist->end = end;
11143   retlist->expr = expr;
11144   retlist->section = section;
11145
11146   return retlist;
11147 }
11148
11149 /* Generate a new internal symbol for this location list node, if it
11150    hasn't got one yet.  */
11151
11152 static inline void
11153 gen_llsym (dw_loc_list_ref list)
11154 {
11155   gcc_assert (!list->ll_symbol);
11156   list->ll_symbol = gen_internal_sym ("LLST");
11157 }
11158
11159 /* Output the location list given to us.  */
11160
11161 static void
11162 output_loc_list (dw_loc_list_ref list_head)
11163 {
11164   dw_loc_list_ref curr = list_head;
11165
11166   if (list_head->emitted)
11167     return;
11168   list_head->emitted = true;
11169
11170   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11171
11172   /* Walk the location list, and output each range + expression.  */
11173   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11174     {
11175       unsigned long size;
11176       /* Don't output an entry that starts and ends at the same address.  */
11177       if (strcmp (curr->begin, curr->end) == 0)
11178         continue;
11179       if (!have_multiple_function_sections)
11180         {
11181           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11182                                 "Location list begin address (%s)",
11183                                 list_head->ll_symbol);
11184           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11185                                 "Location list end address (%s)",
11186                                 list_head->ll_symbol);
11187         }
11188       else
11189         {
11190           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11191                                "Location list begin address (%s)",
11192                                list_head->ll_symbol);
11193           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11194                                "Location list end address (%s)",
11195                                list_head->ll_symbol);
11196         }
11197       size = size_of_locs (curr->expr);
11198
11199       /* Output the block length for this list of location operations.  */
11200       gcc_assert (size <= 0xffff);
11201       dw2_asm_output_data (2, size, "%s", "Location expression size");
11202
11203       output_loc_sequence (curr->expr, -1);
11204     }
11205
11206   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11207                        "Location list terminator begin (%s)",
11208                        list_head->ll_symbol);
11209   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11210                        "Location list terminator end (%s)",
11211                        list_head->ll_symbol);
11212 }
11213
11214 /* Output a type signature.  */
11215
11216 static inline void
11217 output_signature (const char *sig, const char *name)
11218 {
11219   int i;
11220
11221   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11222     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11223 }
11224
11225 /* Output the DIE and its attributes.  Called recursively to generate
11226    the definitions of each child DIE.  */
11227
11228 static void
11229 output_die (dw_die_ref die)
11230 {
11231   dw_attr_ref a;
11232   dw_die_ref c;
11233   unsigned long size;
11234   unsigned ix;
11235
11236   /* If someone in another CU might refer to us, set up a symbol for
11237      them to point to.  */
11238   if (! use_debug_types && die->die_id.die_symbol)
11239     output_die_symbol (die);
11240
11241   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11242                                (unsigned long)die->die_offset,
11243                                dwarf_tag_name (die->die_tag));
11244
11245   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11246     {
11247       const char *name = dwarf_attr_name (a->dw_attr);
11248
11249       switch (AT_class (a))
11250         {
11251         case dw_val_class_addr:
11252           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11253           break;
11254
11255         case dw_val_class_offset:
11256           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11257                                "%s", name);
11258           break;
11259
11260         case dw_val_class_range_list:
11261           {
11262             char *p = strchr (ranges_section_label, '\0');
11263
11264             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11265                      a->dw_attr_val.v.val_offset);
11266             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11267                                    debug_ranges_section, "%s", name);
11268             *p = '\0';
11269           }
11270           break;
11271
11272         case dw_val_class_loc:
11273           size = size_of_locs (AT_loc (a));
11274
11275           /* Output the block length for this list of location operations.  */
11276           if (dwarf_version >= 4)
11277             dw2_asm_output_data_uleb128 (size, "%s", name);
11278           else
11279             dw2_asm_output_data (constant_size (size), size, "%s", name);
11280
11281           output_loc_sequence (AT_loc (a), -1);
11282           break;
11283
11284         case dw_val_class_const:
11285           /* ??? It would be slightly more efficient to use a scheme like is
11286              used for unsigned constants below, but gdb 4.x does not sign
11287              extend.  Gdb 5.x does sign extend.  */
11288           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11289           break;
11290
11291         case dw_val_class_unsigned_const:
11292           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11293                                AT_unsigned (a), "%s", name);
11294           break;
11295
11296         case dw_val_class_const_double:
11297           {
11298             unsigned HOST_WIDE_INT first, second;
11299
11300             if (HOST_BITS_PER_WIDE_INT >= 64)
11301               dw2_asm_output_data (1,
11302                                    2 * HOST_BITS_PER_WIDE_INT
11303                                    / HOST_BITS_PER_CHAR,
11304                                    NULL);
11305
11306             if (WORDS_BIG_ENDIAN)
11307               {
11308                 first = a->dw_attr_val.v.val_double.high;
11309                 second = a->dw_attr_val.v.val_double.low;
11310               }
11311             else
11312               {
11313                 first = a->dw_attr_val.v.val_double.low;
11314                 second = a->dw_attr_val.v.val_double.high;
11315               }
11316
11317             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11318                                  first, name);
11319             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11320                                  second, NULL);
11321           }
11322           break;
11323
11324         case dw_val_class_vec:
11325           {
11326             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11327             unsigned int len = a->dw_attr_val.v.val_vec.length;
11328             unsigned int i;
11329             unsigned char *p;
11330
11331             dw2_asm_output_data (constant_size (len * elt_size),
11332                                  len * elt_size, "%s", name);
11333             if (elt_size > sizeof (HOST_WIDE_INT))
11334               {
11335                 elt_size /= 2;
11336                 len *= 2;
11337               }
11338             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11339                  i < len;
11340                  i++, p += elt_size)
11341               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11342                                    "fp or vector constant word %u", i);
11343             break;
11344           }
11345
11346         case dw_val_class_flag:
11347           if (dwarf_version >= 4)
11348             {
11349               /* Currently all add_AT_flag calls pass in 1 as last argument,
11350                  so DW_FORM_flag_present can be used.  If that ever changes,
11351                  we'll need to use DW_FORM_flag and have some optimization
11352                  in build_abbrev_table that will change those to
11353                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11354                  the same abbrev entry.  */
11355               gcc_assert (AT_flag (a) == 1);
11356               if (flag_debug_asm)
11357                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11358                          ASM_COMMENT_START, name);
11359               break;
11360             }
11361           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11362           break;
11363
11364         case dw_val_class_loc_list:
11365           {
11366             char *sym = AT_loc_list (a)->ll_symbol;
11367
11368             gcc_assert (sym);
11369             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11370                                    "%s", name);
11371           }
11372           break;
11373
11374         case dw_val_class_die_ref:
11375           if (AT_ref_external (a))
11376             {
11377               if (use_debug_types)
11378                 {
11379                   comdat_type_node_ref type_node =
11380                     AT_ref (a)->die_id.die_type_node;
11381
11382                   gcc_assert (type_node);
11383                   output_signature (type_node->signature, name);
11384                 }
11385               else
11386                 {
11387                   char *sym = AT_ref (a)->die_id.die_symbol;
11388                   int size;
11389
11390                   gcc_assert (sym);
11391                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11392                      length, whereas in DWARF3 it's always sized as an
11393                      offset.  */
11394                   if (dwarf_version == 2)
11395                     size = DWARF2_ADDR_SIZE;
11396                   else
11397                     size = DWARF_OFFSET_SIZE;
11398                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11399                                          name);
11400                 }
11401             }
11402           else
11403             {
11404               gcc_assert (AT_ref (a)->die_offset);
11405               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11406                                    "%s", name);
11407             }
11408           break;
11409
11410         case dw_val_class_fde_ref:
11411           {
11412             char l1[20];
11413
11414             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11415                                          a->dw_attr_val.v.val_fde_index * 2);
11416             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11417                                    "%s", name);
11418           }
11419           break;
11420
11421         case dw_val_class_vms_delta:
11422           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11423                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11424                                     "%s", name);
11425           break;
11426
11427         case dw_val_class_lbl_id:
11428           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11429           break;
11430
11431         case dw_val_class_lineptr:
11432           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11433                                  debug_line_section, "%s", name);
11434           break;
11435
11436         case dw_val_class_macptr:
11437           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11438                                  debug_macinfo_section, "%s", name);
11439           break;
11440
11441         case dw_val_class_str:
11442           if (AT_string_form (a) == DW_FORM_strp)
11443             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11444                                    a->dw_attr_val.v.val_str->label,
11445                                    debug_str_section,
11446                                    "%s: \"%s\"", name, AT_string (a));
11447           else
11448             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11449           break;
11450
11451         case dw_val_class_file:
11452           {
11453             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11454
11455             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11456                                  a->dw_attr_val.v.val_file->filename);
11457             break;
11458           }
11459
11460         case dw_val_class_data8:
11461           {
11462             int i;
11463
11464             for (i = 0; i < 8; i++)
11465               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11466                                    i == 0 ? "%s" : NULL, name);
11467             break;
11468           }
11469
11470         default:
11471           gcc_unreachable ();
11472         }
11473     }
11474
11475   FOR_EACH_CHILD (die, c, output_die (c));
11476
11477   /* Add null byte to terminate sibling list.  */
11478   if (die->die_child != NULL)
11479     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11480                          (unsigned long) die->die_offset);
11481 }
11482
11483 /* Output the compilation unit that appears at the beginning of the
11484    .debug_info section, and precedes the DIE descriptions.  */
11485
11486 static void
11487 output_compilation_unit_header (void)
11488 {
11489   int ver = dwarf_version;
11490
11491   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11492     dw2_asm_output_data (4, 0xffffffff,
11493       "Initial length escape value indicating 64-bit DWARF extension");
11494   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11495                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11496                        "Length of Compilation Unit Info");
11497   dw2_asm_output_data (2, ver, "DWARF version number");
11498   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11499                          debug_abbrev_section,
11500                          "Offset Into Abbrev. Section");
11501   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11502 }
11503
11504 /* Output the compilation unit DIE and its children.  */
11505
11506 static void
11507 output_comp_unit (dw_die_ref die, int output_if_empty)
11508 {
11509   const char *secname;
11510   char *oldsym, *tmp;
11511
11512   /* Unless we are outputting main CU, we may throw away empty ones.  */
11513   if (!output_if_empty && die->die_child == NULL)
11514     return;
11515
11516   /* Even if there are no children of this DIE, we must output the information
11517      about the compilation unit.  Otherwise, on an empty translation unit, we
11518      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11519      will then complain when examining the file.  First mark all the DIEs in
11520      this CU so we know which get local refs.  */
11521   mark_dies (die);
11522
11523   build_abbrev_table (die);
11524
11525   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11526   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11527   calc_die_sizes (die);
11528
11529   oldsym = die->die_id.die_symbol;
11530   if (oldsym)
11531     {
11532       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11533
11534       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11535       secname = tmp;
11536       die->die_id.die_symbol = NULL;
11537       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11538     }
11539   else
11540     {
11541       switch_to_section (debug_info_section);
11542       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11543       info_section_emitted = true;
11544     }
11545
11546   /* Output debugging information.  */
11547   output_compilation_unit_header ();
11548   output_die (die);
11549
11550   /* Leave the marks on the main CU, so we can check them in
11551      output_pubnames.  */
11552   if (oldsym)
11553     {
11554       unmark_dies (die);
11555       die->die_id.die_symbol = oldsym;
11556     }
11557 }
11558
11559 /* Output a comdat type unit DIE and its children.  */
11560
11561 static void
11562 output_comdat_type_unit (comdat_type_node *node)
11563 {
11564   const char *secname;
11565   char *tmp;
11566   int i;
11567 #if defined (OBJECT_FORMAT_ELF)
11568   tree comdat_key;
11569 #endif
11570
11571   /* First mark all the DIEs in this CU so we know which get local refs.  */
11572   mark_dies (node->root_die);
11573
11574   build_abbrev_table (node->root_die);
11575
11576   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11577   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11578   calc_die_sizes (node->root_die);
11579
11580 #if defined (OBJECT_FORMAT_ELF)
11581   secname = ".debug_types";
11582   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11583   sprintf (tmp, "wt.");
11584   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11585     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11586   comdat_key = get_identifier (tmp);
11587   targetm.asm_out.named_section (secname,
11588                                  SECTION_DEBUG | SECTION_LINKONCE,
11589                                  comdat_key);
11590 #else
11591   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11592   sprintf (tmp, ".gnu.linkonce.wt.");
11593   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11594     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11595   secname = tmp;
11596   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11597 #endif
11598
11599   /* Output debugging information.  */
11600   output_compilation_unit_header ();
11601   output_signature (node->signature, "Type Signature");
11602   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11603                        "Offset to Type DIE");
11604   output_die (node->root_die);
11605
11606   unmark_dies (node->root_die);
11607 }
11608
11609 /* Return the DWARF2/3 pubname associated with a decl.  */
11610
11611 static const char *
11612 dwarf2_name (tree decl, int scope)
11613 {
11614   if (DECL_NAMELESS (decl))
11615     return NULL;
11616   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11617 }
11618
11619 /* Add a new entry to .debug_pubnames if appropriate.  */
11620
11621 static void
11622 add_pubname_string (const char *str, dw_die_ref die)
11623 {
11624   if (targetm.want_debug_pub_sections)
11625     {
11626       pubname_entry e;
11627
11628       e.die = die;
11629       e.name = xstrdup (str);
11630       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11631     }
11632 }
11633
11634 static void
11635 add_pubname (tree decl, dw_die_ref die)
11636 {
11637   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11638     {
11639       const char *name = dwarf2_name (decl, 1);
11640       if (name)
11641         add_pubname_string (name, die);
11642     }
11643 }
11644
11645 /* Add a new entry to .debug_pubtypes if appropriate.  */
11646
11647 static void
11648 add_pubtype (tree decl, dw_die_ref die)
11649 {
11650   pubname_entry e;
11651
11652   if (!targetm.want_debug_pub_sections)
11653     return;
11654
11655   e.name = NULL;
11656   if ((TREE_PUBLIC (decl)
11657        || is_cu_die (die->die_parent))
11658       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11659     {
11660       e.die = die;
11661       if (TYPE_P (decl))
11662         {
11663           if (TYPE_NAME (decl))
11664             {
11665               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11666                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11667               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11668                        && DECL_NAME (TYPE_NAME (decl)))
11669                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11670               else
11671                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11672             }
11673         }
11674       else
11675         {
11676           e.name = dwarf2_name (decl, 1);
11677           if (e.name)
11678             e.name = xstrdup (e.name);
11679         }
11680
11681       /* If we don't have a name for the type, there's no point in adding
11682          it to the table.  */
11683       if (e.name && e.name[0] != '\0')
11684         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11685     }
11686 }
11687
11688 /* Output the public names table used to speed up access to externally
11689    visible names; or the public types table used to find type definitions.  */
11690
11691 static void
11692 output_pubnames (VEC (pubname_entry, gc) * names)
11693 {
11694   unsigned i;
11695   unsigned long pubnames_length = size_of_pubnames (names);
11696   pubname_ref pub;
11697
11698   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11699     dw2_asm_output_data (4, 0xffffffff,
11700       "Initial length escape value indicating 64-bit DWARF extension");
11701   if (names == pubname_table)
11702     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11703                          "Length of Public Names Info");
11704   else
11705     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11706                          "Length of Public Type Names Info");
11707   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11708   dw2_asm_output_data (2, 2, "DWARF Version");
11709   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11710                          debug_info_section,
11711                          "Offset of Compilation Unit Info");
11712   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11713                        "Compilation Unit Length");
11714
11715   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11716     {
11717       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11718       if (names == pubname_table)
11719         gcc_assert (pub->die->die_mark);
11720
11721       if (names != pubtype_table
11722           || pub->die->die_offset != 0
11723           || !flag_eliminate_unused_debug_types)
11724         {
11725           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11726                                "DIE offset");
11727
11728           dw2_asm_output_nstring (pub->name, -1, "external name");
11729         }
11730     }
11731
11732   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11733 }
11734
11735 /* Output the information that goes into the .debug_aranges table.
11736    Namely, define the beginning and ending address range of the
11737    text section generated for this compilation unit.  */
11738
11739 static void
11740 output_aranges (unsigned long aranges_length)
11741 {
11742   unsigned i;
11743
11744   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11745     dw2_asm_output_data (4, 0xffffffff,
11746       "Initial length escape value indicating 64-bit DWARF extension");
11747   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11748                        "Length of Address Ranges Info");
11749   /* Version number for aranges is still 2, even in DWARF3.  */
11750   dw2_asm_output_data (2, 2, "DWARF Version");
11751   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11752                          debug_info_section,
11753                          "Offset of Compilation Unit Info");
11754   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11755   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11756
11757   /* We need to align to twice the pointer size here.  */
11758   if (DWARF_ARANGES_PAD_SIZE)
11759     {
11760       /* Pad using a 2 byte words so that padding is correct for any
11761          pointer size.  */
11762       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11763                            2 * DWARF2_ADDR_SIZE);
11764       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11765         dw2_asm_output_data (2, 0, NULL);
11766     }
11767
11768   /* It is necessary not to output these entries if the sections were
11769      not used; if the sections were not used, the length will be 0 and
11770      the address may end up as 0 if the section is discarded by ld
11771      --gc-sections, leaving an invalid (0, 0) entry that can be
11772      confused with the terminator.  */
11773   if (text_section_used)
11774     {
11775       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11776       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11777                             text_section_label, "Length");
11778     }
11779   if (cold_text_section_used)
11780     {
11781       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11782                            "Address");
11783       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11784                             cold_text_section_label, "Length");
11785     }
11786
11787   if (have_multiple_function_sections)
11788     {
11789       unsigned fde_idx = 0;
11790
11791       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11792         {
11793           dw_fde_ref fde = &fde_table[fde_idx];
11794
11795           if (!fde->in_std_section)
11796             {
11797               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11798                                    "Address");
11799               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11800                                     fde->dw_fde_begin, "Length");
11801             }
11802           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11803             {
11804               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11805                                    "Address");
11806               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11807                                     fde->dw_fde_second_begin, "Length");
11808             }
11809         }
11810     }
11811
11812   /* Output the terminator words.  */
11813   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11814   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11815 }
11816
11817 /* Add a new entry to .debug_ranges.  Return the offset at which it
11818    was placed.  */
11819
11820 static unsigned int
11821 add_ranges_num (int num)
11822 {
11823   unsigned int in_use = ranges_table_in_use;
11824
11825   if (in_use == ranges_table_allocated)
11826     {
11827       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11828       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11829                                     ranges_table_allocated);
11830       memset (ranges_table + ranges_table_in_use, 0,
11831               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11832     }
11833
11834   ranges_table[in_use].num = num;
11835   ranges_table_in_use = in_use + 1;
11836
11837   return in_use * 2 * DWARF2_ADDR_SIZE;
11838 }
11839
11840 /* Add a new entry to .debug_ranges corresponding to a block, or a
11841    range terminator if BLOCK is NULL.  */
11842
11843 static unsigned int
11844 add_ranges (const_tree block)
11845 {
11846   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11847 }
11848
11849 /* Add a new entry to .debug_ranges corresponding to a pair of
11850    labels.  */
11851
11852 static void
11853 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11854                       bool *added)
11855 {
11856   unsigned int in_use = ranges_by_label_in_use;
11857   unsigned int offset;
11858
11859   if (in_use == ranges_by_label_allocated)
11860     {
11861       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11862       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11863                                        ranges_by_label,
11864                                        ranges_by_label_allocated);
11865       memset (ranges_by_label + ranges_by_label_in_use, 0,
11866               RANGES_TABLE_INCREMENT
11867               * sizeof (struct dw_ranges_by_label_struct));
11868     }
11869
11870   ranges_by_label[in_use].begin = begin;
11871   ranges_by_label[in_use].end = end;
11872   ranges_by_label_in_use = in_use + 1;
11873
11874   offset = add_ranges_num (-(int)in_use - 1);
11875   if (!*added)
11876     {
11877       add_AT_range_list (die, DW_AT_ranges, offset);
11878       *added = true;
11879     }
11880 }
11881
11882 static void
11883 output_ranges (void)
11884 {
11885   unsigned i;
11886   static const char *const start_fmt = "Offset %#x";
11887   const char *fmt = start_fmt;
11888
11889   for (i = 0; i < ranges_table_in_use; i++)
11890     {
11891       int block_num = ranges_table[i].num;
11892
11893       if (block_num > 0)
11894         {
11895           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11896           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11897
11898           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11899           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11900
11901           /* If all code is in the text section, then the compilation
11902              unit base address defaults to DW_AT_low_pc, which is the
11903              base of the text section.  */
11904           if (!have_multiple_function_sections)
11905             {
11906               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11907                                     text_section_label,
11908                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11909               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11910                                     text_section_label, NULL);
11911             }
11912
11913           /* Otherwise, the compilation unit base address is zero,
11914              which allows us to use absolute addresses, and not worry
11915              about whether the target supports cross-section
11916              arithmetic.  */
11917           else
11918             {
11919               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11920                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11921               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11922             }
11923
11924           fmt = NULL;
11925         }
11926
11927       /* Negative block_num stands for an index into ranges_by_label.  */
11928       else if (block_num < 0)
11929         {
11930           int lab_idx = - block_num - 1;
11931
11932           if (!have_multiple_function_sections)
11933             {
11934               gcc_unreachable ();
11935 #if 0
11936               /* If we ever use add_ranges_by_labels () for a single
11937                  function section, all we have to do is to take out
11938                  the #if 0 above.  */
11939               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11940                                     ranges_by_label[lab_idx].begin,
11941                                     text_section_label,
11942                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11943               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11944                                     ranges_by_label[lab_idx].end,
11945                                     text_section_label, NULL);
11946 #endif
11947             }
11948           else
11949             {
11950               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11951                                    ranges_by_label[lab_idx].begin,
11952                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11953               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11954                                    ranges_by_label[lab_idx].end,
11955                                    NULL);
11956             }
11957         }
11958       else
11959         {
11960           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11961           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11962           fmt = start_fmt;
11963         }
11964     }
11965 }
11966
11967 /* Data structure containing information about input files.  */
11968 struct file_info
11969 {
11970   const char *path;     /* Complete file name.  */
11971   const char *fname;    /* File name part.  */
11972   int length;           /* Length of entire string.  */
11973   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11974   int dir_idx;          /* Index in directory table.  */
11975 };
11976
11977 /* Data structure containing information about directories with source
11978    files.  */
11979 struct dir_info
11980 {
11981   const char *path;     /* Path including directory name.  */
11982   int length;           /* Path length.  */
11983   int prefix;           /* Index of directory entry which is a prefix.  */
11984   int count;            /* Number of files in this directory.  */
11985   int dir_idx;          /* Index of directory used as base.  */
11986 };
11987
11988 /* Callback function for file_info comparison.  We sort by looking at
11989    the directories in the path.  */
11990
11991 static int
11992 file_info_cmp (const void *p1, const void *p2)
11993 {
11994   const struct file_info *const s1 = (const struct file_info *) p1;
11995   const struct file_info *const s2 = (const struct file_info *) p2;
11996   const unsigned char *cp1;
11997   const unsigned char *cp2;
11998
11999   /* Take care of file names without directories.  We need to make sure that
12000      we return consistent values to qsort since some will get confused if
12001      we return the same value when identical operands are passed in opposite
12002      orders.  So if neither has a directory, return 0 and otherwise return
12003      1 or -1 depending on which one has the directory.  */
12004   if ((s1->path == s1->fname || s2->path == s2->fname))
12005     return (s2->path == s2->fname) - (s1->path == s1->fname);
12006
12007   cp1 = (const unsigned char *) s1->path;
12008   cp2 = (const unsigned char *) s2->path;
12009
12010   while (1)
12011     {
12012       ++cp1;
12013       ++cp2;
12014       /* Reached the end of the first path?  If so, handle like above.  */
12015       if ((cp1 == (const unsigned char *) s1->fname)
12016           || (cp2 == (const unsigned char *) s2->fname))
12017         return ((cp2 == (const unsigned char *) s2->fname)
12018                 - (cp1 == (const unsigned char *) s1->fname));
12019
12020       /* Character of current path component the same?  */
12021       else if (*cp1 != *cp2)
12022         return *cp1 - *cp2;
12023     }
12024 }
12025
12026 struct file_name_acquire_data
12027 {
12028   struct file_info *files;
12029   int used_files;
12030   int max_files;
12031 };
12032
12033 /* Traversal function for the hash table.  */
12034
12035 static int
12036 file_name_acquire (void ** slot, void *data)
12037 {
12038   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12039   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12040   struct file_info *fi;
12041   const char *f;
12042
12043   gcc_assert (fnad->max_files >= d->emitted_number);
12044
12045   if (! d->emitted_number)
12046     return 1;
12047
12048   gcc_assert (fnad->max_files != fnad->used_files);
12049
12050   fi = fnad->files + fnad->used_files++;
12051
12052   /* Skip all leading "./".  */
12053   f = d->filename;
12054   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12055     f += 2;
12056
12057   /* Create a new array entry.  */
12058   fi->path = f;
12059   fi->length = strlen (f);
12060   fi->file_idx = d;
12061
12062   /* Search for the file name part.  */
12063   f = strrchr (f, DIR_SEPARATOR);
12064 #if defined (DIR_SEPARATOR_2)
12065   {
12066     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12067
12068     if (g != NULL)
12069       {
12070         if (f == NULL || f < g)
12071           f = g;
12072       }
12073   }
12074 #endif
12075
12076   fi->fname = f == NULL ? fi->path : f + 1;
12077   return 1;
12078 }
12079
12080 /* Output the directory table and the file name table.  We try to minimize
12081    the total amount of memory needed.  A heuristic is used to avoid large
12082    slowdowns with many input files.  */
12083
12084 static void
12085 output_file_names (void)
12086 {
12087   struct file_name_acquire_data fnad;
12088   int numfiles;
12089   struct file_info *files;
12090   struct dir_info *dirs;
12091   int *saved;
12092   int *savehere;
12093   int *backmap;
12094   int ndirs;
12095   int idx_offset;
12096   int i;
12097
12098   if (!last_emitted_file)
12099     {
12100       dw2_asm_output_data (1, 0, "End directory table");
12101       dw2_asm_output_data (1, 0, "End file name table");
12102       return;
12103     }
12104
12105   numfiles = last_emitted_file->emitted_number;
12106
12107   /* Allocate the various arrays we need.  */
12108   files = XALLOCAVEC (struct file_info, numfiles);
12109   dirs = XALLOCAVEC (struct dir_info, numfiles);
12110
12111   fnad.files = files;
12112   fnad.used_files = 0;
12113   fnad.max_files = numfiles;
12114   htab_traverse (file_table, file_name_acquire, &fnad);
12115   gcc_assert (fnad.used_files == fnad.max_files);
12116
12117   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12118
12119   /* Find all the different directories used.  */
12120   dirs[0].path = files[0].path;
12121   dirs[0].length = files[0].fname - files[0].path;
12122   dirs[0].prefix = -1;
12123   dirs[0].count = 1;
12124   dirs[0].dir_idx = 0;
12125   files[0].dir_idx = 0;
12126   ndirs = 1;
12127
12128   for (i = 1; i < numfiles; i++)
12129     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12130         && memcmp (dirs[ndirs - 1].path, files[i].path,
12131                    dirs[ndirs - 1].length) == 0)
12132       {
12133         /* Same directory as last entry.  */
12134         files[i].dir_idx = ndirs - 1;
12135         ++dirs[ndirs - 1].count;
12136       }
12137     else
12138       {
12139         int j;
12140
12141         /* This is a new directory.  */
12142         dirs[ndirs].path = files[i].path;
12143         dirs[ndirs].length = files[i].fname - files[i].path;
12144         dirs[ndirs].count = 1;
12145         dirs[ndirs].dir_idx = ndirs;
12146         files[i].dir_idx = ndirs;
12147
12148         /* Search for a prefix.  */
12149         dirs[ndirs].prefix = -1;
12150         for (j = 0; j < ndirs; j++)
12151           if (dirs[j].length < dirs[ndirs].length
12152               && dirs[j].length > 1
12153               && (dirs[ndirs].prefix == -1
12154                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12155               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12156             dirs[ndirs].prefix = j;
12157
12158         ++ndirs;
12159       }
12160
12161   /* Now to the actual work.  We have to find a subset of the directories which
12162      allow expressing the file name using references to the directory table
12163      with the least amount of characters.  We do not do an exhaustive search
12164      where we would have to check out every combination of every single
12165      possible prefix.  Instead we use a heuristic which provides nearly optimal
12166      results in most cases and never is much off.  */
12167   saved = XALLOCAVEC (int, ndirs);
12168   savehere = XALLOCAVEC (int, ndirs);
12169
12170   memset (saved, '\0', ndirs * sizeof (saved[0]));
12171   for (i = 0; i < ndirs; i++)
12172     {
12173       int j;
12174       int total;
12175
12176       /* We can always save some space for the current directory.  But this
12177          does not mean it will be enough to justify adding the directory.  */
12178       savehere[i] = dirs[i].length;
12179       total = (savehere[i] - saved[i]) * dirs[i].count;
12180
12181       for (j = i + 1; j < ndirs; j++)
12182         {
12183           savehere[j] = 0;
12184           if (saved[j] < dirs[i].length)
12185             {
12186               /* Determine whether the dirs[i] path is a prefix of the
12187                  dirs[j] path.  */
12188               int k;
12189
12190               k = dirs[j].prefix;
12191               while (k != -1 && k != (int) i)
12192                 k = dirs[k].prefix;
12193
12194               if (k == (int) i)
12195                 {
12196                   /* Yes it is.  We can possibly save some memory by
12197                      writing the filenames in dirs[j] relative to
12198                      dirs[i].  */
12199                   savehere[j] = dirs[i].length;
12200                   total += (savehere[j] - saved[j]) * dirs[j].count;
12201                 }
12202             }
12203         }
12204
12205       /* Check whether we can save enough to justify adding the dirs[i]
12206          directory.  */
12207       if (total > dirs[i].length + 1)
12208         {
12209           /* It's worthwhile adding.  */
12210           for (j = i; j < ndirs; j++)
12211             if (savehere[j] > 0)
12212               {
12213                 /* Remember how much we saved for this directory so far.  */
12214                 saved[j] = savehere[j];
12215
12216                 /* Remember the prefix directory.  */
12217                 dirs[j].dir_idx = i;
12218               }
12219         }
12220     }
12221
12222   /* Emit the directory name table.  */
12223   idx_offset = dirs[0].length > 0 ? 1 : 0;
12224   for (i = 1 - idx_offset; i < ndirs; i++)
12225     dw2_asm_output_nstring (dirs[i].path,
12226                             dirs[i].length
12227                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12228                             "Directory Entry: %#x", i + idx_offset);
12229
12230   dw2_asm_output_data (1, 0, "End directory table");
12231
12232   /* We have to emit them in the order of emitted_number since that's
12233      used in the debug info generation.  To do this efficiently we
12234      generate a back-mapping of the indices first.  */
12235   backmap = XALLOCAVEC (int, numfiles);
12236   for (i = 0; i < numfiles; i++)
12237     backmap[files[i].file_idx->emitted_number - 1] = i;
12238
12239   /* Now write all the file names.  */
12240   for (i = 0; i < numfiles; i++)
12241     {
12242       int file_idx = backmap[i];
12243       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12244
12245 #ifdef VMS_DEBUGGING_INFO
12246 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12247
12248       /* Setting these fields can lead to debugger miscomparisons,
12249          but VMS Debug requires them to be set correctly.  */
12250
12251       int ver;
12252       long long cdt;
12253       long siz;
12254       int maxfilelen = strlen (files[file_idx].path)
12255                                + dirs[dir_idx].length
12256                                + MAX_VMS_VERSION_LEN + 1;
12257       char *filebuf = XALLOCAVEC (char, maxfilelen);
12258
12259       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12260       snprintf (filebuf, maxfilelen, "%s;%d",
12261                 files[file_idx].path + dirs[dir_idx].length, ver);
12262
12263       dw2_asm_output_nstring
12264         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12265
12266       /* Include directory index.  */
12267       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12268
12269       /* Modification time.  */
12270       dw2_asm_output_data_uleb128
12271         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12272           ? cdt : 0,
12273          NULL);
12274
12275       /* File length in bytes.  */
12276       dw2_asm_output_data_uleb128
12277         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12278           ? siz : 0,
12279          NULL);
12280 #else
12281       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12282                               "File Entry: %#x", (unsigned) i + 1);
12283
12284       /* Include directory index.  */
12285       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12286
12287       /* Modification time.  */
12288       dw2_asm_output_data_uleb128 (0, NULL);
12289
12290       /* File length in bytes.  */
12291       dw2_asm_output_data_uleb128 (0, NULL);
12292 #endif /* VMS_DEBUGGING_INFO */
12293     }
12294
12295   dw2_asm_output_data (1, 0, "End file name table");
12296 }
12297
12298
12299 /* Output one line number table into the .debug_line section.  */
12300
12301 static void
12302 output_one_line_info_table (dw_line_info_table *table)
12303 {
12304   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12305   unsigned int current_line = 1;
12306   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12307   dw_line_info_entry *ent;
12308   size_t i;
12309
12310   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
12311     {
12312       switch (ent->opcode)
12313         {
12314         case LI_set_address:
12315           /* ??? Unfortunately, we have little choice here currently, and
12316              must always use the most general form.  GCC does not know the
12317              address delta itself, so we can't use DW_LNS_advance_pc.  Many
12318              ports do have length attributes which will give an upper bound
12319              on the address range.  We could perhaps use length attributes
12320              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12321           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12322
12323           /* This can handle any delta.  This takes
12324              4+DWARF2_ADDR_SIZE bytes.  */
12325           dw2_asm_output_data (1, 0, "set address %s", line_label);
12326           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12327           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12328           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12329           break;
12330
12331         case LI_set_line:
12332           if (ent->val == current_line)
12333             {
12334               /* We still need to start a new row, so output a copy insn.  */
12335               dw2_asm_output_data (1, DW_LNS_copy,
12336                                    "copy line %u", current_line);
12337             }
12338           else
12339             {
12340               int line_offset = ent->val - current_line;
12341               int line_delta = line_offset - DWARF_LINE_BASE;
12342
12343               current_line = ent->val;
12344               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12345                 {
12346                   /* This can handle deltas from -10 to 234, using the current
12347                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12348                      This takes 1 byte.  */
12349                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12350                                        "line %u", current_line);
12351                 }
12352               else
12353                 {
12354                   /* This can handle any delta.  This takes at least 4 bytes,
12355                      depending on the value being encoded.  */
12356                   dw2_asm_output_data (1, DW_LNS_advance_line,
12357                                        "advance to line %u", current_line);
12358                   dw2_asm_output_data_sleb128 (line_offset, NULL);
12359                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
12360                 }
12361             }
12362           break;
12363
12364         case LI_set_file:
12365           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12366           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12367           break;
12368
12369         case LI_set_column:
12370           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12371           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12372           break;
12373
12374         case LI_negate_stmt:
12375           current_is_stmt = !current_is_stmt;
12376           dw2_asm_output_data (1, DW_LNS_negate_stmt,
12377                                "is_stmt %d", current_is_stmt);
12378           break;
12379
12380         case LI_set_prologue_end:
12381           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12382                                "set prologue end");
12383           break;
12384           
12385         case LI_set_epilogue_begin:
12386           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12387                                "set epilogue begin");
12388           break;
12389
12390         case LI_set_discriminator:
12391           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12392           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12393           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12394           dw2_asm_output_data_uleb128 (ent->val, NULL);
12395           break;
12396         }
12397     }
12398
12399   /* Emit debug info for the address of the end of the table.  */
12400   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12401   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12402   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12403   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12404
12405   dw2_asm_output_data (1, 0, "end sequence");
12406   dw2_asm_output_data_uleb128 (1, NULL);
12407   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12408 }
12409
12410 /* Output the source line number correspondence information.  This
12411    information goes into the .debug_line section.  */
12412
12413 static void
12414 output_line_info (void)
12415 {
12416   char l1[20], l2[20], p1[20], p2[20];
12417   int ver = dwarf_version;
12418   int opc;
12419
12420   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12421   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12422   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12423   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12424
12425   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12426     dw2_asm_output_data (4, 0xffffffff,
12427       "Initial length escape value indicating 64-bit DWARF extension");
12428   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12429                         "Length of Source Line Info");
12430   ASM_OUTPUT_LABEL (asm_out_file, l1);
12431
12432   dw2_asm_output_data (2, ver, "DWARF Version");
12433   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12434   ASM_OUTPUT_LABEL (asm_out_file, p1);
12435
12436   /* Define the architecture-dependent minimum instruction length (in bytes).
12437      In this implementation of DWARF, this field is used for information
12438      purposes only.  Since GCC generates assembly language, we have no
12439      a priori knowledge of how many instruction bytes are generated for each
12440      source line, and therefore can use only the DW_LNE_set_address and
12441      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12442      this as '1', which is "correct enough" for all architectures,
12443      and don't let the target override.  */
12444   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12445
12446   if (ver >= 4)
12447     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12448                          "Maximum Operations Per Instruction");
12449   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12450                        "Default is_stmt_start flag");
12451   dw2_asm_output_data (1, DWARF_LINE_BASE,
12452                        "Line Base Value (Special Opcodes)");
12453   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12454                        "Line Range Value (Special Opcodes)");
12455   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12456                        "Special Opcode Base");
12457
12458   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12459     {
12460       int n_op_args;
12461       switch (opc)
12462         {
12463         case DW_LNS_advance_pc:
12464         case DW_LNS_advance_line:
12465         case DW_LNS_set_file:
12466         case DW_LNS_set_column:
12467         case DW_LNS_fixed_advance_pc:
12468         case DW_LNS_set_isa:
12469           n_op_args = 1;
12470           break;
12471         default:
12472           n_op_args = 0;
12473           break;
12474         }
12475
12476       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12477                            opc, n_op_args);
12478     }
12479
12480   /* Write out the information about the files we use.  */
12481   output_file_names ();
12482   ASM_OUTPUT_LABEL (asm_out_file, p2);
12483
12484   if (text_section_line_info && text_section_line_info->in_use)
12485     output_one_line_info_table (text_section_line_info);
12486   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12487     output_one_line_info_table (cold_text_section_line_info);
12488
12489   if (separate_line_info)
12490     {
12491       dw_line_info_table *table;
12492       size_t i;
12493
12494       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
12495         if (table->in_use)
12496           output_one_line_info_table (table);
12497     }
12498
12499   /* Output the marker for the end of the line number info.  */
12500   ASM_OUTPUT_LABEL (asm_out_file, l2);
12501 }
12502 \f
12503 /* Given a pointer to a tree node for some base type, return a pointer to
12504    a DIE that describes the given type.
12505
12506    This routine must only be called for GCC type nodes that correspond to
12507    Dwarf base (fundamental) types.  */
12508
12509 static dw_die_ref
12510 base_type_die (tree type)
12511 {
12512   dw_die_ref base_type_result;
12513   enum dwarf_type encoding;
12514
12515   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12516     return 0;
12517
12518   /* If this is a subtype that should not be emitted as a subrange type,
12519      use the base type.  See subrange_type_for_debug_p.  */
12520   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12521     type = TREE_TYPE (type);
12522
12523   switch (TREE_CODE (type))
12524     {
12525     case INTEGER_TYPE:
12526       if ((dwarf_version >= 4 || !dwarf_strict)
12527           && TYPE_NAME (type)
12528           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12529           && DECL_IS_BUILTIN (TYPE_NAME (type))
12530           && DECL_NAME (TYPE_NAME (type)))
12531         {
12532           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12533           if (strcmp (name, "char16_t") == 0
12534               || strcmp (name, "char32_t") == 0)
12535             {
12536               encoding = DW_ATE_UTF;
12537               break;
12538             }
12539         }
12540       if (TYPE_STRING_FLAG (type))
12541         {
12542           if (TYPE_UNSIGNED (type))
12543             encoding = DW_ATE_unsigned_char;
12544           else
12545             encoding = DW_ATE_signed_char;
12546         }
12547       else if (TYPE_UNSIGNED (type))
12548         encoding = DW_ATE_unsigned;
12549       else
12550         encoding = DW_ATE_signed;
12551       break;
12552
12553     case REAL_TYPE:
12554       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12555         {
12556           if (dwarf_version >= 3 || !dwarf_strict)
12557             encoding = DW_ATE_decimal_float;
12558           else
12559             encoding = DW_ATE_lo_user;
12560         }
12561       else
12562         encoding = DW_ATE_float;
12563       break;
12564
12565     case FIXED_POINT_TYPE:
12566       if (!(dwarf_version >= 3 || !dwarf_strict))
12567         encoding = DW_ATE_lo_user;
12568       else if (TYPE_UNSIGNED (type))
12569         encoding = DW_ATE_unsigned_fixed;
12570       else
12571         encoding = DW_ATE_signed_fixed;
12572       break;
12573
12574       /* Dwarf2 doesn't know anything about complex ints, so use
12575          a user defined type for it.  */
12576     case COMPLEX_TYPE:
12577       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12578         encoding = DW_ATE_complex_float;
12579       else
12580         encoding = DW_ATE_lo_user;
12581       break;
12582
12583     case BOOLEAN_TYPE:
12584       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12585       encoding = DW_ATE_boolean;
12586       break;
12587
12588     default:
12589       /* No other TREE_CODEs are Dwarf fundamental types.  */
12590       gcc_unreachable ();
12591     }
12592
12593   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12594
12595   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12596                    int_size_in_bytes (type));
12597   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12598
12599   return base_type_result;
12600 }
12601
12602 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12603    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12604
12605 static inline int
12606 is_base_type (tree type)
12607 {
12608   switch (TREE_CODE (type))
12609     {
12610     case ERROR_MARK:
12611     case VOID_TYPE:
12612     case INTEGER_TYPE:
12613     case REAL_TYPE:
12614     case FIXED_POINT_TYPE:
12615     case COMPLEX_TYPE:
12616     case BOOLEAN_TYPE:
12617       return 1;
12618
12619     case ARRAY_TYPE:
12620     case RECORD_TYPE:
12621     case UNION_TYPE:
12622     case QUAL_UNION_TYPE:
12623     case ENUMERAL_TYPE:
12624     case FUNCTION_TYPE:
12625     case METHOD_TYPE:
12626     case POINTER_TYPE:
12627     case REFERENCE_TYPE:
12628     case NULLPTR_TYPE:
12629     case OFFSET_TYPE:
12630     case LANG_TYPE:
12631     case VECTOR_TYPE:
12632       return 0;
12633
12634     default:
12635       gcc_unreachable ();
12636     }
12637
12638   return 0;
12639 }
12640
12641 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12642    node, return the size in bits for the type if it is a constant, or else
12643    return the alignment for the type if the type's size is not constant, or
12644    else return BITS_PER_WORD if the type actually turns out to be an
12645    ERROR_MARK node.  */
12646
12647 static inline unsigned HOST_WIDE_INT
12648 simple_type_size_in_bits (const_tree type)
12649 {
12650   if (TREE_CODE (type) == ERROR_MARK)
12651     return BITS_PER_WORD;
12652   else if (TYPE_SIZE (type) == NULL_TREE)
12653     return 0;
12654   else if (host_integerp (TYPE_SIZE (type), 1))
12655     return tree_low_cst (TYPE_SIZE (type), 1);
12656   else
12657     return TYPE_ALIGN (type);
12658 }
12659
12660 /* Similarly, but return a double_int instead of UHWI.  */
12661
12662 static inline double_int
12663 double_int_type_size_in_bits (const_tree type)
12664 {
12665   if (TREE_CODE (type) == ERROR_MARK)
12666     return uhwi_to_double_int (BITS_PER_WORD);
12667   else if (TYPE_SIZE (type) == NULL_TREE)
12668     return double_int_zero;
12669   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12670     return tree_to_double_int (TYPE_SIZE (type));
12671   else
12672     return uhwi_to_double_int (TYPE_ALIGN (type));
12673 }
12674
12675 /*  Given a pointer to a tree node for a subrange type, return a pointer
12676     to a DIE that describes the given type.  */
12677
12678 static dw_die_ref
12679 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12680 {
12681   dw_die_ref subrange_die;
12682   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12683
12684   if (context_die == NULL)
12685     context_die = comp_unit_die ();
12686
12687   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12688
12689   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12690     {
12691       /* The size of the subrange type and its base type do not match,
12692          so we need to generate a size attribute for the subrange type.  */
12693       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12694     }
12695
12696   if (low)
12697     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12698   if (high)
12699     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12700
12701   return subrange_die;
12702 }
12703
12704 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12705    entry that chains various modifiers in front of the given type.  */
12706
12707 static dw_die_ref
12708 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12709                    dw_die_ref context_die)
12710 {
12711   enum tree_code code = TREE_CODE (type);
12712   dw_die_ref mod_type_die;
12713   dw_die_ref sub_die = NULL;
12714   tree item_type = NULL;
12715   tree qualified_type;
12716   tree name, low, high;
12717
12718   if (code == ERROR_MARK)
12719     return NULL;
12720
12721   /* See if we already have the appropriately qualified variant of
12722      this type.  */
12723   qualified_type
12724     = get_qualified_type (type,
12725                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12726                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12727
12728   if (qualified_type == sizetype
12729       && TYPE_NAME (qualified_type)
12730       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12731     {
12732       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12733
12734       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12735                            && TYPE_PRECISION (t)
12736                            == TYPE_PRECISION (qualified_type)
12737                            && TYPE_UNSIGNED (t)
12738                            == TYPE_UNSIGNED (qualified_type));
12739       qualified_type = t;
12740     }
12741
12742   /* If we do, then we can just use its DIE, if it exists.  */
12743   if (qualified_type)
12744     {
12745       mod_type_die = lookup_type_die (qualified_type);
12746       if (mod_type_die)
12747         return mod_type_die;
12748     }
12749
12750   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12751
12752   /* Handle C typedef types.  */
12753   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12754       && !DECL_ARTIFICIAL (name))
12755     {
12756       tree dtype = TREE_TYPE (name);
12757
12758       if (qualified_type == dtype)
12759         {
12760           /* For a named type, use the typedef.  */
12761           gen_type_die (qualified_type, context_die);
12762           return lookup_type_die (qualified_type);
12763         }
12764       else if (is_const_type < TYPE_READONLY (dtype)
12765                || is_volatile_type < TYPE_VOLATILE (dtype)
12766                || (is_const_type <= TYPE_READONLY (dtype)
12767                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12768                    && DECL_ORIGINAL_TYPE (name) != type))
12769         /* cv-unqualified version of named type.  Just use the unnamed
12770            type to which it refers.  */
12771         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12772                                   is_const_type, is_volatile_type,
12773                                   context_die);
12774       /* Else cv-qualified version of named type; fall through.  */
12775     }
12776
12777   if (is_const_type
12778       /* If both is_const_type and is_volatile_type, prefer the path
12779          which leads to a qualified type.  */
12780       && (!is_volatile_type
12781           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12782           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12783     {
12784       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12785       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12786     }
12787   else if (is_volatile_type)
12788     {
12789       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12790       sub_die = modified_type_die (type, is_const_type, 0, context_die);
12791     }
12792   else if (code == POINTER_TYPE)
12793     {
12794       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12795       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12796                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12797       item_type = TREE_TYPE (type);
12798       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12799         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12800                          TYPE_ADDR_SPACE (item_type));
12801     }
12802   else if (code == REFERENCE_TYPE)
12803     {
12804       if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
12805         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12806                                 type);
12807       else
12808         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12809       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12810                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12811       item_type = TREE_TYPE (type);
12812       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12813         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12814                          TYPE_ADDR_SPACE (item_type));
12815     }
12816   else if (code == INTEGER_TYPE
12817            && TREE_TYPE (type) != NULL_TREE
12818            && subrange_type_for_debug_p (type, &low, &high))
12819     {
12820       mod_type_die = subrange_type_die (type, low, high, context_die);
12821       item_type = TREE_TYPE (type);
12822     }
12823   else if (is_base_type (type))
12824     mod_type_die = base_type_die (type);
12825   else
12826     {
12827       gen_type_die (type, context_die);
12828
12829       /* We have to get the type_main_variant here (and pass that to the
12830          `lookup_type_die' routine) because the ..._TYPE node we have
12831          might simply be a *copy* of some original type node (where the
12832          copy was created to help us keep track of typedef names) and
12833          that copy might have a different TYPE_UID from the original
12834          ..._TYPE node.  */
12835       if (TREE_CODE (type) != VECTOR_TYPE)
12836         return lookup_type_die (type_main_variant (type));
12837       else
12838         /* Vectors have the debugging information in the type,
12839            not the main variant.  */
12840         return lookup_type_die (type);
12841     }
12842
12843   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12844      don't output a DW_TAG_typedef, since there isn't one in the
12845      user's program; just attach a DW_AT_name to the type.
12846      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12847      if the base type already has the same name.  */
12848   if (name
12849       && ((TREE_CODE (name) != TYPE_DECL
12850            && (qualified_type == TYPE_MAIN_VARIANT (type)
12851                || (!is_const_type && !is_volatile_type)))
12852           || (TREE_CODE (name) == TYPE_DECL
12853               && TREE_TYPE (name) == qualified_type
12854               && DECL_NAME (name))))
12855     {
12856       if (TREE_CODE (name) == TYPE_DECL)
12857         /* Could just call add_name_and_src_coords_attributes here,
12858            but since this is a builtin type it doesn't have any
12859            useful source coordinates anyway.  */
12860         name = DECL_NAME (name);
12861       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12862     }
12863   /* This probably indicates a bug.  */
12864   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12865     add_name_attribute (mod_type_die, "__unknown__");
12866
12867   if (qualified_type)
12868     equate_type_number_to_die (qualified_type, mod_type_die);
12869
12870   if (item_type)
12871     /* We must do this after the equate_type_number_to_die call, in case
12872        this is a recursive type.  This ensures that the modified_type_die
12873        recursion will terminate even if the type is recursive.  Recursive
12874        types are possible in Ada.  */
12875     sub_die = modified_type_die (item_type,
12876                                  TYPE_READONLY (item_type),
12877                                  TYPE_VOLATILE (item_type),
12878                                  context_die);
12879
12880   if (sub_die != NULL)
12881     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12882
12883   return mod_type_die;
12884 }
12885
12886 /* Generate DIEs for the generic parameters of T.
12887    T must be either a generic type or a generic function.
12888    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12889
12890 static void
12891 gen_generic_params_dies (tree t)
12892 {
12893   tree parms, args;
12894   int parms_num, i;
12895   dw_die_ref die = NULL;
12896
12897   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12898     return;
12899
12900   if (TYPE_P (t))
12901     die = lookup_type_die (t);
12902   else if (DECL_P (t))
12903     die = lookup_decl_die (t);
12904
12905   gcc_assert (die);
12906
12907   parms = lang_hooks.get_innermost_generic_parms (t);
12908   if (!parms)
12909     /* T has no generic parameter. It means T is neither a generic type
12910        or function. End of story.  */
12911     return;
12912
12913   parms_num = TREE_VEC_LENGTH (parms);
12914   args = lang_hooks.get_innermost_generic_args (t);
12915   for (i = 0; i < parms_num; i++)
12916     {
12917       tree parm, arg, arg_pack_elems;
12918
12919       parm = TREE_VEC_ELT (parms, i);
12920       arg = TREE_VEC_ELT (args, i);
12921       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12922       gcc_assert (parm && TREE_VALUE (parm) && arg);
12923
12924       if (parm && TREE_VALUE (parm) && arg)
12925         {
12926           /* If PARM represents a template parameter pack,
12927              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12928              by DW_TAG_template_*_parameter DIEs for the argument
12929              pack elements of ARG. Note that ARG would then be
12930              an argument pack.  */
12931           if (arg_pack_elems)
12932             template_parameter_pack_die (TREE_VALUE (parm),
12933                                          arg_pack_elems,
12934                                          die);
12935           else
12936             generic_parameter_die (TREE_VALUE (parm), arg,
12937                                    true /* Emit DW_AT_name */, die);
12938         }
12939     }
12940 }
12941
12942 /* Create and return a DIE for PARM which should be
12943    the representation of a generic type parameter.
12944    For instance, in the C++ front end, PARM would be a template parameter.
12945    ARG is the argument to PARM.
12946    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12947    name of the PARM.
12948    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12949    as a child node.  */
12950
12951 static dw_die_ref
12952 generic_parameter_die (tree parm, tree arg,
12953                        bool emit_name_p,
12954                        dw_die_ref parent_die)
12955 {
12956   dw_die_ref tmpl_die = NULL;
12957   const char *name = NULL;
12958
12959   if (!parm || !DECL_NAME (parm) || !arg)
12960     return NULL;
12961
12962   /* We support non-type generic parameters and arguments,
12963      type generic parameters and arguments, as well as
12964      generic generic parameters (a.k.a. template template parameters in C++)
12965      and arguments.  */
12966   if (TREE_CODE (parm) == PARM_DECL)
12967     /* PARM is a nontype generic parameter  */
12968     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12969   else if (TREE_CODE (parm) == TYPE_DECL)
12970     /* PARM is a type generic parameter.  */
12971     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12972   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12973     /* PARM is a generic generic parameter.
12974        Its DIE is a GNU extension. It shall have a
12975        DW_AT_name attribute to represent the name of the template template
12976        parameter, and a DW_AT_GNU_template_name attribute to represent the
12977        name of the template template argument.  */
12978     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12979                         parent_die, parm);
12980   else
12981     gcc_unreachable ();
12982
12983   if (tmpl_die)
12984     {
12985       tree tmpl_type;
12986
12987       /* If PARM is a generic parameter pack, it means we are
12988          emitting debug info for a template argument pack element.
12989          In other terms, ARG is a template argument pack element.
12990          In that case, we don't emit any DW_AT_name attribute for
12991          the die.  */
12992       if (emit_name_p)
12993         {
12994           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12995           gcc_assert (name);
12996           add_AT_string (tmpl_die, DW_AT_name, name);
12997         }
12998
12999       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13000         {
13001           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13002              TMPL_DIE should have a child DW_AT_type attribute that is set
13003              to the type of the argument to PARM, which is ARG.
13004              If PARM is a type generic parameter, TMPL_DIE should have a
13005              child DW_AT_type that is set to ARG.  */
13006           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13007           add_type_attribute (tmpl_die, tmpl_type, 0,
13008                               TREE_THIS_VOLATILE (tmpl_type),
13009                               parent_die);
13010         }
13011       else
13012         {
13013           /* So TMPL_DIE is a DIE representing a
13014              a generic generic template parameter, a.k.a template template
13015              parameter in C++ and arg is a template.  */
13016
13017           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13018              to the name of the argument.  */
13019           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13020           if (name)
13021             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13022         }
13023
13024       if (TREE_CODE (parm) == PARM_DECL)
13025         /* So PARM is a non-type generic parameter.
13026            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13027            attribute of TMPL_DIE which value represents the value
13028            of ARG.
13029            We must be careful here:
13030            The value of ARG might reference some function decls.
13031            We might currently be emitting debug info for a generic
13032            type and types are emitted before function decls, we don't
13033            know if the function decls referenced by ARG will actually be
13034            emitted after cgraph computations.
13035            So must defer the generation of the DW_AT_const_value to
13036            after cgraph is ready.  */
13037         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13038     }
13039
13040   return tmpl_die;
13041 }
13042
13043 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13044    PARM_PACK must be a template parameter pack. The returned DIE
13045    will be child DIE of PARENT_DIE.  */
13046
13047 static dw_die_ref
13048 template_parameter_pack_die (tree parm_pack,
13049                              tree parm_pack_args,
13050                              dw_die_ref parent_die)
13051 {
13052   dw_die_ref die;
13053   int j;
13054
13055   gcc_assert (parent_die && parm_pack);
13056
13057   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13058   add_name_and_src_coords_attributes (die, parm_pack);
13059   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13060     generic_parameter_die (parm_pack,
13061                            TREE_VEC_ELT (parm_pack_args, j),
13062                            false /* Don't emit DW_AT_name */,
13063                            die);
13064   return die;
13065 }
13066
13067 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13068    an enumerated type.  */
13069
13070 static inline int
13071 type_is_enum (const_tree type)
13072 {
13073   return TREE_CODE (type) == ENUMERAL_TYPE;
13074 }
13075
13076 /* Return the DBX register number described by a given RTL node.  */
13077
13078 static unsigned int
13079 dbx_reg_number (const_rtx rtl)
13080 {
13081   unsigned regno = REGNO (rtl);
13082
13083   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13084
13085 #ifdef LEAF_REG_REMAP
13086   if (current_function_uses_only_leaf_regs)
13087     {
13088       int leaf_reg = LEAF_REG_REMAP (regno);
13089       if (leaf_reg != -1)
13090         regno = (unsigned) leaf_reg;
13091     }
13092 #endif
13093
13094   return DBX_REGISTER_NUMBER (regno);
13095 }
13096
13097 /* Optionally add a DW_OP_piece term to a location description expression.
13098    DW_OP_piece is only added if the location description expression already
13099    doesn't end with DW_OP_piece.  */
13100
13101 static void
13102 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13103 {
13104   dw_loc_descr_ref loc;
13105
13106   if (*list_head != NULL)
13107     {
13108       /* Find the end of the chain.  */
13109       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13110         ;
13111
13112       if (loc->dw_loc_opc != DW_OP_piece)
13113         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13114     }
13115 }
13116
13117 /* Return a location descriptor that designates a machine register or
13118    zero if there is none.  */
13119
13120 static dw_loc_descr_ref
13121 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13122 {
13123   rtx regs;
13124
13125   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13126     return 0;
13127
13128   /* We only use "frame base" when we're sure we're talking about the
13129      post-prologue local stack frame.  We do this by *not* running
13130      register elimination until this point, and recognizing the special
13131      argument pointer and soft frame pointer rtx's.
13132      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13133   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13134       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13135     {
13136       dw_loc_descr_ref result = NULL;
13137
13138       if (dwarf_version >= 4 || !dwarf_strict)
13139         {
13140           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13141           if (result)
13142             add_loc_descr (&result,
13143                            new_loc_descr (DW_OP_stack_value, 0, 0));
13144         }
13145       return result;
13146     }
13147
13148   regs = targetm.dwarf_register_span (rtl);
13149
13150   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13151     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13152   else
13153     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13154 }
13155
13156 /* Return a location descriptor that designates a machine register for
13157    a given hard register number.  */
13158
13159 static dw_loc_descr_ref
13160 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13161 {
13162   dw_loc_descr_ref reg_loc_descr;
13163
13164   if (regno <= 31)
13165     reg_loc_descr
13166       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13167   else
13168     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13169
13170   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13171     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13172
13173   return reg_loc_descr;
13174 }
13175
13176 /* Given an RTL of a register, return a location descriptor that
13177    designates a value that spans more than one register.  */
13178
13179 static dw_loc_descr_ref
13180 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13181                              enum var_init_status initialized)
13182 {
13183   int nregs, size, i;
13184   unsigned reg;
13185   dw_loc_descr_ref loc_result = NULL;
13186
13187   reg = REGNO (rtl);
13188 #ifdef LEAF_REG_REMAP
13189   if (current_function_uses_only_leaf_regs)
13190     {
13191       int leaf_reg = LEAF_REG_REMAP (reg);
13192       if (leaf_reg != -1)
13193         reg = (unsigned) leaf_reg;
13194     }
13195 #endif
13196   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13197   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13198
13199   /* Simple, contiguous registers.  */
13200   if (regs == NULL_RTX)
13201     {
13202       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13203
13204       loc_result = NULL;
13205       while (nregs--)
13206         {
13207           dw_loc_descr_ref t;
13208
13209           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13210                                       VAR_INIT_STATUS_INITIALIZED);
13211           add_loc_descr (&loc_result, t);
13212           add_loc_descr_op_piece (&loc_result, size);
13213           ++reg;
13214         }
13215       return loc_result;
13216     }
13217
13218   /* Now onto stupid register sets in non contiguous locations.  */
13219
13220   gcc_assert (GET_CODE (regs) == PARALLEL);
13221
13222   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13223   loc_result = NULL;
13224
13225   for (i = 0; i < XVECLEN (regs, 0); ++i)
13226     {
13227       dw_loc_descr_ref t;
13228
13229       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13230                                   VAR_INIT_STATUS_INITIALIZED);
13231       add_loc_descr (&loc_result, t);
13232       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13233       add_loc_descr_op_piece (&loc_result, size);
13234     }
13235
13236   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13237     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13238   return loc_result;
13239 }
13240
13241 /* Return a location descriptor that designates a constant.  */
13242
13243 static dw_loc_descr_ref
13244 int_loc_descriptor (HOST_WIDE_INT i)
13245 {
13246   enum dwarf_location_atom op;
13247
13248   /* Pick the smallest representation of a constant, rather than just
13249      defaulting to the LEB encoding.  */
13250   if (i >= 0)
13251     {
13252       if (i <= 31)
13253         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13254       else if (i <= 0xff)
13255         op = DW_OP_const1u;
13256       else if (i <= 0xffff)
13257         op = DW_OP_const2u;
13258       else if (HOST_BITS_PER_WIDE_INT == 32
13259                || i <= 0xffffffff)
13260         op = DW_OP_const4u;
13261       else
13262         op = DW_OP_constu;
13263     }
13264   else
13265     {
13266       if (i >= -0x80)
13267         op = DW_OP_const1s;
13268       else if (i >= -0x8000)
13269         op = DW_OP_const2s;
13270       else if (HOST_BITS_PER_WIDE_INT == 32
13271                || i >= -0x80000000)
13272         op = DW_OP_const4s;
13273       else
13274         op = DW_OP_consts;
13275     }
13276
13277   return new_loc_descr (op, i, 0);
13278 }
13279
13280 /* Return loc description representing "address" of integer value.
13281    This can appear only as toplevel expression.  */
13282
13283 static dw_loc_descr_ref
13284 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13285 {
13286   int litsize;
13287   dw_loc_descr_ref loc_result = NULL;
13288
13289   if (!(dwarf_version >= 4 || !dwarf_strict))
13290     return NULL;
13291
13292   if (i >= 0)
13293     {
13294       if (i <= 31)
13295         litsize = 1;
13296       else if (i <= 0xff)
13297         litsize = 2;
13298       else if (i <= 0xffff)
13299         litsize = 3;
13300       else if (HOST_BITS_PER_WIDE_INT == 32
13301                || i <= 0xffffffff)
13302         litsize = 5;
13303       else
13304         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13305     }
13306   else
13307     {
13308       if (i >= -0x80)
13309         litsize = 2;
13310       else if (i >= -0x8000)
13311         litsize = 3;
13312       else if (HOST_BITS_PER_WIDE_INT == 32
13313                || i >= -0x80000000)
13314         litsize = 5;
13315       else
13316         litsize = 1 + size_of_sleb128 (i);
13317     }
13318   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13319      is more compact.  For DW_OP_stack_value we need:
13320      litsize + 1 (DW_OP_stack_value)
13321      and for DW_OP_implicit_value:
13322      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13323   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13324     {
13325       loc_result = int_loc_descriptor (i);
13326       add_loc_descr (&loc_result,
13327                      new_loc_descr (DW_OP_stack_value, 0, 0));
13328       return loc_result;
13329     }
13330
13331   loc_result = new_loc_descr (DW_OP_implicit_value,
13332                               size, 0);
13333   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13334   loc_result->dw_loc_oprnd2.v.val_int = i;
13335   return loc_result;
13336 }
13337
13338 /* Return a location descriptor that designates a base+offset location.  */
13339
13340 static dw_loc_descr_ref
13341 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13342                  enum var_init_status initialized)
13343 {
13344   unsigned int regno;
13345   dw_loc_descr_ref result;
13346   dw_fde_ref fde = current_fde ();
13347
13348   /* We only use "frame base" when we're sure we're talking about the
13349      post-prologue local stack frame.  We do this by *not* running
13350      register elimination until this point, and recognizing the special
13351      argument pointer and soft frame pointer rtx's.  */
13352   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13353     {
13354       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13355
13356       if (elim != reg)
13357         {
13358           if (GET_CODE (elim) == PLUS)
13359             {
13360               offset += INTVAL (XEXP (elim, 1));
13361               elim = XEXP (elim, 0);
13362             }
13363           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13364                        && (elim == hard_frame_pointer_rtx
13365                            || elim == stack_pointer_rtx))
13366                       || elim == (frame_pointer_needed
13367                                   ? hard_frame_pointer_rtx
13368                                   : stack_pointer_rtx));
13369
13370           /* If drap register is used to align stack, use frame
13371              pointer + offset to access stack variables.  If stack
13372              is aligned without drap, use stack pointer + offset to
13373              access stack variables.  */
13374           if (crtl->stack_realign_tried
13375               && reg == frame_pointer_rtx)
13376             {
13377               int base_reg
13378                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13379                                       ? HARD_FRAME_POINTER_REGNUM
13380                                       : STACK_POINTER_REGNUM);
13381               return new_reg_loc_descr (base_reg, offset);
13382             }
13383
13384           offset += frame_pointer_fb_offset;
13385           return new_loc_descr (DW_OP_fbreg, offset, 0);
13386         }
13387     }
13388   else if (!optimize
13389            && fde
13390            && (fde->drap_reg == REGNO (reg)
13391                || fde->vdrap_reg == REGNO (reg)))
13392     {
13393       /* Use cfa+offset to represent the location of arguments passed
13394          on the stack when drap is used to align stack.
13395          Only do this when not optimizing, for optimized code var-tracking
13396          is supposed to track where the arguments live and the register
13397          used as vdrap or drap in some spot might be used for something
13398          else in other part of the routine.  */
13399       return new_loc_descr (DW_OP_fbreg, offset, 0);
13400     }
13401
13402   regno = dbx_reg_number (reg);
13403   if (regno <= 31)
13404     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13405                             offset, 0);
13406   else
13407     result = new_loc_descr (DW_OP_bregx, regno, offset);
13408
13409   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13410     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13411
13412   return result;
13413 }
13414
13415 /* Return true if this RTL expression describes a base+offset calculation.  */
13416
13417 static inline int
13418 is_based_loc (const_rtx rtl)
13419 {
13420   return (GET_CODE (rtl) == PLUS
13421           && ((REG_P (XEXP (rtl, 0))
13422                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13423                && CONST_INT_P (XEXP (rtl, 1)))));
13424 }
13425
13426 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13427    failed.  */
13428
13429 static dw_loc_descr_ref
13430 tls_mem_loc_descriptor (rtx mem)
13431 {
13432   tree base;
13433   dw_loc_descr_ref loc_result;
13434
13435   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13436     return NULL;
13437
13438   base = get_base_address (MEM_EXPR (mem));
13439   if (base == NULL
13440       || TREE_CODE (base) != VAR_DECL
13441       || !DECL_THREAD_LOCAL_P (base))
13442     return NULL;
13443
13444   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13445   if (loc_result == NULL)
13446     return NULL;
13447
13448   if (INTVAL (MEM_OFFSET (mem)))
13449     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13450
13451   return loc_result;
13452 }
13453
13454 /* Output debug info about reason why we failed to expand expression as dwarf
13455    expression.  */
13456
13457 static void
13458 expansion_failed (tree expr, rtx rtl, char const *reason)
13459 {
13460   if (dump_file && (dump_flags & TDF_DETAILS))
13461     {
13462       fprintf (dump_file, "Failed to expand as dwarf: ");
13463       if (expr)
13464         print_generic_expr (dump_file, expr, dump_flags);
13465       if (rtl)
13466         {
13467           fprintf (dump_file, "\n");
13468           print_rtl (dump_file, rtl);
13469         }
13470       fprintf (dump_file, "\nReason: %s\n", reason);
13471     }
13472 }
13473
13474 /* Helper function for const_ok_for_output, called either directly
13475    or via for_each_rtx.  */
13476
13477 static int
13478 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13479 {
13480   rtx rtl = *rtlp;
13481
13482   if (GET_CODE (rtl) == UNSPEC)
13483     {
13484       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13485          we can't express it in the debug info.  */
13486 #ifdef ENABLE_CHECKING
13487       /* Don't complain about TLS UNSPECs, those are just too hard to
13488          delegitimize.  */
13489       if (XVECLEN (rtl, 0) != 1
13490           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13491           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13492           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13493           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13494         inform (current_function_decl
13495                 ? DECL_SOURCE_LOCATION (current_function_decl)
13496                 : UNKNOWN_LOCATION,
13497 #if NUM_UNSPEC_VALUES > 0
13498                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13499                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13500                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13501                 XINT (rtl, 1));
13502 #else
13503                 "non-delegitimized UNSPEC %d found in variable location",
13504                 XINT (rtl, 1));
13505 #endif
13506 #endif
13507       expansion_failed (NULL_TREE, rtl,
13508                         "UNSPEC hasn't been delegitimized.\n");
13509       return 1;
13510     }
13511
13512   if (GET_CODE (rtl) != SYMBOL_REF)
13513     return 0;
13514
13515   if (CONSTANT_POOL_ADDRESS_P (rtl))
13516     {
13517       bool marked;
13518       get_pool_constant_mark (rtl, &marked);
13519       /* If all references to this pool constant were optimized away,
13520          it was not output and thus we can't represent it.  */
13521       if (!marked)
13522         {
13523           expansion_failed (NULL_TREE, rtl,
13524                             "Constant was removed from constant pool.\n");
13525           return 1;
13526         }
13527     }
13528
13529   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13530     return 1;
13531
13532   /* Avoid references to external symbols in debug info, on several targets
13533      the linker might even refuse to link when linking a shared library,
13534      and in many other cases the relocations for .debug_info/.debug_loc are
13535      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13536      to be defined within the same shared library or executable are fine.  */
13537   if (SYMBOL_REF_EXTERNAL_P (rtl))
13538     {
13539       tree decl = SYMBOL_REF_DECL (rtl);
13540
13541       if (decl == NULL || !targetm.binds_local_p (decl))
13542         {
13543           expansion_failed (NULL_TREE, rtl,
13544                             "Symbol not defined in current TU.\n");
13545           return 1;
13546         }
13547     }
13548
13549   return 0;
13550 }
13551
13552 /* Return true if constant RTL can be emitted in DW_OP_addr or
13553    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13554    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13555
13556 static bool
13557 const_ok_for_output (rtx rtl)
13558 {
13559   if (GET_CODE (rtl) == SYMBOL_REF)
13560     return const_ok_for_output_1 (&rtl, NULL) == 0;
13561
13562   if (GET_CODE (rtl) == CONST)
13563     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13564
13565   return true;
13566 }
13567
13568 /* The following routine converts the RTL for a variable or parameter
13569    (resident in memory) into an equivalent Dwarf representation of a
13570    mechanism for getting the address of that same variable onto the top of a
13571    hypothetical "address evaluation" stack.
13572
13573    When creating memory location descriptors, we are effectively transforming
13574    the RTL for a memory-resident object into its Dwarf postfix expression
13575    equivalent.  This routine recursively descends an RTL tree, turning
13576    it into Dwarf postfix code as it goes.
13577
13578    MODE is the mode of the memory reference, needed to handle some
13579    autoincrement addressing modes.
13580
13581    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13582    location list for RTL.
13583
13584    Return 0 if we can't represent the location.  */
13585
13586 static dw_loc_descr_ref
13587 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13588                     enum var_init_status initialized)
13589 {
13590   dw_loc_descr_ref mem_loc_result = NULL;
13591   enum dwarf_location_atom op;
13592   dw_loc_descr_ref op0, op1;
13593
13594   /* Note that for a dynamically sized array, the location we will generate a
13595      description of here will be the lowest numbered location which is
13596      actually within the array.  That's *not* necessarily the same as the
13597      zeroth element of the array.  */
13598
13599   rtl = targetm.delegitimize_address (rtl);
13600
13601   switch (GET_CODE (rtl))
13602     {
13603     case POST_INC:
13604     case POST_DEC:
13605     case POST_MODIFY:
13606       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13607
13608     case SUBREG:
13609       /* The case of a subreg may arise when we have a local (register)
13610          variable or a formal (register) parameter which doesn't quite fill
13611          up an entire register.  For now, just assume that it is
13612          legitimate to make the Dwarf info refer to the whole register which
13613          contains the given subreg.  */
13614       if (!subreg_lowpart_p (rtl))
13615         break;
13616       rtl = SUBREG_REG (rtl);
13617       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13618         break;
13619       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13620         break;
13621       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13622       break;
13623
13624     case REG:
13625       /* Whenever a register number forms a part of the description of the
13626          method for calculating the (dynamic) address of a memory resident
13627          object, DWARF rules require the register number be referred to as
13628          a "base register".  This distinction is not based in any way upon
13629          what category of register the hardware believes the given register
13630          belongs to.  This is strictly DWARF terminology we're dealing with
13631          here. Note that in cases where the location of a memory-resident
13632          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13633          OP_CONST (0)) the actual DWARF location descriptor that we generate
13634          may just be OP_BASEREG (basereg).  This may look deceptively like
13635          the object in question was allocated to a register (rather than in
13636          memory) so DWARF consumers need to be aware of the subtle
13637          distinction between OP_REG and OP_BASEREG.  */
13638       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13639         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13640       else if (stack_realign_drap
13641                && crtl->drap_reg
13642                && crtl->args.internal_arg_pointer == rtl
13643                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13644         {
13645           /* If RTL is internal_arg_pointer, which has been optimized
13646              out, use DRAP instead.  */
13647           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13648                                             VAR_INIT_STATUS_INITIALIZED);
13649         }
13650       break;
13651
13652     case SIGN_EXTEND:
13653     case ZERO_EXTEND:
13654       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13655                                 VAR_INIT_STATUS_INITIALIZED);
13656       if (op0 == 0)
13657         break;
13658       else
13659         {
13660           int shift = DWARF2_ADDR_SIZE
13661                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13662           shift *= BITS_PER_UNIT;
13663           if (GET_CODE (rtl) == SIGN_EXTEND)
13664             op = DW_OP_shra;
13665           else
13666             op = DW_OP_shr;
13667           mem_loc_result = op0;
13668           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13669           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13670           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13671           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13672         }
13673       break;
13674
13675     case MEM:
13676       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13677                                            VAR_INIT_STATUS_INITIALIZED);
13678       if (mem_loc_result == NULL)
13679         mem_loc_result = tls_mem_loc_descriptor (rtl);
13680       if (mem_loc_result != 0)
13681         {
13682           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13683             {
13684               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13685               return 0;
13686             }
13687           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13688             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13689           else
13690             add_loc_descr (&mem_loc_result,
13691                            new_loc_descr (DW_OP_deref_size,
13692                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13693         }
13694       else
13695         {
13696           rtx new_rtl = avoid_constant_pool_reference (rtl);
13697           if (new_rtl != rtl)
13698             return mem_loc_descriptor (new_rtl, mode, initialized);
13699         }
13700       break;
13701
13702     case LO_SUM:
13703          rtl = XEXP (rtl, 1);
13704
13705       /* ... fall through ...  */
13706
13707     case LABEL_REF:
13708       /* Some ports can transform a symbol ref into a label ref, because
13709          the symbol ref is too far away and has to be dumped into a constant
13710          pool.  */
13711     case CONST:
13712     case SYMBOL_REF:
13713       if (GET_CODE (rtl) == SYMBOL_REF
13714           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13715         {
13716           dw_loc_descr_ref temp;
13717
13718           /* If this is not defined, we have no way to emit the data.  */
13719           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13720             break;
13721
13722           /* We used to emit DW_OP_addr here, but that's wrong, since
13723              DW_OP_addr should be relocated by the debug info consumer,
13724              while DW_OP_GNU_push_tls_address operand should not.  */
13725           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13726                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13727           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13728           temp->dw_loc_oprnd1.v.val_addr = rtl;
13729           temp->dtprel = true;
13730
13731           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13732           add_loc_descr (&mem_loc_result, temp);
13733
13734           break;
13735         }
13736
13737       if (!const_ok_for_output (rtl))
13738         break;
13739
13740     symref:
13741       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13742       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13743       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13744       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13745       break;
13746
13747     case CONCAT:
13748     case CONCATN:
13749     case VAR_LOCATION:
13750     case DEBUG_IMPLICIT_PTR:
13751       expansion_failed (NULL_TREE, rtl,
13752                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13753       return 0;
13754
13755     case ENTRY_VALUE:
13756       if (dwarf_strict)
13757         return NULL;
13758       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13759       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13760       if (REG_P (ENTRY_VALUE_EXP (rtl)))
13761         mem_loc_result->dw_loc_oprnd1.v.val_loc
13762           = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
13763                                     VAR_INIT_STATUS_INITIALIZED);
13764       else if (MEM_P (ENTRY_VALUE_EXP (rtl)) && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13765         {
13766           dw_loc_descr_ref ref
13767             = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), GET_MODE (rtl),
13768                                   VAR_INIT_STATUS_INITIALIZED);
13769           if (ref == NULL || ref->dw_loc_opc == DW_OP_fbreg)
13770             return NULL;
13771           mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
13772         }
13773       else
13774         gcc_unreachable ();
13775       return mem_loc_result;
13776
13777     case PRE_MODIFY:
13778       /* Extract the PLUS expression nested inside and fall into
13779          PLUS code below.  */
13780       rtl = XEXP (rtl, 1);
13781       goto plus;
13782
13783     case PRE_INC:
13784     case PRE_DEC:
13785       /* Turn these into a PLUS expression and fall into the PLUS code
13786          below.  */
13787       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13788                           GEN_INT (GET_CODE (rtl) == PRE_INC
13789                                    ? GET_MODE_UNIT_SIZE (mode)
13790                                    : -GET_MODE_UNIT_SIZE (mode)));
13791
13792       /* ... fall through ...  */
13793
13794     case PLUS:
13795     plus:
13796       if (is_based_loc (rtl))
13797         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13798                                           INTVAL (XEXP (rtl, 1)),
13799                                           VAR_INIT_STATUS_INITIALIZED);
13800       else
13801         {
13802           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13803                                                VAR_INIT_STATUS_INITIALIZED);
13804           if (mem_loc_result == 0)
13805             break;
13806
13807           if (CONST_INT_P (XEXP (rtl, 1)))
13808             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13809           else
13810             {
13811               dw_loc_descr_ref mem_loc_result2
13812                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13813                                       VAR_INIT_STATUS_INITIALIZED);
13814               if (mem_loc_result2 == 0)
13815                 break;
13816               add_loc_descr (&mem_loc_result, mem_loc_result2);
13817               add_loc_descr (&mem_loc_result,
13818                              new_loc_descr (DW_OP_plus, 0, 0));
13819             }
13820         }
13821       break;
13822
13823     /* If a pseudo-reg is optimized away, it is possible for it to
13824        be replaced with a MEM containing a multiply or shift.  */
13825     case MINUS:
13826       op = DW_OP_minus;
13827       goto do_binop;
13828
13829     case MULT:
13830       op = DW_OP_mul;
13831       goto do_binop;
13832
13833     case DIV:
13834       op = DW_OP_div;
13835       goto do_binop;
13836
13837     case UMOD:
13838       op = DW_OP_mod;
13839       goto do_binop;
13840
13841     case ASHIFT:
13842       op = DW_OP_shl;
13843       goto do_binop;
13844
13845     case ASHIFTRT:
13846       op = DW_OP_shra;
13847       goto do_binop;
13848
13849     case LSHIFTRT:
13850       op = DW_OP_shr;
13851       goto do_binop;
13852
13853     case AND:
13854       op = DW_OP_and;
13855       goto do_binop;
13856
13857     case IOR:
13858       op = DW_OP_or;
13859       goto do_binop;
13860
13861     case XOR:
13862       op = DW_OP_xor;
13863       goto do_binop;
13864
13865     do_binop:
13866       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13867                                 VAR_INIT_STATUS_INITIALIZED);
13868       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13869                                 VAR_INIT_STATUS_INITIALIZED);
13870
13871       if (op0 == 0 || op1 == 0)
13872         break;
13873
13874       mem_loc_result = op0;
13875       add_loc_descr (&mem_loc_result, op1);
13876       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13877       break;
13878
13879     case MOD:
13880       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13881                                 VAR_INIT_STATUS_INITIALIZED);
13882       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13883                                 VAR_INIT_STATUS_INITIALIZED);
13884
13885       if (op0 == 0 || op1 == 0)
13886         break;
13887
13888       mem_loc_result = op0;
13889       add_loc_descr (&mem_loc_result, op1);
13890       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13891       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13892       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13893       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13894       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13895       break;
13896
13897     case NOT:
13898       op = DW_OP_not;
13899       goto do_unop;
13900
13901     case ABS:
13902       op = DW_OP_abs;
13903       goto do_unop;
13904
13905     case NEG:
13906       op = DW_OP_neg;
13907       goto do_unop;
13908
13909     do_unop:
13910       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13911                                 VAR_INIT_STATUS_INITIALIZED);
13912
13913       if (op0 == 0)
13914         break;
13915
13916       mem_loc_result = op0;
13917       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13918       break;
13919
13920     case CONST_INT:
13921       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13922       break;
13923
13924     case EQ:
13925       op = DW_OP_eq;
13926       goto do_scompare;
13927
13928     case GE:
13929       op = DW_OP_ge;
13930       goto do_scompare;
13931
13932     case GT:
13933       op = DW_OP_gt;
13934       goto do_scompare;
13935
13936     case LE:
13937       op = DW_OP_le;
13938       goto do_scompare;
13939
13940     case LT:
13941       op = DW_OP_lt;
13942       goto do_scompare;
13943
13944     case NE:
13945       op = DW_OP_ne;
13946       goto do_scompare;
13947
13948     do_scompare:
13949       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13950           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13951         break;
13952       else
13953         {
13954           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13955
13956           if (op_mode == VOIDmode)
13957             op_mode = GET_MODE (XEXP (rtl, 1));
13958           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13959             break;
13960
13961           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13962                                     VAR_INIT_STATUS_INITIALIZED);
13963           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13964                                     VAR_INIT_STATUS_INITIALIZED);
13965
13966           if (op0 == 0 || op1 == 0)
13967             break;
13968
13969           if (op_mode != VOIDmode
13970               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13971             {
13972               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13973               shift *= BITS_PER_UNIT;
13974               /* For eq/ne, if the operands are known to be zero-extended,
13975                  there is no need to do the fancy shifting up.  */
13976               if (op == DW_OP_eq || op == DW_OP_ne)
13977                 {
13978                   dw_loc_descr_ref last0, last1;
13979                   for (last0 = op0;
13980                        last0->dw_loc_next != NULL;
13981                        last0 = last0->dw_loc_next)
13982                     ;
13983                   for (last1 = op1;
13984                        last1->dw_loc_next != NULL;
13985                        last1 = last1->dw_loc_next)
13986                     ;
13987                   /* deref_size zero extends, and for constants we can check
13988                      whether they are zero extended or not.  */
13989                   if (((last0->dw_loc_opc == DW_OP_deref_size
13990                         && last0->dw_loc_oprnd1.v.val_int
13991                            <= GET_MODE_SIZE (op_mode))
13992                        || (CONST_INT_P (XEXP (rtl, 0))
13993                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13994                                == (INTVAL (XEXP (rtl, 0))
13995                                    & GET_MODE_MASK (op_mode))))
13996                       && ((last1->dw_loc_opc == DW_OP_deref_size
13997                            && last1->dw_loc_oprnd1.v.val_int
13998                               <= GET_MODE_SIZE (op_mode))
13999                           || (CONST_INT_P (XEXP (rtl, 1))
14000                               && (unsigned HOST_WIDE_INT)
14001                                  INTVAL (XEXP (rtl, 1))
14002                                  == (INTVAL (XEXP (rtl, 1))
14003                                      & GET_MODE_MASK (op_mode)))))
14004                     goto do_compare;
14005                 }
14006               add_loc_descr (&op0, int_loc_descriptor (shift));
14007               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14008               if (CONST_INT_P (XEXP (rtl, 1)))
14009                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14010               else
14011                 {
14012                   add_loc_descr (&op1, int_loc_descriptor (shift));
14013                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14014                 }
14015             }
14016         }
14017
14018     do_compare:
14019       mem_loc_result = op0;
14020       add_loc_descr (&mem_loc_result, op1);
14021       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14022       if (STORE_FLAG_VALUE != 1)
14023         {
14024           add_loc_descr (&mem_loc_result,
14025                          int_loc_descriptor (STORE_FLAG_VALUE));
14026           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14027         }
14028       break;
14029
14030     case GEU:
14031       op = DW_OP_ge;
14032       goto do_ucompare;
14033
14034     case GTU:
14035       op = DW_OP_gt;
14036       goto do_ucompare;
14037
14038     case LEU:
14039       op = DW_OP_le;
14040       goto do_ucompare;
14041
14042     case LTU:
14043       op = DW_OP_lt;
14044       goto do_ucompare;
14045
14046     do_ucompare:
14047       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14048           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14049         break;
14050       else
14051         {
14052           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14053
14054           if (op_mode == VOIDmode)
14055             op_mode = GET_MODE (XEXP (rtl, 1));
14056           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14057             break;
14058
14059           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14060                                     VAR_INIT_STATUS_INITIALIZED);
14061           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14062                                     VAR_INIT_STATUS_INITIALIZED);
14063
14064           if (op0 == 0 || op1 == 0)
14065             break;
14066
14067           if (op_mode != VOIDmode
14068               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14069             {
14070               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14071               dw_loc_descr_ref last0, last1;
14072               for (last0 = op0;
14073                    last0->dw_loc_next != NULL;
14074                    last0 = last0->dw_loc_next)
14075                 ;
14076               for (last1 = op1;
14077                    last1->dw_loc_next != NULL;
14078                    last1 = last1->dw_loc_next)
14079                 ;
14080               if (CONST_INT_P (XEXP (rtl, 0)))
14081                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14082               /* deref_size zero extends, so no need to mask it again.  */
14083               else if (last0->dw_loc_opc != DW_OP_deref_size
14084                        || last0->dw_loc_oprnd1.v.val_int
14085                           > GET_MODE_SIZE (op_mode))
14086                 {
14087                   add_loc_descr (&op0, int_loc_descriptor (mask));
14088                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14089                 }
14090               if (CONST_INT_P (XEXP (rtl, 1)))
14091                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14092               /* deref_size zero extends, so no need to mask it again.  */
14093               else if (last1->dw_loc_opc != DW_OP_deref_size
14094                        || last1->dw_loc_oprnd1.v.val_int
14095                           > GET_MODE_SIZE (op_mode))
14096                 {
14097                   add_loc_descr (&op1, int_loc_descriptor (mask));
14098                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14099                 }
14100             }
14101           else
14102             {
14103               HOST_WIDE_INT bias = 1;
14104               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14105               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14106               if (CONST_INT_P (XEXP (rtl, 1)))
14107                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14108                                           + INTVAL (XEXP (rtl, 1)));
14109               else
14110                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14111                                                     bias, 0));
14112             }
14113         }
14114       goto do_compare;
14115
14116     case SMIN:
14117     case SMAX:
14118     case UMIN:
14119     case UMAX:
14120       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14121           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14122           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14123         break;
14124
14125       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14126                                 VAR_INIT_STATUS_INITIALIZED);
14127       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14128                                 VAR_INIT_STATUS_INITIALIZED);
14129
14130       if (op0 == 0 || op1 == 0)
14131         break;
14132
14133       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14134       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14135       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14136       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14137         {
14138           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14139             {
14140               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14141               add_loc_descr (&op0, int_loc_descriptor (mask));
14142               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14143               add_loc_descr (&op1, int_loc_descriptor (mask));
14144               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14145             }
14146           else
14147             {
14148               HOST_WIDE_INT bias = 1;
14149               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14150               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14151               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14152             }
14153         }
14154       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14155         {
14156           int shift = DWARF2_ADDR_SIZE
14157                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14158           shift *= BITS_PER_UNIT;
14159           add_loc_descr (&op0, int_loc_descriptor (shift));
14160           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14161           add_loc_descr (&op1, int_loc_descriptor (shift));
14162           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14163         }
14164
14165       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14166         op = DW_OP_lt;
14167       else
14168         op = DW_OP_gt;
14169       mem_loc_result = op0;
14170       add_loc_descr (&mem_loc_result, op1);
14171       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14172       {
14173         dw_loc_descr_ref bra_node, drop_node;
14174
14175         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14176         add_loc_descr (&mem_loc_result, bra_node);
14177         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14178         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14179         add_loc_descr (&mem_loc_result, drop_node);
14180         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14181         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14182       }
14183       break;
14184
14185     case ZERO_EXTRACT:
14186     case SIGN_EXTRACT:
14187       if (CONST_INT_P (XEXP (rtl, 1))
14188           && CONST_INT_P (XEXP (rtl, 2))
14189           && ((unsigned) INTVAL (XEXP (rtl, 1))
14190               + (unsigned) INTVAL (XEXP (rtl, 2))
14191               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14192           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14193           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14194         {
14195           int shift, size;
14196           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14197                                     VAR_INIT_STATUS_INITIALIZED);
14198           if (op0 == 0)
14199             break;
14200           if (GET_CODE (rtl) == SIGN_EXTRACT)
14201             op = DW_OP_shra;
14202           else
14203             op = DW_OP_shr;
14204           mem_loc_result = op0;
14205           size = INTVAL (XEXP (rtl, 1));
14206           shift = INTVAL (XEXP (rtl, 2));
14207           if (BITS_BIG_ENDIAN)
14208             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14209                     - shift - size;
14210           if (shift + size != (int) DWARF2_ADDR_SIZE)
14211             {
14212               add_loc_descr (&mem_loc_result,
14213                              int_loc_descriptor (DWARF2_ADDR_SIZE
14214                                                  - shift - size));
14215               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14216             }
14217           if (size != (int) DWARF2_ADDR_SIZE)
14218             {
14219               add_loc_descr (&mem_loc_result,
14220                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14221               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14222             }
14223         }
14224       break;
14225
14226     case IF_THEN_ELSE:
14227       {
14228         dw_loc_descr_ref op2, bra_node, drop_node;
14229         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14230                                   VAR_INIT_STATUS_INITIALIZED);
14231         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14232                                   VAR_INIT_STATUS_INITIALIZED);
14233         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14234                                   VAR_INIT_STATUS_INITIALIZED);
14235         if (op0 == NULL || op1 == NULL || op2 == NULL)
14236           break;
14237
14238         mem_loc_result = op1;
14239         add_loc_descr (&mem_loc_result, op2);
14240         add_loc_descr (&mem_loc_result, op0);
14241         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14242         add_loc_descr (&mem_loc_result, bra_node);
14243         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14244         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14245         add_loc_descr (&mem_loc_result, drop_node);
14246         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14247         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14248       }
14249       break;
14250
14251     case COMPARE:
14252     case ROTATE:
14253     case ROTATERT:
14254     case TRUNCATE:
14255       /* In theory, we could implement the above.  */
14256       /* DWARF cannot represent the unsigned compare operations
14257          natively.  */
14258     case SS_MULT:
14259     case US_MULT:
14260     case SS_DIV:
14261     case US_DIV:
14262     case SS_PLUS:
14263     case US_PLUS:
14264     case SS_MINUS:
14265     case US_MINUS:
14266     case SS_NEG:
14267     case US_NEG:
14268     case SS_ABS:
14269     case SS_ASHIFT:
14270     case US_ASHIFT:
14271     case SS_TRUNCATE:
14272     case US_TRUNCATE:
14273     case UDIV:
14274     case UNORDERED:
14275     case ORDERED:
14276     case UNEQ:
14277     case UNGE:
14278     case UNGT:
14279     case UNLE:
14280     case UNLT:
14281     case LTGT:
14282     case FLOAT_EXTEND:
14283     case FLOAT_TRUNCATE:
14284     case FLOAT:
14285     case UNSIGNED_FLOAT:
14286     case FIX:
14287     case UNSIGNED_FIX:
14288     case FRACT_CONVERT:
14289     case UNSIGNED_FRACT_CONVERT:
14290     case SAT_FRACT:
14291     case UNSIGNED_SAT_FRACT:
14292     case SQRT:
14293     case BSWAP:
14294     case FFS:
14295     case CLZ:
14296     case CTZ:
14297     case POPCOUNT:
14298     case PARITY:
14299     case ASM_OPERANDS:
14300     case VEC_MERGE:
14301     case VEC_SELECT:
14302     case VEC_CONCAT:
14303     case VEC_DUPLICATE:
14304     case UNSPEC:
14305     case HIGH:
14306       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14307          can't express it in the debug info.  This can happen e.g. with some
14308          TLS UNSPECs.  */
14309       break;
14310
14311     case CONST_STRING:
14312       resolve_one_addr (&rtl, NULL);
14313       goto symref;
14314
14315     default:
14316 #ifdef ENABLE_CHECKING
14317       print_rtl (stderr, rtl);
14318       gcc_unreachable ();
14319 #else
14320       break;
14321 #endif
14322     }
14323
14324   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14325     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14326
14327   return mem_loc_result;
14328 }
14329
14330 /* Return a descriptor that describes the concatenation of two locations.
14331    This is typically a complex variable.  */
14332
14333 static dw_loc_descr_ref
14334 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14335 {
14336   dw_loc_descr_ref cc_loc_result = NULL;
14337   dw_loc_descr_ref x0_ref
14338     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14339   dw_loc_descr_ref x1_ref
14340     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14341
14342   if (x0_ref == 0 || x1_ref == 0)
14343     return 0;
14344
14345   cc_loc_result = x0_ref;
14346   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14347
14348   add_loc_descr (&cc_loc_result, x1_ref);
14349   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14350
14351   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14352     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14353
14354   return cc_loc_result;
14355 }
14356
14357 /* Return a descriptor that describes the concatenation of N
14358    locations.  */
14359
14360 static dw_loc_descr_ref
14361 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14362 {
14363   unsigned int i;
14364   dw_loc_descr_ref cc_loc_result = NULL;
14365   unsigned int n = XVECLEN (concatn, 0);
14366
14367   for (i = 0; i < n; ++i)
14368     {
14369       dw_loc_descr_ref ref;
14370       rtx x = XVECEXP (concatn, 0, i);
14371
14372       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14373       if (ref == NULL)
14374         return NULL;
14375
14376       add_loc_descr (&cc_loc_result, ref);
14377       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14378     }
14379
14380   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14381     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14382
14383   return cc_loc_result;
14384 }
14385
14386 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14387    for DEBUG_IMPLICIT_PTR RTL.  */
14388
14389 static dw_loc_descr_ref
14390 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14391 {
14392   dw_loc_descr_ref ret;
14393   dw_die_ref ref;
14394
14395   if (dwarf_strict)
14396     return NULL;
14397   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14398               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14399               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14400   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14401   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14402   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14403   if (ref)
14404     {
14405       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14406       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14407       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14408     }
14409   else
14410     {
14411       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14412       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14413     }
14414   return ret;
14415 }
14416
14417 /* Output a proper Dwarf location descriptor for a variable or parameter
14418    which is either allocated in a register or in a memory location.  For a
14419    register, we just generate an OP_REG and the register number.  For a
14420    memory location we provide a Dwarf postfix expression describing how to
14421    generate the (dynamic) address of the object onto the address stack.
14422
14423    MODE is mode of the decl if this loc_descriptor is going to be used in
14424    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14425    allowed, VOIDmode otherwise.
14426
14427    If we don't know how to describe it, return 0.  */
14428
14429 static dw_loc_descr_ref
14430 loc_descriptor (rtx rtl, enum machine_mode mode,
14431                 enum var_init_status initialized)
14432 {
14433   dw_loc_descr_ref loc_result = NULL;
14434
14435   switch (GET_CODE (rtl))
14436     {
14437     case SUBREG:
14438       /* The case of a subreg may arise when we have a local (register)
14439          variable or a formal (register) parameter which doesn't quite fill
14440          up an entire register.  For now, just assume that it is
14441          legitimate to make the Dwarf info refer to the whole register which
14442          contains the given subreg.  */
14443       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14444       break;
14445
14446     case REG:
14447       loc_result = reg_loc_descriptor (rtl, initialized);
14448       break;
14449
14450     case MEM:
14451       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14452                                        initialized);
14453       if (loc_result == NULL)
14454         loc_result = tls_mem_loc_descriptor (rtl);
14455       if (loc_result == NULL)
14456         {
14457           rtx new_rtl = avoid_constant_pool_reference (rtl);
14458           if (new_rtl != rtl)
14459             loc_result = loc_descriptor (new_rtl, mode, initialized);
14460         }
14461       break;
14462
14463     case CONCAT:
14464       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14465                                           initialized);
14466       break;
14467
14468     case CONCATN:
14469       loc_result = concatn_loc_descriptor (rtl, initialized);
14470       break;
14471
14472     case VAR_LOCATION:
14473       /* Single part.  */
14474       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14475         {
14476           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14477           if (GET_CODE (loc) == EXPR_LIST)
14478             loc = XEXP (loc, 0);
14479           loc_result = loc_descriptor (loc, mode, initialized);
14480           break;
14481         }
14482
14483       rtl = XEXP (rtl, 1);
14484       /* FALLTHRU */
14485
14486     case PARALLEL:
14487       {
14488         rtvec par_elems = XVEC (rtl, 0);
14489         int num_elem = GET_NUM_ELEM (par_elems);
14490         enum machine_mode mode;
14491         int i;
14492
14493         /* Create the first one, so we have something to add to.  */
14494         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14495                                      VOIDmode, initialized);
14496         if (loc_result == NULL)
14497           return NULL;
14498         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14499         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14500         for (i = 1; i < num_elem; i++)
14501           {
14502             dw_loc_descr_ref temp;
14503
14504             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14505                                    VOIDmode, initialized);
14506             if (temp == NULL)
14507               return NULL;
14508             add_loc_descr (&loc_result, temp);
14509             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14510             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14511           }
14512       }
14513       break;
14514
14515     case CONST_INT:
14516       if (mode != VOIDmode && mode != BLKmode)
14517         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14518                                                     INTVAL (rtl));
14519       break;
14520
14521     case CONST_DOUBLE:
14522       if (mode == VOIDmode)
14523         mode = GET_MODE (rtl);
14524
14525       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14526         {
14527           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14528
14529           /* Note that a CONST_DOUBLE rtx could represent either an integer
14530              or a floating-point constant.  A CONST_DOUBLE is used whenever
14531              the constant requires more than one word in order to be
14532              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14533           loc_result = new_loc_descr (DW_OP_implicit_value,
14534                                       GET_MODE_SIZE (mode), 0);
14535           if (SCALAR_FLOAT_MODE_P (mode))
14536             {
14537               unsigned int length = GET_MODE_SIZE (mode);
14538               unsigned char *array
14539                   = (unsigned char*) ggc_alloc_atomic (length);
14540
14541               insert_float (rtl, array);
14542               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14543               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14544               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14545               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14546             }
14547           else
14548             {
14549               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14550               loc_result->dw_loc_oprnd2.v.val_double
14551                 = rtx_to_double_int (rtl);
14552             }
14553         }
14554       break;
14555
14556     case CONST_VECTOR:
14557       if (mode == VOIDmode)
14558         mode = GET_MODE (rtl);
14559
14560       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14561         {
14562           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14563           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14564           unsigned char *array = (unsigned char *)
14565             ggc_alloc_atomic (length * elt_size);
14566           unsigned int i;
14567           unsigned char *p;
14568
14569           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14570           switch (GET_MODE_CLASS (mode))
14571             {
14572             case MODE_VECTOR_INT:
14573               for (i = 0, p = array; i < length; i++, p += elt_size)
14574                 {
14575                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14576                   double_int val = rtx_to_double_int (elt);
14577
14578                   if (elt_size <= sizeof (HOST_WIDE_INT))
14579                     insert_int (double_int_to_shwi (val), elt_size, p);
14580                   else
14581                     {
14582                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14583                       insert_double (val, p);
14584                     }
14585                 }
14586               break;
14587
14588             case MODE_VECTOR_FLOAT:
14589               for (i = 0, p = array; i < length; i++, p += elt_size)
14590                 {
14591                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14592                   insert_float (elt, p);
14593                 }
14594               break;
14595
14596             default:
14597               gcc_unreachable ();
14598             }
14599
14600           loc_result = new_loc_descr (DW_OP_implicit_value,
14601                                       length * elt_size, 0);
14602           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14603           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14604           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14605           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14606         }
14607       break;
14608
14609     case CONST:
14610       if (mode == VOIDmode
14611           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14612           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14613           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14614         {
14615           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14616           break;
14617         }
14618       /* FALLTHROUGH */
14619     case SYMBOL_REF:
14620       if (!const_ok_for_output (rtl))
14621         break;
14622     case LABEL_REF:
14623       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14624           && (dwarf_version >= 4 || !dwarf_strict))
14625         {
14626           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14627           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14628           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14629           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14630           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14631         }
14632       break;
14633
14634     case DEBUG_IMPLICIT_PTR:
14635       loc_result = implicit_ptr_descriptor (rtl, 0);
14636       break;
14637
14638     case PLUS:
14639       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14640           && CONST_INT_P (XEXP (rtl, 1)))
14641         {
14642           loc_result
14643             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14644           break;
14645         }
14646       /* FALLTHRU */
14647     default:
14648       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14649           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14650           && (dwarf_version >= 4 || !dwarf_strict))
14651         {
14652           /* Value expression.  */
14653           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14654           if (loc_result)
14655             add_loc_descr (&loc_result,
14656                            new_loc_descr (DW_OP_stack_value, 0, 0));
14657         }
14658       break;
14659     }
14660
14661   return loc_result;
14662 }
14663
14664 /* We need to figure out what section we should use as the base for the
14665    address ranges where a given location is valid.
14666    1. If this particular DECL has a section associated with it, use that.
14667    2. If this function has a section associated with it, use that.
14668    3. Otherwise, use the text section.
14669    XXX: If you split a variable across multiple sections, we won't notice.  */
14670
14671 static const char *
14672 secname_for_decl (const_tree decl)
14673 {
14674   const char *secname;
14675
14676   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14677     {
14678       tree sectree = DECL_SECTION_NAME (decl);
14679       secname = TREE_STRING_POINTER (sectree);
14680     }
14681   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14682     {
14683       tree sectree = DECL_SECTION_NAME (current_function_decl);
14684       secname = TREE_STRING_POINTER (sectree);
14685     }
14686   else if (cfun && in_cold_section_p)
14687     secname = crtl->subsections.cold_section_label;
14688   else
14689     secname = text_section_label;
14690
14691   return secname;
14692 }
14693
14694 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14695
14696 static bool
14697 decl_by_reference_p (tree decl)
14698 {
14699   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14700            || TREE_CODE (decl) == VAR_DECL)
14701           && DECL_BY_REFERENCE (decl));
14702 }
14703
14704 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14705    for VARLOC.  */
14706
14707 static dw_loc_descr_ref
14708 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14709                enum var_init_status initialized)
14710 {
14711   int have_address = 0;
14712   dw_loc_descr_ref descr;
14713   enum machine_mode mode;
14714
14715   if (want_address != 2)
14716     {
14717       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14718       /* Single part.  */
14719       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14720         {
14721           varloc = PAT_VAR_LOCATION_LOC (varloc);
14722           if (GET_CODE (varloc) == EXPR_LIST)
14723             varloc = XEXP (varloc, 0);
14724           mode = GET_MODE (varloc);
14725           if (MEM_P (varloc))
14726             {
14727               rtx addr = XEXP (varloc, 0);
14728               descr = mem_loc_descriptor (addr, mode, initialized);
14729               if (descr)
14730                 have_address = 1;
14731               else
14732                 {
14733                   rtx x = avoid_constant_pool_reference (varloc);
14734                   if (x != varloc)
14735                     descr = mem_loc_descriptor (x, mode, initialized);
14736                 }
14737             }
14738           else
14739             descr = mem_loc_descriptor (varloc, mode, initialized);
14740         }
14741       else
14742         return 0;
14743     }
14744   else
14745     {
14746       if (GET_CODE (varloc) == VAR_LOCATION)
14747         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14748       else
14749         mode = DECL_MODE (loc);
14750       descr = loc_descriptor (varloc, mode, initialized);
14751       have_address = 1;
14752     }
14753
14754   if (!descr)
14755     return 0;
14756
14757   if (want_address == 2 && !have_address
14758       && (dwarf_version >= 4 || !dwarf_strict))
14759     {
14760       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14761         {
14762           expansion_failed (loc, NULL_RTX,
14763                             "DWARF address size mismatch");
14764           return 0;
14765         }
14766       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14767       have_address = 1;
14768     }
14769   /* Show if we can't fill the request for an address.  */
14770   if (want_address && !have_address)
14771     {
14772       expansion_failed (loc, NULL_RTX,
14773                         "Want address and only have value");
14774       return 0;
14775     }
14776
14777   /* If we've got an address and don't want one, dereference.  */
14778   if (!want_address && have_address)
14779     {
14780       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14781       enum dwarf_location_atom op;
14782
14783       if (size > DWARF2_ADDR_SIZE || size == -1)
14784         {
14785           expansion_failed (loc, NULL_RTX,
14786                             "DWARF address size mismatch");
14787           return 0;
14788         }
14789       else if (size == DWARF2_ADDR_SIZE)
14790         op = DW_OP_deref;
14791       else
14792         op = DW_OP_deref_size;
14793
14794       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14795     }
14796
14797   return descr;
14798 }
14799
14800 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14801    if it is not possible.  */
14802
14803 static dw_loc_descr_ref
14804 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14805 {
14806   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14807     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14808   else if (dwarf_version >= 3 || !dwarf_strict)
14809     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14810   else
14811     return NULL;
14812 }
14813
14814 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14815    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14816
14817 static dw_loc_descr_ref
14818 dw_sra_loc_expr (tree decl, rtx loc)
14819 {
14820   rtx p;
14821   unsigned int padsize = 0;
14822   dw_loc_descr_ref descr, *descr_tail;
14823   unsigned HOST_WIDE_INT decl_size;
14824   rtx varloc;
14825   enum var_init_status initialized;
14826
14827   if (DECL_SIZE (decl) == NULL
14828       || !host_integerp (DECL_SIZE (decl), 1))
14829     return NULL;
14830
14831   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14832   descr = NULL;
14833   descr_tail = &descr;
14834
14835   for (p = loc; p; p = XEXP (p, 1))
14836     {
14837       unsigned int bitsize = decl_piece_bitsize (p);
14838       rtx loc_note = *decl_piece_varloc_ptr (p);
14839       dw_loc_descr_ref cur_descr;
14840       dw_loc_descr_ref *tail, last = NULL;
14841       unsigned int opsize = 0;
14842
14843       if (loc_note == NULL_RTX
14844           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14845         {
14846           padsize += bitsize;
14847           continue;
14848         }
14849       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14850       varloc = NOTE_VAR_LOCATION (loc_note);
14851       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14852       if (cur_descr == NULL)
14853         {
14854           padsize += bitsize;
14855           continue;
14856         }
14857
14858       /* Check that cur_descr either doesn't use
14859          DW_OP_*piece operations, or their sum is equal
14860          to bitsize.  Otherwise we can't embed it.  */
14861       for (tail = &cur_descr; *tail != NULL;
14862            tail = &(*tail)->dw_loc_next)
14863         if ((*tail)->dw_loc_opc == DW_OP_piece)
14864           {
14865             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14866                       * BITS_PER_UNIT;
14867             last = *tail;
14868           }
14869         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14870           {
14871             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14872             last = *tail;
14873           }
14874
14875       if (last != NULL && opsize != bitsize)
14876         {
14877           padsize += bitsize;
14878           continue;
14879         }
14880
14881       /* If there is a hole, add DW_OP_*piece after empty DWARF
14882          expression, which means that those bits are optimized out.  */
14883       if (padsize)
14884         {
14885           if (padsize > decl_size)
14886             return NULL;
14887           decl_size -= padsize;
14888           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14889           if (*descr_tail == NULL)
14890             return NULL;
14891           descr_tail = &(*descr_tail)->dw_loc_next;
14892           padsize = 0;
14893         }
14894       *descr_tail = cur_descr;
14895       descr_tail = tail;
14896       if (bitsize > decl_size)
14897         return NULL;
14898       decl_size -= bitsize;
14899       if (last == NULL)
14900         {
14901           HOST_WIDE_INT offset = 0;
14902           if (GET_CODE (varloc) == VAR_LOCATION
14903               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14904             {
14905               varloc = PAT_VAR_LOCATION_LOC (varloc);
14906               if (GET_CODE (varloc) == EXPR_LIST)
14907                 varloc = XEXP (varloc, 0);
14908             }
14909           do 
14910             {
14911               if (GET_CODE (varloc) == CONST
14912                   || GET_CODE (varloc) == SIGN_EXTEND
14913                   || GET_CODE (varloc) == ZERO_EXTEND)
14914                 varloc = XEXP (varloc, 0);
14915               else if (GET_CODE (varloc) == SUBREG)
14916                 varloc = SUBREG_REG (varloc);
14917               else
14918                 break;
14919             }
14920           while (1);
14921           /* DW_OP_bit_size offset should be zero for register
14922              or implicit location descriptions and empty location
14923              descriptions, but for memory addresses needs big endian
14924              adjustment.  */
14925           if (MEM_P (varloc))
14926             {
14927               unsigned HOST_WIDE_INT memsize
14928                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14929               if (memsize != bitsize)
14930                 {
14931                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14932                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14933                     return NULL;
14934                   if (memsize < bitsize)
14935                     return NULL;
14936                   if (BITS_BIG_ENDIAN)
14937                     offset = memsize - bitsize;
14938                 }
14939             }
14940
14941           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14942           if (*descr_tail == NULL)
14943             return NULL;
14944           descr_tail = &(*descr_tail)->dw_loc_next;
14945         }
14946     }
14947
14948   /* If there were any non-empty expressions, add padding till the end of
14949      the decl.  */
14950   if (descr != NULL && decl_size != 0)
14951     {
14952       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14953       if (*descr_tail == NULL)
14954         return NULL;
14955     }
14956   return descr;
14957 }
14958
14959 /* Return the dwarf representation of the location list LOC_LIST of
14960    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14961    function.  */
14962
14963 static dw_loc_list_ref
14964 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14965 {
14966   const char *endname, *secname;
14967   rtx varloc;
14968   enum var_init_status initialized;
14969   struct var_loc_node *node;
14970   dw_loc_descr_ref descr;
14971   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14972   dw_loc_list_ref list = NULL;
14973   dw_loc_list_ref *listp = &list;
14974
14975   /* Now that we know what section we are using for a base,
14976      actually construct the list of locations.
14977      The first location information is what is passed to the
14978      function that creates the location list, and the remaining
14979      locations just get added on to that list.
14980      Note that we only know the start address for a location
14981      (IE location changes), so to build the range, we use
14982      the range [current location start, next location start].
14983      This means we have to special case the last node, and generate
14984      a range of [last location start, end of function label].  */
14985
14986   secname = secname_for_decl (decl);
14987
14988   for (node = loc_list->first; node; node = node->next)
14989     if (GET_CODE (node->loc) == EXPR_LIST
14990         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14991       {
14992         if (GET_CODE (node->loc) == EXPR_LIST)
14993           {
14994             /* This requires DW_OP_{,bit_}piece, which is not usable
14995                inside DWARF expressions.  */
14996             if (want_address != 2)
14997               continue;
14998             descr = dw_sra_loc_expr (decl, node->loc);
14999             if (descr == NULL)
15000               continue;
15001           }
15002         else
15003           {
15004             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15005             varloc = NOTE_VAR_LOCATION (node->loc);
15006             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15007           }
15008         if (descr)
15009           {
15010             bool range_across_switch = false;
15011             /* If section switch happens in between node->label
15012                and node->next->label (or end of function) and
15013                we can't emit it as a single entry list,
15014                emit two ranges, first one ending at the end
15015                of first partition and second one starting at the
15016                beginning of second partition.  */
15017             if (node == loc_list->last_before_switch
15018                 && (node != loc_list->first || loc_list->first->next)
15019                 && current_function_decl)
15020               {
15021                 endname = current_fde ()->dw_fde_end;
15022                 range_across_switch = true;
15023               }
15024             /* The variable has a location between NODE->LABEL and
15025                NODE->NEXT->LABEL.  */
15026             else if (node->next)
15027               endname = node->next->label;
15028             /* If the variable has a location at the last label
15029                it keeps its location until the end of function.  */
15030             else if (!current_function_decl)
15031               endname = text_end_label;
15032             else
15033               {
15034                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15035                                              current_function_funcdef_no);
15036                 endname = ggc_strdup (label_id);
15037               }
15038
15039             *listp = new_loc_list (descr, node->label, endname, secname);
15040             listp = &(*listp)->dw_loc_next;
15041
15042             if (range_across_switch)
15043               {
15044                 if (GET_CODE (node->loc) == EXPR_LIST)
15045                   descr = dw_sra_loc_expr (decl, node->loc);
15046                 else
15047                   {
15048                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15049                     varloc = NOTE_VAR_LOCATION (node->loc);
15050                     descr = dw_loc_list_1 (decl, varloc, want_address,
15051                                            initialized);
15052                   }
15053                 gcc_assert (descr);
15054                 /* The variable has a location between NODE->LABEL and
15055                    NODE->NEXT->LABEL.  */
15056                 if (node->next)
15057                   endname = node->next->label;
15058                 else
15059                   endname = current_fde ()->dw_fde_second_end;
15060                 *listp = new_loc_list (descr,
15061                                        current_fde ()->dw_fde_second_begin,
15062                                        endname, secname);
15063                 listp = &(*listp)->dw_loc_next;
15064               }
15065           }
15066       }
15067
15068   /* Try to avoid the overhead of a location list emitting a location
15069      expression instead, but only if we didn't have more than one
15070      location entry in the first place.  If some entries were not
15071      representable, we don't want to pretend a single entry that was
15072      applies to the entire scope in which the variable is
15073      available.  */
15074   if (list && loc_list->first->next)
15075     gen_llsym (list);
15076
15077   return list;
15078 }
15079
15080 /* Return if the loc_list has only single element and thus can be represented
15081    as location description.   */
15082
15083 static bool
15084 single_element_loc_list_p (dw_loc_list_ref list)
15085 {
15086   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15087   return !list->ll_symbol;
15088 }
15089
15090 /* To each location in list LIST add loc descr REF.  */
15091
15092 static void
15093 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15094 {
15095   dw_loc_descr_ref copy;
15096   add_loc_descr (&list->expr, ref);
15097   list = list->dw_loc_next;
15098   while (list)
15099     {
15100       copy = ggc_alloc_dw_loc_descr_node ();
15101       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15102       add_loc_descr (&list->expr, copy);
15103       while (copy->dw_loc_next)
15104         {
15105           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15106           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15107           copy->dw_loc_next = new_copy;
15108           copy = new_copy;
15109         }
15110       list = list->dw_loc_next;
15111     }
15112 }
15113
15114 /* Given two lists RET and LIST
15115    produce location list that is result of adding expression in LIST
15116    to expression in RET on each possition in program.
15117    Might be destructive on both RET and LIST.
15118
15119    TODO: We handle only simple cases of RET or LIST having at most one
15120    element. General case would inolve sorting the lists in program order
15121    and merging them that will need some additional work.
15122    Adding that will improve quality of debug info especially for SRA-ed
15123    structures.  */
15124
15125 static void
15126 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15127 {
15128   if (!list)
15129     return;
15130   if (!*ret)
15131     {
15132       *ret = list;
15133       return;
15134     }
15135   if (!list->dw_loc_next)
15136     {
15137       add_loc_descr_to_each (*ret, list->expr);
15138       return;
15139     }
15140   if (!(*ret)->dw_loc_next)
15141     {
15142       add_loc_descr_to_each (list, (*ret)->expr);
15143       *ret = list;
15144       return;
15145     }
15146   expansion_failed (NULL_TREE, NULL_RTX,
15147                     "Don't know how to merge two non-trivial"
15148                     " location lists.\n");
15149   *ret = NULL;
15150   return;
15151 }
15152
15153 /* LOC is constant expression.  Try a luck, look it up in constant
15154    pool and return its loc_descr of its address.  */
15155
15156 static dw_loc_descr_ref
15157 cst_pool_loc_descr (tree loc)
15158 {
15159   /* Get an RTL for this, if something has been emitted.  */
15160   rtx rtl = lookup_constant_def (loc);
15161   enum machine_mode mode;
15162
15163   if (!rtl || !MEM_P (rtl))
15164     {
15165       gcc_assert (!rtl);
15166       return 0;
15167     }
15168   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15169
15170   /* TODO: We might get more coverage if we was actually delaying expansion
15171      of all expressions till end of compilation when constant pools are fully
15172      populated.  */
15173   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15174     {
15175       expansion_failed (loc, NULL_RTX,
15176                         "CST value in contant pool but not marked.");
15177       return 0;
15178     }
15179   mode = GET_MODE (rtl);
15180   rtl = XEXP (rtl, 0);
15181   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15182 }
15183
15184 /* Return dw_loc_list representing address of addr_expr LOC
15185    by looking for innder INDIRECT_REF expression and turing it
15186    into simple arithmetics.  */
15187
15188 static dw_loc_list_ref
15189 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15190 {
15191   tree obj, offset;
15192   HOST_WIDE_INT bitsize, bitpos, bytepos;
15193   enum machine_mode mode;
15194   int volatilep;
15195   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15196   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15197
15198   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15199                              &bitsize, &bitpos, &offset, &mode,
15200                              &unsignedp, &volatilep, false);
15201   STRIP_NOPS (obj);
15202   if (bitpos % BITS_PER_UNIT)
15203     {
15204       expansion_failed (loc, NULL_RTX, "bitfield access");
15205       return 0;
15206     }
15207   if (!INDIRECT_REF_P (obj))
15208     {
15209       expansion_failed (obj,
15210                         NULL_RTX, "no indirect ref in inner refrence");
15211       return 0;
15212     }
15213   if (!offset && !bitpos)
15214     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15215   else if (toplev
15216            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15217            && (dwarf_version >= 4 || !dwarf_strict))
15218     {
15219       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15220       if (!list_ret)
15221         return 0;
15222       if (offset)
15223         {
15224           /* Variable offset.  */
15225           list_ret1 = loc_list_from_tree (offset, 0);
15226           if (list_ret1 == 0)
15227             return 0;
15228           add_loc_list (&list_ret, list_ret1);
15229           if (!list_ret)
15230             return 0;
15231           add_loc_descr_to_each (list_ret,
15232                                  new_loc_descr (DW_OP_plus, 0, 0));
15233         }
15234       bytepos = bitpos / BITS_PER_UNIT;
15235       if (bytepos > 0)
15236         add_loc_descr_to_each (list_ret,
15237                                new_loc_descr (DW_OP_plus_uconst,
15238                                               bytepos, 0));
15239       else if (bytepos < 0)
15240         loc_list_plus_const (list_ret, bytepos);
15241       add_loc_descr_to_each (list_ret,
15242                              new_loc_descr (DW_OP_stack_value, 0, 0));
15243     }
15244   return list_ret;
15245 }
15246
15247
15248 /* Generate Dwarf location list representing LOC.
15249    If WANT_ADDRESS is false, expression computing LOC will be computed
15250    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15251    if WANT_ADDRESS is 2, expression computing address useable in location
15252      will be returned (i.e. DW_OP_reg can be used
15253      to refer to register values).  */
15254
15255 static dw_loc_list_ref
15256 loc_list_from_tree (tree loc, int want_address)
15257 {
15258   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15259   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15260   int have_address = 0;
15261   enum dwarf_location_atom op;
15262
15263   /* ??? Most of the time we do not take proper care for sign/zero
15264      extending the values properly.  Hopefully this won't be a real
15265      problem...  */
15266
15267   switch (TREE_CODE (loc))
15268     {
15269     case ERROR_MARK:
15270       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15271       return 0;
15272
15273     case PLACEHOLDER_EXPR:
15274       /* This case involves extracting fields from an object to determine the
15275          position of other fields.  We don't try to encode this here.  The
15276          only user of this is Ada, which encodes the needed information using
15277          the names of types.  */
15278       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15279       return 0;
15280
15281     case CALL_EXPR:
15282       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15283       /* There are no opcodes for these operations.  */
15284       return 0;
15285
15286     case PREINCREMENT_EXPR:
15287     case PREDECREMENT_EXPR:
15288     case POSTINCREMENT_EXPR:
15289     case POSTDECREMENT_EXPR:
15290       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15291       /* There are no opcodes for these operations.  */
15292       return 0;
15293
15294     case ADDR_EXPR:
15295       /* If we already want an address, see if there is INDIRECT_REF inside
15296          e.g. for &this->field.  */
15297       if (want_address)
15298         {
15299           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15300                        (loc, want_address == 2);
15301           if (list_ret)
15302             have_address = 1;
15303           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15304                    && (ret = cst_pool_loc_descr (loc)))
15305             have_address = 1;
15306         }
15307         /* Otherwise, process the argument and look for the address.  */
15308       if (!list_ret && !ret)
15309         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15310       else
15311         {
15312           if (want_address)
15313             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15314           return NULL;
15315         }
15316       break;
15317
15318     case VAR_DECL:
15319       if (DECL_THREAD_LOCAL_P (loc))
15320         {
15321           rtx rtl;
15322           enum dwarf_location_atom first_op;
15323           enum dwarf_location_atom second_op;
15324           bool dtprel = false;
15325
15326           if (targetm.have_tls)
15327             {
15328               /* If this is not defined, we have no way to emit the
15329                  data.  */
15330               if (!targetm.asm_out.output_dwarf_dtprel)
15331                 return 0;
15332
15333                /* The way DW_OP_GNU_push_tls_address is specified, we
15334                   can only look up addresses of objects in the current
15335                   module.  We used DW_OP_addr as first op, but that's
15336                   wrong, because DW_OP_addr is relocated by the debug
15337                   info consumer, while DW_OP_GNU_push_tls_address
15338                   operand shouldn't be.  */
15339               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15340                 return 0;
15341               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15342               dtprel = true;
15343               second_op = DW_OP_GNU_push_tls_address;
15344             }
15345           else
15346             {
15347               if (!targetm.emutls.debug_form_tls_address
15348                   || !(dwarf_version >= 3 || !dwarf_strict))
15349                 return 0;
15350               /* We stuffed the control variable into the DECL_VALUE_EXPR
15351                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15352                  no longer appear in gimple code.  We used the control
15353                  variable in specific so that we could pick it up here.  */
15354               loc = DECL_VALUE_EXPR (loc);
15355               first_op = DW_OP_addr;
15356               second_op = DW_OP_form_tls_address;
15357             }
15358
15359           rtl = rtl_for_decl_location (loc);
15360           if (rtl == NULL_RTX)
15361             return 0;
15362
15363           if (!MEM_P (rtl))
15364             return 0;
15365           rtl = XEXP (rtl, 0);
15366           if (! CONSTANT_P (rtl))
15367             return 0;
15368
15369           ret = new_loc_descr (first_op, 0, 0);
15370           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15371           ret->dw_loc_oprnd1.v.val_addr = rtl;
15372           ret->dtprel = dtprel;
15373
15374           ret1 = new_loc_descr (second_op, 0, 0);
15375           add_loc_descr (&ret, ret1);
15376
15377           have_address = 1;
15378           break;
15379         }
15380       /* FALLTHRU */
15381
15382     case PARM_DECL:
15383     case RESULT_DECL:
15384       if (DECL_HAS_VALUE_EXPR_P (loc))
15385         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15386                                    want_address);
15387       /* FALLTHRU */
15388
15389     case FUNCTION_DECL:
15390       {
15391         rtx rtl;
15392         var_loc_list *loc_list = lookup_decl_loc (loc);
15393
15394         if (loc_list && loc_list->first)
15395           {
15396             list_ret = dw_loc_list (loc_list, loc, want_address);
15397             have_address = want_address != 0;
15398             break;
15399           }
15400         rtl = rtl_for_decl_location (loc);
15401         if (rtl == NULL_RTX)
15402           {
15403             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15404             return 0;
15405           }
15406         else if (CONST_INT_P (rtl))
15407           {
15408             HOST_WIDE_INT val = INTVAL (rtl);
15409             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15410               val &= GET_MODE_MASK (DECL_MODE (loc));
15411             ret = int_loc_descriptor (val);
15412           }
15413         else if (GET_CODE (rtl) == CONST_STRING)
15414           {
15415             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15416             return 0;
15417           }
15418         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15419           {
15420             ret = new_loc_descr (DW_OP_addr, 0, 0);
15421             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15422             ret->dw_loc_oprnd1.v.val_addr = rtl;
15423           }
15424         else
15425           {
15426             enum machine_mode mode;
15427
15428             /* Certain constructs can only be represented at top-level.  */
15429             if (want_address == 2)
15430               {
15431                 ret = loc_descriptor (rtl, VOIDmode,
15432                                       VAR_INIT_STATUS_INITIALIZED);
15433                 have_address = 1;
15434               }
15435             else
15436               {
15437                 mode = GET_MODE (rtl);
15438                 if (MEM_P (rtl))
15439                   {
15440                     rtl = XEXP (rtl, 0);
15441                     have_address = 1;
15442                   }
15443                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15444               }
15445             if (!ret)
15446               expansion_failed (loc, rtl,
15447                                 "failed to produce loc descriptor for rtl");
15448           }
15449       }
15450       break;
15451
15452     case MEM_REF:
15453       /* ??? FIXME.  */
15454       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15455         return 0;
15456       /* Fallthru.  */
15457     case INDIRECT_REF:
15458       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15459       have_address = 1;
15460       break;
15461
15462     case COMPOUND_EXPR:
15463       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15464
15465     CASE_CONVERT:
15466     case VIEW_CONVERT_EXPR:
15467     case SAVE_EXPR:
15468     case MODIFY_EXPR:
15469       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15470
15471     case COMPONENT_REF:
15472     case BIT_FIELD_REF:
15473     case ARRAY_REF:
15474     case ARRAY_RANGE_REF:
15475     case REALPART_EXPR:
15476     case IMAGPART_EXPR:
15477       {
15478         tree obj, offset;
15479         HOST_WIDE_INT bitsize, bitpos, bytepos;
15480         enum machine_mode mode;
15481         int volatilep;
15482         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15483
15484         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15485                                    &unsignedp, &volatilep, false);
15486
15487         gcc_assert (obj != loc);
15488
15489         list_ret = loc_list_from_tree (obj,
15490                                        want_address == 2
15491                                        && !bitpos && !offset ? 2 : 1);
15492         /* TODO: We can extract value of the small expression via shifting even
15493            for nonzero bitpos.  */
15494         if (list_ret == 0)
15495           return 0;
15496         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15497           {
15498             expansion_failed (loc, NULL_RTX,
15499                               "bitfield access");
15500             return 0;
15501           }
15502
15503         if (offset != NULL_TREE)
15504           {
15505             /* Variable offset.  */
15506             list_ret1 = loc_list_from_tree (offset, 0);
15507             if (list_ret1 == 0)
15508               return 0;
15509             add_loc_list (&list_ret, list_ret1);
15510             if (!list_ret)
15511               return 0;
15512             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15513           }
15514
15515         bytepos = bitpos / BITS_PER_UNIT;
15516         if (bytepos > 0)
15517           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15518         else if (bytepos < 0)
15519           loc_list_plus_const (list_ret, bytepos);
15520
15521         have_address = 1;
15522         break;
15523       }
15524
15525     case INTEGER_CST:
15526       if ((want_address || !host_integerp (loc, 0))
15527           && (ret = cst_pool_loc_descr (loc)))
15528         have_address = 1;
15529       else if (want_address == 2
15530                && host_integerp (loc, 0)
15531                && (ret = address_of_int_loc_descriptor
15532                            (int_size_in_bytes (TREE_TYPE (loc)),
15533                             tree_low_cst (loc, 0))))
15534         have_address = 1;
15535       else if (host_integerp (loc, 0))
15536         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15537       else
15538         {
15539           expansion_failed (loc, NULL_RTX,
15540                             "Integer operand is not host integer");
15541           return 0;
15542         }
15543       break;
15544
15545     case CONSTRUCTOR:
15546     case REAL_CST:
15547     case STRING_CST:
15548     case COMPLEX_CST:
15549       if ((ret = cst_pool_loc_descr (loc)))
15550         have_address = 1;
15551       else
15552       /* We can construct small constants here using int_loc_descriptor.  */
15553         expansion_failed (loc, NULL_RTX,
15554                           "constructor or constant not in constant pool");
15555       break;
15556
15557     case TRUTH_AND_EXPR:
15558     case TRUTH_ANDIF_EXPR:
15559     case BIT_AND_EXPR:
15560       op = DW_OP_and;
15561       goto do_binop;
15562
15563     case TRUTH_XOR_EXPR:
15564     case BIT_XOR_EXPR:
15565       op = DW_OP_xor;
15566       goto do_binop;
15567
15568     case TRUTH_OR_EXPR:
15569     case TRUTH_ORIF_EXPR:
15570     case BIT_IOR_EXPR:
15571       op = DW_OP_or;
15572       goto do_binop;
15573
15574     case FLOOR_DIV_EXPR:
15575     case CEIL_DIV_EXPR:
15576     case ROUND_DIV_EXPR:
15577     case TRUNC_DIV_EXPR:
15578       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15579         return 0;
15580       op = DW_OP_div;
15581       goto do_binop;
15582
15583     case MINUS_EXPR:
15584       op = DW_OP_minus;
15585       goto do_binop;
15586
15587     case FLOOR_MOD_EXPR:
15588     case CEIL_MOD_EXPR:
15589     case ROUND_MOD_EXPR:
15590     case TRUNC_MOD_EXPR:
15591       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15592         {
15593           op = DW_OP_mod;
15594           goto do_binop;
15595         }
15596       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15597       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15598       if (list_ret == 0 || list_ret1 == 0)
15599         return 0;
15600
15601       add_loc_list (&list_ret, list_ret1);
15602       if (list_ret == 0)
15603         return 0;
15604       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15605       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15606       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15607       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15608       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15609       break;
15610
15611     case MULT_EXPR:
15612       op = DW_OP_mul;
15613       goto do_binop;
15614
15615     case LSHIFT_EXPR:
15616       op = DW_OP_shl;
15617       goto do_binop;
15618
15619     case RSHIFT_EXPR:
15620       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15621       goto do_binop;
15622
15623     case POINTER_PLUS_EXPR:
15624     case PLUS_EXPR:
15625       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15626         {
15627           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15628           if (list_ret == 0)
15629             return 0;
15630
15631           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15632           break;
15633         }
15634
15635       op = DW_OP_plus;
15636       goto do_binop;
15637
15638     case LE_EXPR:
15639       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15640         return 0;
15641
15642       op = DW_OP_le;
15643       goto do_binop;
15644
15645     case GE_EXPR:
15646       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15647         return 0;
15648
15649       op = DW_OP_ge;
15650       goto do_binop;
15651
15652     case LT_EXPR:
15653       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15654         return 0;
15655
15656       op = DW_OP_lt;
15657       goto do_binop;
15658
15659     case GT_EXPR:
15660       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15661         return 0;
15662
15663       op = DW_OP_gt;
15664       goto do_binop;
15665
15666     case EQ_EXPR:
15667       op = DW_OP_eq;
15668       goto do_binop;
15669
15670     case NE_EXPR:
15671       op = DW_OP_ne;
15672       goto do_binop;
15673
15674     do_binop:
15675       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15676       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15677       if (list_ret == 0 || list_ret1 == 0)
15678         return 0;
15679
15680       add_loc_list (&list_ret, list_ret1);
15681       if (list_ret == 0)
15682         return 0;
15683       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15684       break;
15685
15686     case TRUTH_NOT_EXPR:
15687     case BIT_NOT_EXPR:
15688       op = DW_OP_not;
15689       goto do_unop;
15690
15691     case ABS_EXPR:
15692       op = DW_OP_abs;
15693       goto do_unop;
15694
15695     case NEGATE_EXPR:
15696       op = DW_OP_neg;
15697       goto do_unop;
15698
15699     do_unop:
15700       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15701       if (list_ret == 0)
15702         return 0;
15703
15704       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15705       break;
15706
15707     case MIN_EXPR:
15708     case MAX_EXPR:
15709       {
15710         const enum tree_code code =
15711           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15712
15713         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15714                       build2 (code, integer_type_node,
15715                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15716                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15717       }
15718
15719       /* ... fall through ...  */
15720
15721     case COND_EXPR:
15722       {
15723         dw_loc_descr_ref lhs
15724           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15725         dw_loc_list_ref rhs
15726           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15727         dw_loc_descr_ref bra_node, jump_node, tmp;
15728
15729         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15730         if (list_ret == 0 || lhs == 0 || rhs == 0)
15731           return 0;
15732
15733         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15734         add_loc_descr_to_each (list_ret, bra_node);
15735
15736         add_loc_list (&list_ret, rhs);
15737         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15738         add_loc_descr_to_each (list_ret, jump_node);
15739
15740         add_loc_descr_to_each (list_ret, lhs);
15741         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15742         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15743
15744         /* ??? Need a node to point the skip at.  Use a nop.  */
15745         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15746         add_loc_descr_to_each (list_ret, tmp);
15747         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15748         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15749       }
15750       break;
15751
15752     case FIX_TRUNC_EXPR:
15753       return 0;
15754
15755     default:
15756       /* Leave front-end specific codes as simply unknown.  This comes
15757          up, for instance, with the C STMT_EXPR.  */
15758       if ((unsigned int) TREE_CODE (loc)
15759           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15760         {
15761           expansion_failed (loc, NULL_RTX,
15762                             "language specific tree node");
15763           return 0;
15764         }
15765
15766 #ifdef ENABLE_CHECKING
15767       /* Otherwise this is a generic code; we should just lists all of
15768          these explicitly.  We forgot one.  */
15769       gcc_unreachable ();
15770 #else
15771       /* In a release build, we want to degrade gracefully: better to
15772          generate incomplete debugging information than to crash.  */
15773       return NULL;
15774 #endif
15775     }
15776
15777   if (!ret && !list_ret)
15778     return 0;
15779
15780   if (want_address == 2 && !have_address
15781       && (dwarf_version >= 4 || !dwarf_strict))
15782     {
15783       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15784         {
15785           expansion_failed (loc, NULL_RTX,
15786                             "DWARF address size mismatch");
15787           return 0;
15788         }
15789       if (ret)
15790         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15791       else
15792         add_loc_descr_to_each (list_ret,
15793                                new_loc_descr (DW_OP_stack_value, 0, 0));
15794       have_address = 1;
15795     }
15796   /* Show if we can't fill the request for an address.  */
15797   if (want_address && !have_address)
15798     {
15799       expansion_failed (loc, NULL_RTX,
15800                         "Want address and only have value");
15801       return 0;
15802     }
15803
15804   gcc_assert (!ret || !list_ret);
15805
15806   /* If we've got an address and don't want one, dereference.  */
15807   if (!want_address && have_address)
15808     {
15809       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15810
15811       if (size > DWARF2_ADDR_SIZE || size == -1)
15812         {
15813           expansion_failed (loc, NULL_RTX,
15814                             "DWARF address size mismatch");
15815           return 0;
15816         }
15817       else if (size == DWARF2_ADDR_SIZE)
15818         op = DW_OP_deref;
15819       else
15820         op = DW_OP_deref_size;
15821
15822       if (ret)
15823         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15824       else
15825         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15826     }
15827   if (ret)
15828     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15829
15830   return list_ret;
15831 }
15832
15833 /* Same as above but return only single location expression.  */
15834 static dw_loc_descr_ref
15835 loc_descriptor_from_tree (tree loc, int want_address)
15836 {
15837   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15838   if (!ret)
15839     return NULL;
15840   if (ret->dw_loc_next)
15841     {
15842       expansion_failed (loc, NULL_RTX,
15843                         "Location list where only loc descriptor needed");
15844       return NULL;
15845     }
15846   return ret->expr;
15847 }
15848
15849 /* Given a value, round it up to the lowest multiple of `boundary'
15850    which is not less than the value itself.  */
15851
15852 static inline HOST_WIDE_INT
15853 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15854 {
15855   return (((value + boundary - 1) / boundary) * boundary);
15856 }
15857
15858 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15859    pointer to the declared type for the relevant field variable, or return
15860    `integer_type_node' if the given node turns out to be an
15861    ERROR_MARK node.  */
15862
15863 static inline tree
15864 field_type (const_tree decl)
15865 {
15866   tree type;
15867
15868   if (TREE_CODE (decl) == ERROR_MARK)
15869     return integer_type_node;
15870
15871   type = DECL_BIT_FIELD_TYPE (decl);
15872   if (type == NULL_TREE)
15873     type = TREE_TYPE (decl);
15874
15875   return type;
15876 }
15877
15878 /* Given a pointer to a tree node, return the alignment in bits for
15879    it, or else return BITS_PER_WORD if the node actually turns out to
15880    be an ERROR_MARK node.  */
15881
15882 static inline unsigned
15883 simple_type_align_in_bits (const_tree type)
15884 {
15885   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15886 }
15887
15888 static inline unsigned
15889 simple_decl_align_in_bits (const_tree decl)
15890 {
15891   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15892 }
15893
15894 /* Return the result of rounding T up to ALIGN.  */
15895
15896 static inline double_int
15897 round_up_to_align (double_int t, unsigned int align)
15898 {
15899   double_int alignd = uhwi_to_double_int (align);
15900   t = double_int_add (t, alignd);
15901   t = double_int_add (t, double_int_minus_one);
15902   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15903   t = double_int_mul (t, alignd);
15904   return t;
15905 }
15906
15907 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15908    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15909    or return 0 if we are unable to determine what that offset is, either
15910    because the argument turns out to be a pointer to an ERROR_MARK node, or
15911    because the offset is actually variable.  (We can't handle the latter case
15912    just yet).  */
15913
15914 static HOST_WIDE_INT
15915 field_byte_offset (const_tree decl)
15916 {
15917   double_int object_offset_in_bits;
15918   double_int object_offset_in_bytes;
15919   double_int bitpos_int;
15920
15921   if (TREE_CODE (decl) == ERROR_MARK)
15922     return 0;
15923
15924   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15925
15926   /* We cannot yet cope with fields whose positions are variable, so
15927      for now, when we see such things, we simply return 0.  Someday, we may
15928      be able to handle such cases, but it will be damn difficult.  */
15929   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15930     return 0;
15931
15932   bitpos_int = tree_to_double_int (bit_position (decl));
15933
15934 #ifdef PCC_BITFIELD_TYPE_MATTERS
15935   if (PCC_BITFIELD_TYPE_MATTERS)
15936     {
15937       tree type;
15938       tree field_size_tree;
15939       double_int deepest_bitpos;
15940       double_int field_size_in_bits;
15941       unsigned int type_align_in_bits;
15942       unsigned int decl_align_in_bits;
15943       double_int type_size_in_bits;
15944
15945       type = field_type (decl);
15946       type_size_in_bits = double_int_type_size_in_bits (type);
15947       type_align_in_bits = simple_type_align_in_bits (type);
15948
15949       field_size_tree = DECL_SIZE (decl);
15950
15951       /* The size could be unspecified if there was an error, or for
15952          a flexible array member.  */
15953       if (!field_size_tree)
15954         field_size_tree = bitsize_zero_node;
15955
15956       /* If the size of the field is not constant, use the type size.  */
15957       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15958         field_size_in_bits = tree_to_double_int (field_size_tree);
15959       else
15960         field_size_in_bits = type_size_in_bits;
15961
15962       decl_align_in_bits = simple_decl_align_in_bits (decl);
15963
15964       /* The GCC front-end doesn't make any attempt to keep track of the
15965          starting bit offset (relative to the start of the containing
15966          structure type) of the hypothetical "containing object" for a
15967          bit-field.  Thus, when computing the byte offset value for the
15968          start of the "containing object" of a bit-field, we must deduce
15969          this information on our own. This can be rather tricky to do in
15970          some cases.  For example, handling the following structure type
15971          definition when compiling for an i386/i486 target (which only
15972          aligns long long's to 32-bit boundaries) can be very tricky:
15973
15974          struct S { int field1; long long field2:31; };
15975
15976          Fortunately, there is a simple rule-of-thumb which can be used
15977          in such cases.  When compiling for an i386/i486, GCC will
15978          allocate 8 bytes for the structure shown above.  It decides to
15979          do this based upon one simple rule for bit-field allocation.
15980          GCC allocates each "containing object" for each bit-field at
15981          the first (i.e. lowest addressed) legitimate alignment boundary
15982          (based upon the required minimum alignment for the declared
15983          type of the field) which it can possibly use, subject to the
15984          condition that there is still enough available space remaining
15985          in the containing object (when allocated at the selected point)
15986          to fully accommodate all of the bits of the bit-field itself.
15987
15988          This simple rule makes it obvious why GCC allocates 8 bytes for
15989          each object of the structure type shown above.  When looking
15990          for a place to allocate the "containing object" for `field2',
15991          the compiler simply tries to allocate a 64-bit "containing
15992          object" at each successive 32-bit boundary (starting at zero)
15993          until it finds a place to allocate that 64- bit field such that
15994          at least 31 contiguous (and previously unallocated) bits remain
15995          within that selected 64 bit field.  (As it turns out, for the
15996          example above, the compiler finds it is OK to allocate the
15997          "containing object" 64-bit field at bit-offset zero within the
15998          structure type.)
15999
16000          Here we attempt to work backwards from the limited set of facts
16001          we're given, and we try to deduce from those facts, where GCC
16002          must have believed that the containing object started (within
16003          the structure type). The value we deduce is then used (by the
16004          callers of this routine) to generate DW_AT_location and
16005          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16006          the case of DW_AT_location, regular fields as well).  */
16007
16008       /* Figure out the bit-distance from the start of the structure to
16009          the "deepest" bit of the bit-field.  */
16010       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16011
16012       /* This is the tricky part.  Use some fancy footwork to deduce
16013          where the lowest addressed bit of the containing object must
16014          be.  */
16015       object_offset_in_bits
16016         = double_int_sub (deepest_bitpos, type_size_in_bits);
16017
16018       /* Round up to type_align by default.  This works best for
16019          bitfields.  */
16020       object_offset_in_bits
16021         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16022
16023       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16024         {
16025           object_offset_in_bits
16026             = double_int_sub (deepest_bitpos, type_size_in_bits);
16027
16028           /* Round up to decl_align instead.  */
16029           object_offset_in_bits
16030             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16031         }
16032     }
16033   else
16034 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16035     object_offset_in_bits = bitpos_int;
16036
16037   object_offset_in_bytes
16038     = double_int_div (object_offset_in_bits,
16039                       uhwi_to_double_int (BITS_PER_UNIT), true,
16040                       TRUNC_DIV_EXPR);
16041   return double_int_to_shwi (object_offset_in_bytes);
16042 }
16043 \f
16044 /* The following routines define various Dwarf attributes and any data
16045    associated with them.  */
16046
16047 /* Add a location description attribute value to a DIE.
16048
16049    This emits location attributes suitable for whole variables and
16050    whole parameters.  Note that the location attributes for struct fields are
16051    generated by the routine `data_member_location_attribute' below.  */
16052
16053 static inline void
16054 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16055                              dw_loc_list_ref descr)
16056 {
16057   if (descr == 0)
16058     return;
16059   if (single_element_loc_list_p (descr))
16060     add_AT_loc (die, attr_kind, descr->expr);
16061   else
16062     add_AT_loc_list (die, attr_kind, descr);
16063 }
16064
16065 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16066
16067 static void
16068 add_accessibility_attribute (dw_die_ref die, tree decl)
16069 {
16070   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16071      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16072      the default has always been DW_ACCESS_public.  */
16073   if (TREE_PROTECTED (decl))
16074     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16075   else if (TREE_PRIVATE (decl))
16076     {
16077       if (dwarf_version == 2
16078           || die->die_parent == NULL
16079           || die->die_parent->die_tag != DW_TAG_class_type)
16080         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16081     }
16082   else if (dwarf_version > 2
16083            && die->die_parent
16084            && die->die_parent->die_tag == DW_TAG_class_type)
16085     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16086 }
16087
16088 /* Attach the specialized form of location attribute used for data members of
16089    struct and union types.  In the special case of a FIELD_DECL node which
16090    represents a bit-field, the "offset" part of this special location
16091    descriptor must indicate the distance in bytes from the lowest-addressed
16092    byte of the containing struct or union type to the lowest-addressed byte of
16093    the "containing object" for the bit-field.  (See the `field_byte_offset'
16094    function above).
16095
16096    For any given bit-field, the "containing object" is a hypothetical object
16097    (of some integral or enum type) within which the given bit-field lives.  The
16098    type of this hypothetical "containing object" is always the same as the
16099    declared type of the individual bit-field itself (for GCC anyway... the
16100    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16101    bytes) of the hypothetical "containing object" which will be given in the
16102    DW_AT_byte_size attribute for this bit-field.  (See the
16103    `byte_size_attribute' function below.)  It is also used when calculating the
16104    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16105    function below.)  */
16106
16107 static void
16108 add_data_member_location_attribute (dw_die_ref die, tree decl)
16109 {
16110   HOST_WIDE_INT offset;
16111   dw_loc_descr_ref loc_descr = 0;
16112
16113   if (TREE_CODE (decl) == TREE_BINFO)
16114     {
16115       /* We're working on the TAG_inheritance for a base class.  */
16116       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16117         {
16118           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16119              aren't at a fixed offset from all (sub)objects of the same
16120              type.  We need to extract the appropriate offset from our
16121              vtable.  The following dwarf expression means
16122
16123                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16124
16125              This is specific to the V3 ABI, of course.  */
16126
16127           dw_loc_descr_ref tmp;
16128
16129           /* Make a copy of the object address.  */
16130           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16131           add_loc_descr (&loc_descr, tmp);
16132
16133           /* Extract the vtable address.  */
16134           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16135           add_loc_descr (&loc_descr, tmp);
16136
16137           /* Calculate the address of the offset.  */
16138           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16139           gcc_assert (offset < 0);
16140
16141           tmp = int_loc_descriptor (-offset);
16142           add_loc_descr (&loc_descr, tmp);
16143           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16144           add_loc_descr (&loc_descr, tmp);
16145
16146           /* Extract the offset.  */
16147           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16148           add_loc_descr (&loc_descr, tmp);
16149
16150           /* Add it to the object address.  */
16151           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16152           add_loc_descr (&loc_descr, tmp);
16153         }
16154       else
16155         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16156     }
16157   else
16158     offset = field_byte_offset (decl);
16159
16160   if (! loc_descr)
16161     {
16162       if (dwarf_version > 2)
16163         {
16164           /* Don't need to output a location expression, just the constant. */
16165           if (offset < 0)
16166             add_AT_int (die, DW_AT_data_member_location, offset);
16167           else
16168             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16169           return;
16170         }
16171       else
16172         {
16173           enum dwarf_location_atom op;
16174
16175           /* The DWARF2 standard says that we should assume that the structure
16176              address is already on the stack, so we can specify a structure
16177              field address by using DW_OP_plus_uconst.  */
16178
16179 #ifdef MIPS_DEBUGGING_INFO
16180           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16181              operator correctly.  It works only if we leave the offset on the
16182              stack.  */
16183           op = DW_OP_constu;
16184 #else
16185           op = DW_OP_plus_uconst;
16186 #endif
16187
16188           loc_descr = new_loc_descr (op, offset, 0);
16189         }
16190     }
16191
16192   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16193 }
16194
16195 /* Writes integer values to dw_vec_const array.  */
16196
16197 static void
16198 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16199 {
16200   while (size != 0)
16201     {
16202       *dest++ = val & 0xff;
16203       val >>= 8;
16204       --size;
16205     }
16206 }
16207
16208 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16209
16210 static HOST_WIDE_INT
16211 extract_int (const unsigned char *src, unsigned int size)
16212 {
16213   HOST_WIDE_INT val = 0;
16214
16215   src += size;
16216   while (size != 0)
16217     {
16218       val <<= 8;
16219       val |= *--src & 0xff;
16220       --size;
16221     }
16222   return val;
16223 }
16224
16225 /* Writes double_int values to dw_vec_const array.  */
16226
16227 static void
16228 insert_double (double_int val, unsigned char *dest)
16229 {
16230   unsigned char *p0 = dest;
16231   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16232
16233   if (WORDS_BIG_ENDIAN)
16234     {
16235       p0 = p1;
16236       p1 = dest;
16237     }
16238
16239   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16240   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16241 }
16242
16243 /* Writes floating point values to dw_vec_const array.  */
16244
16245 static void
16246 insert_float (const_rtx rtl, unsigned char *array)
16247 {
16248   REAL_VALUE_TYPE rv;
16249   long val[4];
16250   int i;
16251
16252   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16253   real_to_target (val, &rv, GET_MODE (rtl));
16254
16255   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16256   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16257     {
16258       insert_int (val[i], 4, array);
16259       array += 4;
16260     }
16261 }
16262
16263 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16264    does not have a "location" either in memory or in a register.  These
16265    things can arise in GNU C when a constant is passed as an actual parameter
16266    to an inlined function.  They can also arise in C++ where declared
16267    constants do not necessarily get memory "homes".  */
16268
16269 static bool
16270 add_const_value_attribute (dw_die_ref die, rtx rtl)
16271 {
16272   switch (GET_CODE (rtl))
16273     {
16274     case CONST_INT:
16275       {
16276         HOST_WIDE_INT val = INTVAL (rtl);
16277
16278         if (val < 0)
16279           add_AT_int (die, DW_AT_const_value, val);
16280         else
16281           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16282       }
16283       return true;
16284
16285     case CONST_DOUBLE:
16286       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16287          floating-point constant.  A CONST_DOUBLE is used whenever the
16288          constant requires more than one word in order to be adequately
16289          represented.  */
16290       {
16291         enum machine_mode mode = GET_MODE (rtl);
16292
16293         if (SCALAR_FLOAT_MODE_P (mode))
16294           {
16295             unsigned int length = GET_MODE_SIZE (mode);
16296             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16297
16298             insert_float (rtl, array);
16299             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16300           }
16301         else
16302           add_AT_double (die, DW_AT_const_value,
16303                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16304       }
16305       return true;
16306
16307     case CONST_VECTOR:
16308       {
16309         enum machine_mode mode = GET_MODE (rtl);
16310         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16311         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16312         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16313           (length * elt_size);
16314         unsigned int i;
16315         unsigned char *p;
16316
16317         switch (GET_MODE_CLASS (mode))
16318           {
16319           case MODE_VECTOR_INT:
16320             for (i = 0, p = array; i < length; i++, p += elt_size)
16321               {
16322                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16323                 double_int val = rtx_to_double_int (elt);
16324
16325                 if (elt_size <= sizeof (HOST_WIDE_INT))
16326                   insert_int (double_int_to_shwi (val), elt_size, p);
16327                 else
16328                   {
16329                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16330                     insert_double (val, p);
16331                   }
16332               }
16333             break;
16334
16335           case MODE_VECTOR_FLOAT:
16336             for (i = 0, p = array; i < length; i++, p += elt_size)
16337               {
16338                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16339                 insert_float (elt, p);
16340               }
16341             break;
16342
16343           default:
16344             gcc_unreachable ();
16345           }
16346
16347         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16348       }
16349       return true;
16350
16351     case CONST_STRING:
16352       if (dwarf_version >= 4 || !dwarf_strict)
16353         {
16354           dw_loc_descr_ref loc_result;
16355           resolve_one_addr (&rtl, NULL);
16356         rtl_addr:
16357           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16358           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16359           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16360           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16361           add_AT_loc (die, DW_AT_location, loc_result);
16362           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16363           return true;
16364         }
16365       return false;
16366
16367     case CONST:
16368       if (CONSTANT_P (XEXP (rtl, 0)))
16369         return add_const_value_attribute (die, XEXP (rtl, 0));
16370       /* FALLTHROUGH */
16371     case SYMBOL_REF:
16372       if (!const_ok_for_output (rtl))
16373         return false;
16374     case LABEL_REF:
16375       if (dwarf_version >= 4 || !dwarf_strict)
16376         goto rtl_addr;
16377       return false;
16378
16379     case PLUS:
16380       /* In cases where an inlined instance of an inline function is passed
16381          the address of an `auto' variable (which is local to the caller) we
16382          can get a situation where the DECL_RTL of the artificial local
16383          variable (for the inlining) which acts as a stand-in for the
16384          corresponding formal parameter (of the inline function) will look
16385          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16386          exactly a compile-time constant expression, but it isn't the address
16387          of the (artificial) local variable either.  Rather, it represents the
16388          *value* which the artificial local variable always has during its
16389          lifetime.  We currently have no way to represent such quasi-constant
16390          values in Dwarf, so for now we just punt and generate nothing.  */
16391       return false;
16392
16393     case HIGH:
16394     case CONST_FIXED:
16395       return false;
16396
16397     case MEM:
16398       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16399           && MEM_READONLY_P (rtl)
16400           && GET_MODE (rtl) == BLKmode)
16401         {
16402           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16403           return true;
16404         }
16405       return false;
16406
16407     default:
16408       /* No other kinds of rtx should be possible here.  */
16409       gcc_unreachable ();
16410     }
16411   return false;
16412 }
16413
16414 /* Determine whether the evaluation of EXPR references any variables
16415    or functions which aren't otherwise used (and therefore may not be
16416    output).  */
16417 static tree
16418 reference_to_unused (tree * tp, int * walk_subtrees,
16419                      void * data ATTRIBUTE_UNUSED)
16420 {
16421   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16422     *walk_subtrees = 0;
16423
16424   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16425       && ! TREE_ASM_WRITTEN (*tp))
16426     return *tp;
16427   /* ???  The C++ FE emits debug information for using decls, so
16428      putting gcc_unreachable here falls over.  See PR31899.  For now
16429      be conservative.  */
16430   else if (!cgraph_global_info_ready
16431            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16432     return *tp;
16433   else if (TREE_CODE (*tp) == VAR_DECL)
16434     {
16435       struct varpool_node *node = varpool_get_node (*tp);
16436       if (!node || !node->needed)
16437         return *tp;
16438     }
16439   else if (TREE_CODE (*tp) == FUNCTION_DECL
16440            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16441     {
16442       /* The call graph machinery must have finished analyzing,
16443          optimizing and gimplifying the CU by now.
16444          So if *TP has no call graph node associated
16445          to it, it means *TP will not be emitted.  */
16446       if (!cgraph_get_node (*tp))
16447         return *tp;
16448     }
16449   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16450     return *tp;
16451
16452   return NULL_TREE;
16453 }
16454
16455 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16456    for use in a later add_const_value_attribute call.  */
16457
16458 static rtx
16459 rtl_for_decl_init (tree init, tree type)
16460 {
16461   rtx rtl = NULL_RTX;
16462
16463   STRIP_NOPS (init);
16464
16465   /* If a variable is initialized with a string constant without embedded
16466      zeros, build CONST_STRING.  */
16467   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16468     {
16469       tree enttype = TREE_TYPE (type);
16470       tree domain = TYPE_DOMAIN (type);
16471       enum machine_mode mode = TYPE_MODE (enttype);
16472
16473       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16474           && domain
16475           && integer_zerop (TYPE_MIN_VALUE (domain))
16476           && compare_tree_int (TYPE_MAX_VALUE (domain),
16477                                TREE_STRING_LENGTH (init) - 1) == 0
16478           && ((size_t) TREE_STRING_LENGTH (init)
16479               == strlen (TREE_STRING_POINTER (init)) + 1))
16480         {
16481           rtl = gen_rtx_CONST_STRING (VOIDmode,
16482                                       ggc_strdup (TREE_STRING_POINTER (init)));
16483           rtl = gen_rtx_MEM (BLKmode, rtl);
16484           MEM_READONLY_P (rtl) = 1;
16485         }
16486     }
16487   /* Other aggregates, and complex values, could be represented using
16488      CONCAT: FIXME!  */
16489   else if (AGGREGATE_TYPE_P (type)
16490            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16491                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16492            || TREE_CODE (type) == COMPLEX_TYPE)
16493     ;
16494   /* Vectors only work if their mode is supported by the target.
16495      FIXME: generic vectors ought to work too.  */
16496   else if (TREE_CODE (type) == VECTOR_TYPE
16497            && !VECTOR_MODE_P (TYPE_MODE (type)))
16498     ;
16499   /* If the initializer is something that we know will expand into an
16500      immediate RTL constant, expand it now.  We must be careful not to
16501      reference variables which won't be output.  */
16502   else if (initializer_constant_valid_p (init, type)
16503            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16504     {
16505       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16506          possible.  */
16507       if (TREE_CODE (type) == VECTOR_TYPE)
16508         switch (TREE_CODE (init))
16509           {
16510           case VECTOR_CST:
16511             break;
16512           case CONSTRUCTOR:
16513             if (TREE_CONSTANT (init))
16514               {
16515                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16516                 bool constant_p = true;
16517                 tree value;
16518                 unsigned HOST_WIDE_INT ix;
16519
16520                 /* Even when ctor is constant, it might contain non-*_CST
16521                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16522                    belong into VECTOR_CST nodes.  */
16523                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16524                   if (!CONSTANT_CLASS_P (value))
16525                     {
16526                       constant_p = false;
16527                       break;
16528                     }
16529
16530                 if (constant_p)
16531                   {
16532                     init = build_vector_from_ctor (type, elts);
16533                     break;
16534                   }
16535               }
16536             /* FALLTHRU */
16537
16538           default:
16539             return NULL;
16540           }
16541
16542       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16543
16544       /* If expand_expr returns a MEM, it wasn't immediate.  */
16545       gcc_assert (!rtl || !MEM_P (rtl));
16546     }
16547
16548   return rtl;
16549 }
16550
16551 /* Generate RTL for the variable DECL to represent its location.  */
16552
16553 static rtx
16554 rtl_for_decl_location (tree decl)
16555 {
16556   rtx rtl;
16557
16558   /* Here we have to decide where we are going to say the parameter "lives"
16559      (as far as the debugger is concerned).  We only have a couple of
16560      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16561
16562      DECL_RTL normally indicates where the parameter lives during most of the
16563      activation of the function.  If optimization is enabled however, this
16564      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16565      that the parameter doesn't really live anywhere (as far as the code
16566      generation parts of GCC are concerned) during most of the function's
16567      activation.  That will happen (for example) if the parameter is never
16568      referenced within the function.
16569
16570      We could just generate a location descriptor here for all non-NULL
16571      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16572      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16573      where DECL_RTL is NULL or is a pseudo-reg.
16574
16575      Note however that we can only get away with using DECL_INCOMING_RTL as
16576      a backup substitute for DECL_RTL in certain limited cases.  In cases
16577      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16578      we can be sure that the parameter was passed using the same type as it is
16579      declared to have within the function, and that its DECL_INCOMING_RTL
16580      points us to a place where a value of that type is passed.
16581
16582      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16583      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16584      because in these cases DECL_INCOMING_RTL points us to a value of some
16585      type which is *different* from the type of the parameter itself.  Thus,
16586      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16587      such cases, the debugger would end up (for example) trying to fetch a
16588      `float' from a place which actually contains the first part of a
16589      `double'.  That would lead to really incorrect and confusing
16590      output at debug-time.
16591
16592      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16593      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16594      are a couple of exceptions however.  On little-endian machines we can
16595      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16596      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16597      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16598      when (on a little-endian machine) a non-prototyped function has a
16599      parameter declared to be of type `short' or `char'.  In such cases,
16600      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16601      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16602      passed `int' value.  If the debugger then uses that address to fetch
16603      a `short' or a `char' (on a little-endian machine) the result will be
16604      the correct data, so we allow for such exceptional cases below.
16605
16606      Note that our goal here is to describe the place where the given formal
16607      parameter lives during most of the function's activation (i.e. between the
16608      end of the prologue and the start of the epilogue).  We'll do that as best
16609      as we can. Note however that if the given formal parameter is modified
16610      sometime during the execution of the function, then a stack backtrace (at
16611      debug-time) will show the function as having been called with the *new*
16612      value rather than the value which was originally passed in.  This happens
16613      rarely enough that it is not a major problem, but it *is* a problem, and
16614      I'd like to fix it.
16615
16616      A future version of dwarf2out.c may generate two additional attributes for
16617      any given DW_TAG_formal_parameter DIE which will describe the "passed
16618      type" and the "passed location" for the given formal parameter in addition
16619      to the attributes we now generate to indicate the "declared type" and the
16620      "active location" for each parameter.  This additional set of attributes
16621      could be used by debuggers for stack backtraces. Separately, note that
16622      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16623      This happens (for example) for inlined-instances of inline function formal
16624      parameters which are never referenced.  This really shouldn't be
16625      happening.  All PARM_DECL nodes should get valid non-NULL
16626      DECL_INCOMING_RTL values.  FIXME.  */
16627
16628   /* Use DECL_RTL as the "location" unless we find something better.  */
16629   rtl = DECL_RTL_IF_SET (decl);
16630
16631   /* When generating abstract instances, ignore everything except
16632      constants, symbols living in memory, and symbols living in
16633      fixed registers.  */
16634   if (! reload_completed)
16635     {
16636       if (rtl
16637           && (CONSTANT_P (rtl)
16638               || (MEM_P (rtl)
16639                   && CONSTANT_P (XEXP (rtl, 0)))
16640               || (REG_P (rtl)
16641                   && TREE_CODE (decl) == VAR_DECL
16642                   && TREE_STATIC (decl))))
16643         {
16644           rtl = targetm.delegitimize_address (rtl);
16645           return rtl;
16646         }
16647       rtl = NULL_RTX;
16648     }
16649   else if (TREE_CODE (decl) == PARM_DECL)
16650     {
16651       if (rtl == NULL_RTX
16652           || is_pseudo_reg (rtl)
16653           || (MEM_P (rtl)
16654               && is_pseudo_reg (XEXP (rtl, 0))
16655               && DECL_INCOMING_RTL (decl)
16656               && MEM_P (DECL_INCOMING_RTL (decl))
16657               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
16658         {
16659           tree declared_type = TREE_TYPE (decl);
16660           tree passed_type = DECL_ARG_TYPE (decl);
16661           enum machine_mode dmode = TYPE_MODE (declared_type);
16662           enum machine_mode pmode = TYPE_MODE (passed_type);
16663
16664           /* This decl represents a formal parameter which was optimized out.
16665              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16666              all cases where (rtl == NULL_RTX) just below.  */
16667           if (dmode == pmode)
16668             rtl = DECL_INCOMING_RTL (decl);
16669           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
16670                    && SCALAR_INT_MODE_P (dmode)
16671                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16672                    && DECL_INCOMING_RTL (decl))
16673             {
16674               rtx inc = DECL_INCOMING_RTL (decl);
16675               if (REG_P (inc))
16676                 rtl = inc;
16677               else if (MEM_P (inc))
16678                 {
16679                   if (BYTES_BIG_ENDIAN)
16680                     rtl = adjust_address_nv (inc, dmode,
16681                                              GET_MODE_SIZE (pmode)
16682                                              - GET_MODE_SIZE (dmode));
16683                   else
16684                     rtl = inc;
16685                 }
16686             }
16687         }
16688
16689       /* If the parm was passed in registers, but lives on the stack, then
16690          make a big endian correction if the mode of the type of the
16691          parameter is not the same as the mode of the rtl.  */
16692       /* ??? This is the same series of checks that are made in dbxout.c before
16693          we reach the big endian correction code there.  It isn't clear if all
16694          of these checks are necessary here, but keeping them all is the safe
16695          thing to do.  */
16696       else if (MEM_P (rtl)
16697                && XEXP (rtl, 0) != const0_rtx
16698                && ! CONSTANT_P (XEXP (rtl, 0))
16699                /* Not passed in memory.  */
16700                && !MEM_P (DECL_INCOMING_RTL (decl))
16701                /* Not passed by invisible reference.  */
16702                && (!REG_P (XEXP (rtl, 0))
16703                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16704                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16705 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16706                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16707 #endif
16708                      )
16709                /* Big endian correction check.  */
16710                && BYTES_BIG_ENDIAN
16711                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16712                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16713                    < UNITS_PER_WORD))
16714         {
16715           int offset = (UNITS_PER_WORD
16716                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16717
16718           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16719                              plus_constant (XEXP (rtl, 0), offset));
16720         }
16721     }
16722   else if (TREE_CODE (decl) == VAR_DECL
16723            && rtl
16724            && MEM_P (rtl)
16725            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16726            && BYTES_BIG_ENDIAN)
16727     {
16728       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16729       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16730
16731       /* If a variable is declared "register" yet is smaller than
16732          a register, then if we store the variable to memory, it
16733          looks like we're storing a register-sized value, when in
16734          fact we are not.  We need to adjust the offset of the
16735          storage location to reflect the actual value's bytes,
16736          else gdb will not be able to display it.  */
16737       if (rsize > dsize)
16738         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16739                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16740     }
16741
16742   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16743      and will have been substituted directly into all expressions that use it.
16744      C does not have such a concept, but C++ and other languages do.  */
16745   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16746     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16747
16748   if (rtl)
16749     rtl = targetm.delegitimize_address (rtl);
16750
16751   /* If we don't look past the constant pool, we risk emitting a
16752      reference to a constant pool entry that isn't referenced from
16753      code, and thus is not emitted.  */
16754   if (rtl)
16755     rtl = avoid_constant_pool_reference (rtl);
16756
16757   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16758      in the current CU, resolve_addr will remove the expression referencing
16759      it.  */
16760   if (rtl == NULL_RTX
16761       && TREE_CODE (decl) == VAR_DECL
16762       && !DECL_EXTERNAL (decl)
16763       && TREE_STATIC (decl)
16764       && DECL_NAME (decl)
16765       && !DECL_HARD_REGISTER (decl)
16766       && DECL_MODE (decl) != VOIDmode)
16767     {
16768       rtl = make_decl_rtl_for_debug (decl);
16769       if (!MEM_P (rtl)
16770           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16771           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16772         rtl = NULL_RTX;
16773     }
16774
16775   return rtl;
16776 }
16777
16778 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16779    returned.  If so, the decl for the COMMON block is returned, and the
16780    value is the offset into the common block for the symbol.  */
16781
16782 static tree
16783 fortran_common (tree decl, HOST_WIDE_INT *value)
16784 {
16785   tree val_expr, cvar;
16786   enum machine_mode mode;
16787   HOST_WIDE_INT bitsize, bitpos;
16788   tree offset;
16789   int volatilep = 0, unsignedp = 0;
16790
16791   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16792      it does not have a value (the offset into the common area), or if it
16793      is thread local (as opposed to global) then it isn't common, and shouldn't
16794      be handled as such.  */
16795   if (TREE_CODE (decl) != VAR_DECL
16796       || !TREE_STATIC (decl)
16797       || !DECL_HAS_VALUE_EXPR_P (decl)
16798       || !is_fortran ())
16799     return NULL_TREE;
16800
16801   val_expr = DECL_VALUE_EXPR (decl);
16802   if (TREE_CODE (val_expr) != COMPONENT_REF)
16803     return NULL_TREE;
16804
16805   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16806                               &mode, &unsignedp, &volatilep, true);
16807
16808   if (cvar == NULL_TREE
16809       || TREE_CODE (cvar) != VAR_DECL
16810       || DECL_ARTIFICIAL (cvar)
16811       || !TREE_PUBLIC (cvar))
16812     return NULL_TREE;
16813
16814   *value = 0;
16815   if (offset != NULL)
16816     {
16817       if (!host_integerp (offset, 0))
16818         return NULL_TREE;
16819       *value = tree_low_cst (offset, 0);
16820     }
16821   if (bitpos != 0)
16822     *value += bitpos / BITS_PER_UNIT;
16823
16824   return cvar;
16825 }
16826
16827 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16828    data attribute for a variable or a parameter.  We generate the
16829    DW_AT_const_value attribute only in those cases where the given variable
16830    or parameter does not have a true "location" either in memory or in a
16831    register.  This can happen (for example) when a constant is passed as an
16832    actual argument in a call to an inline function.  (It's possible that
16833    these things can crop up in other ways also.)  Note that one type of
16834    constant value which can be passed into an inlined function is a constant
16835    pointer.  This can happen for example if an actual argument in an inlined
16836    function call evaluates to a compile-time constant address.
16837
16838    CACHE_P is true if it is worth caching the location list for DECL,
16839    so that future calls can reuse it rather than regenerate it from scratch.
16840    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16841    since we will need to refer to them each time the function is inlined.  */
16842
16843 static bool
16844 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16845                                        enum dwarf_attribute attr)
16846 {
16847   rtx rtl;
16848   dw_loc_list_ref list;
16849   var_loc_list *loc_list;
16850   cached_dw_loc_list *cache;
16851   void **slot;
16852
16853   if (TREE_CODE (decl) == ERROR_MARK)
16854     return false;
16855
16856   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16857               || TREE_CODE (decl) == RESULT_DECL);
16858
16859   /* Try to get some constant RTL for this decl, and use that as the value of
16860      the location.  */
16861
16862   rtl = rtl_for_decl_location (decl);
16863   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16864       && add_const_value_attribute (die, rtl))
16865     return true;
16866
16867   /* See if we have single element location list that is equivalent to
16868      a constant value.  That way we are better to use add_const_value_attribute
16869      rather than expanding constant value equivalent.  */
16870   loc_list = lookup_decl_loc (decl);
16871   if (loc_list
16872       && loc_list->first
16873       && loc_list->first->next == NULL
16874       && NOTE_P (loc_list->first->loc)
16875       && NOTE_VAR_LOCATION (loc_list->first->loc)
16876       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16877     {
16878       struct var_loc_node *node;
16879
16880       node = loc_list->first;
16881       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16882       if (GET_CODE (rtl) == EXPR_LIST)
16883         rtl = XEXP (rtl, 0);
16884       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16885           && add_const_value_attribute (die, rtl))
16886          return true;
16887     }
16888   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16889      list several times.  See if we've already cached the contents.  */
16890   list = NULL;
16891   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16892     cache_p = false;
16893   if (cache_p)
16894     {
16895       cache = (cached_dw_loc_list *)
16896         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
16897       if (cache)
16898         list = cache->loc_list;
16899     }
16900   if (list == NULL)
16901     {
16902       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16903       /* It is usually worth caching this result if the decl is from
16904          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
16905       if (cache_p && list && list->dw_loc_next)
16906         {
16907           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
16908                                            DECL_UID (decl), INSERT);
16909           cache = ggc_alloc_cleared_cached_dw_loc_list ();
16910           cache->decl_id = DECL_UID (decl);
16911           cache->loc_list = list;
16912           *slot = cache;
16913         }
16914     }
16915   if (list)
16916     {
16917       add_AT_location_description (die, attr, list);
16918       return true;
16919     }
16920   /* None of that worked, so it must not really have a location;
16921      try adding a constant value attribute from the DECL_INITIAL.  */
16922   return tree_add_const_value_attribute_for_decl (die, decl);
16923 }
16924
16925 /* Add VARIABLE and DIE into deferred locations list.  */
16926
16927 static void
16928 defer_location (tree variable, dw_die_ref die)
16929 {
16930   deferred_locations entry;
16931   entry.variable = variable;
16932   entry.die = die;
16933   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16934 }
16935
16936 /* Helper function for tree_add_const_value_attribute.  Natively encode
16937    initializer INIT into an array.  Return true if successful.  */
16938
16939 static bool
16940 native_encode_initializer (tree init, unsigned char *array, int size)
16941 {
16942   tree type;
16943
16944   if (init == NULL_TREE)
16945     return false;
16946
16947   STRIP_NOPS (init);
16948   switch (TREE_CODE (init))
16949     {
16950     case STRING_CST:
16951       type = TREE_TYPE (init);
16952       if (TREE_CODE (type) == ARRAY_TYPE)
16953         {
16954           tree enttype = TREE_TYPE (type);
16955           enum machine_mode mode = TYPE_MODE (enttype);
16956
16957           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16958             return false;
16959           if (int_size_in_bytes (type) != size)
16960             return false;
16961           if (size > TREE_STRING_LENGTH (init))
16962             {
16963               memcpy (array, TREE_STRING_POINTER (init),
16964                       TREE_STRING_LENGTH (init));
16965               memset (array + TREE_STRING_LENGTH (init),
16966                       '\0', size - TREE_STRING_LENGTH (init));
16967             }
16968           else
16969             memcpy (array, TREE_STRING_POINTER (init), size);
16970           return true;
16971         }
16972       return false;
16973     case CONSTRUCTOR:
16974       type = TREE_TYPE (init);
16975       if (int_size_in_bytes (type) != size)
16976         return false;
16977       if (TREE_CODE (type) == ARRAY_TYPE)
16978         {
16979           HOST_WIDE_INT min_index;
16980           unsigned HOST_WIDE_INT cnt;
16981           int curpos = 0, fieldsize;
16982           constructor_elt *ce;
16983
16984           if (TYPE_DOMAIN (type) == NULL_TREE
16985               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16986             return false;
16987
16988           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16989           if (fieldsize <= 0)
16990             return false;
16991
16992           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16993           memset (array, '\0', size);
16994           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
16995             {
16996               tree val = ce->value;
16997               tree index = ce->index;
16998               int pos = curpos;
16999               if (index && TREE_CODE (index) == RANGE_EXPR)
17000                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17001                       * fieldsize;
17002               else if (index)
17003                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17004
17005               if (val)
17006                 {
17007                   STRIP_NOPS (val);
17008                   if (!native_encode_initializer (val, array + pos, fieldsize))
17009                     return false;
17010                 }
17011               curpos = pos + fieldsize;
17012               if (index && TREE_CODE (index) == RANGE_EXPR)
17013                 {
17014                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17015                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17016                   while (count > 0)
17017                     {
17018                       if (val)
17019                         memcpy (array + curpos, array + pos, fieldsize);
17020                       curpos += fieldsize;
17021                     }
17022                 }
17023               gcc_assert (curpos <= size);
17024             }
17025           return true;
17026         }
17027       else if (TREE_CODE (type) == RECORD_TYPE
17028                || TREE_CODE (type) == UNION_TYPE)
17029         {
17030           tree field = NULL_TREE;
17031           unsigned HOST_WIDE_INT cnt;
17032           constructor_elt *ce;
17033
17034           if (int_size_in_bytes (type) != size)
17035             return false;
17036
17037           if (TREE_CODE (type) == RECORD_TYPE)
17038             field = TYPE_FIELDS (type);
17039
17040           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17041             {
17042               tree val = ce->value;
17043               int pos, fieldsize;
17044
17045               if (ce->index != 0)
17046                 field = ce->index;
17047
17048               if (val)
17049                 STRIP_NOPS (val);
17050
17051               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17052                 return false;
17053
17054               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17055                   && TYPE_DOMAIN (TREE_TYPE (field))
17056                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17057                 return false;
17058               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17059                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17060                 return false;
17061               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17062               pos = int_byte_position (field);
17063               gcc_assert (pos + fieldsize <= size);
17064               if (val
17065                   && !native_encode_initializer (val, array + pos, fieldsize))
17066                 return false;
17067             }
17068           return true;
17069         }
17070       return false;
17071     case VIEW_CONVERT_EXPR:
17072     case NON_LVALUE_EXPR:
17073       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17074     default:
17075       return native_encode_expr (init, array, size) == size;
17076     }
17077 }
17078
17079 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17080    attribute is the const value T.  */
17081
17082 static bool
17083 tree_add_const_value_attribute (dw_die_ref die, tree t)
17084 {
17085   tree init;
17086   tree type = TREE_TYPE (t);
17087   rtx rtl;
17088
17089   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17090     return false;
17091
17092   init = t;
17093   gcc_assert (!DECL_P (init));
17094
17095   rtl = rtl_for_decl_init (init, type);
17096   if (rtl)
17097     return add_const_value_attribute (die, rtl);
17098   /* If the host and target are sane, try harder.  */
17099   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17100            && initializer_constant_valid_p (init, type))
17101     {
17102       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17103       if (size > 0 && (int) size == size)
17104         {
17105           unsigned char *array = (unsigned char *)
17106             ggc_alloc_cleared_atomic (size);
17107
17108           if (native_encode_initializer (init, array, size))
17109             {
17110               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17111               return true;
17112             }
17113         }
17114     }
17115   return false;
17116 }
17117
17118 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17119    attribute is the const value of T, where T is an integral constant
17120    variable with static storage duration
17121    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17122
17123 static bool
17124 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17125 {
17126
17127   if (!decl
17128       || (TREE_CODE (decl) != VAR_DECL
17129           && TREE_CODE (decl) != CONST_DECL))
17130     return false;
17131
17132     if (TREE_READONLY (decl)
17133         && ! TREE_THIS_VOLATILE (decl)
17134         && DECL_INITIAL (decl))
17135       /* OK */;
17136     else
17137       return false;
17138
17139   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17140   if (get_AT (var_die, DW_AT_const_value))
17141     return false;
17142
17143   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17144 }
17145
17146 /* Convert the CFI instructions for the current function into a
17147    location list.  This is used for DW_AT_frame_base when we targeting
17148    a dwarf2 consumer that does not support the dwarf3
17149    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17150    expressions.  */
17151
17152 static dw_loc_list_ref
17153 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17154 {
17155   dw_fde_ref fde;
17156   dw_loc_list_ref list, *list_tail;
17157   dw_cfi_ref cfi;
17158   dw_cfa_location last_cfa, next_cfa;
17159   const char *start_label, *last_label, *section;
17160   dw_cfa_location remember;
17161
17162   fde = current_fde ();
17163   gcc_assert (fde != NULL);
17164
17165   section = secname_for_decl (current_function_decl);
17166   list_tail = &list;
17167   list = NULL;
17168
17169   memset (&next_cfa, 0, sizeof (next_cfa));
17170   next_cfa.reg = INVALID_REGNUM;
17171   remember = next_cfa;
17172
17173   start_label = fde->dw_fde_begin;
17174
17175   /* ??? Bald assumption that the CIE opcode list does not contain
17176      advance opcodes.  */
17177   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17178     lookup_cfa_1 (cfi, &next_cfa, &remember);
17179
17180   last_cfa = next_cfa;
17181   last_label = start_label;
17182
17183   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi == NULL)
17184     {
17185       /* If the first partition contained no CFI adjustments, the
17186          CIE opcodes apply to the whole first partition.  */
17187       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17188                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17189       list_tail =&(*list_tail)->dw_loc_next;
17190       start_label = last_label = fde->dw_fde_second_begin;
17191     }
17192
17193   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17194     {
17195       switch (cfi->dw_cfi_opc)
17196         {
17197         case DW_CFA_set_loc:
17198         case DW_CFA_advance_loc1:
17199         case DW_CFA_advance_loc2:
17200         case DW_CFA_advance_loc4:
17201           if (!cfa_equal_p (&last_cfa, &next_cfa))
17202             {
17203               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17204                                          start_label, last_label, section);
17205
17206               list_tail = &(*list_tail)->dw_loc_next;
17207               last_cfa = next_cfa;
17208               start_label = last_label;
17209             }
17210           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17211           break;
17212
17213         case DW_CFA_advance_loc:
17214           /* The encoding is complex enough that we should never emit this.  */
17215           gcc_unreachable ();
17216
17217         default:
17218           lookup_cfa_1 (cfi, &next_cfa, &remember);
17219           break;
17220         }
17221       if (cfi == fde->dw_fde_switch_cfi)
17222         {
17223           if (!cfa_equal_p (&last_cfa, &next_cfa))
17224             {
17225               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17226                                          start_label, last_label, section);
17227
17228               list_tail = &(*list_tail)->dw_loc_next;
17229               last_cfa = next_cfa;
17230               start_label = last_label;
17231             }
17232           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17233                                      start_label, fde->dw_fde_end, section);
17234           list_tail = &(*list_tail)->dw_loc_next;
17235           start_label = last_label = fde->dw_fde_second_begin;
17236         }
17237     }
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       list_tail = &(*list_tail)->dw_loc_next;
17244       start_label = last_label;
17245     }
17246
17247   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17248                              start_label,
17249                              fde->dw_fde_second_begin
17250                              ? fde->dw_fde_second_end : fde->dw_fde_end,
17251                              section);
17252
17253   if (list && list->dw_loc_next)
17254     gen_llsym (list);
17255
17256   return list;
17257 }
17258
17259 /* Compute a displacement from the "steady-state frame pointer" to the
17260    frame base (often the same as the CFA), and store it in
17261    frame_pointer_fb_offset.  OFFSET is added to the displacement
17262    before the latter is negated.  */
17263
17264 static void
17265 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17266 {
17267   rtx reg, elim;
17268
17269 #ifdef FRAME_POINTER_CFA_OFFSET
17270   reg = frame_pointer_rtx;
17271   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17272 #else
17273   reg = arg_pointer_rtx;
17274   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17275 #endif
17276
17277   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17278   if (GET_CODE (elim) == PLUS)
17279     {
17280       offset += INTVAL (XEXP (elim, 1));
17281       elim = XEXP (elim, 0);
17282     }
17283
17284   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17285                && (elim == hard_frame_pointer_rtx
17286                    || elim == stack_pointer_rtx))
17287               || elim == (frame_pointer_needed
17288                           ? hard_frame_pointer_rtx
17289                           : stack_pointer_rtx));
17290
17291   frame_pointer_fb_offset = -offset;
17292 }
17293
17294 /* Generate a DW_AT_name attribute given some string value to be included as
17295    the value of the attribute.  */
17296
17297 static void
17298 add_name_attribute (dw_die_ref die, const char *name_string)
17299 {
17300   if (name_string != NULL && *name_string != 0)
17301     {
17302       if (demangle_name_func)
17303         name_string = (*demangle_name_func) (name_string);
17304
17305       add_AT_string (die, DW_AT_name, name_string);
17306     }
17307 }
17308
17309 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17310
17311 static void
17312 add_comp_dir_attribute (dw_die_ref die)
17313 {
17314   const char *wd = get_src_pwd ();
17315   char *wd1;
17316
17317   if (wd == NULL)
17318     return;
17319
17320   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17321     {
17322       int wdlen;
17323
17324       wdlen = strlen (wd);
17325       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17326       strcpy (wd1, wd);
17327       wd1 [wdlen] = DIR_SEPARATOR;
17328       wd1 [wdlen + 1] = 0;
17329       wd = wd1;
17330     }
17331
17332     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17333 }
17334
17335 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17336    default.  */
17337
17338 static int
17339 lower_bound_default (void)
17340 {
17341   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17342     {
17343     case DW_LANG_C:
17344     case DW_LANG_C89:
17345     case DW_LANG_C99:
17346     case DW_LANG_C_plus_plus:
17347     case DW_LANG_ObjC:
17348     case DW_LANG_ObjC_plus_plus:
17349     case DW_LANG_Java:
17350       return 0;
17351     case DW_LANG_Fortran77:
17352     case DW_LANG_Fortran90:
17353     case DW_LANG_Fortran95:
17354       return 1;
17355     case DW_LANG_UPC:
17356     case DW_LANG_D:
17357     case DW_LANG_Python:
17358       return dwarf_version >= 4 ? 0 : -1;
17359     case DW_LANG_Ada95:
17360     case DW_LANG_Ada83:
17361     case DW_LANG_Cobol74:
17362     case DW_LANG_Cobol85:
17363     case DW_LANG_Pascal83:
17364     case DW_LANG_Modula2:
17365     case DW_LANG_PLI:
17366       return dwarf_version >= 4 ? 1 : -1;
17367     default:
17368       return -1;
17369     }
17370 }
17371
17372 /* Given a tree node describing an array bound (either lower or upper) output
17373    a representation for that bound.  */
17374
17375 static void
17376 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17377 {
17378   switch (TREE_CODE (bound))
17379     {
17380     case ERROR_MARK:
17381       return;
17382
17383     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17384     case INTEGER_CST:
17385       {
17386         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17387         int dflt;
17388
17389         /* Use the default if possible.  */
17390         if (bound_attr == DW_AT_lower_bound
17391             && host_integerp (bound, 0)
17392             && (dflt = lower_bound_default ()) != -1
17393             && tree_low_cst (bound, 0) == dflt)
17394           ;
17395
17396         /* Otherwise represent the bound as an unsigned value with the
17397            precision of its type.  The precision and signedness of the
17398            type will be necessary to re-interpret it unambiguously.  */
17399         else if (prec < HOST_BITS_PER_WIDE_INT)
17400           {
17401             unsigned HOST_WIDE_INT mask
17402               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17403             add_AT_unsigned (subrange_die, bound_attr,
17404                              TREE_INT_CST_LOW (bound) & mask);
17405           }
17406         else if (prec == HOST_BITS_PER_WIDE_INT
17407                  || TREE_INT_CST_HIGH (bound) == 0)
17408           add_AT_unsigned (subrange_die, bound_attr,
17409                            TREE_INT_CST_LOW (bound));
17410         else
17411           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17412                          TREE_INT_CST_LOW (bound));
17413       }
17414       break;
17415
17416     CASE_CONVERT:
17417     case VIEW_CONVERT_EXPR:
17418       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17419       break;
17420
17421     case SAVE_EXPR:
17422       break;
17423
17424     case VAR_DECL:
17425     case PARM_DECL:
17426     case RESULT_DECL:
17427       {
17428         dw_die_ref decl_die = lookup_decl_die (bound);
17429
17430         /* ??? Can this happen, or should the variable have been bound
17431            first?  Probably it can, since I imagine that we try to create
17432            the types of parameters in the order in which they exist in
17433            the list, and won't have created a forward reference to a
17434            later parameter.  */
17435         if (decl_die != NULL)
17436           {
17437             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17438             break;
17439           }
17440       }
17441       /* FALLTHRU */
17442
17443     default:
17444       {
17445         /* Otherwise try to create a stack operation procedure to
17446            evaluate the value of the array bound.  */
17447
17448         dw_die_ref ctx, decl_die;
17449         dw_loc_list_ref list;
17450
17451         list = loc_list_from_tree (bound, 2);
17452         if (list == NULL || single_element_loc_list_p (list))
17453           {
17454             /* If DW_AT_*bound is not a reference nor constant, it is
17455                a DWARF expression rather than location description.
17456                For that loc_list_from_tree (bound, 0) is needed.
17457                If that fails to give a single element list,
17458                fall back to outputting this as a reference anyway.  */
17459             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17460             if (list2 && single_element_loc_list_p (list2))
17461               {
17462                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17463                 break;
17464               }
17465           }
17466         if (list == NULL)
17467           break;
17468
17469         if (current_function_decl == 0)
17470           ctx = comp_unit_die ();
17471         else
17472           ctx = lookup_decl_die (current_function_decl);
17473
17474         decl_die = new_die (DW_TAG_variable, ctx, bound);
17475         add_AT_flag (decl_die, DW_AT_artificial, 1);
17476         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17477         add_AT_location_description (decl_die, DW_AT_location, list);
17478         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17479         break;
17480       }
17481     }
17482 }
17483
17484 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17485    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17486    Note that the block of subscript information for an array type also
17487    includes information about the element type of the given array type.  */
17488
17489 static void
17490 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17491 {
17492   unsigned dimension_number;
17493   tree lower, upper;
17494   dw_die_ref subrange_die;
17495
17496   for (dimension_number = 0;
17497        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17498        type = TREE_TYPE (type), dimension_number++)
17499     {
17500       tree domain = TYPE_DOMAIN (type);
17501
17502       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17503         break;
17504
17505       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17506          and (in GNU C only) variable bounds.  Handle all three forms
17507          here.  */
17508       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17509       if (domain)
17510         {
17511           /* We have an array type with specified bounds.  */
17512           lower = TYPE_MIN_VALUE (domain);
17513           upper = TYPE_MAX_VALUE (domain);
17514
17515           /* Define the index type.  */
17516           if (TREE_TYPE (domain))
17517             {
17518               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17519                  TREE_TYPE field.  We can't emit debug info for this
17520                  because it is an unnamed integral type.  */
17521               if (TREE_CODE (domain) == INTEGER_TYPE
17522                   && TYPE_NAME (domain) == NULL_TREE
17523                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17524                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17525                 ;
17526               else
17527                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17528                                     type_die);
17529             }
17530
17531           /* ??? If upper is NULL, the array has unspecified length,
17532              but it does have a lower bound.  This happens with Fortran
17533                dimension arr(N:*)
17534              Since the debugger is definitely going to need to know N
17535              to produce useful results, go ahead and output the lower
17536              bound solo, and hope the debugger can cope.  */
17537
17538           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17539           if (upper)
17540             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17541         }
17542
17543       /* Otherwise we have an array type with an unspecified length.  The
17544          DWARF-2 spec does not say how to handle this; let's just leave out the
17545          bounds.  */
17546     }
17547 }
17548
17549 static void
17550 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17551 {
17552   unsigned size;
17553
17554   switch (TREE_CODE (tree_node))
17555     {
17556     case ERROR_MARK:
17557       size = 0;
17558       break;
17559     case ENUMERAL_TYPE:
17560     case RECORD_TYPE:
17561     case UNION_TYPE:
17562     case QUAL_UNION_TYPE:
17563       size = int_size_in_bytes (tree_node);
17564       break;
17565     case FIELD_DECL:
17566       /* For a data member of a struct or union, the DW_AT_byte_size is
17567          generally given as the number of bytes normally allocated for an
17568          object of the *declared* type of the member itself.  This is true
17569          even for bit-fields.  */
17570       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17571       break;
17572     default:
17573       gcc_unreachable ();
17574     }
17575
17576   /* Note that `size' might be -1 when we get to this point.  If it is, that
17577      indicates that the byte size of the entity in question is variable.  We
17578      have no good way of expressing this fact in Dwarf at the present time,
17579      so just let the -1 pass on through.  */
17580   add_AT_unsigned (die, DW_AT_byte_size, size);
17581 }
17582
17583 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17584    which specifies the distance in bits from the highest order bit of the
17585    "containing object" for the bit-field to the highest order bit of the
17586    bit-field itself.
17587
17588    For any given bit-field, the "containing object" is a hypothetical object
17589    (of some integral or enum type) within which the given bit-field lives.  The
17590    type of this hypothetical "containing object" is always the same as the
17591    declared type of the individual bit-field itself.  The determination of the
17592    exact location of the "containing object" for a bit-field is rather
17593    complicated.  It's handled by the `field_byte_offset' function (above).
17594
17595    Note that it is the size (in bytes) of the hypothetical "containing object"
17596    which will be given in the DW_AT_byte_size attribute for this bit-field.
17597    (See `byte_size_attribute' above).  */
17598
17599 static inline void
17600 add_bit_offset_attribute (dw_die_ref die, tree decl)
17601 {
17602   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17603   tree type = DECL_BIT_FIELD_TYPE (decl);
17604   HOST_WIDE_INT bitpos_int;
17605   HOST_WIDE_INT highest_order_object_bit_offset;
17606   HOST_WIDE_INT highest_order_field_bit_offset;
17607   HOST_WIDE_INT bit_offset;
17608
17609   /* Must be a field and a bit field.  */
17610   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17611
17612   /* We can't yet handle bit-fields whose offsets are variable, so if we
17613      encounter such things, just return without generating any attribute
17614      whatsoever.  Likewise for variable or too large size.  */
17615   if (! host_integerp (bit_position (decl), 0)
17616       || ! host_integerp (DECL_SIZE (decl), 1))
17617     return;
17618
17619   bitpos_int = int_bit_position (decl);
17620
17621   /* Note that the bit offset is always the distance (in bits) from the
17622      highest-order bit of the "containing object" to the highest-order bit of
17623      the bit-field itself.  Since the "high-order end" of any object or field
17624      is different on big-endian and little-endian machines, the computation
17625      below must take account of these differences.  */
17626   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17627   highest_order_field_bit_offset = bitpos_int;
17628
17629   if (! BYTES_BIG_ENDIAN)
17630     {
17631       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17632       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17633     }
17634
17635   bit_offset
17636     = (! BYTES_BIG_ENDIAN
17637        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17638        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17639
17640   if (bit_offset < 0)
17641     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17642   else
17643     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17644 }
17645
17646 /* For a FIELD_DECL node which represents a bit field, output an attribute
17647    which specifies the length in bits of the given field.  */
17648
17649 static inline void
17650 add_bit_size_attribute (dw_die_ref die, tree decl)
17651 {
17652   /* Must be a field and a bit field.  */
17653   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17654               && DECL_BIT_FIELD_TYPE (decl));
17655
17656   if (host_integerp (DECL_SIZE (decl), 1))
17657     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17658 }
17659
17660 /* If the compiled language is ANSI C, then add a 'prototyped'
17661    attribute, if arg types are given for the parameters of a function.  */
17662
17663 static inline void
17664 add_prototyped_attribute (dw_die_ref die, tree func_type)
17665 {
17666   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17667       && prototype_p (func_type))
17668     add_AT_flag (die, DW_AT_prototyped, 1);
17669 }
17670
17671 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17672    by looking in either the type declaration or object declaration
17673    equate table.  */
17674
17675 static inline dw_die_ref
17676 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17677 {
17678   dw_die_ref origin_die = NULL;
17679
17680   if (TREE_CODE (origin) != FUNCTION_DECL)
17681     {
17682       /* We may have gotten separated from the block for the inlined
17683          function, if we're in an exception handler or some such; make
17684          sure that the abstract function has been written out.
17685
17686          Doing this for nested functions is wrong, however; functions are
17687          distinct units, and our context might not even be inline.  */
17688       tree fn = origin;
17689
17690       if (TYPE_P (fn))
17691         fn = TYPE_STUB_DECL (fn);
17692
17693       fn = decl_function_context (fn);
17694       if (fn)
17695         dwarf2out_abstract_function (fn);
17696     }
17697
17698   if (DECL_P (origin))
17699     origin_die = lookup_decl_die (origin);
17700   else if (TYPE_P (origin))
17701     origin_die = lookup_type_die (origin);
17702
17703   /* XXX: Functions that are never lowered don't always have correct block
17704      trees (in the case of java, they simply have no block tree, in some other
17705      languages).  For these functions, there is nothing we can really do to
17706      output correct debug info for inlined functions in all cases.  Rather
17707      than die, we'll just produce deficient debug info now, in that we will
17708      have variables without a proper abstract origin.  In the future, when all
17709      functions are lowered, we should re-add a gcc_assert (origin_die)
17710      here.  */
17711
17712   if (origin_die)
17713     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17714   return origin_die;
17715 }
17716
17717 /* We do not currently support the pure_virtual attribute.  */
17718
17719 static inline void
17720 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17721 {
17722   if (DECL_VINDEX (func_decl))
17723     {
17724       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17725
17726       if (host_integerp (DECL_VINDEX (func_decl), 0))
17727         add_AT_loc (die, DW_AT_vtable_elem_location,
17728                     new_loc_descr (DW_OP_constu,
17729                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17730                                    0));
17731
17732       /* GNU extension: Record what type this method came from originally.  */
17733       if (debug_info_level > DINFO_LEVEL_TERSE
17734           && DECL_CONTEXT (func_decl))
17735         add_AT_die_ref (die, DW_AT_containing_type,
17736                         lookup_type_die (DECL_CONTEXT (func_decl)));
17737     }
17738 }
17739 \f
17740 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17741    given decl.  This used to be a vendor extension until after DWARF 4
17742    standardized it.  */
17743
17744 static void
17745 add_linkage_attr (dw_die_ref die, tree decl)
17746 {
17747   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17748
17749   /* Mimic what assemble_name_raw does with a leading '*'.  */
17750   if (name[0] == '*')
17751     name = &name[1];
17752
17753   if (dwarf_version >= 4)
17754     add_AT_string (die, DW_AT_linkage_name, name);
17755   else
17756     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17757 }
17758
17759 /* Add source coordinate attributes for the given decl.  */
17760
17761 static void
17762 add_src_coords_attributes (dw_die_ref die, tree decl)
17763 {
17764   expanded_location s;
17765
17766   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17767     return;
17768   s = expand_location (DECL_SOURCE_LOCATION (decl));
17769   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17770   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17771 }
17772
17773 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17774
17775 static void
17776 add_linkage_name (dw_die_ref die, tree decl)
17777 {
17778   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17779        && TREE_PUBLIC (decl)
17780        && !DECL_ABSTRACT (decl)
17781        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17782        && die->die_tag != DW_TAG_member)
17783     {
17784       /* Defer until we have an assembler name set.  */
17785       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17786         {
17787           limbo_die_node *asm_name;
17788
17789           asm_name = ggc_alloc_cleared_limbo_die_node ();
17790           asm_name->die = die;
17791           asm_name->created_for = decl;
17792           asm_name->next = deferred_asm_name;
17793           deferred_asm_name = asm_name;
17794         }
17795       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17796         add_linkage_attr (die, decl);
17797     }
17798 }
17799
17800 /* Add a DW_AT_name attribute and source coordinate attribute for the
17801    given decl, but only if it actually has a name.  */
17802
17803 static void
17804 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17805 {
17806   tree decl_name;
17807
17808   decl_name = DECL_NAME (decl);
17809   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17810     {
17811       const char *name = dwarf2_name (decl, 0);
17812       if (name)
17813         add_name_attribute (die, name);
17814       if (! DECL_ARTIFICIAL (decl))
17815         add_src_coords_attributes (die, decl);
17816
17817       add_linkage_name (die, decl);
17818     }
17819
17820 #ifdef VMS_DEBUGGING_INFO
17821   /* Get the function's name, as described by its RTL.  This may be different
17822      from the DECL_NAME name used in the source file.  */
17823   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17824     {
17825       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17826                    XEXP (DECL_RTL (decl), 0));
17827       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17828     }
17829 #endif /* VMS_DEBUGGING_INFO */
17830 }
17831
17832 #ifdef VMS_DEBUGGING_INFO
17833 /* Output the debug main pointer die for VMS */
17834
17835 void
17836 dwarf2out_vms_debug_main_pointer (void)
17837 {
17838   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17839   dw_die_ref die;
17840
17841   /* Allocate the VMS debug main subprogram die.  */
17842   die = ggc_alloc_cleared_die_node ();
17843   die->die_tag = DW_TAG_subprogram;
17844   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17845   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17846                                current_function_funcdef_no);
17847   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17848
17849   /* Make it the first child of comp_unit_die ().  */
17850   die->die_parent = comp_unit_die ();
17851   if (comp_unit_die ()->die_child)
17852     {
17853       die->die_sib = comp_unit_die ()->die_child->die_sib;
17854       comp_unit_die ()->die_child->die_sib = die;
17855     }
17856   else
17857     {
17858       die->die_sib = die;
17859       comp_unit_die ()->die_child = die;
17860     }
17861 }
17862 #endif /* VMS_DEBUGGING_INFO */
17863
17864 /* Push a new declaration scope.  */
17865
17866 static void
17867 push_decl_scope (tree scope)
17868 {
17869   VEC_safe_push (tree, gc, decl_scope_table, scope);
17870 }
17871
17872 /* Pop a declaration scope.  */
17873
17874 static inline void
17875 pop_decl_scope (void)
17876 {
17877   VEC_pop (tree, decl_scope_table);
17878 }
17879
17880 /* Return the DIE for the scope that immediately contains this type.
17881    Non-named types get global scope.  Named types nested in other
17882    types get their containing scope if it's open, or global scope
17883    otherwise.  All other types (i.e. function-local named types) get
17884    the current active scope.  */
17885
17886 static dw_die_ref
17887 scope_die_for (tree t, dw_die_ref context_die)
17888 {
17889   dw_die_ref scope_die = NULL;
17890   tree containing_scope;
17891   int i;
17892
17893   /* Non-types always go in the current scope.  */
17894   gcc_assert (TYPE_P (t));
17895
17896   containing_scope = TYPE_CONTEXT (t);
17897
17898   /* Use the containing namespace if it was passed in (for a declaration).  */
17899   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17900     {
17901       if (context_die == lookup_decl_die (containing_scope))
17902         /* OK */;
17903       else
17904         containing_scope = NULL_TREE;
17905     }
17906
17907   /* Ignore function type "scopes" from the C frontend.  They mean that
17908      a tagged type is local to a parmlist of a function declarator, but
17909      that isn't useful to DWARF.  */
17910   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17911     containing_scope = NULL_TREE;
17912
17913   if (SCOPE_FILE_SCOPE_P (containing_scope))
17914     scope_die = comp_unit_die ();
17915   else if (TYPE_P (containing_scope))
17916     {
17917       /* For types, we can just look up the appropriate DIE.  But
17918          first we check to see if we're in the middle of emitting it
17919          so we know where the new DIE should go.  */
17920       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17921         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17922           break;
17923
17924       if (i < 0)
17925         {
17926           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17927                       || TREE_ASM_WRITTEN (containing_scope));
17928           /*We are not in the middle of emitting the type
17929             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17930           scope_die = lookup_type_die (containing_scope);
17931
17932           /* If none of the current dies are suitable, we get file scope.  */
17933           if (scope_die == NULL)
17934             scope_die = comp_unit_die ();
17935         }
17936       else
17937         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
17938     }
17939   else
17940     scope_die = context_die;
17941
17942   return scope_die;
17943 }
17944
17945 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17946
17947 static inline int
17948 local_scope_p (dw_die_ref context_die)
17949 {
17950   for (; context_die; context_die = context_die->die_parent)
17951     if (context_die->die_tag == DW_TAG_inlined_subroutine
17952         || context_die->die_tag == DW_TAG_subprogram)
17953       return 1;
17954
17955   return 0;
17956 }
17957
17958 /* Returns nonzero if CONTEXT_DIE is a class.  */
17959
17960 static inline int
17961 class_scope_p (dw_die_ref context_die)
17962 {
17963   return (context_die
17964           && (context_die->die_tag == DW_TAG_structure_type
17965               || context_die->die_tag == DW_TAG_class_type
17966               || context_die->die_tag == DW_TAG_interface_type
17967               || context_die->die_tag == DW_TAG_union_type));
17968 }
17969
17970 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17971    whether or not to treat a DIE in this context as a declaration.  */
17972
17973 static inline int
17974 class_or_namespace_scope_p (dw_die_ref context_die)
17975 {
17976   return (class_scope_p (context_die)
17977           || (context_die && context_die->die_tag == DW_TAG_namespace));
17978 }
17979
17980 /* Many forms of DIEs require a "type description" attribute.  This
17981    routine locates the proper "type descriptor" die for the type given
17982    by 'type', and adds a DW_AT_type attribute below the given die.  */
17983
17984 static void
17985 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17986                     int decl_volatile, dw_die_ref context_die)
17987 {
17988   enum tree_code code  = TREE_CODE (type);
17989   dw_die_ref type_die  = NULL;
17990
17991   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17992      or fixed-point type, use the inner type.  This is because we have no
17993      support for unnamed types in base_type_die.  This can happen if this is
17994      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17995   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17996       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17997     type = TREE_TYPE (type), code = TREE_CODE (type);
17998
17999   if (code == ERROR_MARK
18000       /* Handle a special case.  For functions whose return type is void, we
18001          generate *no* type attribute.  (Note that no object may have type
18002          `void', so this only applies to function return types).  */
18003       || code == VOID_TYPE)
18004     return;
18005
18006   type_die = modified_type_die (type,
18007                                 decl_const || TYPE_READONLY (type),
18008                                 decl_volatile || TYPE_VOLATILE (type),
18009                                 context_die);
18010
18011   if (type_die != NULL)
18012     add_AT_die_ref (object_die, DW_AT_type, type_die);
18013 }
18014
18015 /* Given an object die, add the calling convention attribute for the
18016    function call type.  */
18017 static void
18018 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18019 {
18020   enum dwarf_calling_convention value = DW_CC_normal;
18021
18022   value = ((enum dwarf_calling_convention)
18023            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18024
18025   if (is_fortran ()
18026       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18027     {
18028       /* DWARF 2 doesn't provide a way to identify a program's source-level
18029         entry point.  DW_AT_calling_convention attributes are only meant
18030         to describe functions' calling conventions.  However, lacking a
18031         better way to signal the Fortran main program, we used this for 
18032         a long time, following existing custom.  Now, DWARF 4 has 
18033         DW_AT_main_subprogram, which we add below, but some tools still
18034         rely on the old way, which we thus keep.  */
18035       value = DW_CC_program;
18036
18037       if (dwarf_version >= 4 || !dwarf_strict)
18038         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18039     }
18040
18041   /* Only add the attribute if the backend requests it, and
18042      is not DW_CC_normal.  */
18043   if (value && (value != DW_CC_normal))
18044     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18045 }
18046
18047 /* Given a tree pointer to a struct, class, union, or enum type node, return
18048    a pointer to the (string) tag name for the given type, or zero if the type
18049    was declared without a tag.  */
18050
18051 static const char *
18052 type_tag (const_tree type)
18053 {
18054   const char *name = 0;
18055
18056   if (TYPE_NAME (type) != 0)
18057     {
18058       tree t = 0;
18059
18060       /* Find the IDENTIFIER_NODE for the type name.  */
18061       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18062           && !TYPE_NAMELESS (type))
18063         t = TYPE_NAME (type);
18064
18065       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18066          a TYPE_DECL node, regardless of whether or not a `typedef' was
18067          involved.  */
18068       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18069                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18070         {
18071           /* We want to be extra verbose.  Don't call dwarf_name if
18072              DECL_NAME isn't set.  The default hook for decl_printable_name
18073              doesn't like that, and in this context it's correct to return
18074              0, instead of "<anonymous>" or the like.  */
18075           if (DECL_NAME (TYPE_NAME (type))
18076               && !DECL_NAMELESS (TYPE_NAME (type)))
18077             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18078         }
18079
18080       /* Now get the name as a string, or invent one.  */
18081       if (!name && t != 0)
18082         name = IDENTIFIER_POINTER (t);
18083     }
18084
18085   return (name == 0 || *name == '\0') ? 0 : name;
18086 }
18087
18088 /* Return the type associated with a data member, make a special check
18089    for bit field types.  */
18090
18091 static inline tree
18092 member_declared_type (const_tree member)
18093 {
18094   return (DECL_BIT_FIELD_TYPE (member)
18095           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18096 }
18097
18098 /* Get the decl's label, as described by its RTL. This may be different
18099    from the DECL_NAME name used in the source file.  */
18100
18101 #if 0
18102 static const char *
18103 decl_start_label (tree decl)
18104 {
18105   rtx x;
18106   const char *fnname;
18107
18108   x = DECL_RTL (decl);
18109   gcc_assert (MEM_P (x));
18110
18111   x = XEXP (x, 0);
18112   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18113
18114   fnname = XSTR (x, 0);
18115   return fnname;
18116 }
18117 #endif
18118 \f
18119 /* These routines generate the internal representation of the DIE's for
18120    the compilation unit.  Debugging information is collected by walking
18121    the declaration trees passed in from dwarf2out_decl().  */
18122
18123 static void
18124 gen_array_type_die (tree type, dw_die_ref context_die)
18125 {
18126   dw_die_ref scope_die = scope_die_for (type, context_die);
18127   dw_die_ref array_die;
18128
18129   /* GNU compilers represent multidimensional array types as sequences of one
18130      dimensional array types whose element types are themselves array types.
18131      We sometimes squish that down to a single array_type DIE with multiple
18132      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18133      say that we are allowed to do this kind of compression in C, because
18134      there is no difference between an array of arrays and a multidimensional
18135      array.  We don't do this for Ada to remain as close as possible to the
18136      actual representation, which is especially important against the language
18137      flexibilty wrt arrays of variable size.  */
18138
18139   bool collapse_nested_arrays = !is_ada ();
18140   tree element_type;
18141
18142   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18143      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18144   if (TYPE_STRING_FLAG (type)
18145       && TREE_CODE (type) == ARRAY_TYPE
18146       && is_fortran ()
18147       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18148     {
18149       HOST_WIDE_INT size;
18150
18151       array_die = new_die (DW_TAG_string_type, scope_die, type);
18152       add_name_attribute (array_die, type_tag (type));
18153       equate_type_number_to_die (type, array_die);
18154       size = int_size_in_bytes (type);
18155       if (size >= 0)
18156         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18157       else if (TYPE_DOMAIN (type) != NULL_TREE
18158                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18159                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18160         {
18161           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18162           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18163
18164           size = int_size_in_bytes (TREE_TYPE (szdecl));
18165           if (loc && size > 0)
18166             {
18167               add_AT_location_description (array_die, DW_AT_string_length, loc);
18168               if (size != DWARF2_ADDR_SIZE)
18169                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18170             }
18171         }
18172       return;
18173     }
18174
18175   /* ??? The SGI dwarf reader fails for array of array of enum types
18176      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18177      array type comes before the outer array type.  We thus call gen_type_die
18178      before we new_die and must prevent nested array types collapsing for this
18179      target.  */
18180
18181 #ifdef MIPS_DEBUGGING_INFO
18182   gen_type_die (TREE_TYPE (type), context_die);
18183   collapse_nested_arrays = false;
18184 #endif
18185
18186   array_die = new_die (DW_TAG_array_type, scope_die, type);
18187   add_name_attribute (array_die, type_tag (type));
18188   equate_type_number_to_die (type, array_die);
18189
18190   if (TREE_CODE (type) == VECTOR_TYPE)
18191     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18192
18193   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18194   if (is_fortran ()
18195       && TREE_CODE (type) == ARRAY_TYPE
18196       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18197       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18198     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18199
18200 #if 0
18201   /* We default the array ordering.  SDB will probably do
18202      the right things even if DW_AT_ordering is not present.  It's not even
18203      an issue until we start to get into multidimensional arrays anyway.  If
18204      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18205      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18206      and when we find out that we need to put these in, we will only do so
18207      for multidimensional arrays.  */
18208   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18209 #endif
18210
18211 #ifdef MIPS_DEBUGGING_INFO
18212   /* The SGI compilers handle arrays of unknown bound by setting
18213      AT_declaration and not emitting any subrange DIEs.  */
18214   if (TREE_CODE (type) == ARRAY_TYPE
18215       && ! TYPE_DOMAIN (type))
18216     add_AT_flag (array_die, DW_AT_declaration, 1);
18217   else
18218 #endif
18219   if (TREE_CODE (type) == VECTOR_TYPE)
18220     {
18221       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18222       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18223       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18224       add_bound_info (subrange_die, DW_AT_upper_bound,
18225                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18226     }
18227   else
18228     add_subscript_info (array_die, type, collapse_nested_arrays);
18229
18230   /* Add representation of the type of the elements of this array type and
18231      emit the corresponding DIE if we haven't done it already.  */
18232   element_type = TREE_TYPE (type);
18233   if (collapse_nested_arrays)
18234     while (TREE_CODE (element_type) == ARRAY_TYPE)
18235       {
18236         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18237           break;
18238         element_type = TREE_TYPE (element_type);
18239       }
18240
18241 #ifndef MIPS_DEBUGGING_INFO
18242   gen_type_die (element_type, context_die);
18243 #endif
18244
18245   add_type_attribute (array_die, element_type, 0, 0, context_die);
18246
18247   if (get_AT (array_die, DW_AT_name))
18248     add_pubtype (type, array_die);
18249 }
18250
18251 static dw_loc_descr_ref
18252 descr_info_loc (tree val, tree base_decl)
18253 {
18254   HOST_WIDE_INT size;
18255   dw_loc_descr_ref loc, loc2;
18256   enum dwarf_location_atom op;
18257
18258   if (val == base_decl)
18259     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18260
18261   switch (TREE_CODE (val))
18262     {
18263     CASE_CONVERT:
18264       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18265     case VAR_DECL:
18266       return loc_descriptor_from_tree (val, 0);
18267     case INTEGER_CST:
18268       if (host_integerp (val, 0))
18269         return int_loc_descriptor (tree_low_cst (val, 0));
18270       break;
18271     case INDIRECT_REF:
18272       size = int_size_in_bytes (TREE_TYPE (val));
18273       if (size < 0)
18274         break;
18275       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18276       if (!loc)
18277         break;
18278       if (size == DWARF2_ADDR_SIZE)
18279         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18280       else
18281         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18282       return loc;
18283     case POINTER_PLUS_EXPR:
18284     case PLUS_EXPR:
18285       if (host_integerp (TREE_OPERAND (val, 1), 1)
18286           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18287              < 16384)
18288         {
18289           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18290           if (!loc)
18291             break;
18292           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18293         }
18294       else
18295         {
18296           op = DW_OP_plus;
18297         do_binop:
18298           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18299           if (!loc)
18300             break;
18301           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18302           if (!loc2)
18303             break;
18304           add_loc_descr (&loc, loc2);
18305           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18306         }
18307       return loc;
18308     case MINUS_EXPR:
18309       op = DW_OP_minus;
18310       goto do_binop;
18311     case MULT_EXPR:
18312       op = DW_OP_mul;
18313       goto do_binop;
18314     case EQ_EXPR:
18315       op = DW_OP_eq;
18316       goto do_binop;
18317     case NE_EXPR:
18318       op = DW_OP_ne;
18319       goto do_binop;
18320     default:
18321       break;
18322     }
18323   return NULL;
18324 }
18325
18326 static void
18327 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18328                       tree val, tree base_decl)
18329 {
18330   dw_loc_descr_ref loc;
18331
18332   if (host_integerp (val, 0))
18333     {
18334       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18335       return;
18336     }
18337
18338   loc = descr_info_loc (val, base_decl);
18339   if (!loc)
18340     return;
18341
18342   add_AT_loc (die, attr, loc);
18343 }
18344
18345 /* This routine generates DIE for array with hidden descriptor, details
18346    are filled into *info by a langhook.  */
18347
18348 static void
18349 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18350                           dw_die_ref context_die)
18351 {
18352   dw_die_ref scope_die = scope_die_for (type, context_die);
18353   dw_die_ref array_die;
18354   int dim;
18355
18356   array_die = new_die (DW_TAG_array_type, scope_die, type);
18357   add_name_attribute (array_die, type_tag (type));
18358   equate_type_number_to_die (type, array_die);
18359
18360   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18361   if (is_fortran ()
18362       && info->ndimensions >= 2)
18363     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18364
18365   if (info->data_location)
18366     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18367                           info->base_decl);
18368   if (info->associated)
18369     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18370                           info->base_decl);
18371   if (info->allocated)
18372     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18373                           info->base_decl);
18374
18375   for (dim = 0; dim < info->ndimensions; dim++)
18376     {
18377       dw_die_ref subrange_die
18378         = new_die (DW_TAG_subrange_type, array_die, NULL);
18379
18380       if (info->dimen[dim].lower_bound)
18381         {
18382           /* If it is the default value, omit it.  */
18383           int dflt;
18384
18385           if (host_integerp (info->dimen[dim].lower_bound, 0)
18386               && (dflt = lower_bound_default ()) != -1
18387               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18388             ;
18389           else
18390             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18391                                   info->dimen[dim].lower_bound,
18392                                   info->base_decl);
18393         }
18394       if (info->dimen[dim].upper_bound)
18395         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18396                               info->dimen[dim].upper_bound,
18397                               info->base_decl);
18398       if (info->dimen[dim].stride)
18399         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18400                               info->dimen[dim].stride,
18401                               info->base_decl);
18402     }
18403
18404   gen_type_die (info->element_type, context_die);
18405   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18406
18407   if (get_AT (array_die, DW_AT_name))
18408     add_pubtype (type, array_die);
18409 }
18410
18411 #if 0
18412 static void
18413 gen_entry_point_die (tree decl, dw_die_ref context_die)
18414 {
18415   tree origin = decl_ultimate_origin (decl);
18416   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18417
18418   if (origin != NULL)
18419     add_abstract_origin_attribute (decl_die, origin);
18420   else
18421     {
18422       add_name_and_src_coords_attributes (decl_die, decl);
18423       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18424                           0, 0, context_die);
18425     }
18426
18427   if (DECL_ABSTRACT (decl))
18428     equate_decl_number_to_die (decl, decl_die);
18429   else
18430     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18431 }
18432 #endif
18433
18434 /* Walk through the list of incomplete types again, trying once more to
18435    emit full debugging info for them.  */
18436
18437 static void
18438 retry_incomplete_types (void)
18439 {
18440   int i;
18441
18442   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18443     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18444                                   DINFO_USAGE_DIR_USE))
18445       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18446 }
18447
18448 /* Determine what tag to use for a record type.  */
18449
18450 static enum dwarf_tag
18451 record_type_tag (tree type)
18452 {
18453   if (! lang_hooks.types.classify_record)
18454     return DW_TAG_structure_type;
18455
18456   switch (lang_hooks.types.classify_record (type))
18457     {
18458     case RECORD_IS_STRUCT:
18459       return DW_TAG_structure_type;
18460
18461     case RECORD_IS_CLASS:
18462       return DW_TAG_class_type;
18463
18464     case RECORD_IS_INTERFACE:
18465       if (dwarf_version >= 3 || !dwarf_strict)
18466         return DW_TAG_interface_type;
18467       return DW_TAG_structure_type;
18468
18469     default:
18470       gcc_unreachable ();
18471     }
18472 }
18473
18474 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18475    include all of the information about the enumeration values also. Each
18476    enumerated type name/value is listed as a child of the enumerated type
18477    DIE.  */
18478
18479 static dw_die_ref
18480 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18481 {
18482   dw_die_ref type_die = lookup_type_die (type);
18483
18484   if (type_die == NULL)
18485     {
18486       type_die = new_die (DW_TAG_enumeration_type,
18487                           scope_die_for (type, context_die), type);
18488       equate_type_number_to_die (type, type_die);
18489       add_name_attribute (type_die, type_tag (type));
18490       if (dwarf_version >= 4 || !dwarf_strict)
18491         {
18492           if (ENUM_IS_SCOPED (type))
18493             add_AT_flag (type_die, DW_AT_enum_class, 1);
18494           if (ENUM_IS_OPAQUE (type))
18495             add_AT_flag (type_die, DW_AT_declaration, 1);
18496         }
18497     }
18498   else if (! TYPE_SIZE (type))
18499     return type_die;
18500   else
18501     remove_AT (type_die, DW_AT_declaration);
18502
18503   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18504      given enum type is incomplete, do not generate the DW_AT_byte_size
18505      attribute or the DW_AT_element_list attribute.  */
18506   if (TYPE_SIZE (type))
18507     {
18508       tree link;
18509
18510       TREE_ASM_WRITTEN (type) = 1;
18511       add_byte_size_attribute (type_die, type);
18512       if (TYPE_STUB_DECL (type) != NULL_TREE)
18513         {
18514           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18515           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18516         }
18517
18518       /* If the first reference to this type was as the return type of an
18519          inline function, then it may not have a parent.  Fix this now.  */
18520       if (type_die->die_parent == NULL)
18521         add_child_die (scope_die_for (type, context_die), type_die);
18522
18523       for (link = TYPE_VALUES (type);
18524            link != NULL; link = TREE_CHAIN (link))
18525         {
18526           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18527           tree value = TREE_VALUE (link);
18528
18529           add_name_attribute (enum_die,
18530                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18531
18532           if (TREE_CODE (value) == CONST_DECL)
18533             value = DECL_INITIAL (value);
18534
18535           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18536             /* DWARF2 does not provide a way of indicating whether or
18537                not enumeration constants are signed or unsigned.  GDB
18538                always assumes the values are signed, so we output all
18539                values as if they were signed.  That means that
18540                enumeration constants with very large unsigned values
18541                will appear to have negative values in the debugger.  */
18542             add_AT_int (enum_die, DW_AT_const_value,
18543                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18544         }
18545     }
18546   else
18547     add_AT_flag (type_die, DW_AT_declaration, 1);
18548
18549   if (get_AT (type_die, DW_AT_name))
18550     add_pubtype (type, type_die);
18551
18552   return type_die;
18553 }
18554
18555 /* Generate a DIE to represent either a real live formal parameter decl or to
18556    represent just the type of some formal parameter position in some function
18557    type.
18558
18559    Note that this routine is a bit unusual because its argument may be a
18560    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18561    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18562    node.  If it's the former then this function is being called to output a
18563    DIE to represent a formal parameter object (or some inlining thereof).  If
18564    it's the latter, then this function is only being called to output a
18565    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18566    argument type of some subprogram type.
18567    If EMIT_NAME_P is true, name and source coordinate attributes
18568    are emitted.  */
18569
18570 static dw_die_ref
18571 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18572                           dw_die_ref context_die)
18573 {
18574   tree node_or_origin = node ? node : origin;
18575   tree ultimate_origin;
18576   dw_die_ref parm_die
18577     = new_die (DW_TAG_formal_parameter, context_die, node);
18578
18579   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18580     {
18581     case tcc_declaration:
18582       ultimate_origin = decl_ultimate_origin (node_or_origin);
18583       if (node || ultimate_origin)
18584         origin = ultimate_origin;
18585       if (origin != NULL)
18586         add_abstract_origin_attribute (parm_die, origin);
18587       else if (emit_name_p)
18588         add_name_and_src_coords_attributes (parm_die, node);
18589       if (origin == NULL
18590           || (! DECL_ABSTRACT (node_or_origin)
18591               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18592                                            decl_function_context
18593                                                             (node_or_origin))))
18594         {
18595           tree type = TREE_TYPE (node_or_origin);
18596           if (decl_by_reference_p (node_or_origin))
18597             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18598                                 context_die);
18599           else
18600             add_type_attribute (parm_die, type,
18601                                 TREE_READONLY (node_or_origin),
18602                                 TREE_THIS_VOLATILE (node_or_origin),
18603                                 context_die);
18604         }
18605       if (origin == NULL && DECL_ARTIFICIAL (node))
18606         add_AT_flag (parm_die, DW_AT_artificial, 1);
18607
18608       if (node && node != origin)
18609         equate_decl_number_to_die (node, parm_die);
18610       if (! DECL_ABSTRACT (node_or_origin))
18611         add_location_or_const_value_attribute (parm_die, node_or_origin,
18612                                                node == NULL, DW_AT_location);
18613
18614       break;
18615
18616     case tcc_type:
18617       /* We were called with some kind of a ..._TYPE node.  */
18618       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18619       break;
18620
18621     default:
18622       gcc_unreachable ();
18623     }
18624
18625   return parm_die;
18626 }
18627
18628 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18629    children DW_TAG_formal_parameter DIEs representing the arguments of the
18630    parameter pack.
18631
18632    PARM_PACK must be a function parameter pack.
18633    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18634    must point to the subsequent arguments of the function PACK_ARG belongs to.
18635    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18636    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18637    following the last one for which a DIE was generated.  */
18638
18639 static dw_die_ref
18640 gen_formal_parameter_pack_die  (tree parm_pack,
18641                                 tree pack_arg,
18642                                 dw_die_ref subr_die,
18643                                 tree *next_arg)
18644 {
18645   tree arg;
18646   dw_die_ref parm_pack_die;
18647
18648   gcc_assert (parm_pack
18649               && lang_hooks.function_parameter_pack_p (parm_pack)
18650               && subr_die);
18651
18652   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18653   add_src_coords_attributes (parm_pack_die, parm_pack);
18654
18655   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18656     {
18657       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18658                                                                  parm_pack))
18659         break;
18660       gen_formal_parameter_die (arg, NULL,
18661                                 false /* Don't emit name attribute.  */,
18662                                 parm_pack_die);
18663     }
18664   if (next_arg)
18665     *next_arg = arg;
18666   return parm_pack_die;
18667 }
18668
18669 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18670    at the end of an (ANSI prototyped) formal parameters list.  */
18671
18672 static void
18673 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18674 {
18675   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18676 }
18677
18678 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18679    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18680    parameters as specified in some function type specification (except for
18681    those which appear as part of a function *definition*).  */
18682
18683 static void
18684 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18685 {
18686   tree link;
18687   tree formal_type = NULL;
18688   tree first_parm_type;
18689   tree arg;
18690
18691   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18692     {
18693       arg = DECL_ARGUMENTS (function_or_method_type);
18694       function_or_method_type = TREE_TYPE (function_or_method_type);
18695     }
18696   else
18697     arg = NULL_TREE;
18698
18699   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18700
18701   /* Make our first pass over the list of formal parameter types and output a
18702      DW_TAG_formal_parameter DIE for each one.  */
18703   for (link = first_parm_type; link; )
18704     {
18705       dw_die_ref parm_die;
18706
18707       formal_type = TREE_VALUE (link);
18708       if (formal_type == void_type_node)
18709         break;
18710
18711       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18712       parm_die = gen_formal_parameter_die (formal_type, NULL,
18713                                            true /* Emit name attribute.  */,
18714                                            context_die);
18715       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18716           && link == first_parm_type)
18717         {
18718           add_AT_flag (parm_die, DW_AT_artificial, 1);
18719           if (dwarf_version >= 3 || !dwarf_strict)
18720             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18721         }
18722       else if (arg && DECL_ARTIFICIAL (arg))
18723         add_AT_flag (parm_die, DW_AT_artificial, 1);
18724
18725       link = TREE_CHAIN (link);
18726       if (arg)
18727         arg = DECL_CHAIN (arg);
18728     }
18729
18730   /* If this function type has an ellipsis, add a
18731      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18732   if (formal_type != void_type_node)
18733     gen_unspecified_parameters_die (function_or_method_type, context_die);
18734
18735   /* Make our second (and final) pass over the list of formal parameter types
18736      and output DIEs to represent those types (as necessary).  */
18737   for (link = TYPE_ARG_TYPES (function_or_method_type);
18738        link && TREE_VALUE (link);
18739        link = TREE_CHAIN (link))
18740     gen_type_die (TREE_VALUE (link), context_die);
18741 }
18742
18743 /* We want to generate the DIE for TYPE so that we can generate the
18744    die for MEMBER, which has been defined; we will need to refer back
18745    to the member declaration nested within TYPE.  If we're trying to
18746    generate minimal debug info for TYPE, processing TYPE won't do the
18747    trick; we need to attach the member declaration by hand.  */
18748
18749 static void
18750 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18751 {
18752   gen_type_die (type, context_die);
18753
18754   /* If we're trying to avoid duplicate debug info, we may not have
18755      emitted the member decl for this function.  Emit it now.  */
18756   if (TYPE_STUB_DECL (type)
18757       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18758       && ! lookup_decl_die (member))
18759     {
18760       dw_die_ref type_die;
18761       gcc_assert (!decl_ultimate_origin (member));
18762
18763       push_decl_scope (type);
18764       type_die = lookup_type_die_strip_naming_typedef (type);
18765       if (TREE_CODE (member) == FUNCTION_DECL)
18766         gen_subprogram_die (member, type_die);
18767       else if (TREE_CODE (member) == FIELD_DECL)
18768         {
18769           /* Ignore the nameless fields that are used to skip bits but handle
18770              C++ anonymous unions and structs.  */
18771           if (DECL_NAME (member) != NULL_TREE
18772               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18773               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18774             {
18775               gen_type_die (member_declared_type (member), type_die);
18776               gen_field_die (member, type_die);
18777             }
18778         }
18779       else
18780         gen_variable_die (member, NULL_TREE, type_die);
18781
18782       pop_decl_scope ();
18783     }
18784 }
18785
18786 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18787    may later generate inlined and/or out-of-line instances of.  */
18788
18789 static void
18790 dwarf2out_abstract_function (tree decl)
18791 {
18792   dw_die_ref old_die;
18793   tree save_fn;
18794   tree context;
18795   int was_abstract;
18796   htab_t old_decl_loc_table;
18797   htab_t old_cached_dw_loc_list_table;
18798   int old_call_site_count, old_tail_call_site_count;
18799   struct call_arg_loc_node *old_call_arg_locations;
18800
18801   /* Make sure we have the actual abstract inline, not a clone.  */
18802   decl = DECL_ORIGIN (decl);
18803
18804   old_die = lookup_decl_die (decl);
18805   if (old_die && get_AT (old_die, DW_AT_inline))
18806     /* We've already generated the abstract instance.  */
18807     return;
18808
18809   /* We can be called while recursively when seeing block defining inlined subroutine
18810      DIE.  Be sure to not clobber the outer location table nor use it or we would
18811      get locations in abstract instantces.  */
18812   old_decl_loc_table = decl_loc_table;
18813   decl_loc_table = NULL;
18814   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18815   cached_dw_loc_list_table = NULL;
18816   old_call_arg_locations = call_arg_locations;
18817   call_arg_locations = NULL;
18818   old_call_site_count = call_site_count;
18819   call_site_count = -1;
18820   old_tail_call_site_count = tail_call_site_count;
18821   tail_call_site_count = -1;
18822
18823   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18824      we don't get confused by DECL_ABSTRACT.  */
18825   if (debug_info_level > DINFO_LEVEL_TERSE)
18826     {
18827       context = decl_class_context (decl);
18828       if (context)
18829         gen_type_die_for_member
18830           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18831     }
18832
18833   /* Pretend we've just finished compiling this function.  */
18834   save_fn = current_function_decl;
18835   current_function_decl = decl;
18836   push_cfun (DECL_STRUCT_FUNCTION (decl));
18837
18838   was_abstract = DECL_ABSTRACT (decl);
18839   set_decl_abstract_flags (decl, 1);
18840   dwarf2out_decl (decl);
18841   if (! was_abstract)
18842     set_decl_abstract_flags (decl, 0);
18843
18844   current_function_decl = save_fn;
18845   decl_loc_table = old_decl_loc_table;
18846   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18847   call_arg_locations = old_call_arg_locations;
18848   call_site_count = old_call_site_count;
18849   tail_call_site_count = old_tail_call_site_count;
18850   pop_cfun ();
18851 }
18852
18853 /* Helper function of premark_used_types() which gets called through
18854    htab_traverse.
18855
18856    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18857    marked as unused by prune_unused_types.  */
18858
18859 static int
18860 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18861 {
18862   tree type;
18863   dw_die_ref die;
18864
18865   type = (tree) *slot;
18866   die = lookup_type_die (type);
18867   if (die != NULL)
18868     die->die_perennial_p = 1;
18869   return 1;
18870 }
18871
18872 /* Helper function of premark_types_used_by_global_vars which gets called
18873    through htab_traverse.
18874
18875    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18876    marked as unused by prune_unused_types. The DIE of the type is marked
18877    only if the global variable using the type will actually be emitted.  */
18878
18879 static int
18880 premark_types_used_by_global_vars_helper (void **slot,
18881                                           void *data ATTRIBUTE_UNUSED)
18882 {
18883   struct types_used_by_vars_entry *entry;
18884   dw_die_ref die;
18885
18886   entry = (struct types_used_by_vars_entry *) *slot;
18887   gcc_assert (entry->type != NULL
18888               && entry->var_decl != NULL);
18889   die = lookup_type_die (entry->type);
18890   if (die)
18891     {
18892       /* Ask cgraph if the global variable really is to be emitted.
18893          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18894       struct varpool_node *node = varpool_get_node (entry->var_decl);
18895       if (node && node->needed)
18896         {
18897           die->die_perennial_p = 1;
18898           /* Keep the parent DIEs as well.  */
18899           while ((die = die->die_parent) && die->die_perennial_p == 0)
18900             die->die_perennial_p = 1;
18901         }
18902     }
18903   return 1;
18904 }
18905
18906 /* Mark all members of used_types_hash as perennial.  */
18907
18908 static void
18909 premark_used_types (void)
18910 {
18911   if (cfun && cfun->used_types_hash)
18912     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18913 }
18914
18915 /* Mark all members of types_used_by_vars_entry as perennial.  */
18916
18917 static void
18918 premark_types_used_by_global_vars (void)
18919 {
18920   if (types_used_by_vars_hash)
18921     htab_traverse (types_used_by_vars_hash,
18922                    premark_types_used_by_global_vars_helper, NULL);
18923 }
18924
18925 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
18926    for CA_LOC call arg loc node.  */
18927
18928 static dw_die_ref
18929 gen_call_site_die (tree decl, dw_die_ref subr_die,
18930                    struct call_arg_loc_node *ca_loc)
18931 {
18932   dw_die_ref stmt_die = NULL, die;
18933   tree block = ca_loc->block;
18934
18935   while (block
18936          && block != DECL_INITIAL (decl)
18937          && TREE_CODE (block) == BLOCK)
18938     {
18939       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
18940         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
18941       if (stmt_die)
18942         break;
18943       block = BLOCK_SUPERCONTEXT (block);
18944     }
18945   if (stmt_die == NULL)
18946     stmt_die = subr_die;
18947   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
18948   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
18949   if (ca_loc->tail_call_p)
18950     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
18951   if (ca_loc->symbol_ref)
18952     {
18953       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
18954       if (tdie)
18955         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
18956       else
18957         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
18958     }
18959   return die;
18960 }
18961
18962 /* Generate a DIE to represent a declared function (either file-scope or
18963    block-local).  */
18964
18965 static void
18966 gen_subprogram_die (tree decl, dw_die_ref context_die)
18967 {
18968   tree origin = decl_ultimate_origin (decl);
18969   dw_die_ref subr_die;
18970   tree outer_scope;
18971   dw_die_ref old_die = lookup_decl_die (decl);
18972   int declaration = (current_function_decl != decl
18973                      || class_or_namespace_scope_p (context_die));
18974
18975   premark_used_types ();
18976
18977   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18978      started to generate the abstract instance of an inline, decided to output
18979      its containing class, and proceeded to emit the declaration of the inline
18980      from the member list for the class.  If so, DECLARATION takes priority;
18981      we'll get back to the abstract instance when done with the class.  */
18982
18983   /* The class-scope declaration DIE must be the primary DIE.  */
18984   if (origin && declaration && class_or_namespace_scope_p (context_die))
18985     {
18986       origin = NULL;
18987       gcc_assert (!old_die);
18988     }
18989
18990   /* Now that the C++ front end lazily declares artificial member fns, we
18991      might need to retrofit the declaration into its class.  */
18992   if (!declaration && !origin && !old_die
18993       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18994       && !class_or_namespace_scope_p (context_die)
18995       && debug_info_level > DINFO_LEVEL_TERSE)
18996     old_die = force_decl_die (decl);
18997
18998   if (origin != NULL)
18999     {
19000       gcc_assert (!declaration || local_scope_p (context_die));
19001
19002       /* Fixup die_parent for the abstract instance of a nested
19003          inline function.  */
19004       if (old_die && old_die->die_parent == NULL)
19005         add_child_die (context_die, old_die);
19006
19007       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19008       add_abstract_origin_attribute (subr_die, origin);
19009     }
19010   else if (old_die)
19011     {
19012       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19013       struct dwarf_file_data * file_index = lookup_filename (s.file);
19014
19015       if (!get_AT_flag (old_die, DW_AT_declaration)
19016           /* We can have a normal definition following an inline one in the
19017              case of redefinition of GNU C extern inlines.
19018              It seems reasonable to use AT_specification in this case.  */
19019           && !get_AT (old_die, DW_AT_inline))
19020         {
19021           /* Detect and ignore this case, where we are trying to output
19022              something we have already output.  */
19023           return;
19024         }
19025
19026       /* If the definition comes from the same place as the declaration,
19027          maybe use the old DIE.  We always want the DIE for this function
19028          that has the *_pc attributes to be under comp_unit_die so the
19029          debugger can find it.  We also need to do this for abstract
19030          instances of inlines, since the spec requires the out-of-line copy
19031          to have the same parent.  For local class methods, this doesn't
19032          apply; we just use the old DIE.  */
19033       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19034           && (DECL_ARTIFICIAL (decl)
19035               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19036                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19037                       == (unsigned) s.line))))
19038         {
19039           subr_die = old_die;
19040
19041           /* Clear out the declaration attribute and the formal parameters.
19042              Do not remove all children, because it is possible that this
19043              declaration die was forced using force_decl_die(). In such
19044              cases die that forced declaration die (e.g. TAG_imported_module)
19045              is one of the children that we do not want to remove.  */
19046           remove_AT (subr_die, DW_AT_declaration);
19047           remove_AT (subr_die, DW_AT_object_pointer);
19048           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19049         }
19050       else
19051         {
19052           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19053           add_AT_specification (subr_die, old_die);
19054           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19055             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19056           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19057             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19058         }
19059     }
19060   else
19061     {
19062       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19063
19064       if (TREE_PUBLIC (decl))
19065         add_AT_flag (subr_die, DW_AT_external, 1);
19066
19067       add_name_and_src_coords_attributes (subr_die, decl);
19068       if (debug_info_level > DINFO_LEVEL_TERSE)
19069         {
19070           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19071           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19072                               0, 0, context_die);
19073         }
19074
19075       add_pure_or_virtual_attribute (subr_die, decl);
19076       if (DECL_ARTIFICIAL (decl))
19077         add_AT_flag (subr_die, DW_AT_artificial, 1);
19078
19079       add_accessibility_attribute (subr_die, decl);
19080     }
19081
19082   if (declaration)
19083     {
19084       if (!old_die || !get_AT (old_die, DW_AT_inline))
19085         {
19086           add_AT_flag (subr_die, DW_AT_declaration, 1);
19087
19088           /* If this is an explicit function declaration then generate
19089              a DW_AT_explicit attribute.  */
19090           if (lang_hooks.decls.function_decl_explicit_p (decl)
19091               && (dwarf_version >= 3 || !dwarf_strict))
19092             add_AT_flag (subr_die, DW_AT_explicit, 1);
19093
19094           /* The first time we see a member function, it is in the context of
19095              the class to which it belongs.  We make sure of this by emitting
19096              the class first.  The next time is the definition, which is
19097              handled above.  The two may come from the same source text.
19098
19099              Note that force_decl_die() forces function declaration die. It is
19100              later reused to represent definition.  */
19101           equate_decl_number_to_die (decl, subr_die);
19102         }
19103     }
19104   else if (DECL_ABSTRACT (decl))
19105     {
19106       if (DECL_DECLARED_INLINE_P (decl))
19107         {
19108           if (cgraph_function_possibly_inlined_p (decl))
19109             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19110           else
19111             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19112         }
19113       else
19114         {
19115           if (cgraph_function_possibly_inlined_p (decl))
19116             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19117           else
19118             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19119         }
19120
19121       if (DECL_DECLARED_INLINE_P (decl)
19122           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19123         add_AT_flag (subr_die, DW_AT_artificial, 1);
19124
19125       equate_decl_number_to_die (decl, subr_die);
19126     }
19127   else if (!DECL_EXTERNAL (decl))
19128     {
19129       HOST_WIDE_INT cfa_fb_offset;
19130
19131       if (!old_die || !get_AT (old_die, DW_AT_inline))
19132         equate_decl_number_to_die (decl, subr_die);
19133
19134       if (!flag_reorder_blocks_and_partition)
19135         {
19136           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19137           if (fde->dw_fde_begin)
19138             {
19139               /* We have already generated the labels.  */
19140               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19141               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19142             }
19143           else
19144             {
19145               /* Create start/end labels and add the range.  */
19146               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19147               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19148                                            current_function_funcdef_no);
19149               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19150               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19151                                            current_function_funcdef_no);
19152               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19153             }
19154
19155 #if VMS_DEBUGGING_INFO
19156       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19157          Section 2.3 Prologue and Epilogue Attributes:
19158          When a breakpoint is set on entry to a function, it is generally
19159          desirable for execution to be suspended, not on the very first
19160          instruction of the function, but rather at a point after the
19161          function's frame has been set up, after any language defined local
19162          declaration processing has been completed, and before execution of
19163          the first statement of the function begins. Debuggers generally
19164          cannot properly determine where this point is.  Similarly for a
19165          breakpoint set on exit from a function. The prologue and epilogue
19166          attributes allow a compiler to communicate the location(s) to use.  */
19167
19168       {
19169         if (fde->dw_fde_vms_end_prologue)
19170           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19171             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19172
19173         if (fde->dw_fde_vms_begin_epilogue)
19174           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19175             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19176       }
19177 #endif
19178
19179           add_pubname (decl, subr_die);
19180         }
19181       else
19182         {  /* Generate pubnames entries for the split function code
19183               ranges.  */
19184           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19185
19186           if (fde->dw_fde_second_begin)
19187             {
19188               if (dwarf_version >= 3 || !dwarf_strict)
19189                 {
19190                   /* We should use ranges for non-contiguous code section 
19191                      addresses.  Use the actual code range for the initial
19192                      section, since the HOT/COLD labels might precede an 
19193                      alignment offset.  */
19194                   bool range_list_added = false;
19195                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19196                                         fde->dw_fde_end, &range_list_added);
19197                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19198                                         fde->dw_fde_second_end,
19199                                         &range_list_added);
19200                   add_pubname (decl, subr_die);
19201                   if (range_list_added)
19202                     add_ranges (NULL);
19203                 }
19204               else
19205                 {
19206                   /* There is no real support in DW2 for this .. so we make
19207                      a work-around.  First, emit the pub name for the segment
19208                      containing the function label.  Then make and emit a
19209                      simplified subprogram DIE for the second segment with the
19210                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19211                      linkage name for the second die so that gdb will find both
19212                      sections when given "b foo".  */
19213                   const char *name = NULL;
19214                   tree decl_name = DECL_NAME (decl);
19215                   dw_die_ref seg_die;
19216
19217                   /* Do the 'primary' section.   */
19218                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19219                                  fde->dw_fde_begin);
19220                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19221                                  fde->dw_fde_end);
19222                   /* Add it.   */
19223                   add_pubname (decl, subr_die);
19224
19225                   /* Build a minimal DIE for the secondary section.  */
19226                   seg_die = new_die (DW_TAG_subprogram,
19227                                      subr_die->die_parent, decl);
19228
19229                   if (TREE_PUBLIC (decl))
19230                     add_AT_flag (seg_die, DW_AT_external, 1);
19231
19232                   if (decl_name != NULL 
19233                       && IDENTIFIER_POINTER (decl_name) != NULL)
19234                     {
19235                       name = dwarf2_name (decl, 1);
19236                       if (! DECL_ARTIFICIAL (decl))
19237                         add_src_coords_attributes (seg_die, decl);
19238
19239                       add_linkage_name (seg_die, decl);
19240                     }
19241                   gcc_assert (name != NULL);
19242                   add_pure_or_virtual_attribute (seg_die, decl);
19243                   if (DECL_ARTIFICIAL (decl))
19244                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19245
19246                   name = concat ("__second_sect_of_", name, NULL); 
19247                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
19248                                  fde->dw_fde_second_begin);
19249                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
19250                                  fde->dw_fde_second_end);
19251                   add_name_attribute (seg_die, name);
19252                   add_pubname_string (name, seg_die);
19253                 }
19254             }
19255           else
19256             {
19257               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19258               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19259               add_pubname (decl, subr_die);
19260             }
19261         }
19262
19263 #ifdef MIPS_DEBUGGING_INFO
19264       /* Add a reference to the FDE for this routine.  */
19265       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19266 #endif
19267
19268       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19269
19270       /* We define the "frame base" as the function's CFA.  This is more
19271          convenient for several reasons: (1) It's stable across the prologue
19272          and epilogue, which makes it better than just a frame pointer,
19273          (2) With dwarf3, there exists a one-byte encoding that allows us
19274          to reference the .debug_frame data by proxy, but failing that,
19275          (3) We can at least reuse the code inspection and interpretation
19276          code that determines the CFA position at various points in the
19277          function.  */
19278       if (dwarf_version >= 3)
19279         {
19280           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19281           add_AT_loc (subr_die, DW_AT_frame_base, op);
19282         }
19283       else
19284         {
19285           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19286           if (list->dw_loc_next)
19287             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19288           else
19289             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19290         }
19291
19292       /* Compute a displacement from the "steady-state frame pointer" to
19293          the CFA.  The former is what all stack slots and argument slots
19294          will reference in the rtl; the later is what we've told the
19295          debugger about.  We'll need to adjust all frame_base references
19296          by this displacement.  */
19297       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19298
19299       if (cfun->static_chain_decl)
19300         add_AT_location_description (subr_die, DW_AT_static_link,
19301                  loc_list_from_tree (cfun->static_chain_decl, 2));
19302     }
19303
19304   /* Generate child dies for template paramaters.  */
19305   if (debug_info_level > DINFO_LEVEL_TERSE)
19306     gen_generic_params_dies (decl);
19307
19308   /* Now output descriptions of the arguments for this function. This gets
19309      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19310      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19311      `...' at the end of the formal parameter list.  In order to find out if
19312      there was a trailing ellipsis or not, we must instead look at the type
19313      associated with the FUNCTION_DECL.  This will be a node of type
19314      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19315      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19316      an ellipsis at the end.  */
19317
19318   /* In the case where we are describing a mere function declaration, all we
19319      need to do here (and all we *can* do here) is to describe the *types* of
19320      its formal parameters.  */
19321   if (debug_info_level <= DINFO_LEVEL_TERSE)
19322     ;
19323   else if (declaration)
19324     gen_formal_types_die (decl, subr_die);
19325   else
19326     {
19327       /* Generate DIEs to represent all known formal parameters.  */
19328       tree parm = DECL_ARGUMENTS (decl);
19329       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19330       tree generic_decl_parm = generic_decl
19331                                 ? DECL_ARGUMENTS (generic_decl)
19332                                 : NULL;
19333
19334       /* Now we want to walk the list of parameters of the function and
19335          emit their relevant DIEs.
19336
19337          We consider the case of DECL being an instance of a generic function
19338          as well as it being a normal function.
19339
19340          If DECL is an instance of a generic function we walk the
19341          parameters of the generic function declaration _and_ the parameters of
19342          DECL itself. This is useful because we want to emit specific DIEs for
19343          function parameter packs and those are declared as part of the
19344          generic function declaration. In that particular case,
19345          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19346          That DIE has children DIEs representing the set of arguments
19347          of the pack. Note that the set of pack arguments can be empty.
19348          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19349          children DIE.
19350
19351          Otherwise, we just consider the parameters of DECL.  */
19352       while (generic_decl_parm || parm)
19353         {
19354           if (generic_decl_parm
19355               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19356             gen_formal_parameter_pack_die (generic_decl_parm,
19357                                            parm, subr_die,
19358                                            &parm);
19359           else if (parm)
19360             {
19361               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19362
19363               if (parm == DECL_ARGUMENTS (decl)
19364                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19365                   && parm_die
19366                   && (dwarf_version >= 3 || !dwarf_strict))
19367                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19368
19369               parm = DECL_CHAIN (parm);
19370             }
19371
19372           if (generic_decl_parm)
19373             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19374         }
19375
19376       /* Decide whether we need an unspecified_parameters DIE at the end.
19377          There are 2 more cases to do this for: 1) the ansi ... declaration -
19378          this is detectable when the end of the arg list is not a
19379          void_type_node 2) an unprototyped function declaration (not a
19380          definition).  This just means that we have no info about the
19381          parameters at all.  */
19382       if (prototype_p (TREE_TYPE (decl)))
19383         {
19384           /* This is the prototyped case, check for....  */
19385           if (stdarg_p (TREE_TYPE (decl)))
19386             gen_unspecified_parameters_die (decl, subr_die);
19387         }
19388       else if (DECL_INITIAL (decl) == NULL_TREE)
19389         gen_unspecified_parameters_die (decl, subr_die);
19390     }
19391
19392   /* Output Dwarf info for all of the stuff within the body of the function
19393      (if it has one - it may be just a declaration).  */
19394   outer_scope = DECL_INITIAL (decl);
19395
19396   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19397      a function.  This BLOCK actually represents the outermost binding contour
19398      for the function, i.e. the contour in which the function's formal
19399      parameters and labels get declared. Curiously, it appears that the front
19400      end doesn't actually put the PARM_DECL nodes for the current function onto
19401      the BLOCK_VARS list for this outer scope, but are strung off of the
19402      DECL_ARGUMENTS list for the function instead.
19403
19404      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19405      the LABEL_DECL nodes for the function however, and we output DWARF info
19406      for those in decls_for_scope.  Just within the `outer_scope' there will be
19407      a BLOCK node representing the function's outermost pair of curly braces,
19408      and any blocks used for the base and member initializers of a C++
19409      constructor function.  */
19410   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19411     {
19412       int call_site_note_count = 0;
19413       int tail_call_site_note_count = 0;
19414
19415       /* Emit a DW_TAG_variable DIE for a named return value.  */
19416       if (DECL_NAME (DECL_RESULT (decl)))
19417         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19418
19419       current_function_has_inlines = 0;
19420       decls_for_scope (outer_scope, subr_die, 0);
19421
19422       if (call_arg_locations && !dwarf_strict)
19423         {
19424           struct call_arg_loc_node *ca_loc;
19425           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19426             {
19427               dw_die_ref die = NULL;
19428               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19429               rtx arg, next_arg;
19430
19431               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19432                    arg; arg = next_arg)
19433                 {
19434                   dw_loc_descr_ref reg, val;
19435                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19436                   dw_die_ref cdie;
19437
19438                   next_arg = XEXP (arg, 1);
19439                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19440                       && next_arg
19441                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19442                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19443                       && REGNO (XEXP (XEXP (arg, 0), 0))
19444                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19445                     next_arg = XEXP (next_arg, 1);
19446                   if (mode == VOIDmode)
19447                     {
19448                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19449                       if (mode == VOIDmode)
19450                         mode = GET_MODE (XEXP (arg, 0));
19451                     }
19452                   if (GET_MODE_CLASS (mode) != MODE_INT
19453                       || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19454                     continue;
19455                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19456                     {
19457                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19458                       tloc = XEXP (XEXP (arg, 0), 1);
19459                       continue;
19460                     }
19461                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19462                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19463                     {
19464                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19465                       tlocc = XEXP (XEXP (arg, 0), 1);
19466                       continue;
19467                     }
19468                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19469                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19470                                               VAR_INIT_STATUS_INITIALIZED);
19471                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19472                     reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19473                                                           0), 0), mode,
19474                                               VAR_INIT_STATUS_INITIALIZED);
19475                   else
19476                     continue;
19477                   if (reg == NULL)
19478                     continue;
19479                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19480                                             VAR_INIT_STATUS_INITIALIZED);
19481                   if (val == NULL)
19482                     continue;
19483                   if (die == NULL)
19484                     die = gen_call_site_die (decl, subr_die, ca_loc);
19485                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19486                                   NULL_TREE);           
19487                   add_AT_loc (cdie, DW_AT_location, reg);
19488                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19489                   if (next_arg != XEXP (arg, 1))
19490                     {
19491                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19492                                                             0), 1), VOIDmode,
19493                                                 VAR_INIT_STATUS_INITIALIZED);
19494                       if (val != NULL)
19495                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19496                     }
19497                 }
19498               if (die == NULL
19499                   && (ca_loc->symbol_ref || tloc))
19500                 die = gen_call_site_die (decl, subr_die, ca_loc);
19501               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19502                 {
19503                   dw_loc_descr_ref tval = NULL;
19504
19505                   if (tloc != NULL_RTX)
19506                     tval = mem_loc_descriptor (tloc, VOIDmode,
19507                                                VAR_INIT_STATUS_INITIALIZED);
19508                   if (tval)
19509                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19510                   else if (tlocc != NULL_RTX)
19511                     {
19512                       tval = mem_loc_descriptor (tlocc, VOIDmode,
19513                                                  VAR_INIT_STATUS_INITIALIZED);
19514                       if (tval)
19515                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19516                                     tval);
19517                     }
19518                 }
19519               if (die != NULL)
19520                 {
19521                   call_site_note_count++;
19522                   if (ca_loc->tail_call_p)
19523                     tail_call_site_note_count++;
19524                 }
19525             }
19526         }
19527       call_arg_locations = NULL;
19528       call_arg_loc_last = NULL;
19529       if (tail_call_site_count >= 0
19530           && tail_call_site_count == tail_call_site_note_count
19531           && !dwarf_strict)
19532         {
19533           if (call_site_count >= 0
19534               && call_site_count == call_site_note_count)
19535             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19536           else
19537             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19538         }
19539       call_site_count = -1;
19540       tail_call_site_count = -1;
19541     }
19542   /* Add the calling convention attribute if requested.  */
19543   add_calling_convention_attribute (subr_die, decl);
19544
19545 }
19546
19547 /* Returns a hash value for X (which really is a die_struct).  */
19548
19549 static hashval_t
19550 common_block_die_table_hash (const void *x)
19551 {
19552   const_dw_die_ref d = (const_dw_die_ref) x;
19553   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19554 }
19555
19556 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19557    as decl_id and die_parent of die_struct Y.  */
19558
19559 static int
19560 common_block_die_table_eq (const void *x, const void *y)
19561 {
19562   const_dw_die_ref d = (const_dw_die_ref) x;
19563   const_dw_die_ref e = (const_dw_die_ref) y;
19564   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19565 }
19566
19567 /* Generate a DIE to represent a declared data object.
19568    Either DECL or ORIGIN must be non-null.  */
19569
19570 static void
19571 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19572 {
19573   HOST_WIDE_INT off;
19574   tree com_decl;
19575   tree decl_or_origin = decl ? decl : origin;
19576   tree ultimate_origin;
19577   dw_die_ref var_die;
19578   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19579   dw_die_ref origin_die;
19580   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19581                       || class_or_namespace_scope_p (context_die));
19582   bool specialization_p = false;
19583
19584   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19585   if (decl || ultimate_origin)
19586     origin = ultimate_origin;
19587   com_decl = fortran_common (decl_or_origin, &off);
19588
19589   /* Symbol in common gets emitted as a child of the common block, in the form
19590      of a data member.  */
19591   if (com_decl)
19592     {
19593       dw_die_ref com_die;
19594       dw_loc_list_ref loc;
19595       die_node com_die_arg;
19596
19597       var_die = lookup_decl_die (decl_or_origin);
19598       if (var_die)
19599         {
19600           if (get_AT (var_die, DW_AT_location) == NULL)
19601             {
19602               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19603               if (loc)
19604                 {
19605                   if (off)
19606                     {
19607                       /* Optimize the common case.  */
19608                       if (single_element_loc_list_p (loc)
19609                           && loc->expr->dw_loc_opc == DW_OP_addr
19610                           && loc->expr->dw_loc_next == NULL
19611                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19612                              == SYMBOL_REF)
19613                         loc->expr->dw_loc_oprnd1.v.val_addr
19614                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19615                         else
19616                           loc_list_plus_const (loc, off);
19617                     }
19618                   add_AT_location_description (var_die, DW_AT_location, loc);
19619                   remove_AT (var_die, DW_AT_declaration);
19620                 }
19621             }
19622           return;
19623         }
19624
19625       if (common_block_die_table == NULL)
19626         common_block_die_table
19627           = htab_create_ggc (10, common_block_die_table_hash,
19628                              common_block_die_table_eq, NULL);
19629
19630       com_die_arg.decl_id = DECL_UID (com_decl);
19631       com_die_arg.die_parent = context_die;
19632       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19633       loc = loc_list_from_tree (com_decl, 2);
19634       if (com_die == NULL)
19635         {
19636           const char *cnam
19637             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19638           void **slot;
19639
19640           com_die = new_die (DW_TAG_common_block, context_die, decl);
19641           add_name_and_src_coords_attributes (com_die, com_decl);
19642           if (loc)
19643             {
19644               add_AT_location_description (com_die, DW_AT_location, loc);
19645               /* Avoid sharing the same loc descriptor between
19646                  DW_TAG_common_block and DW_TAG_variable.  */
19647               loc = loc_list_from_tree (com_decl, 2);
19648             }
19649           else if (DECL_EXTERNAL (decl))
19650             add_AT_flag (com_die, DW_AT_declaration, 1);
19651           add_pubname_string (cnam, com_die); /* ??? needed? */
19652           com_die->decl_id = DECL_UID (com_decl);
19653           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19654           *slot = (void *) com_die;
19655         }
19656       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19657         {
19658           add_AT_location_description (com_die, DW_AT_location, loc);
19659           loc = loc_list_from_tree (com_decl, 2);
19660           remove_AT (com_die, DW_AT_declaration);
19661         }
19662       var_die = new_die (DW_TAG_variable, com_die, decl);
19663       add_name_and_src_coords_attributes (var_die, decl);
19664       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19665                           TREE_THIS_VOLATILE (decl), context_die);
19666       add_AT_flag (var_die, DW_AT_external, 1);
19667       if (loc)
19668         {
19669           if (off)
19670             {
19671               /* Optimize the common case.  */
19672               if (single_element_loc_list_p (loc)
19673                   && loc->expr->dw_loc_opc == DW_OP_addr
19674                   && loc->expr->dw_loc_next == NULL
19675                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19676                 loc->expr->dw_loc_oprnd1.v.val_addr
19677                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19678               else
19679                 loc_list_plus_const (loc, off);
19680             }
19681           add_AT_location_description (var_die, DW_AT_location, loc);
19682         }
19683       else if (DECL_EXTERNAL (decl))
19684         add_AT_flag (var_die, DW_AT_declaration, 1);
19685       equate_decl_number_to_die (decl, var_die);
19686       return;
19687     }
19688
19689   /* If the compiler emitted a definition for the DECL declaration
19690      and if we already emitted a DIE for it, don't emit a second
19691      DIE for it again. Allow re-declarations of DECLs that are
19692      inside functions, though.  */
19693   if (old_die && declaration && !local_scope_p (context_die))
19694     return;
19695
19696   /* For static data members, the declaration in the class is supposed
19697      to have DW_TAG_member tag; the specification should still be
19698      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19699   if (declaration && class_scope_p (context_die))
19700     var_die = new_die (DW_TAG_member, context_die, decl);
19701   else
19702     var_die = new_die (DW_TAG_variable, context_die, decl);
19703
19704   origin_die = NULL;
19705   if (origin != NULL)
19706     origin_die = add_abstract_origin_attribute (var_die, origin);
19707
19708   /* Loop unrolling can create multiple blocks that refer to the same
19709      static variable, so we must test for the DW_AT_declaration flag.
19710
19711      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19712      copy decls and set the DECL_ABSTRACT flag on them instead of
19713      sharing them.
19714
19715      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19716
19717      ??? The declare_in_namespace support causes us to get two DIEs for one
19718      variable, both of which are declarations.  We want to avoid considering
19719      one to be a specification, so we must test that this DIE is not a
19720      declaration.  */
19721   else if (old_die && TREE_STATIC (decl) && ! declaration
19722            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19723     {
19724       /* This is a definition of a C++ class level static.  */
19725       add_AT_specification (var_die, old_die);
19726       specialization_p = true;
19727       if (DECL_NAME (decl))
19728         {
19729           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19730           struct dwarf_file_data * file_index = lookup_filename (s.file);
19731
19732           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19733             add_AT_file (var_die, DW_AT_decl_file, file_index);
19734
19735           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19736             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19737
19738           if (old_die->die_tag == DW_TAG_member)
19739             add_linkage_name (var_die, decl);
19740         }
19741     }
19742   else
19743     add_name_and_src_coords_attributes (var_die, decl);
19744
19745   if ((origin == NULL && !specialization_p)
19746       || (origin != NULL
19747           && !DECL_ABSTRACT (decl_or_origin)
19748           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19749                                        decl_function_context
19750                                                         (decl_or_origin))))
19751     {
19752       tree type = TREE_TYPE (decl_or_origin);
19753
19754       if (decl_by_reference_p (decl_or_origin))
19755         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19756       else
19757         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19758                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19759     }
19760
19761   if (origin == NULL && !specialization_p)
19762     {
19763       if (TREE_PUBLIC (decl))
19764         add_AT_flag (var_die, DW_AT_external, 1);
19765
19766       if (DECL_ARTIFICIAL (decl))
19767         add_AT_flag (var_die, DW_AT_artificial, 1);
19768
19769       add_accessibility_attribute (var_die, decl);
19770     }
19771
19772   if (declaration)
19773     add_AT_flag (var_die, DW_AT_declaration, 1);
19774
19775   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19776     equate_decl_number_to_die (decl, var_die);
19777
19778   if (! declaration
19779       && (! DECL_ABSTRACT (decl_or_origin)
19780           /* Local static vars are shared between all clones/inlines,
19781              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19782              already set.  */
19783           || (TREE_CODE (decl_or_origin) == VAR_DECL
19784               && TREE_STATIC (decl_or_origin)
19785               && DECL_RTL_SET_P (decl_or_origin)))
19786       /* When abstract origin already has DW_AT_location attribute, no need
19787          to add it again.  */
19788       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19789     {
19790       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19791           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19792         defer_location (decl_or_origin, var_die);
19793       else
19794         add_location_or_const_value_attribute (var_die, decl_or_origin,
19795                                                decl == NULL, DW_AT_location);
19796       add_pubname (decl_or_origin, var_die);
19797     }
19798   else
19799     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19800 }
19801
19802 /* Generate a DIE to represent a named constant.  */
19803
19804 static void
19805 gen_const_die (tree decl, dw_die_ref context_die)
19806 {
19807   dw_die_ref const_die;
19808   tree type = TREE_TYPE (decl);
19809
19810   const_die = new_die (DW_TAG_constant, context_die, decl);
19811   add_name_and_src_coords_attributes (const_die, decl);
19812   add_type_attribute (const_die, type, 1, 0, context_die);
19813   if (TREE_PUBLIC (decl))
19814     add_AT_flag (const_die, DW_AT_external, 1);
19815   if (DECL_ARTIFICIAL (decl))
19816     add_AT_flag (const_die, DW_AT_artificial, 1);
19817   tree_add_const_value_attribute_for_decl (const_die, decl);
19818 }
19819
19820 /* Generate a DIE to represent a label identifier.  */
19821
19822 static void
19823 gen_label_die (tree decl, dw_die_ref context_die)
19824 {
19825   tree origin = decl_ultimate_origin (decl);
19826   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19827   rtx insn;
19828   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19829
19830   if (origin != NULL)
19831     add_abstract_origin_attribute (lbl_die, origin);
19832   else
19833     add_name_and_src_coords_attributes (lbl_die, decl);
19834
19835   if (DECL_ABSTRACT (decl))
19836     equate_decl_number_to_die (decl, lbl_die);
19837   else
19838     {
19839       insn = DECL_RTL_IF_SET (decl);
19840
19841       /* Deleted labels are programmer specified labels which have been
19842          eliminated because of various optimizations.  We still emit them
19843          here so that it is possible to put breakpoints on them.  */
19844       if (insn
19845           && (LABEL_P (insn)
19846               || ((NOTE_P (insn)
19847                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19848         {
19849           /* When optimization is enabled (via -O) some parts of the compiler
19850              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19851              represent source-level labels which were explicitly declared by
19852              the user.  This really shouldn't be happening though, so catch
19853              it if it ever does happen.  */
19854           gcc_assert (!INSN_DELETED_P (insn));
19855
19856           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19857           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19858         }
19859     }
19860 }
19861
19862 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19863    attributes to the DIE for a block STMT, to describe where the inlined
19864    function was called from.  This is similar to add_src_coords_attributes.  */
19865
19866 static inline void
19867 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19868 {
19869   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19870
19871   if (dwarf_version >= 3 || !dwarf_strict)
19872     {
19873       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19874       add_AT_unsigned (die, DW_AT_call_line, s.line);
19875     }
19876 }
19877
19878
19879 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19880    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19881
19882 static inline void
19883 add_high_low_attributes (tree stmt, dw_die_ref die)
19884 {
19885   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19886
19887   if (BLOCK_FRAGMENT_CHAIN (stmt)
19888       && (dwarf_version >= 3 || !dwarf_strict))
19889     {
19890       tree chain;
19891
19892       if (inlined_function_outer_scope_p (stmt))
19893         {
19894           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19895                                        BLOCK_NUMBER (stmt));
19896           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19897         }
19898
19899       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19900
19901       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19902       do
19903         {
19904           add_ranges (chain);
19905           chain = BLOCK_FRAGMENT_CHAIN (chain);
19906         }
19907       while (chain);
19908       add_ranges (NULL);
19909     }
19910   else
19911     {
19912       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19913                                    BLOCK_NUMBER (stmt));
19914       add_AT_lbl_id (die, DW_AT_low_pc, label);
19915       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19916                                    BLOCK_NUMBER (stmt));
19917       add_AT_lbl_id (die, DW_AT_high_pc, label);
19918     }
19919 }
19920
19921 /* Generate a DIE for a lexical block.  */
19922
19923 static void
19924 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19925 {
19926   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19927
19928   if (call_arg_locations)
19929     {
19930       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
19931         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
19932                                BLOCK_NUMBER (stmt) + 1);
19933       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
19934     }
19935
19936   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19937     add_high_low_attributes (stmt, stmt_die);
19938
19939   decls_for_scope (stmt, stmt_die, depth);
19940 }
19941
19942 /* Generate a DIE for an inlined subprogram.  */
19943
19944 static void
19945 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19946 {
19947   tree decl;
19948
19949   /* The instance of function that is effectively being inlined shall not
19950      be abstract.  */
19951   gcc_assert (! BLOCK_ABSTRACT (stmt));
19952
19953   decl = block_ultimate_origin (stmt);
19954
19955   /* Emit info for the abstract instance first, if we haven't yet.  We
19956      must emit this even if the block is abstract, otherwise when we
19957      emit the block below (or elsewhere), we may end up trying to emit
19958      a die whose origin die hasn't been emitted, and crashing.  */
19959   dwarf2out_abstract_function (decl);
19960
19961   if (! BLOCK_ABSTRACT (stmt))
19962     {
19963       dw_die_ref subr_die
19964         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19965
19966       if (call_arg_locations)
19967         {
19968           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
19969             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
19970                                    BLOCK_NUMBER (stmt) + 1);
19971           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
19972         }
19973       add_abstract_origin_attribute (subr_die, decl);
19974       if (TREE_ASM_WRITTEN (stmt))
19975         add_high_low_attributes (stmt, subr_die);
19976       add_call_src_coords_attributes (stmt, subr_die);
19977
19978       decls_for_scope (stmt, subr_die, depth);
19979       current_function_has_inlines = 1;
19980     }
19981 }
19982
19983 /* Generate a DIE for a field in a record, or structure.  */
19984
19985 static void
19986 gen_field_die (tree decl, dw_die_ref context_die)
19987 {
19988   dw_die_ref decl_die;
19989
19990   if (TREE_TYPE (decl) == error_mark_node)
19991     return;
19992
19993   decl_die = new_die (DW_TAG_member, context_die, decl);
19994   add_name_and_src_coords_attributes (decl_die, decl);
19995   add_type_attribute (decl_die, member_declared_type (decl),
19996                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19997                       context_die);
19998
19999   if (DECL_BIT_FIELD_TYPE (decl))
20000     {
20001       add_byte_size_attribute (decl_die, decl);
20002       add_bit_size_attribute (decl_die, decl);
20003       add_bit_offset_attribute (decl_die, decl);
20004     }
20005
20006   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20007     add_data_member_location_attribute (decl_die, decl);
20008
20009   if (DECL_ARTIFICIAL (decl))
20010     add_AT_flag (decl_die, DW_AT_artificial, 1);
20011
20012   add_accessibility_attribute (decl_die, decl);
20013
20014   /* Equate decl number to die, so that we can look up this decl later on.  */
20015   equate_decl_number_to_die (decl, decl_die);
20016 }
20017
20018 #if 0
20019 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20020    Use modified_type_die instead.
20021    We keep this code here just in case these types of DIEs may be needed to
20022    represent certain things in other languages (e.g. Pascal) someday.  */
20023
20024 static void
20025 gen_pointer_type_die (tree type, dw_die_ref context_die)
20026 {
20027   dw_die_ref ptr_die
20028     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20029
20030   equate_type_number_to_die (type, ptr_die);
20031   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20032   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20033 }
20034
20035 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20036    Use modified_type_die instead.
20037    We keep this code here just in case these types of DIEs may be needed to
20038    represent certain things in other languages (e.g. Pascal) someday.  */
20039
20040 static void
20041 gen_reference_type_die (tree type, dw_die_ref context_die)
20042 {
20043   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20044
20045   if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
20046     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20047   else
20048     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20049
20050   equate_type_number_to_die (type, ref_die);
20051   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20052   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20053 }
20054 #endif
20055
20056 /* Generate a DIE for a pointer to a member type.  */
20057
20058 static void
20059 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20060 {
20061   dw_die_ref ptr_die
20062     = new_die (DW_TAG_ptr_to_member_type,
20063                scope_die_for (type, context_die), type);
20064
20065   equate_type_number_to_die (type, ptr_die);
20066   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20067                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20068   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20069 }
20070
20071 /* Generate the DIE for the compilation unit.  */
20072
20073 static dw_die_ref
20074 gen_compile_unit_die (const char *filename)
20075 {
20076   dw_die_ref die;
20077   char producer[250];
20078   const char *language_string = lang_hooks.name;
20079   int language;
20080
20081   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20082
20083   if (filename)
20084     {
20085       add_name_attribute (die, filename);
20086       /* Don't add cwd for <built-in>.  */
20087       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20088         add_comp_dir_attribute (die);
20089     }
20090
20091   sprintf (producer, "%s %s", language_string, version_string);
20092
20093 #ifdef MIPS_DEBUGGING_INFO
20094   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20095      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20096      not appear in the producer string, the debugger reaches the conclusion
20097      that the object file is stripped and has no debugging information.
20098      To get the MIPS/SGI debugger to believe that there is debugging
20099      information in the object file, we add a -g to the producer string.  */
20100   if (debug_info_level > DINFO_LEVEL_TERSE)
20101     strcat (producer, " -g");
20102 #endif
20103
20104   add_AT_string (die, DW_AT_producer, producer);
20105
20106   /* If our producer is LTO try to figure out a common language to use
20107      from the global list of translation units.  */
20108   if (strcmp (language_string, "GNU GIMPLE") == 0)
20109     {
20110       unsigned i;
20111       tree t;
20112       const char *common_lang = NULL;
20113
20114       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20115         {
20116           if (!TRANSLATION_UNIT_LANGUAGE (t))
20117             continue;
20118           if (!common_lang)
20119             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20120           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20121             ;
20122           else if (strncmp (common_lang, "GNU C", 5) == 0
20123                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20124             /* Mixing C and C++ is ok, use C++ in that case.  */
20125             common_lang = "GNU C++";
20126           else
20127             {
20128               /* Fall back to C.  */
20129               common_lang = NULL;
20130               break;
20131             }
20132         }
20133
20134       if (common_lang)
20135         language_string = common_lang;
20136     }
20137
20138   language = DW_LANG_C89;
20139   if (strcmp (language_string, "GNU C++") == 0)
20140     language = DW_LANG_C_plus_plus;
20141   else if (strcmp (language_string, "GNU F77") == 0)
20142     language = DW_LANG_Fortran77;
20143   else if (strcmp (language_string, "GNU Pascal") == 0)
20144     language = DW_LANG_Pascal83;
20145   else if (dwarf_version >= 3 || !dwarf_strict)
20146     {
20147       if (strcmp (language_string, "GNU Ada") == 0)
20148         language = DW_LANG_Ada95;
20149       else if (strcmp (language_string, "GNU Fortran") == 0)
20150         language = DW_LANG_Fortran95;
20151       else if (strcmp (language_string, "GNU Java") == 0)
20152         language = DW_LANG_Java;
20153       else if (strcmp (language_string, "GNU Objective-C") == 0)
20154         language = DW_LANG_ObjC;
20155       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20156         language = DW_LANG_ObjC_plus_plus;
20157     }
20158
20159   add_AT_unsigned (die, DW_AT_language, language);
20160
20161   switch (language)
20162     {
20163     case DW_LANG_Fortran77:
20164     case DW_LANG_Fortran90:
20165     case DW_LANG_Fortran95:
20166       /* Fortran has case insensitive identifiers and the front-end
20167          lowercases everything.  */
20168       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20169       break;
20170     default:
20171       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20172       break;
20173     }
20174   return die;
20175 }
20176
20177 /* Generate the DIE for a base class.  */
20178
20179 static void
20180 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20181 {
20182   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20183
20184   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20185   add_data_member_location_attribute (die, binfo);
20186
20187   if (BINFO_VIRTUAL_P (binfo))
20188     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20189
20190   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20191      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20192      the default has always been DW_ACCESS_private.  */
20193   if (access == access_public_node)
20194     {
20195       if (dwarf_version == 2
20196           || context_die->die_tag == DW_TAG_class_type)
20197       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20198     }
20199   else if (access == access_protected_node)
20200     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20201   else if (dwarf_version > 2
20202            && context_die->die_tag != DW_TAG_class_type)
20203     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20204 }
20205
20206 /* Generate a DIE for a class member.  */
20207
20208 static void
20209 gen_member_die (tree type, dw_die_ref context_die)
20210 {
20211   tree member;
20212   tree binfo = TYPE_BINFO (type);
20213   dw_die_ref child;
20214
20215   /* If this is not an incomplete type, output descriptions of each of its
20216      members. Note that as we output the DIEs necessary to represent the
20217      members of this record or union type, we will also be trying to output
20218      DIEs to represent the *types* of those members. However the `type'
20219      function (above) will specifically avoid generating type DIEs for member
20220      types *within* the list of member DIEs for this (containing) type except
20221      for those types (of members) which are explicitly marked as also being
20222      members of this (containing) type themselves.  The g++ front- end can
20223      force any given type to be treated as a member of some other (containing)
20224      type by setting the TYPE_CONTEXT of the given (member) type to point to
20225      the TREE node representing the appropriate (containing) type.  */
20226
20227   /* First output info about the base classes.  */
20228   if (binfo)
20229     {
20230       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20231       int i;
20232       tree base;
20233
20234       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20235         gen_inheritance_die (base,
20236                              (accesses ? VEC_index (tree, accesses, i)
20237                               : access_public_node), context_die);
20238     }
20239
20240   /* Now output info about the data members and type members.  */
20241   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20242     {
20243       /* If we thought we were generating minimal debug info for TYPE
20244          and then changed our minds, some of the member declarations
20245          may have already been defined.  Don't define them again, but
20246          do put them in the right order.  */
20247
20248       child = lookup_decl_die (member);
20249       if (child)
20250         splice_child_die (context_die, child);
20251       else
20252         gen_decl_die (member, NULL, context_die);
20253     }
20254
20255   /* Now output info about the function members (if any).  */
20256   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20257     {
20258       /* Don't include clones in the member list.  */
20259       if (DECL_ABSTRACT_ORIGIN (member))
20260         continue;
20261
20262       child = lookup_decl_die (member);
20263       if (child)
20264         splice_child_die (context_die, child);
20265       else
20266         gen_decl_die (member, NULL, context_die);
20267     }
20268 }
20269
20270 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20271    is set, we pretend that the type was never defined, so we only get the
20272    member DIEs needed by later specification DIEs.  */
20273
20274 static void
20275 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20276                                 enum debug_info_usage usage)
20277 {
20278   dw_die_ref type_die = lookup_type_die (type);
20279   dw_die_ref scope_die = 0;
20280   int nested = 0;
20281   int complete = (TYPE_SIZE (type)
20282                   && (! TYPE_STUB_DECL (type)
20283                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20284   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20285   complete = complete && should_emit_struct_debug (type, usage);
20286
20287   if (type_die && ! complete)
20288     return;
20289
20290   if (TYPE_CONTEXT (type) != NULL_TREE
20291       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20292           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20293     nested = 1;
20294
20295   scope_die = scope_die_for (type, context_die);
20296
20297   if (! type_die || (nested && is_cu_die (scope_die)))
20298     /* First occurrence of type or toplevel definition of nested class.  */
20299     {
20300       dw_die_ref old_die = type_die;
20301
20302       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20303                           ? record_type_tag (type) : DW_TAG_union_type,
20304                           scope_die, type);
20305       equate_type_number_to_die (type, type_die);
20306       if (old_die)
20307         add_AT_specification (type_die, old_die);
20308       else
20309         add_name_attribute (type_die, type_tag (type));
20310     }
20311   else
20312     remove_AT (type_die, DW_AT_declaration);
20313
20314   /* Generate child dies for template paramaters.  */
20315   if (debug_info_level > DINFO_LEVEL_TERSE
20316       && COMPLETE_TYPE_P (type))
20317     schedule_generic_params_dies_gen (type);
20318
20319   /* If this type has been completed, then give it a byte_size attribute and
20320      then give a list of members.  */
20321   if (complete && !ns_decl)
20322     {
20323       /* Prevent infinite recursion in cases where the type of some member of
20324          this type is expressed in terms of this type itself.  */
20325       TREE_ASM_WRITTEN (type) = 1;
20326       add_byte_size_attribute (type_die, type);
20327       if (TYPE_STUB_DECL (type) != NULL_TREE)
20328         {
20329           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20330           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20331         }
20332
20333       /* If the first reference to this type was as the return type of an
20334          inline function, then it may not have a parent.  Fix this now.  */
20335       if (type_die->die_parent == NULL)
20336         add_child_die (scope_die, type_die);
20337
20338       push_decl_scope (type);
20339       gen_member_die (type, type_die);
20340       pop_decl_scope ();
20341
20342       /* GNU extension: Record what type our vtable lives in.  */
20343       if (TYPE_VFIELD (type))
20344         {
20345           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20346
20347           gen_type_die (vtype, context_die);
20348           add_AT_die_ref (type_die, DW_AT_containing_type,
20349                           lookup_type_die (vtype));
20350         }
20351     }
20352   else
20353     {
20354       add_AT_flag (type_die, DW_AT_declaration, 1);
20355
20356       /* We don't need to do this for function-local types.  */
20357       if (TYPE_STUB_DECL (type)
20358           && ! decl_function_context (TYPE_STUB_DECL (type)))
20359         VEC_safe_push (tree, gc, incomplete_types, type);
20360     }
20361
20362   if (get_AT (type_die, DW_AT_name))
20363     add_pubtype (type, type_die);
20364 }
20365
20366 /* Generate a DIE for a subroutine _type_.  */
20367
20368 static void
20369 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20370 {
20371   tree return_type = TREE_TYPE (type);
20372   dw_die_ref subr_die
20373     = new_die (DW_TAG_subroutine_type,
20374                scope_die_for (type, context_die), type);
20375
20376   equate_type_number_to_die (type, subr_die);
20377   add_prototyped_attribute (subr_die, type);
20378   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20379   gen_formal_types_die (type, subr_die);
20380
20381   if (get_AT (subr_die, DW_AT_name))
20382     add_pubtype (type, subr_die);
20383 }
20384
20385 /* Generate a DIE for a type definition.  */
20386
20387 static void
20388 gen_typedef_die (tree decl, dw_die_ref context_die)
20389 {
20390   dw_die_ref type_die;
20391   tree origin;
20392
20393   if (TREE_ASM_WRITTEN (decl))
20394     return;
20395
20396   TREE_ASM_WRITTEN (decl) = 1;
20397   type_die = new_die (DW_TAG_typedef, context_die, decl);
20398   origin = decl_ultimate_origin (decl);
20399   if (origin != NULL)
20400     add_abstract_origin_attribute (type_die, origin);
20401   else
20402     {
20403       tree type;
20404
20405       add_name_and_src_coords_attributes (type_die, decl);
20406       if (DECL_ORIGINAL_TYPE (decl))
20407         {
20408           type = DECL_ORIGINAL_TYPE (decl);
20409
20410           gcc_assert (type != TREE_TYPE (decl));
20411           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20412         }
20413       else
20414         {
20415           type = TREE_TYPE (decl);
20416
20417           if (is_naming_typedef_decl (TYPE_NAME (type)))
20418             {
20419               /* Here, we are in the case of decl being a typedef naming
20420                  an anonymous type, e.g:
20421                      typedef struct {...} foo;
20422                  In that case TREE_TYPE (decl) is not a typedef variant
20423                  type and TYPE_NAME of the anonymous type is set to the
20424                  TYPE_DECL of the typedef. This construct is emitted by
20425                  the C++ FE.
20426
20427                  TYPE is the anonymous struct named by the typedef
20428                  DECL. As we need the DW_AT_type attribute of the
20429                  DW_TAG_typedef to point to the DIE of TYPE, let's
20430                  generate that DIE right away. add_type_attribute
20431                  called below will then pick (via lookup_type_die) that
20432                  anonymous struct DIE.  */
20433               if (!TREE_ASM_WRITTEN (type))
20434                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20435
20436               /* This is a GNU Extension.  We are adding a
20437                  DW_AT_linkage_name attribute to the DIE of the
20438                  anonymous struct TYPE.  The value of that attribute
20439                  is the name of the typedef decl naming the anonymous
20440                  struct.  This greatly eases the work of consumers of
20441                  this debug info.  */
20442               add_linkage_attr (lookup_type_die (type), decl);
20443             }
20444         }
20445
20446       add_type_attribute (type_die, type, TREE_READONLY (decl),
20447                           TREE_THIS_VOLATILE (decl), context_die);
20448
20449       if (is_naming_typedef_decl (decl))
20450         /* We want that all subsequent calls to lookup_type_die with
20451            TYPE in argument yield the DW_TAG_typedef we have just
20452            created.  */
20453         equate_type_number_to_die (type, type_die);
20454
20455       add_accessibility_attribute (type_die, decl);
20456     }
20457
20458   if (DECL_ABSTRACT (decl))
20459     equate_decl_number_to_die (decl, type_die);
20460
20461   if (get_AT (type_die, DW_AT_name))
20462     add_pubtype (decl, type_die);
20463 }
20464
20465 /* Generate a DIE for a struct, class, enum or union type.  */
20466
20467 static void
20468 gen_tagged_type_die (tree type,
20469                      dw_die_ref context_die,
20470                      enum debug_info_usage usage)
20471 {
20472   int need_pop;
20473
20474   if (type == NULL_TREE
20475       || !is_tagged_type (type))
20476     return;
20477
20478   /* If this is a nested type whose containing class hasn't been written
20479      out yet, writing it out will cover this one, too.  This does not apply
20480      to instantiations of member class templates; they need to be added to
20481      the containing class as they are generated.  FIXME: This hurts the
20482      idea of combining type decls from multiple TUs, since we can't predict
20483      what set of template instantiations we'll get.  */
20484   if (TYPE_CONTEXT (type)
20485       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20486       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20487     {
20488       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20489
20490       if (TREE_ASM_WRITTEN (type))
20491         return;
20492
20493       /* If that failed, attach ourselves to the stub.  */
20494       push_decl_scope (TYPE_CONTEXT (type));
20495       context_die = lookup_type_die (TYPE_CONTEXT (type));
20496       need_pop = 1;
20497     }
20498   else if (TYPE_CONTEXT (type) != NULL_TREE
20499            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20500     {
20501       /* If this type is local to a function that hasn't been written
20502          out yet, use a NULL context for now; it will be fixed up in
20503          decls_for_scope.  */
20504       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20505       /* A declaration DIE doesn't count; nested types need to go in the
20506          specification.  */
20507       if (context_die && is_declaration_die (context_die))
20508         context_die = NULL;
20509       need_pop = 0;
20510     }
20511   else
20512     {
20513       context_die = declare_in_namespace (type, context_die);
20514       need_pop = 0;
20515     }
20516
20517   if (TREE_CODE (type) == ENUMERAL_TYPE)
20518     {
20519       /* This might have been written out by the call to
20520          declare_in_namespace.  */
20521       if (!TREE_ASM_WRITTEN (type))
20522         gen_enumeration_type_die (type, context_die);
20523     }
20524   else
20525     gen_struct_or_union_type_die (type, context_die, usage);
20526
20527   if (need_pop)
20528     pop_decl_scope ();
20529
20530   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20531      it up if it is ever completed.  gen_*_type_die will set it for us
20532      when appropriate.  */
20533 }
20534
20535 /* Generate a type description DIE.  */
20536
20537 static void
20538 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20539                          enum debug_info_usage usage)
20540 {
20541   struct array_descr_info info;
20542
20543   if (type == NULL_TREE || type == error_mark_node)
20544     return;
20545
20546   if (TYPE_NAME (type) != NULL_TREE
20547       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20548       && is_redundant_typedef (TYPE_NAME (type))
20549       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20550     /* The DECL of this type is a typedef we don't want to emit debug
20551        info for but we want debug info for its underlying typedef.
20552        This can happen for e.g, the injected-class-name of a C++
20553        type.  */
20554     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20555
20556   /* If TYPE is a typedef type variant, let's generate debug info
20557      for the parent typedef which TYPE is a type of.  */
20558   if (typedef_variant_p (type))
20559     {
20560       if (TREE_ASM_WRITTEN (type))
20561         return;
20562
20563       /* Prevent broken recursion; we can't hand off to the same type.  */
20564       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20565
20566       /* Use the DIE of the containing namespace as the parent DIE of
20567          the type description DIE we want to generate.  */
20568       if (DECL_CONTEXT (TYPE_NAME (type))
20569           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20570         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20571
20572       TREE_ASM_WRITTEN (type) = 1;
20573
20574       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20575       return;
20576     }
20577
20578   /* If type is an anonymous tagged type named by a typedef, let's
20579      generate debug info for the typedef.  */
20580   if (is_naming_typedef_decl (TYPE_NAME (type)))
20581     {
20582       /* Use the DIE of the containing namespace as the parent DIE of
20583          the type description DIE we want to generate.  */
20584       if (DECL_CONTEXT (TYPE_NAME (type))
20585           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20586         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20587       
20588       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20589       return;
20590     }
20591
20592   /* If this is an array type with hidden descriptor, handle it first.  */
20593   if (!TREE_ASM_WRITTEN (type)
20594       && lang_hooks.types.get_array_descr_info
20595       && lang_hooks.types.get_array_descr_info (type, &info)
20596       && (dwarf_version >= 3 || !dwarf_strict))
20597     {
20598       gen_descr_array_type_die (type, &info, context_die);
20599       TREE_ASM_WRITTEN (type) = 1;
20600       return;
20601     }
20602
20603   /* We are going to output a DIE to represent the unqualified version
20604      of this type (i.e. without any const or volatile qualifiers) so
20605      get the main variant (i.e. the unqualified version) of this type
20606      now.  (Vectors are special because the debugging info is in the
20607      cloned type itself).  */
20608   if (TREE_CODE (type) != VECTOR_TYPE)
20609     type = type_main_variant (type);
20610
20611   if (TREE_ASM_WRITTEN (type))
20612     return;
20613
20614   switch (TREE_CODE (type))
20615     {
20616     case ERROR_MARK:
20617       break;
20618
20619     case POINTER_TYPE:
20620     case REFERENCE_TYPE:
20621       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20622          ensures that the gen_type_die recursion will terminate even if the
20623          type is recursive.  Recursive types are possible in Ada.  */
20624       /* ??? We could perhaps do this for all types before the switch
20625          statement.  */
20626       TREE_ASM_WRITTEN (type) = 1;
20627
20628       /* For these types, all that is required is that we output a DIE (or a
20629          set of DIEs) to represent the "basis" type.  */
20630       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20631                                 DINFO_USAGE_IND_USE);
20632       break;
20633
20634     case OFFSET_TYPE:
20635       /* This code is used for C++ pointer-to-data-member types.
20636          Output a description of the relevant class type.  */
20637       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20638                                         DINFO_USAGE_IND_USE);
20639
20640       /* Output a description of the type of the object pointed to.  */
20641       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20642                                         DINFO_USAGE_IND_USE);
20643
20644       /* Now output a DIE to represent this pointer-to-data-member type
20645          itself.  */
20646       gen_ptr_to_mbr_type_die (type, context_die);
20647       break;
20648
20649     case FUNCTION_TYPE:
20650       /* Force out return type (in case it wasn't forced out already).  */
20651       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20652                                         DINFO_USAGE_DIR_USE);
20653       gen_subroutine_type_die (type, context_die);
20654       break;
20655
20656     case METHOD_TYPE:
20657       /* Force out return type (in case it wasn't forced out already).  */
20658       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20659                                         DINFO_USAGE_DIR_USE);
20660       gen_subroutine_type_die (type, context_die);
20661       break;
20662
20663     case ARRAY_TYPE:
20664       gen_array_type_die (type, context_die);
20665       break;
20666
20667     case VECTOR_TYPE:
20668       gen_array_type_die (type, context_die);
20669       break;
20670
20671     case ENUMERAL_TYPE:
20672     case RECORD_TYPE:
20673     case UNION_TYPE:
20674     case QUAL_UNION_TYPE:
20675       gen_tagged_type_die (type, context_die, usage);
20676       return;
20677
20678     case VOID_TYPE:
20679     case INTEGER_TYPE:
20680     case REAL_TYPE:
20681     case FIXED_POINT_TYPE:
20682     case COMPLEX_TYPE:
20683     case BOOLEAN_TYPE:
20684       /* No DIEs needed for fundamental types.  */
20685       break;
20686
20687     case NULLPTR_TYPE:
20688     case LANG_TYPE:
20689       /* Just use DW_TAG_unspecified_type.  */
20690       {
20691         dw_die_ref type_die = lookup_type_die (type);
20692         if (type_die == NULL)
20693           {
20694             tree name = TYPE_NAME (type);
20695             if (TREE_CODE (name) == TYPE_DECL)
20696               name = DECL_NAME (name);
20697             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20698             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20699             equate_type_number_to_die (type, type_die);
20700           }
20701       }
20702       break;
20703
20704     default:
20705       gcc_unreachable ();
20706     }
20707
20708   TREE_ASM_WRITTEN (type) = 1;
20709 }
20710
20711 static void
20712 gen_type_die (tree type, dw_die_ref context_die)
20713 {
20714   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20715 }
20716
20717 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20718    things which are local to the given block.  */
20719
20720 static void
20721 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20722 {
20723   int must_output_die = 0;
20724   bool inlined_func;
20725
20726   /* Ignore blocks that are NULL.  */
20727   if (stmt == NULL_TREE)
20728     return;
20729
20730   inlined_func = inlined_function_outer_scope_p (stmt);
20731
20732   /* If the block is one fragment of a non-contiguous block, do not
20733      process the variables, since they will have been done by the
20734      origin block.  Do process subblocks.  */
20735   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20736     {
20737       tree sub;
20738
20739       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20740         gen_block_die (sub, context_die, depth + 1);
20741
20742       return;
20743     }
20744
20745   /* Determine if we need to output any Dwarf DIEs at all to represent this
20746      block.  */
20747   if (inlined_func)
20748     /* The outer scopes for inlinings *must* always be represented.  We
20749        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20750     must_output_die = 1;
20751   else
20752     {
20753       /* Determine if this block directly contains any "significant"
20754          local declarations which we will need to output DIEs for.  */
20755       if (debug_info_level > DINFO_LEVEL_TERSE)
20756         /* We are not in terse mode so *any* local declaration counts
20757            as being a "significant" one.  */
20758         must_output_die = ((BLOCK_VARS (stmt) != NULL
20759                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20760                            && (TREE_USED (stmt)
20761                                || TREE_ASM_WRITTEN (stmt)
20762                                || BLOCK_ABSTRACT (stmt)));
20763       else if ((TREE_USED (stmt)
20764                 || TREE_ASM_WRITTEN (stmt)
20765                 || BLOCK_ABSTRACT (stmt))
20766                && !dwarf2out_ignore_block (stmt))
20767         must_output_die = 1;
20768     }
20769
20770   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20771      DIE for any block which contains no significant local declarations at
20772      all.  Rather, in such cases we just call `decls_for_scope' so that any
20773      needed Dwarf info for any sub-blocks will get properly generated. Note
20774      that in terse mode, our definition of what constitutes a "significant"
20775      local declaration gets restricted to include only inlined function
20776      instances and local (nested) function definitions.  */
20777   if (must_output_die)
20778     {
20779       if (inlined_func)
20780         {
20781           /* If STMT block is abstract, that means we have been called
20782              indirectly from dwarf2out_abstract_function.
20783              That function rightfully marks the descendent blocks (of
20784              the abstract function it is dealing with) as being abstract,
20785              precisely to prevent us from emitting any
20786              DW_TAG_inlined_subroutine DIE as a descendent
20787              of an abstract function instance. So in that case, we should
20788              not call gen_inlined_subroutine_die.
20789
20790              Later though, when cgraph asks dwarf2out to emit info
20791              for the concrete instance of the function decl into which
20792              the concrete instance of STMT got inlined, the later will lead
20793              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20794           if (! BLOCK_ABSTRACT (stmt))
20795             gen_inlined_subroutine_die (stmt, context_die, depth);
20796         }
20797       else
20798         gen_lexical_block_die (stmt, context_die, depth);
20799     }
20800   else
20801     decls_for_scope (stmt, context_die, depth);
20802 }
20803
20804 /* Process variable DECL (or variable with origin ORIGIN) within
20805    block STMT and add it to CONTEXT_DIE.  */
20806 static void
20807 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20808 {
20809   dw_die_ref die;
20810   tree decl_or_origin = decl ? decl : origin;
20811
20812   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20813     die = lookup_decl_die (decl_or_origin);
20814   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20815            && TYPE_DECL_IS_STUB (decl_or_origin))
20816     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20817   else
20818     die = NULL;
20819
20820   if (die != NULL && die->die_parent == NULL)
20821     add_child_die (context_die, die);
20822   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20823     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20824                                          stmt, context_die);
20825   else
20826     gen_decl_die (decl, origin, context_die);
20827 }
20828
20829 /* Generate all of the decls declared within a given scope and (recursively)
20830    all of its sub-blocks.  */
20831
20832 static void
20833 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20834 {
20835   tree decl;
20836   unsigned int i;
20837   tree subblocks;
20838
20839   /* Ignore NULL blocks.  */
20840   if (stmt == NULL_TREE)
20841     return;
20842
20843   /* Output the DIEs to represent all of the data objects and typedefs
20844      declared directly within this block but not within any nested
20845      sub-blocks.  Also, nested function and tag DIEs have been
20846      generated with a parent of NULL; fix that up now.  */
20847   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20848     process_scope_var (stmt, decl, NULL_TREE, context_die);
20849   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20850     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20851                        context_die);
20852
20853   /* If we're at -g1, we're not interested in subblocks.  */
20854   if (debug_info_level <= DINFO_LEVEL_TERSE)
20855     return;
20856
20857   /* Output the DIEs to represent all sub-blocks (and the items declared
20858      therein) of this block.  */
20859   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20860        subblocks != NULL;
20861        subblocks = BLOCK_CHAIN (subblocks))
20862     gen_block_die (subblocks, context_die, depth + 1);
20863 }
20864
20865 /* Is this a typedef we can avoid emitting?  */
20866
20867 static inline int
20868 is_redundant_typedef (const_tree decl)
20869 {
20870   if (TYPE_DECL_IS_STUB (decl))
20871     return 1;
20872
20873   if (DECL_ARTIFICIAL (decl)
20874       && DECL_CONTEXT (decl)
20875       && is_tagged_type (DECL_CONTEXT (decl))
20876       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20877       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20878     /* Also ignore the artificial member typedef for the class name.  */
20879     return 1;
20880
20881   return 0;
20882 }
20883
20884 /* Return TRUE if TYPE is a typedef that names a type for linkage
20885    purposes. This kind of typedefs is produced by the C++ FE for
20886    constructs like:
20887
20888    typedef struct {...} foo;
20889
20890    In that case, there is no typedef variant type produced for foo.
20891    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20892    struct type.  */
20893
20894 static bool
20895 is_naming_typedef_decl (const_tree decl)
20896 {
20897   if (decl == NULL_TREE
20898       || TREE_CODE (decl) != TYPE_DECL
20899       || !is_tagged_type (TREE_TYPE (decl))
20900       || DECL_IS_BUILTIN (decl)
20901       || is_redundant_typedef (decl)
20902       /* It looks like Ada produces TYPE_DECLs that are very similar
20903          to C++ naming typedefs but that have different
20904          semantics. Let's be specific to c++ for now.  */
20905       || !is_cxx ())
20906     return FALSE;
20907
20908   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20909           && TYPE_NAME (TREE_TYPE (decl)) == decl
20910           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20911               != TYPE_NAME (TREE_TYPE (decl))));
20912 }
20913
20914 /* Returns the DIE for a context.  */
20915
20916 static inline dw_die_ref
20917 get_context_die (tree context)
20918 {
20919   if (context)
20920     {
20921       /* Find die that represents this context.  */
20922       if (TYPE_P (context))
20923         {
20924           context = TYPE_MAIN_VARIANT (context);
20925           return strip_naming_typedef (context, force_type_die (context));
20926         }
20927       else
20928         return force_decl_die (context);
20929     }
20930   return comp_unit_die ();
20931 }
20932
20933 /* Returns the DIE for decl.  A DIE will always be returned.  */
20934
20935 static dw_die_ref
20936 force_decl_die (tree decl)
20937 {
20938   dw_die_ref decl_die;
20939   unsigned saved_external_flag;
20940   tree save_fn = NULL_TREE;
20941   decl_die = lookup_decl_die (decl);
20942   if (!decl_die)
20943     {
20944       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20945
20946       decl_die = lookup_decl_die (decl);
20947       if (decl_die)
20948         return decl_die;
20949
20950       switch (TREE_CODE (decl))
20951         {
20952         case FUNCTION_DECL:
20953           /* Clear current_function_decl, so that gen_subprogram_die thinks
20954              that this is a declaration. At this point, we just want to force
20955              declaration die.  */
20956           save_fn = current_function_decl;
20957           current_function_decl = NULL_TREE;
20958           gen_subprogram_die (decl, context_die);
20959           current_function_decl = save_fn;
20960           break;
20961
20962         case VAR_DECL:
20963           /* Set external flag to force declaration die. Restore it after
20964            gen_decl_die() call.  */
20965           saved_external_flag = DECL_EXTERNAL (decl);
20966           DECL_EXTERNAL (decl) = 1;
20967           gen_decl_die (decl, NULL, context_die);
20968           DECL_EXTERNAL (decl) = saved_external_flag;
20969           break;
20970
20971         case NAMESPACE_DECL:
20972           if (dwarf_version >= 3 || !dwarf_strict)
20973             dwarf2out_decl (decl);
20974           else
20975             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20976             decl_die = comp_unit_die ();
20977           break;
20978
20979         case TRANSLATION_UNIT_DECL:
20980           decl_die = comp_unit_die ();
20981           break;
20982
20983         default:
20984           gcc_unreachable ();
20985         }
20986
20987       /* We should be able to find the DIE now.  */
20988       if (!decl_die)
20989         decl_die = lookup_decl_die (decl);
20990       gcc_assert (decl_die);
20991     }
20992
20993   return decl_die;
20994 }
20995
20996 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20997    always returned.  */
20998
20999 static dw_die_ref
21000 force_type_die (tree type)
21001 {
21002   dw_die_ref type_die;
21003
21004   type_die = lookup_type_die (type);
21005   if (!type_die)
21006     {
21007       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21008
21009       type_die = modified_type_die (type, TYPE_READONLY (type),
21010                                     TYPE_VOLATILE (type), context_die);
21011       gcc_assert (type_die);
21012     }
21013   return type_die;
21014 }
21015
21016 /* Force out any required namespaces to be able to output DECL,
21017    and return the new context_die for it, if it's changed.  */
21018
21019 static dw_die_ref
21020 setup_namespace_context (tree thing, dw_die_ref context_die)
21021 {
21022   tree context = (DECL_P (thing)
21023                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21024   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21025     /* Force out the namespace.  */
21026     context_die = force_decl_die (context);
21027
21028   return context_die;
21029 }
21030
21031 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21032    type) within its namespace, if appropriate.
21033
21034    For compatibility with older debuggers, namespace DIEs only contain
21035    declarations; all definitions are emitted at CU scope.  */
21036
21037 static dw_die_ref
21038 declare_in_namespace (tree thing, dw_die_ref context_die)
21039 {
21040   dw_die_ref ns_context;
21041
21042   if (debug_info_level <= DINFO_LEVEL_TERSE)
21043     return context_die;
21044
21045   /* If this decl is from an inlined function, then don't try to emit it in its
21046      namespace, as we will get confused.  It would have already been emitted
21047      when the abstract instance of the inline function was emitted anyways.  */
21048   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21049     return context_die;
21050
21051   ns_context = setup_namespace_context (thing, context_die);
21052
21053   if (ns_context != context_die)
21054     {
21055       if (is_fortran ())
21056         return ns_context;
21057       if (DECL_P (thing))
21058         gen_decl_die (thing, NULL, ns_context);
21059       else
21060         gen_type_die (thing, ns_context);
21061     }
21062   return context_die;
21063 }
21064
21065 /* Generate a DIE for a namespace or namespace alias.  */
21066
21067 static void
21068 gen_namespace_die (tree decl, dw_die_ref context_die)
21069 {
21070   dw_die_ref namespace_die;
21071
21072   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21073      they are an alias of.  */
21074   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21075     {
21076       /* Output a real namespace or module.  */
21077       context_die = setup_namespace_context (decl, comp_unit_die ());
21078       namespace_die = new_die (is_fortran ()
21079                                ? DW_TAG_module : DW_TAG_namespace,
21080                                context_die, decl);
21081       /* For Fortran modules defined in different CU don't add src coords.  */
21082       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21083         {
21084           const char *name = dwarf2_name (decl, 0);
21085           if (name)
21086             add_name_attribute (namespace_die, name);
21087         }
21088       else
21089         add_name_and_src_coords_attributes (namespace_die, decl);
21090       if (DECL_EXTERNAL (decl))
21091         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21092       equate_decl_number_to_die (decl, namespace_die);
21093     }
21094   else
21095     {
21096       /* Output a namespace alias.  */
21097
21098       /* Force out the namespace we are an alias of, if necessary.  */
21099       dw_die_ref origin_die
21100         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21101
21102       if (DECL_FILE_SCOPE_P (decl)
21103           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21104         context_die = setup_namespace_context (decl, comp_unit_die ());
21105       /* Now create the namespace alias DIE.  */
21106       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21107       add_name_and_src_coords_attributes (namespace_die, decl);
21108       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21109       equate_decl_number_to_die (decl, namespace_die);
21110     }
21111 }
21112
21113 /* Generate Dwarf debug information for a decl described by DECL.
21114    The return value is currently only meaningful for PARM_DECLs,
21115    for all other decls it returns NULL.  */
21116
21117 static dw_die_ref
21118 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21119 {
21120   tree decl_or_origin = decl ? decl : origin;
21121   tree class_origin = NULL, ultimate_origin;
21122
21123   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21124     return NULL;
21125
21126   switch (TREE_CODE (decl_or_origin))
21127     {
21128     case ERROR_MARK:
21129       break;
21130
21131     case CONST_DECL:
21132       if (!is_fortran () && !is_ada ())
21133         {
21134           /* The individual enumerators of an enum type get output when we output
21135              the Dwarf representation of the relevant enum type itself.  */
21136           break;
21137         }
21138
21139       /* Emit its type.  */
21140       gen_type_die (TREE_TYPE (decl), context_die);
21141
21142       /* And its containing namespace.  */
21143       context_die = declare_in_namespace (decl, context_die);
21144
21145       gen_const_die (decl, context_die);
21146       break;
21147
21148     case FUNCTION_DECL:
21149       /* Don't output any DIEs to represent mere function declarations,
21150          unless they are class members or explicit block externs.  */
21151       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21152           && DECL_FILE_SCOPE_P (decl_or_origin)
21153           && (current_function_decl == NULL_TREE
21154               || DECL_ARTIFICIAL (decl_or_origin)))
21155         break;
21156
21157 #if 0
21158       /* FIXME */
21159       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21160          on local redeclarations of global functions.  That seems broken.  */
21161       if (current_function_decl != decl)
21162         /* This is only a declaration.  */;
21163 #endif
21164
21165       /* If we're emitting a clone, emit info for the abstract instance.  */
21166       if (origin || DECL_ORIGIN (decl) != decl)
21167         dwarf2out_abstract_function (origin
21168                                      ? DECL_ORIGIN (origin)
21169                                      : DECL_ABSTRACT_ORIGIN (decl));
21170
21171       /* If we're emitting an out-of-line copy of an inline function,
21172          emit info for the abstract instance and set up to refer to it.  */
21173       else if (cgraph_function_possibly_inlined_p (decl)
21174                && ! DECL_ABSTRACT (decl)
21175                && ! class_or_namespace_scope_p (context_die)
21176                /* dwarf2out_abstract_function won't emit a die if this is just
21177                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21178                   that case, because that works only if we have a die.  */
21179                && DECL_INITIAL (decl) != NULL_TREE)
21180         {
21181           dwarf2out_abstract_function (decl);
21182           set_decl_origin_self (decl);
21183         }
21184
21185       /* Otherwise we're emitting the primary DIE for this decl.  */
21186       else if (debug_info_level > DINFO_LEVEL_TERSE)
21187         {
21188           /* Before we describe the FUNCTION_DECL itself, make sure that we
21189              have its containing type.  */
21190           if (!origin)
21191             origin = decl_class_context (decl);
21192           if (origin != NULL_TREE)
21193             gen_type_die (origin, context_die);
21194
21195           /* And its return type.  */
21196           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21197
21198           /* And its virtual context.  */
21199           if (DECL_VINDEX (decl) != NULL_TREE)
21200             gen_type_die (DECL_CONTEXT (decl), context_die);
21201
21202           /* Make sure we have a member DIE for decl.  */
21203           if (origin != NULL_TREE)
21204             gen_type_die_for_member (origin, decl, context_die);
21205
21206           /* And its containing namespace.  */
21207           context_die = declare_in_namespace (decl, context_die);
21208         }
21209
21210       /* Now output a DIE to represent the function itself.  */
21211       if (decl)
21212         gen_subprogram_die (decl, context_die);
21213       break;
21214
21215     case TYPE_DECL:
21216       /* If we are in terse mode, don't generate any DIEs to represent any
21217          actual typedefs.  */
21218       if (debug_info_level <= DINFO_LEVEL_TERSE)
21219         break;
21220
21221       /* In the special case of a TYPE_DECL node representing the declaration
21222          of some type tag, if the given TYPE_DECL is marked as having been
21223          instantiated from some other (original) TYPE_DECL node (e.g. one which
21224          was generated within the original definition of an inline function) we
21225          used to generate a special (abbreviated) DW_TAG_structure_type,
21226          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21227          should be actually referencing those DIEs, as variable DIEs with that
21228          type would be emitted already in the abstract origin, so it was always
21229          removed during unused type prunning.  Don't add anything in this
21230          case.  */
21231       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21232         break;
21233
21234       if (is_redundant_typedef (decl))
21235         gen_type_die (TREE_TYPE (decl), context_die);
21236       else
21237         /* Output a DIE to represent the typedef itself.  */
21238         gen_typedef_die (decl, context_die);
21239       break;
21240
21241     case LABEL_DECL:
21242       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21243         gen_label_die (decl, context_die);
21244       break;
21245
21246     case VAR_DECL:
21247     case RESULT_DECL:
21248       /* If we are in terse mode, don't generate any DIEs to represent any
21249          variable declarations or definitions.  */
21250       if (debug_info_level <= DINFO_LEVEL_TERSE)
21251         break;
21252
21253       /* Output any DIEs that are needed to specify the type of this data
21254          object.  */
21255       if (decl_by_reference_p (decl_or_origin))
21256         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21257       else
21258         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21259
21260       /* And its containing type.  */
21261       class_origin = decl_class_context (decl_or_origin);
21262       if (class_origin != NULL_TREE)
21263         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21264
21265       /* And its containing namespace.  */
21266       context_die = declare_in_namespace (decl_or_origin, context_die);
21267
21268       /* Now output the DIE to represent the data object itself.  This gets
21269          complicated because of the possibility that the VAR_DECL really
21270          represents an inlined instance of a formal parameter for an inline
21271          function.  */
21272       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21273       if (ultimate_origin != NULL_TREE
21274           && TREE_CODE (ultimate_origin) == PARM_DECL)
21275         gen_formal_parameter_die (decl, origin,
21276                                   true /* Emit name attribute.  */,
21277                                   context_die);
21278       else
21279         gen_variable_die (decl, origin, context_die);
21280       break;
21281
21282     case FIELD_DECL:
21283       /* Ignore the nameless fields that are used to skip bits but handle C++
21284          anonymous unions and structs.  */
21285       if (DECL_NAME (decl) != NULL_TREE
21286           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21287           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21288         {
21289           gen_type_die (member_declared_type (decl), context_die);
21290           gen_field_die (decl, context_die);
21291         }
21292       break;
21293
21294     case PARM_DECL:
21295       if (DECL_BY_REFERENCE (decl_or_origin))
21296         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21297       else
21298         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21299       return gen_formal_parameter_die (decl, origin,
21300                                        true /* Emit name attribute.  */,
21301                                        context_die);
21302
21303     case NAMESPACE_DECL:
21304     case IMPORTED_DECL:
21305       if (dwarf_version >= 3 || !dwarf_strict)
21306         gen_namespace_die (decl, context_die);
21307       break;
21308
21309     default:
21310       /* Probably some frontend-internal decl.  Assume we don't care.  */
21311       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21312       break;
21313     }
21314
21315   return NULL;
21316 }
21317 \f
21318 /* Output debug information for global decl DECL.  Called from toplev.c after
21319    compilation proper has finished.  */
21320
21321 static void
21322 dwarf2out_global_decl (tree decl)
21323 {
21324   /* Output DWARF2 information for file-scope tentative data object
21325      declarations, file-scope (extern) function declarations (which
21326      had no corresponding body) and file-scope tagged type declarations
21327      and definitions which have not yet been forced out.  */
21328   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21329     dwarf2out_decl (decl);
21330 }
21331
21332 /* Output debug information for type decl DECL.  Called from toplev.c
21333    and from language front ends (to record built-in types).  */
21334 static void
21335 dwarf2out_type_decl (tree decl, int local)
21336 {
21337   if (!local)
21338     dwarf2out_decl (decl);
21339 }
21340
21341 /* Output debug information for imported module or decl DECL.
21342    NAME is non-NULL name in the lexical block if the decl has been renamed.
21343    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21344    that DECL belongs to.
21345    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21346 static void
21347 dwarf2out_imported_module_or_decl_1 (tree decl,
21348                                      tree name,
21349                                      tree lexical_block,
21350                                      dw_die_ref lexical_block_die)
21351 {
21352   expanded_location xloc;
21353   dw_die_ref imported_die = NULL;
21354   dw_die_ref at_import_die;
21355
21356   if (TREE_CODE (decl) == IMPORTED_DECL)
21357     {
21358       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21359       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21360       gcc_assert (decl);
21361     }
21362   else
21363     xloc = expand_location (input_location);
21364
21365   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21366     {
21367       at_import_die = force_type_die (TREE_TYPE (decl));
21368       /* For namespace N { typedef void T; } using N::T; base_type_die
21369          returns NULL, but DW_TAG_imported_declaration requires
21370          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21371       if (!at_import_die)
21372         {
21373           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21374           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21375           at_import_die = lookup_type_die (TREE_TYPE (decl));
21376           gcc_assert (at_import_die);
21377         }
21378     }
21379   else
21380     {
21381       at_import_die = lookup_decl_die (decl);
21382       if (!at_import_die)
21383         {
21384           /* If we're trying to avoid duplicate debug info, we may not have
21385              emitted the member decl for this field.  Emit it now.  */
21386           if (TREE_CODE (decl) == FIELD_DECL)
21387             {
21388               tree type = DECL_CONTEXT (decl);
21389
21390               if (TYPE_CONTEXT (type)
21391                   && TYPE_P (TYPE_CONTEXT (type))
21392                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21393                                                 DINFO_USAGE_DIR_USE))
21394                 return;
21395               gen_type_die_for_member (type, decl,
21396                                        get_context_die (TYPE_CONTEXT (type)));
21397             }
21398           at_import_die = force_decl_die (decl);
21399         }
21400     }
21401
21402   if (TREE_CODE (decl) == NAMESPACE_DECL)
21403     {
21404       if (dwarf_version >= 3 || !dwarf_strict)
21405         imported_die = new_die (DW_TAG_imported_module,
21406                                 lexical_block_die,
21407                                 lexical_block);
21408       else
21409         return;
21410     }
21411   else
21412     imported_die = new_die (DW_TAG_imported_declaration,
21413                             lexical_block_die,
21414                             lexical_block);
21415
21416   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21417   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21418   if (name)
21419     add_AT_string (imported_die, DW_AT_name,
21420                    IDENTIFIER_POINTER (name));
21421   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21422 }
21423
21424 /* Output debug information for imported module or decl DECL.
21425    NAME is non-NULL name in context if the decl has been renamed.
21426    CHILD is true if decl is one of the renamed decls as part of
21427    importing whole module.  */
21428
21429 static void
21430 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21431                                    bool child)
21432 {
21433   /* dw_die_ref at_import_die;  */
21434   dw_die_ref scope_die;
21435
21436   if (debug_info_level <= DINFO_LEVEL_TERSE)
21437     return;
21438
21439   gcc_assert (decl);
21440
21441   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21442      We need decl DIE for reference and scope die. First, get DIE for the decl
21443      itself.  */
21444
21445   /* Get the scope die for decl context. Use comp_unit_die for global module
21446      or decl. If die is not found for non globals, force new die.  */
21447   if (context
21448       && TYPE_P (context)
21449       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21450     return;
21451
21452   if (!(dwarf_version >= 3 || !dwarf_strict))
21453     return;
21454
21455   scope_die = get_context_die (context);
21456
21457   if (child)
21458     {
21459       gcc_assert (scope_die->die_child);
21460       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21461       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21462       scope_die = scope_die->die_child;
21463     }
21464
21465   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21466   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21467
21468 }
21469
21470 /* Write the debugging output for DECL.  */
21471
21472 void
21473 dwarf2out_decl (tree decl)
21474 {
21475   dw_die_ref context_die = comp_unit_die ();
21476
21477   switch (TREE_CODE (decl))
21478     {
21479     case ERROR_MARK:
21480       return;
21481
21482     case FUNCTION_DECL:
21483       /* What we would really like to do here is to filter out all mere
21484          file-scope declarations of file-scope functions which are never
21485          referenced later within this translation unit (and keep all of ones
21486          that *are* referenced later on) but we aren't clairvoyant, so we have
21487          no idea which functions will be referenced in the future (i.e. later
21488          on within the current translation unit). So here we just ignore all
21489          file-scope function declarations which are not also definitions.  If
21490          and when the debugger needs to know something about these functions,
21491          it will have to hunt around and find the DWARF information associated
21492          with the definition of the function.
21493
21494          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21495          nodes represent definitions and which ones represent mere
21496          declarations.  We have to check DECL_INITIAL instead. That's because
21497          the C front-end supports some weird semantics for "extern inline"
21498          function definitions.  These can get inlined within the current
21499          translation unit (and thus, we need to generate Dwarf info for their
21500          abstract instances so that the Dwarf info for the concrete inlined
21501          instances can have something to refer to) but the compiler never
21502          generates any out-of-lines instances of such things (despite the fact
21503          that they *are* definitions).
21504
21505          The important point is that the C front-end marks these "extern
21506          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21507          them anyway. Note that the C++ front-end also plays some similar games
21508          for inline function definitions appearing within include files which
21509          also contain `#pragma interface' pragmas.  */
21510       if (DECL_INITIAL (decl) == NULL_TREE)
21511         return;
21512
21513       /* If we're a nested function, initially use a parent of NULL; if we're
21514          a plain function, this will be fixed up in decls_for_scope.  If
21515          we're a method, it will be ignored, since we already have a DIE.  */
21516       if (decl_function_context (decl)
21517           /* But if we're in terse mode, we don't care about scope.  */
21518           && debug_info_level > DINFO_LEVEL_TERSE)
21519         context_die = NULL;
21520       break;
21521
21522     case VAR_DECL:
21523       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21524          declaration and if the declaration was never even referenced from
21525          within this entire compilation unit.  We suppress these DIEs in
21526          order to save space in the .debug section (by eliminating entries
21527          which are probably useless).  Note that we must not suppress
21528          block-local extern declarations (whether used or not) because that
21529          would screw-up the debugger's name lookup mechanism and cause it to
21530          miss things which really ought to be in scope at a given point.  */
21531       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21532         return;
21533
21534       /* For local statics lookup proper context die.  */
21535       if (TREE_STATIC (decl) && decl_function_context (decl))
21536         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21537
21538       /* If we are in terse mode, don't generate any DIEs to represent any
21539          variable declarations or definitions.  */
21540       if (debug_info_level <= DINFO_LEVEL_TERSE)
21541         return;
21542       break;
21543
21544     case CONST_DECL:
21545       if (debug_info_level <= DINFO_LEVEL_TERSE)
21546         return;
21547       if (!is_fortran () && !is_ada ())
21548         return;
21549       if (TREE_STATIC (decl) && decl_function_context (decl))
21550         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21551       break;
21552
21553     case NAMESPACE_DECL:
21554     case IMPORTED_DECL:
21555       if (debug_info_level <= DINFO_LEVEL_TERSE)
21556         return;
21557       if (lookup_decl_die (decl) != NULL)
21558         return;
21559       break;
21560
21561     case TYPE_DECL:
21562       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21563       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21564         return;
21565
21566       /* Don't bother trying to generate any DIEs to represent any of the
21567          normal built-in types for the language we are compiling.  */
21568       if (DECL_IS_BUILTIN (decl))
21569         return;
21570
21571       /* If we are in terse mode, don't generate any DIEs for types.  */
21572       if (debug_info_level <= DINFO_LEVEL_TERSE)
21573         return;
21574
21575       /* If we're a function-scope tag, initially use a parent of NULL;
21576          this will be fixed up in decls_for_scope.  */
21577       if (decl_function_context (decl))
21578         context_die = NULL;
21579
21580       break;
21581
21582     default:
21583       return;
21584     }
21585
21586   gen_decl_die (decl, NULL, context_die);
21587 }
21588
21589 /* Write the debugging output for DECL.  */
21590
21591 static void
21592 dwarf2out_function_decl (tree decl)
21593 {
21594   dwarf2out_decl (decl);
21595   call_arg_locations = NULL;
21596   call_arg_loc_last = NULL;
21597   call_site_count = -1;
21598   tail_call_site_count = -1;
21599   VEC_free (dw_die_ref, heap, block_map);
21600   htab_empty (decl_loc_table);
21601   htab_empty (cached_dw_loc_list_table);
21602 }
21603
21604 /* Output a marker (i.e. a label) for the beginning of the generated code for
21605    a lexical block.  */
21606
21607 static void
21608 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21609                        unsigned int blocknum)
21610 {
21611   switch_to_section (current_function_section ());
21612   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21613 }
21614
21615 /* Output a marker (i.e. a label) for the end of the generated code for a
21616    lexical block.  */
21617
21618 static void
21619 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21620 {
21621   switch_to_section (current_function_section ());
21622   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21623 }
21624
21625 /* Returns nonzero if it is appropriate not to emit any debugging
21626    information for BLOCK, because it doesn't contain any instructions.
21627
21628    Don't allow this for blocks with nested functions or local classes
21629    as we would end up with orphans, and in the presence of scheduling
21630    we may end up calling them anyway.  */
21631
21632 static bool
21633 dwarf2out_ignore_block (const_tree block)
21634 {
21635   tree decl;
21636   unsigned int i;
21637
21638   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21639     if (TREE_CODE (decl) == FUNCTION_DECL
21640         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21641       return 0;
21642   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21643     {
21644       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21645       if (TREE_CODE (decl) == FUNCTION_DECL
21646           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21647       return 0;
21648     }
21649
21650   return 1;
21651 }
21652
21653 /* Hash table routines for file_hash.  */
21654
21655 static int
21656 file_table_eq (const void *p1_p, const void *p2_p)
21657 {
21658   const struct dwarf_file_data *const p1 =
21659     (const struct dwarf_file_data *) p1_p;
21660   const char *const p2 = (const char *) p2_p;
21661   return filename_cmp (p1->filename, p2) == 0;
21662 }
21663
21664 static hashval_t
21665 file_table_hash (const void *p_p)
21666 {
21667   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21668   return htab_hash_string (p->filename);
21669 }
21670
21671 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21672    dwarf2out.c) and return its "index".  The index of each (known) filename is
21673    just a unique number which is associated with only that one filename.  We
21674    need such numbers for the sake of generating labels (in the .debug_sfnames
21675    section) and references to those files numbers (in the .debug_srcinfo
21676    and.debug_macinfo sections).  If the filename given as an argument is not
21677    found in our current list, add it to the list and assign it the next
21678    available unique index number.  In order to speed up searches, we remember
21679    the index of the filename was looked up last.  This handles the majority of
21680    all searches.  */
21681
21682 static struct dwarf_file_data *
21683 lookup_filename (const char *file_name)
21684 {
21685   void ** slot;
21686   struct dwarf_file_data * created;
21687
21688   /* Check to see if the file name that was searched on the previous
21689      call matches this file name.  If so, return the index.  */
21690   if (file_table_last_lookup
21691       && (file_name == file_table_last_lookup->filename
21692           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21693     return file_table_last_lookup;
21694
21695   /* Didn't match the previous lookup, search the table.  */
21696   slot = htab_find_slot_with_hash (file_table, file_name,
21697                                    htab_hash_string (file_name), INSERT);
21698   if (*slot)
21699     return (struct dwarf_file_data *) *slot;
21700
21701   created = ggc_alloc_dwarf_file_data ();
21702   created->filename = file_name;
21703   created->emitted_number = 0;
21704   *slot = created;
21705   return created;
21706 }
21707
21708 /* If the assembler will construct the file table, then translate the compiler
21709    internal file table number into the assembler file table number, and emit
21710    a .file directive if we haven't already emitted one yet.  The file table
21711    numbers are different because we prune debug info for unused variables and
21712    types, which may include filenames.  */
21713
21714 static int
21715 maybe_emit_file (struct dwarf_file_data * fd)
21716 {
21717   if (! fd->emitted_number)
21718     {
21719       if (last_emitted_file)
21720         fd->emitted_number = last_emitted_file->emitted_number + 1;
21721       else
21722         fd->emitted_number = 1;
21723       last_emitted_file = fd;
21724
21725       if (DWARF2_ASM_LINE_DEBUG_INFO)
21726         {
21727           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21728           output_quoted_string (asm_out_file,
21729                                 remap_debug_filename (fd->filename));
21730           fputc ('\n', asm_out_file);
21731         }
21732     }
21733
21734   return fd->emitted_number;
21735 }
21736
21737 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21738    That generation should happen after function debug info has been
21739    generated. The value of the attribute is the constant value of ARG.  */
21740
21741 static void
21742 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21743 {
21744   die_arg_entry entry;
21745
21746   if (!die || !arg)
21747     return;
21748
21749   if (!tmpl_value_parm_die_table)
21750     tmpl_value_parm_die_table
21751       = VEC_alloc (die_arg_entry, gc, 32);
21752
21753   entry.die = die;
21754   entry.arg = arg;
21755   VEC_safe_push (die_arg_entry, gc,
21756                  tmpl_value_parm_die_table,
21757                  &entry);
21758 }
21759
21760 /* Return TRUE if T is an instance of generic type, FALSE
21761    otherwise.  */
21762
21763 static bool
21764 generic_type_p (tree t)
21765 {
21766   if (t == NULL_TREE || !TYPE_P (t))
21767     return false;
21768   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21769 }
21770
21771 /* Schedule the generation of the generic parameter dies for the
21772   instance of generic type T. The proper generation itself is later
21773   done by gen_scheduled_generic_parms_dies. */
21774
21775 static void
21776 schedule_generic_params_dies_gen (tree t)
21777 {
21778   if (!generic_type_p (t))
21779     return;
21780
21781   if (generic_type_instances == NULL)
21782     generic_type_instances = VEC_alloc (tree, gc, 256);
21783
21784   VEC_safe_push (tree, gc, generic_type_instances, t);
21785 }
21786
21787 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21788    by append_entry_to_tmpl_value_parm_die_table. This function must
21789    be called after function DIEs have been generated.  */
21790
21791 static void
21792 gen_remaining_tmpl_value_param_die_attribute (void)
21793 {
21794   if (tmpl_value_parm_die_table)
21795     {
21796       unsigned i;
21797       die_arg_entry *e;
21798
21799       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21800         tree_add_const_value_attribute (e->die, e->arg);
21801     }
21802 }
21803
21804 /* Generate generic parameters DIEs for instances of generic types
21805    that have been previously scheduled by
21806    schedule_generic_params_dies_gen. This function must be called
21807    after all the types of the CU have been laid out.  */
21808
21809 static void
21810 gen_scheduled_generic_parms_dies (void)
21811 {
21812   unsigned i;
21813   tree t;
21814
21815   if (generic_type_instances == NULL)
21816     return;
21817   
21818   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21819     gen_generic_params_dies (t);
21820 }
21821
21822
21823 /* Replace DW_AT_name for the decl with name.  */
21824
21825 static void
21826 dwarf2out_set_name (tree decl, tree name)
21827 {
21828   dw_die_ref die;
21829   dw_attr_ref attr;
21830   const char *dname;
21831
21832   die = TYPE_SYMTAB_DIE (decl);
21833   if (!die)
21834     return;
21835
21836   dname = dwarf2_name (name, 0);
21837   if (!dname)
21838     return;
21839
21840   attr = get_AT (die, DW_AT_name);
21841   if (attr)
21842     {
21843       struct indirect_string_node *node;
21844
21845       node = find_AT_string (dname);
21846       /* replace the string.  */
21847       attr->dw_attr_val.v.val_str = node;
21848     }
21849
21850   else
21851     add_name_attribute (die, dname);
21852 }
21853
21854 /* Called by the final INSN scan whenever we see a var location.  We
21855    use it to drop labels in the right places, and throw the location in
21856    our lookup table.  */
21857
21858 static void
21859 dwarf2out_var_location (rtx loc_note)
21860 {
21861   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21862   struct var_loc_node *newloc;
21863   rtx next_real;
21864   static const char *last_label;
21865   static const char *last_postcall_label;
21866   static bool last_in_cold_section_p;
21867   tree decl;
21868   bool var_loc_p;
21869
21870   if (!NOTE_P (loc_note))
21871     {
21872       if (CALL_P (loc_note))
21873         {
21874           call_site_count++;
21875           if (SIBLING_CALL_P (loc_note))
21876             tail_call_site_count++;
21877         }
21878       return;
21879     }
21880
21881   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21882   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21883     return;
21884
21885   next_real = next_real_insn (loc_note);
21886
21887   /* If there are no instructions which would be affected by this note,
21888      don't do anything.  */
21889   if (var_loc_p
21890       && next_real == NULL_RTX
21891       && !NOTE_DURING_CALL_P (loc_note))
21892     return;
21893
21894   if (next_real == NULL_RTX)
21895     next_real = get_last_insn ();
21896
21897   /* If there were any real insns between note we processed last time
21898      and this note (or if it is the first note), clear
21899      last_{,postcall_}label so that they are not reused this time.  */
21900   if (last_var_location_insn == NULL_RTX
21901       || last_var_location_insn != next_real
21902       || last_in_cold_section_p != in_cold_section_p)
21903     {
21904       last_label = NULL;
21905       last_postcall_label = NULL;
21906     }
21907
21908   if (var_loc_p)
21909     {
21910       decl = NOTE_VAR_LOCATION_DECL (loc_note);
21911       newloc = add_var_loc_to_decl (decl, loc_note,
21912                                     NOTE_DURING_CALL_P (loc_note)
21913                                     ? last_postcall_label : last_label);
21914       if (newloc == NULL)
21915         return;
21916     }
21917   else
21918     {
21919       decl = NULL_TREE;
21920       newloc = NULL;
21921     }
21922
21923   /* If there were no real insns between note we processed last time
21924      and this note, use the label we emitted last time.  Otherwise
21925      create a new label and emit it.  */
21926   if (last_label == NULL)
21927     {
21928       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21929       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21930       loclabel_num++;
21931       last_label = ggc_strdup (loclabel);
21932     }
21933
21934   if (!var_loc_p)
21935     {
21936       struct call_arg_loc_node *ca_loc
21937         = ggc_alloc_cleared_call_arg_loc_node ();
21938       rtx prev = prev_real_insn (loc_note), x;
21939       ca_loc->call_arg_loc_note = loc_note;
21940       ca_loc->next = NULL;
21941       ca_loc->label = last_label;
21942       gcc_assert (prev
21943                   && (CALL_P (prev)
21944                       || (NONJUMP_INSN_P (prev)
21945                           && GET_CODE (PATTERN (prev)) == SEQUENCE
21946                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
21947       if (!CALL_P (prev))
21948         prev = XVECEXP (PATTERN (prev), 0, 0);
21949       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
21950       x = PATTERN (prev);
21951       if (GET_CODE (x) == PARALLEL)
21952         x = XVECEXP (x, 0, 0);
21953       if (GET_CODE (x) == SET)
21954         x = SET_SRC (x);
21955       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
21956         {
21957           x = XEXP (XEXP (x, 0), 0);
21958           if (GET_CODE (x) == SYMBOL_REF
21959               && SYMBOL_REF_DECL (x)
21960               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
21961             ca_loc->symbol_ref = x;
21962         }
21963       ca_loc->block = insn_scope (prev);
21964       if (call_arg_locations)
21965         call_arg_loc_last->next = ca_loc;
21966       else
21967         call_arg_locations = ca_loc;
21968       call_arg_loc_last = ca_loc;
21969     }
21970   else if (!NOTE_DURING_CALL_P (loc_note))
21971     newloc->label = last_label;
21972   else
21973     {
21974       if (!last_postcall_label)
21975         {
21976           sprintf (loclabel, "%s-1", last_label);
21977           last_postcall_label = ggc_strdup (loclabel);
21978         }
21979       newloc->label = last_postcall_label;
21980     }
21981
21982   last_var_location_insn = next_real;
21983   last_in_cold_section_p = in_cold_section_p;
21984 }
21985
21986 /* Note in one location list that text section has changed.  */
21987
21988 static int
21989 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
21990 {
21991   var_loc_list *list = (var_loc_list *) *slot;
21992   if (list->first)
21993     list->last_before_switch
21994       = list->last->next ? list->last->next : list->last;
21995   return 1;
21996 }
21997
21998 /* Note in all location lists that text section has changed.  */
21999
22000 static void
22001 var_location_switch_text_section (void)
22002 {
22003   if (decl_loc_table == NULL)
22004     return;
22005
22006   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
22007 }
22008
22009 /* Create a new line number table.  */
22010
22011 static dw_line_info_table *
22012 new_line_info_table (void)
22013 {
22014   dw_line_info_table *table;
22015
22016   table = ggc_alloc_cleared_dw_line_info_table_struct ();
22017   table->file_num = 1;
22018   table->line_num = 1;
22019   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22020
22021   return table;
22022 }
22023
22024 /* Lookup the "current" table into which we emit line info, so
22025    that we don't have to do it for every source line.  */
22026
22027 static void
22028 set_cur_line_info_table (section *sec)
22029 {
22030   dw_line_info_table *table;
22031
22032   if (sec == text_section)
22033     {
22034       table = text_section_line_info;
22035       if (!table)
22036         {
22037           text_section_line_info = table = new_line_info_table ();
22038           table->end_label = text_end_label;
22039         }
22040     }
22041   else if (sec == cold_text_section)
22042     {
22043       table = cold_text_section_line_info;
22044       if (!table)
22045         {
22046           cold_text_section_line_info = table = new_line_info_table ();
22047           table->end_label = cold_end_label;
22048         }
22049     }
22050   else
22051     {
22052       const char *end_label;
22053
22054       if (flag_reorder_blocks_and_partition)
22055         {
22056           if (in_cold_section_p)
22057             end_label = crtl->subsections.cold_section_end_label;
22058           else
22059             end_label = crtl->subsections.hot_section_end_label;
22060         }
22061       else
22062         {
22063           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22064           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22065                                        current_function_funcdef_no);
22066           end_label = ggc_strdup (label);
22067         }
22068
22069       table = new_line_info_table ();
22070       table->end_label = end_label;
22071
22072       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
22073     }
22074
22075   cur_line_info_table = table;
22076 }
22077
22078
22079 /* We need to reset the locations at the beginning of each
22080    function. We can't do this in the end_function hook, because the
22081    declarations that use the locations won't have been output when
22082    that hook is called.  Also compute have_multiple_function_sections here.  */
22083
22084 static void
22085 dwarf2out_begin_function (tree fun)
22086 {
22087   section *sec = function_section (fun);
22088
22089   if (sec != text_section)
22090     have_multiple_function_sections = true;
22091
22092   if (flag_reorder_blocks_and_partition && !cold_text_section)
22093     {
22094       gcc_assert (current_function_decl == fun);
22095       cold_text_section = unlikely_text_section ();
22096       switch_to_section (cold_text_section);
22097       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22098       switch_to_section (sec);
22099     }
22100
22101   dwarf2out_note_section_used ();
22102   call_site_count = 0;
22103   tail_call_site_count = 0;
22104
22105   set_cur_line_info_table (sec);
22106 }
22107
22108 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22109
22110 static void
22111 push_dw_line_info_entry (dw_line_info_table *table,
22112                          enum dw_line_info_opcode opcode, unsigned int val)
22113 {
22114   dw_line_info_entry e;
22115   e.opcode = opcode;
22116   e.val = val;
22117   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
22118 }
22119
22120 /* Output a label to mark the beginning of a source code line entry
22121    and record information relating to this source line, in
22122    'line_info_table' for later output of the .debug_line section.  */
22123 /* ??? The discriminator parameter ought to be unsigned.  */
22124
22125 static void
22126 dwarf2out_source_line (unsigned int line, const char *filename,
22127                        int discriminator, bool is_stmt)
22128 {
22129   unsigned int file_num;
22130   dw_line_info_table *table;
22131
22132   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
22133     return;
22134
22135   /* The discriminator column was added in dwarf4.  Simplify the below
22136      by simply removing it if we're not supposed to output it.  */
22137   if (dwarf_version < 4 && dwarf_strict)
22138     discriminator = 0;
22139
22140   table = cur_line_info_table;
22141   file_num = maybe_emit_file (lookup_filename (filename));
22142
22143   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22144      the debugger has used the second (possibly duplicate) line number
22145      at the beginning of the function to mark the end of the prologue.
22146      We could eliminate any other duplicates within the function.  For
22147      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22148      that second line number entry.  */
22149   /* Recall that this end-of-prologue indication is *not* the same thing
22150      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22151      to which the hook corresponds, follows the last insn that was 
22152      emitted by gen_prologue.  What we need is to preceed the first insn
22153      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22154      insn that corresponds to something the user wrote.  These may be
22155      very different locations once scheduling is enabled.  */
22156
22157   if (0 && file_num == table->file_num
22158       && line == table->line_num
22159       && discriminator == table->discrim_num
22160       && is_stmt == table->is_stmt)
22161     return;
22162
22163   switch_to_section (current_function_section ());
22164
22165   /* If requested, emit something human-readable.  */
22166   if (flag_debug_asm)
22167     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22168
22169   if (DWARF2_ASM_LINE_DEBUG_INFO)
22170     {
22171       /* Emit the .loc directive understood by GNU as.  */
22172       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22173       if (is_stmt != table->is_stmt)
22174         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22175       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22176         fprintf (asm_out_file, " discriminator %d", discriminator);
22177       fputc ('\n', asm_out_file);
22178     }
22179   else
22180     {
22181       unsigned int label_num = ++line_info_label_num;
22182
22183       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22184
22185       push_dw_line_info_entry (table, LI_set_address, label_num);
22186       if (file_num != table->file_num)
22187         push_dw_line_info_entry (table, LI_set_file, file_num);
22188       if (discriminator != table->discrim_num)
22189         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22190       if (is_stmt != table->is_stmt)
22191         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22192       push_dw_line_info_entry (table, LI_set_line, line);
22193     }
22194
22195   table->file_num = file_num;
22196   table->line_num = line;
22197   table->discrim_num = discriminator;
22198   table->is_stmt = is_stmt;
22199   table->in_use = true;
22200 }
22201
22202 /* Record the beginning of a new source file.  */
22203
22204 static void
22205 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22206 {
22207   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22208     {
22209       /* Record the beginning of the file for break_out_includes.  */
22210       dw_die_ref bincl_die;
22211
22212       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22213       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22214     }
22215
22216   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22217     {
22218       macinfo_entry e;
22219       e.code = DW_MACINFO_start_file;
22220       e.lineno = lineno;
22221       e.info = xstrdup (filename);
22222       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22223     }
22224 }
22225
22226 /* Record the end of a source file.  */
22227
22228 static void
22229 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22230 {
22231   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22232     /* Record the end of the file for break_out_includes.  */
22233     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22234
22235   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22236     {
22237       macinfo_entry e;
22238       e.code = DW_MACINFO_end_file;
22239       e.lineno = lineno;
22240       e.info = NULL;
22241       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22242     }
22243 }
22244
22245 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22246    the tail part of the directive line, i.e. the part which is past the
22247    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22248
22249 static void
22250 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22251                   const char *buffer ATTRIBUTE_UNUSED)
22252 {
22253   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22254     {
22255       macinfo_entry e;
22256       e.code = DW_MACINFO_define;
22257       e.lineno = lineno;
22258       e.info = xstrdup (buffer);;
22259       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22260     }
22261 }
22262
22263 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22264    the tail part of the directive line, i.e. the part which is past the
22265    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22266
22267 static void
22268 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22269                  const char *buffer ATTRIBUTE_UNUSED)
22270 {
22271   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22272     {
22273       macinfo_entry e;
22274       e.code = DW_MACINFO_undef;
22275       e.lineno = lineno;
22276       e.info = xstrdup (buffer);;
22277       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22278     }
22279 }
22280
22281 static void
22282 output_macinfo (void)
22283 {
22284   unsigned i;
22285   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22286   macinfo_entry *ref;
22287
22288   if (! length)
22289     return;
22290
22291   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22292     {
22293       switch (ref->code)
22294         {
22295           case DW_MACINFO_start_file:
22296             {
22297               int file_num = maybe_emit_file (lookup_filename (ref->info));
22298               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22299               dw2_asm_output_data_uleb128 
22300                         (ref->lineno, "Included from line number %lu", 
22301                                                 (unsigned long)ref->lineno);
22302               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22303             }
22304             break;
22305           case DW_MACINFO_end_file:
22306             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22307             break;
22308           case DW_MACINFO_define:
22309             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22310             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22311                                                 (unsigned long)ref->lineno);
22312             dw2_asm_output_nstring (ref->info, -1, "The macro");
22313             break;
22314           case DW_MACINFO_undef:
22315             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22316             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22317                                                 (unsigned long)ref->lineno);
22318             dw2_asm_output_nstring (ref->info, -1, "The macro");
22319             break;
22320           default:
22321            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22322              ASM_COMMENT_START, (unsigned long)ref->code);
22323           break;
22324         }
22325     }
22326 }
22327
22328 /* Set up for Dwarf output at the start of compilation.  */
22329
22330 static void
22331 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22332 {
22333   /* Allocate the file_table.  */
22334   file_table = htab_create_ggc (50, file_table_hash,
22335                                 file_table_eq, NULL);
22336
22337   /* Allocate the decl_die_table.  */
22338   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22339                                     decl_die_table_eq, NULL);
22340
22341   /* Allocate the decl_loc_table.  */
22342   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22343                                     decl_loc_table_eq, NULL);
22344
22345   /* Allocate the cached_dw_loc_list_table.  */
22346   cached_dw_loc_list_table
22347     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22348                        cached_dw_loc_list_table_eq, NULL);
22349
22350   /* Allocate the initial hunk of the decl_scope_table.  */
22351   decl_scope_table = VEC_alloc (tree, gc, 256);
22352
22353   /* Allocate the initial hunk of the abbrev_die_table.  */
22354   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22355     (ABBREV_DIE_TABLE_INCREMENT);
22356   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22357   /* Zero-th entry is allocated, but unused.  */
22358   abbrev_die_table_in_use = 1;
22359
22360   /* Allocate the pubtypes and pubnames vectors.  */
22361   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22362   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22363
22364   incomplete_types = VEC_alloc (tree, gc, 64);
22365
22366   used_rtx_array = VEC_alloc (rtx, gc, 32);
22367
22368   debug_info_section = get_section (DEBUG_INFO_SECTION,
22369                                     SECTION_DEBUG, NULL);
22370   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22371                                       SECTION_DEBUG, NULL);
22372   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22373                                        SECTION_DEBUG, NULL);
22374   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22375                                        SECTION_DEBUG, NULL);
22376   debug_line_section = get_section (DEBUG_LINE_SECTION,
22377                                     SECTION_DEBUG, NULL);
22378   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22379                                    SECTION_DEBUG, NULL);
22380   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22381                                         SECTION_DEBUG, NULL);
22382   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22383                                         SECTION_DEBUG, NULL);
22384   debug_str_section = get_section (DEBUG_STR_SECTION,
22385                                    DEBUG_STR_SECTION_FLAGS, NULL);
22386   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22387                                       SECTION_DEBUG, NULL);
22388   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22389                                      SECTION_DEBUG, NULL);
22390
22391   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22392   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22393                                DEBUG_ABBREV_SECTION_LABEL, 0);
22394   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22395   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22396                                COLD_TEXT_SECTION_LABEL, 0);
22397   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22398
22399   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22400                                DEBUG_INFO_SECTION_LABEL, 0);
22401   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22402                                DEBUG_LINE_SECTION_LABEL, 0);
22403   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22404                                DEBUG_RANGES_SECTION_LABEL, 0);
22405   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22406                                DEBUG_MACINFO_SECTION_LABEL, 0);
22407
22408   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22409     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22410
22411   switch_to_section (text_section);
22412   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22413 }
22414
22415 /* Called before cgraph_optimize starts outputtting functions, variables
22416    and toplevel asms into assembly.  */
22417
22418 static void
22419 dwarf2out_assembly_start (void)
22420 {
22421   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22422       && dwarf2out_do_cfi_asm ()
22423       && (!(flag_unwind_tables || flag_exceptions)
22424           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22425     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22426 }
22427
22428 /* A helper function for dwarf2out_finish called through
22429    htab_traverse.  Emit one queued .debug_str string.  */
22430
22431 static int
22432 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22433 {
22434   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22435
22436   if (node->label && node->refcount)
22437     {
22438       switch_to_section (debug_str_section);
22439       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22440       assemble_string (node->str, strlen (node->str) + 1);
22441     }
22442
22443   return 1;
22444 }
22445
22446 #if ENABLE_ASSERT_CHECKING
22447 /* Verify that all marks are clear.  */
22448
22449 static void
22450 verify_marks_clear (dw_die_ref die)
22451 {
22452   dw_die_ref c;
22453
22454   gcc_assert (! die->die_mark);
22455   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22456 }
22457 #endif /* ENABLE_ASSERT_CHECKING */
22458
22459 /* Clear the marks for a die and its children.
22460    Be cool if the mark isn't set.  */
22461
22462 static void
22463 prune_unmark_dies (dw_die_ref die)
22464 {
22465   dw_die_ref c;
22466
22467   if (die->die_mark)
22468     die->die_mark = 0;
22469   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22470 }
22471
22472 /* Given DIE that we're marking as used, find any other dies
22473    it references as attributes and mark them as used.  */
22474
22475 static void
22476 prune_unused_types_walk_attribs (dw_die_ref die)
22477 {
22478   dw_attr_ref a;
22479   unsigned ix;
22480
22481   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22482     {
22483       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22484         {
22485           /* A reference to another DIE.
22486              Make sure that it will get emitted.
22487              If it was broken out into a comdat group, don't follow it.  */
22488           if (! use_debug_types
22489               || a->dw_attr == DW_AT_specification
22490               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22491             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22492         }
22493       /* Set the string's refcount to 0 so that prune_unused_types_mark
22494          accounts properly for it.  */
22495       if (AT_class (a) == dw_val_class_str)
22496         a->dw_attr_val.v.val_str->refcount = 0;
22497     }
22498 }
22499
22500 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22501
22502 static void
22503 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22504 {
22505   dw_die_ref c;
22506
22507   if (die == NULL || die->die_child == NULL)
22508     return;
22509   c = die->die_child;
22510   do
22511     {
22512       switch (c->die_tag)
22513         {
22514         case DW_TAG_template_type_param:
22515         case DW_TAG_template_value_param:
22516         case DW_TAG_GNU_template_template_param:
22517         case DW_TAG_GNU_template_parameter_pack:
22518           prune_unused_types_mark (c, 1);
22519           break;
22520         default:
22521           break;
22522         }
22523       c = c->die_sib;
22524     } while (c && c != die->die_child);
22525 }
22526
22527 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22528    to DIE's children.  */
22529
22530 static void
22531 prune_unused_types_mark (dw_die_ref die, int dokids)
22532 {
22533   dw_die_ref c;
22534
22535   if (die->die_mark == 0)
22536     {
22537       /* We haven't done this node yet.  Mark it as used.  */
22538       die->die_mark = 1;
22539       /* If this is the DIE of a generic type instantiation,
22540          mark the children DIEs that describe its generic parms and
22541          args.  */
22542       prune_unused_types_mark_generic_parms_dies (die);
22543
22544       /* We also have to mark its parents as used.
22545          (But we don't want to mark our parents' kids due to this.)  */
22546       if (die->die_parent)
22547         prune_unused_types_mark (die->die_parent, 0);
22548
22549       /* Mark any referenced nodes.  */
22550       prune_unused_types_walk_attribs (die);
22551
22552       /* If this node is a specification,
22553          also mark the definition, if it exists.  */
22554       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22555         prune_unused_types_mark (die->die_definition, 1);
22556     }
22557
22558   if (dokids && die->die_mark != 2)
22559     {
22560       /* We need to walk the children, but haven't done so yet.
22561          Remember that we've walked the kids.  */
22562       die->die_mark = 2;
22563
22564       /* If this is an array type, we need to make sure our
22565          kids get marked, even if they're types.  If we're
22566          breaking out types into comdat sections, do this
22567          for all type definitions.  */
22568       if (die->die_tag == DW_TAG_array_type
22569           || (use_debug_types
22570               && is_type_die (die) && ! is_declaration_die (die)))
22571         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22572       else
22573         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22574     }
22575 }
22576
22577 /* For local classes, look if any static member functions were emitted
22578    and if so, mark them.  */
22579
22580 static void
22581 prune_unused_types_walk_local_classes (dw_die_ref die)
22582 {
22583   dw_die_ref c;
22584
22585   if (die->die_mark == 2)
22586     return;
22587
22588   switch (die->die_tag)
22589     {
22590     case DW_TAG_structure_type:
22591     case DW_TAG_union_type:
22592     case DW_TAG_class_type:
22593       break;
22594
22595     case DW_TAG_subprogram:
22596       if (!get_AT_flag (die, DW_AT_declaration)
22597           || die->die_definition != NULL)
22598         prune_unused_types_mark (die, 1);
22599       return;
22600
22601     default:
22602       return;
22603     }
22604
22605   /* Mark children.  */
22606   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22607 }
22608
22609 /* Walk the tree DIE and mark types that we actually use.  */
22610
22611 static void
22612 prune_unused_types_walk (dw_die_ref die)
22613 {
22614   dw_die_ref c;
22615
22616   /* Don't do anything if this node is already marked and
22617      children have been marked as well.  */
22618   if (die->die_mark == 2)
22619     return;
22620
22621   switch (die->die_tag)
22622     {
22623     case DW_TAG_structure_type:
22624     case DW_TAG_union_type:
22625     case DW_TAG_class_type:
22626       if (die->die_perennial_p)
22627         break;
22628
22629       for (c = die->die_parent; c; c = c->die_parent)
22630         if (c->die_tag == DW_TAG_subprogram)
22631           break;
22632
22633       /* Finding used static member functions inside of classes
22634          is needed just for local classes, because for other classes
22635          static member function DIEs with DW_AT_specification
22636          are emitted outside of the DW_TAG_*_type.  If we ever change
22637          it, we'd need to call this even for non-local classes.  */
22638       if (c)
22639         prune_unused_types_walk_local_classes (die);
22640
22641       /* It's a type node --- don't mark it.  */
22642       return;
22643
22644     case DW_TAG_const_type:
22645     case DW_TAG_packed_type:
22646     case DW_TAG_pointer_type:
22647     case DW_TAG_reference_type:
22648     case DW_TAG_rvalue_reference_type:
22649     case DW_TAG_volatile_type:
22650     case DW_TAG_typedef:
22651     case DW_TAG_array_type:
22652     case DW_TAG_interface_type:
22653     case DW_TAG_friend:
22654     case DW_TAG_variant_part:
22655     case DW_TAG_enumeration_type:
22656     case DW_TAG_subroutine_type:
22657     case DW_TAG_string_type:
22658     case DW_TAG_set_type:
22659     case DW_TAG_subrange_type:
22660     case DW_TAG_ptr_to_member_type:
22661     case DW_TAG_file_type:
22662       if (die->die_perennial_p)
22663         break;
22664
22665       /* It's a type node --- don't mark it.  */
22666       return;
22667
22668     default:
22669       /* Mark everything else.  */
22670       break;
22671   }
22672
22673   if (die->die_mark == 0)
22674     {
22675       die->die_mark = 1;
22676
22677       /* Now, mark any dies referenced from here.  */
22678       prune_unused_types_walk_attribs (die);
22679     }
22680
22681   die->die_mark = 2;
22682
22683   /* Mark children.  */
22684   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22685 }
22686
22687 /* Increment the string counts on strings referred to from DIE's
22688    attributes.  */
22689
22690 static void
22691 prune_unused_types_update_strings (dw_die_ref die)
22692 {
22693   dw_attr_ref a;
22694   unsigned ix;
22695
22696   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22697     if (AT_class (a) == dw_val_class_str)
22698       {
22699         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22700         s->refcount++;
22701         /* Avoid unnecessarily putting strings that are used less than
22702            twice in the hash table.  */
22703         if (s->refcount
22704             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22705           {
22706             void ** slot;
22707             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22708                                              htab_hash_string (s->str),
22709                                              INSERT);
22710             gcc_assert (*slot == NULL);
22711             *slot = s;
22712           }
22713       }
22714 }
22715
22716 /* Remove from the tree DIE any dies that aren't marked.  */
22717
22718 static void
22719 prune_unused_types_prune (dw_die_ref die)
22720 {
22721   dw_die_ref c;
22722
22723   gcc_assert (die->die_mark);
22724   prune_unused_types_update_strings (die);
22725
22726   if (! die->die_child)
22727     return;
22728
22729   c = die->die_child;
22730   do {
22731     dw_die_ref prev = c;
22732     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22733       if (c == die->die_child)
22734         {
22735           /* No marked children between 'prev' and the end of the list.  */
22736           if (prev == c)
22737             /* No marked children at all.  */
22738             die->die_child = NULL;
22739           else
22740             {
22741               prev->die_sib = c->die_sib;
22742               die->die_child = prev;
22743             }
22744           return;
22745         }
22746
22747     if (c != prev->die_sib)
22748       prev->die_sib = c;
22749     prune_unused_types_prune (c);
22750   } while (c != die->die_child);
22751 }
22752
22753 /* A helper function for dwarf2out_finish called through
22754    htab_traverse.  Clear .debug_str strings that we haven't already
22755    decided to emit.  */
22756
22757 static int
22758 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22759 {
22760   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22761
22762   if (!node->label || !node->refcount)
22763     htab_clear_slot (debug_str_hash, h);
22764
22765   return 1;
22766 }
22767
22768 /* Remove dies representing declarations that we never use.  */
22769
22770 static void
22771 prune_unused_types (void)
22772 {
22773   unsigned int i;
22774   limbo_die_node *node;
22775   comdat_type_node *ctnode;
22776   pubname_ref pub;
22777
22778 #if ENABLE_ASSERT_CHECKING
22779   /* All the marks should already be clear.  */
22780   verify_marks_clear (comp_unit_die ());
22781   for (node = limbo_die_list; node; node = node->next)
22782     verify_marks_clear (node->die);
22783   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22784     verify_marks_clear (ctnode->root_die);
22785 #endif /* ENABLE_ASSERT_CHECKING */
22786
22787   /* Mark types that are used in global variables.  */
22788   premark_types_used_by_global_vars ();
22789
22790   /* Set the mark on nodes that are actually used.  */
22791   prune_unused_types_walk (comp_unit_die ());
22792   for (node = limbo_die_list; node; node = node->next)
22793     prune_unused_types_walk (node->die);
22794   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22795     {
22796       prune_unused_types_walk (ctnode->root_die);
22797       prune_unused_types_mark (ctnode->type_die, 1);
22798     }
22799
22800   /* Also set the mark on nodes referenced from the
22801      pubname_table.  */
22802   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22803     prune_unused_types_mark (pub->die, 1);
22804
22805   /* Get rid of nodes that aren't marked; and update the string counts.  */
22806   if (debug_str_hash && debug_str_hash_forced)
22807     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22808   else if (debug_str_hash)
22809     htab_empty (debug_str_hash);
22810   prune_unused_types_prune (comp_unit_die ());
22811   for (node = limbo_die_list; node; node = node->next)
22812     prune_unused_types_prune (node->die);
22813   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22814     prune_unused_types_prune (ctnode->root_die);
22815
22816   /* Leave the marks clear.  */
22817   prune_unmark_dies (comp_unit_die ());
22818   for (node = limbo_die_list; node; node = node->next)
22819     prune_unmark_dies (node->die);
22820   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22821     prune_unmark_dies (ctnode->root_die);
22822 }
22823
22824 /* Set the parameter to true if there are any relative pathnames in
22825    the file table.  */
22826 static int
22827 file_table_relative_p (void ** slot, void *param)
22828 {
22829   bool *p = (bool *) param;
22830   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22831   if (!IS_ABSOLUTE_PATH (d->filename))
22832     {
22833       *p = true;
22834       return 0;
22835     }
22836   return 1;
22837 }
22838
22839 /* Routines to manipulate hash table of comdat type units.  */
22840
22841 static hashval_t
22842 htab_ct_hash (const void *of)
22843 {
22844   hashval_t h;
22845   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22846
22847   memcpy (&h, type_node->signature, sizeof (h));
22848   return h;
22849 }
22850
22851 static int
22852 htab_ct_eq (const void *of1, const void *of2)
22853 {
22854   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22855   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22856
22857   return (! memcmp (type_node_1->signature, type_node_2->signature,
22858                     DWARF_TYPE_SIGNATURE_SIZE));
22859 }
22860
22861 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22862    to the location it would have been added, should we know its
22863    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22864    probably improve compactness of debug info, removing equivalent
22865    abbrevs, and hide any differences caused by deferring the
22866    computation of the assembler name, triggered by e.g. PCH.  */
22867
22868 static inline void
22869 move_linkage_attr (dw_die_ref die)
22870 {
22871   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22872   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22873
22874   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22875               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22876
22877   while (--ix > 0)
22878     {
22879       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22880
22881       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22882         break;
22883     }
22884
22885   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22886     {
22887       VEC_pop (dw_attr_node, die->die_attr);
22888       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22889     }
22890 }
22891
22892 /* Helper function for resolve_addr, attempt to resolve
22893    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22894    SYMBOL_REFs refer to variables emitted in the current CU.  */
22895
22896 static int
22897 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22898 {
22899   rtx rtl = *addr;
22900
22901   if (GET_CODE (rtl) == CONST_STRING)
22902     {
22903       size_t len = strlen (XSTR (rtl, 0)) + 1;
22904       tree t = build_string (len, XSTR (rtl, 0));
22905       tree tlen = build_int_cst (NULL_TREE, len - 1);
22906       TREE_TYPE (t)
22907         = build_array_type (char_type_node, build_index_type (tlen));
22908       rtl = lookup_constant_def (t);
22909       if (!rtl || !MEM_P (rtl))
22910         return 1;
22911       rtl = XEXP (rtl, 0);
22912       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22913       *addr = rtl;
22914       return 0;
22915     }
22916
22917   if (GET_CODE (rtl) == SYMBOL_REF
22918       && SYMBOL_REF_DECL (rtl))
22919     {
22920       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22921         {
22922           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22923             return 1;
22924         }
22925       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22926         return 1;
22927     }
22928
22929   if (GET_CODE (rtl) == CONST
22930       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22931     return 1;
22932
22933   return 0;
22934 }
22935
22936 /* Helper function for resolve_addr, handle one location
22937    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22938    the location list couldn't be resolved.  */
22939
22940 static bool
22941 resolve_addr_in_expr (dw_loc_descr_ref loc)
22942 {
22943   for (; loc; loc = loc->dw_loc_next)
22944     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22945          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22946         || (loc->dw_loc_opc == DW_OP_implicit_value
22947             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22948             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22949       return false;
22950     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22951              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22952       {
22953         dw_die_ref ref
22954           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22955         if (ref == NULL)
22956           return false;
22957         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22958         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22959         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22960       }
22961   return true;
22962 }
22963
22964 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22965    an address in .rodata section if the string literal is emitted there,
22966    or remove the containing location list or replace DW_AT_const_value
22967    with DW_AT_location and empty location expression, if it isn't found
22968    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22969    to something that has been emitted in the current CU.  */
22970
22971 static void
22972 resolve_addr (dw_die_ref die)
22973 {
22974   dw_die_ref c;
22975   dw_attr_ref a;
22976   dw_loc_list_ref *curr, *start, loc;
22977   unsigned ix;
22978
22979   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22980     switch (AT_class (a))
22981       {
22982       case dw_val_class_loc_list:
22983         start = curr = AT_loc_list_ptr (a);
22984         loc = *curr;
22985         gcc_assert (loc);
22986         /* The same list can be referenced more than once.  See if we have
22987            already recorded the result from a previous pass.  */
22988         if (loc->replaced)
22989           *curr = loc->dw_loc_next;
22990         else if (!loc->resolved_addr)
22991           {
22992             /* As things stand, we do not expect or allow one die to
22993                reference a suffix of another die's location list chain.
22994                References must be identical or completely separate.
22995                There is therefore no need to cache the result of this
22996                pass on any list other than the first; doing so
22997                would lead to unnecessary writes.  */
22998             while (*curr)
22999               {
23000                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23001                 if (!resolve_addr_in_expr ((*curr)->expr))
23002                   {
23003                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23004                     if (next && (*curr)->ll_symbol)
23005                       {
23006                         gcc_assert (!next->ll_symbol);
23007                         next->ll_symbol = (*curr)->ll_symbol;
23008                       }
23009                     *curr = next;
23010                   }
23011                 else
23012                   curr = &(*curr)->dw_loc_next;
23013               }
23014             if (loc == *start)
23015               loc->resolved_addr = 1;
23016             else
23017               {
23018                 loc->replaced = 1;
23019                 loc->dw_loc_next = *start;
23020               }
23021           }
23022         if (!*start)
23023           {
23024             remove_AT (die, a->dw_attr);
23025             ix--;
23026           }
23027         break;
23028       case dw_val_class_loc:
23029         if (!resolve_addr_in_expr (AT_loc (a)))
23030           {
23031             remove_AT (die, a->dw_attr);
23032             ix--;
23033           }
23034         break;
23035       case dw_val_class_addr:
23036         if (a->dw_attr == DW_AT_const_value
23037             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23038           {
23039             remove_AT (die, a->dw_attr);
23040             ix--;
23041           }
23042         if (die->die_tag == DW_TAG_GNU_call_site
23043             && a->dw_attr == DW_AT_abstract_origin)
23044           {
23045             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23046             dw_die_ref tdie = lookup_decl_die (tdecl);
23047             if (tdie == NULL
23048                 && DECL_EXTERNAL (tdecl)
23049                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23050               {
23051                 force_decl_die (tdecl);
23052                 tdie = lookup_decl_die (tdecl);
23053               }
23054             if (tdie)
23055               {
23056                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23057                 a->dw_attr_val.v.val_die_ref.die = tdie;
23058                 a->dw_attr_val.v.val_die_ref.external = 0;
23059               }
23060             else
23061               {
23062                 remove_AT (die, a->dw_attr);
23063                 ix--;
23064               }
23065           }
23066         break;
23067       default:
23068         break;
23069       }
23070
23071   FOR_EACH_CHILD (die, c, resolve_addr (c));
23072 }
23073 \f
23074 /* Helper routines for optimize_location_lists.
23075    This pass tries to share identical local lists in .debug_loc
23076    section.  */
23077
23078 /* Iteratively hash operands of LOC opcode.  */
23079
23080 static inline hashval_t
23081 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23082 {
23083   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23084   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23085
23086   switch (loc->dw_loc_opc)
23087     {
23088     case DW_OP_const4u:
23089     case DW_OP_const8u:
23090       if (loc->dtprel)
23091         goto hash_addr;
23092       /* FALLTHRU */
23093     case DW_OP_const1u:
23094     case DW_OP_const1s:
23095     case DW_OP_const2u:
23096     case DW_OP_const2s:
23097     case DW_OP_const4s:
23098     case DW_OP_const8s:
23099     case DW_OP_constu:
23100     case DW_OP_consts:
23101     case DW_OP_pick:
23102     case DW_OP_plus_uconst:
23103     case DW_OP_breg0:
23104     case DW_OP_breg1:
23105     case DW_OP_breg2:
23106     case DW_OP_breg3:
23107     case DW_OP_breg4:
23108     case DW_OP_breg5:
23109     case DW_OP_breg6:
23110     case DW_OP_breg7:
23111     case DW_OP_breg8:
23112     case DW_OP_breg9:
23113     case DW_OP_breg10:
23114     case DW_OP_breg11:
23115     case DW_OP_breg12:
23116     case DW_OP_breg13:
23117     case DW_OP_breg14:
23118     case DW_OP_breg15:
23119     case DW_OP_breg16:
23120     case DW_OP_breg17:
23121     case DW_OP_breg18:
23122     case DW_OP_breg19:
23123     case DW_OP_breg20:
23124     case DW_OP_breg21:
23125     case DW_OP_breg22:
23126     case DW_OP_breg23:
23127     case DW_OP_breg24:
23128     case DW_OP_breg25:
23129     case DW_OP_breg26:
23130     case DW_OP_breg27:
23131     case DW_OP_breg28:
23132     case DW_OP_breg29:
23133     case DW_OP_breg30:
23134     case DW_OP_breg31:
23135     case DW_OP_regx:
23136     case DW_OP_fbreg:
23137     case DW_OP_piece:
23138     case DW_OP_deref_size:
23139     case DW_OP_xderef_size:
23140       hash = iterative_hash_object (val1->v.val_int, hash);
23141       break;
23142     case DW_OP_skip:
23143     case DW_OP_bra:
23144       {
23145         int offset;
23146
23147         gcc_assert (val1->val_class == dw_val_class_loc);
23148         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23149         hash = iterative_hash_object (offset, hash);
23150       }
23151       break;
23152     case DW_OP_implicit_value:
23153       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23154       switch (val2->val_class)
23155         {
23156         case dw_val_class_const:
23157           hash = iterative_hash_object (val2->v.val_int, hash);
23158           break;
23159         case dw_val_class_vec:
23160           {
23161             unsigned int elt_size = val2->v.val_vec.elt_size;
23162             unsigned int len = val2->v.val_vec.length;
23163
23164             hash = iterative_hash_object (elt_size, hash);
23165             hash = iterative_hash_object (len, hash);
23166             hash = iterative_hash (val2->v.val_vec.array,
23167                                    len * elt_size, hash);
23168           }
23169           break;
23170         case dw_val_class_const_double:
23171           hash = iterative_hash_object (val2->v.val_double.low, hash);
23172           hash = iterative_hash_object (val2->v.val_double.high, hash);
23173           break;
23174         case dw_val_class_addr:
23175           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23176           break;
23177         default:
23178           gcc_unreachable ();
23179         }
23180       break;
23181     case DW_OP_bregx:
23182     case DW_OP_bit_piece:
23183       hash = iterative_hash_object (val1->v.val_int, hash);
23184       hash = iterative_hash_object (val2->v.val_int, hash);
23185       break;
23186     case DW_OP_addr:
23187     hash_addr:
23188       if (loc->dtprel)
23189         {
23190           unsigned char dtprel = 0xd1;
23191           hash = iterative_hash_object (dtprel, hash);
23192         }
23193       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23194       break;
23195     case DW_OP_GNU_implicit_pointer:
23196       hash = iterative_hash_object (val2->v.val_int, hash);
23197       break;
23198     case DW_OP_GNU_entry_value:
23199       hash = hash_loc_operands (val1->v.val_loc, hash);
23200       break;
23201
23202     default:
23203       /* Other codes have no operands.  */
23204       break;
23205     }
23206   return hash;
23207 }
23208
23209 /* Iteratively hash the whole DWARF location expression LOC.  */
23210
23211 static inline hashval_t
23212 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23213 {
23214   dw_loc_descr_ref l;
23215   bool sizes_computed = false;
23216   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23217   size_of_locs (loc);
23218
23219   for (l = loc; l != NULL; l = l->dw_loc_next)
23220     {
23221       enum dwarf_location_atom opc = l->dw_loc_opc;
23222       hash = iterative_hash_object (opc, hash);
23223       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23224         {
23225           size_of_locs (loc);
23226           sizes_computed = true;
23227         }
23228       hash = hash_loc_operands (l, hash);
23229     }
23230   return hash;
23231 }
23232
23233 /* Compute hash of the whole location list LIST_HEAD.  */
23234
23235 static inline void
23236 hash_loc_list (dw_loc_list_ref list_head)
23237 {
23238   dw_loc_list_ref curr = list_head;
23239   hashval_t hash = 0;
23240
23241   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23242     {
23243       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23244       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23245       if (curr->section)
23246         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23247                                hash);
23248       hash = hash_locs (curr->expr, hash);
23249     }
23250   list_head->hash = hash;
23251 }
23252
23253 /* Return true if X and Y opcodes have the same operands.  */
23254
23255 static inline bool
23256 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23257 {
23258   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23259   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23260   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23261   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23262
23263   switch (x->dw_loc_opc)
23264     {
23265     case DW_OP_const4u:
23266     case DW_OP_const8u:
23267       if (x->dtprel)
23268         goto hash_addr;
23269       /* FALLTHRU */
23270     case DW_OP_const1u:
23271     case DW_OP_const1s:
23272     case DW_OP_const2u:
23273     case DW_OP_const2s:
23274     case DW_OP_const4s:
23275     case DW_OP_const8s:
23276     case DW_OP_constu:
23277     case DW_OP_consts:
23278     case DW_OP_pick:
23279     case DW_OP_plus_uconst:
23280     case DW_OP_breg0:
23281     case DW_OP_breg1:
23282     case DW_OP_breg2:
23283     case DW_OP_breg3:
23284     case DW_OP_breg4:
23285     case DW_OP_breg5:
23286     case DW_OP_breg6:
23287     case DW_OP_breg7:
23288     case DW_OP_breg8:
23289     case DW_OP_breg9:
23290     case DW_OP_breg10:
23291     case DW_OP_breg11:
23292     case DW_OP_breg12:
23293     case DW_OP_breg13:
23294     case DW_OP_breg14:
23295     case DW_OP_breg15:
23296     case DW_OP_breg16:
23297     case DW_OP_breg17:
23298     case DW_OP_breg18:
23299     case DW_OP_breg19:
23300     case DW_OP_breg20:
23301     case DW_OP_breg21:
23302     case DW_OP_breg22:
23303     case DW_OP_breg23:
23304     case DW_OP_breg24:
23305     case DW_OP_breg25:
23306     case DW_OP_breg26:
23307     case DW_OP_breg27:
23308     case DW_OP_breg28:
23309     case DW_OP_breg29:
23310     case DW_OP_breg30:
23311     case DW_OP_breg31:
23312     case DW_OP_regx:
23313     case DW_OP_fbreg:
23314     case DW_OP_piece:
23315     case DW_OP_deref_size:
23316     case DW_OP_xderef_size:
23317       return valx1->v.val_int == valy1->v.val_int;
23318     case DW_OP_skip:
23319     case DW_OP_bra:
23320       gcc_assert (valx1->val_class == dw_val_class_loc
23321                   && valy1->val_class == dw_val_class_loc
23322                   && x->dw_loc_addr == y->dw_loc_addr);
23323       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23324     case DW_OP_implicit_value:
23325       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23326           || valx2->val_class != valy2->val_class)
23327         return false;
23328       switch (valx2->val_class)
23329         {
23330         case dw_val_class_const:
23331           return valx2->v.val_int == valy2->v.val_int;
23332         case dw_val_class_vec:
23333           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23334                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23335                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23336                             valx2->v.val_vec.elt_size
23337                             * valx2->v.val_vec.length) == 0;
23338         case dw_val_class_const_double:
23339           return valx2->v.val_double.low == valy2->v.val_double.low
23340                  && valx2->v.val_double.high == valy2->v.val_double.high;
23341         case dw_val_class_addr:
23342           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23343         default:
23344           gcc_unreachable ();
23345         }
23346     case DW_OP_bregx:
23347     case DW_OP_bit_piece:
23348       return valx1->v.val_int == valy1->v.val_int
23349              && valx2->v.val_int == valy2->v.val_int;
23350     case DW_OP_addr:
23351     hash_addr:
23352       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23353     case DW_OP_GNU_implicit_pointer:
23354       return valx1->val_class == dw_val_class_die_ref
23355              && valx1->val_class == valy1->val_class
23356              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23357              && valx2->v.val_int == valy2->v.val_int;
23358     case DW_OP_GNU_entry_value:
23359       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23360     default:
23361       /* Other codes have no operands.  */
23362       return true;
23363     }
23364 }
23365
23366 /* Return true if DWARF location expressions X and Y are the same.  */
23367
23368 static inline bool
23369 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23370 {
23371   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23372     if (x->dw_loc_opc != y->dw_loc_opc
23373         || x->dtprel != y->dtprel
23374         || !compare_loc_operands (x, y))
23375       break;
23376   return x == NULL && y == NULL;
23377 }
23378
23379 /* Return precomputed hash of location list X.  */
23380
23381 static hashval_t
23382 loc_list_hash (const void *x)
23383 {
23384   return ((const struct dw_loc_list_struct *) x)->hash;
23385 }
23386
23387 /* Return 1 if location lists X and Y are the same.  */
23388
23389 static int
23390 loc_list_eq (const void *x, const void *y)
23391 {
23392   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23393   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23394   if (a == b)
23395     return 1;
23396   if (a->hash != b->hash)
23397     return 0;
23398   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23399     if (strcmp (a->begin, b->begin) != 0
23400         || strcmp (a->end, b->end) != 0
23401         || (a->section == NULL) != (b->section == NULL)
23402         || (a->section && strcmp (a->section, b->section) != 0)
23403         || !compare_locs (a->expr, b->expr))
23404       break;
23405   return a == NULL && b == NULL;
23406 }
23407
23408 /* Recursively optimize location lists referenced from DIE
23409    children and share them whenever possible.  */
23410
23411 static void
23412 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23413 {
23414   dw_die_ref c;
23415   dw_attr_ref a;
23416   unsigned ix;
23417   void **slot;
23418
23419   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23420     if (AT_class (a) == dw_val_class_loc_list)
23421       {
23422         dw_loc_list_ref list = AT_loc_list (a);
23423         /* TODO: perform some optimizations here, before hashing
23424            it and storing into the hash table.  */
23425         hash_loc_list (list);
23426         slot = htab_find_slot_with_hash (htab, list, list->hash,
23427                                          INSERT);
23428         if (*slot == NULL)
23429           *slot = (void *) list;
23430         else
23431           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23432       }
23433
23434   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23435 }
23436
23437 /* Optimize location lists referenced from DIE
23438    children and share them whenever possible.  */
23439
23440 static void
23441 optimize_location_lists (dw_die_ref die)
23442 {
23443   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23444   optimize_location_lists_1 (die, htab);
23445   htab_delete (htab);
23446 }
23447 \f
23448 /* Output stuff that dwarf requires at the end of every file,
23449    and generate the DWARF-2 debugging info.  */
23450
23451 static void
23452 dwarf2out_finish (const char *filename)
23453 {
23454   limbo_die_node *node, *next_node;
23455   comdat_type_node *ctnode;
23456   htab_t comdat_type_table;
23457   unsigned int i;
23458
23459   gen_scheduled_generic_parms_dies ();
23460   gen_remaining_tmpl_value_param_die_attribute ();
23461
23462   /* Add the name for the main input file now.  We delayed this from
23463      dwarf2out_init to avoid complications with PCH.  */
23464   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23465   if (!IS_ABSOLUTE_PATH (filename))
23466     add_comp_dir_attribute (comp_unit_die ());
23467   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23468     {
23469       bool p = false;
23470       htab_traverse (file_table, file_table_relative_p, &p);
23471       if (p)
23472         add_comp_dir_attribute (comp_unit_die ());
23473     }
23474
23475   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23476     {
23477       add_location_or_const_value_attribute (
23478         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23479         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23480         false,
23481         DW_AT_location);
23482     }
23483
23484   /* Traverse the limbo die list, and add parent/child links.  The only
23485      dies without parents that should be here are concrete instances of
23486      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23487      For concrete instances, we can get the parent die from the abstract
23488      instance.  */
23489   for (node = limbo_die_list; node; node = next_node)
23490     {
23491       dw_die_ref die = node->die;
23492       next_node = node->next;
23493
23494       if (die->die_parent == NULL)
23495         {
23496           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23497
23498           if (origin)
23499             add_child_die (origin->die_parent, die);
23500           else if (is_cu_die (die))
23501             ;
23502           else if (seen_error ())
23503             /* It's OK to be confused by errors in the input.  */
23504             add_child_die (comp_unit_die (), die);
23505           else
23506             {
23507               /* In certain situations, the lexical block containing a
23508                  nested function can be optimized away, which results
23509                  in the nested function die being orphaned.  Likewise
23510                  with the return type of that nested function.  Force
23511                  this to be a child of the containing function.
23512
23513                  It may happen that even the containing function got fully
23514                  inlined and optimized out.  In that case we are lost and
23515                  assign the empty child.  This should not be big issue as
23516                  the function is likely unreachable too.  */
23517               tree context = NULL_TREE;
23518
23519               gcc_assert (node->created_for);
23520
23521               if (DECL_P (node->created_for))
23522                 context = DECL_CONTEXT (node->created_for);
23523               else if (TYPE_P (node->created_for))
23524                 context = TYPE_CONTEXT (node->created_for);
23525
23526               gcc_assert (context
23527                           && (TREE_CODE (context) == FUNCTION_DECL
23528                               || TREE_CODE (context) == NAMESPACE_DECL));
23529
23530               origin = lookup_decl_die (context);
23531               if (origin)
23532                 add_child_die (origin, die);
23533               else
23534                 add_child_die (comp_unit_die (), die);
23535             }
23536         }
23537     }
23538
23539   limbo_die_list = NULL;
23540
23541   resolve_addr (comp_unit_die ());
23542
23543   for (node = deferred_asm_name; node; node = node->next)
23544     {
23545       tree decl = node->created_for;
23546       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23547         {
23548           add_linkage_attr (node->die, decl);
23549           move_linkage_attr (node->die);
23550         }
23551     }
23552
23553   deferred_asm_name = NULL;
23554
23555   /* Walk through the list of incomplete types again, trying once more to
23556      emit full debugging info for them.  */
23557   retry_incomplete_types ();
23558
23559   if (flag_eliminate_unused_debug_types)
23560     prune_unused_types ();
23561
23562   /* Generate separate CUs for each of the include files we've seen.
23563      They will go into limbo_die_list.  */
23564   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23565     break_out_includes (comp_unit_die ());
23566
23567   /* Generate separate COMDAT sections for type DIEs. */
23568   if (use_debug_types)
23569     {
23570       break_out_comdat_types (comp_unit_die ());
23571
23572       /* Each new type_unit DIE was added to the limbo die list when created.
23573          Since these have all been added to comdat_type_list, clear the
23574          limbo die list.  */
23575       limbo_die_list = NULL;
23576
23577       /* For each new comdat type unit, copy declarations for incomplete
23578          types to make the new unit self-contained (i.e., no direct
23579          references to the main compile unit).  */
23580       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23581         copy_decls_for_unworthy_types (ctnode->root_die);
23582       copy_decls_for_unworthy_types (comp_unit_die ());
23583
23584       /* In the process of copying declarations from one unit to another,
23585          we may have left some declarations behind that are no longer
23586          referenced.  Prune them.  */
23587       prune_unused_types ();
23588     }
23589
23590   /* Traverse the DIE's and add add sibling attributes to those DIE's
23591      that have children.  */
23592   add_sibling_attributes (comp_unit_die ());
23593   for (node = limbo_die_list; node; node = node->next)
23594     add_sibling_attributes (node->die);
23595   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23596     add_sibling_attributes (ctnode->root_die);
23597
23598   /* Output a terminator label for the .text section.  */
23599   switch_to_section (text_section);
23600   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23601   if (cold_text_section)
23602     {
23603       switch_to_section (cold_text_section);
23604       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23605     }
23606
23607   /* We can only use the low/high_pc attributes if all of the code was
23608      in .text.  */
23609   if (!have_multiple_function_sections 
23610       || (dwarf_version < 3 && dwarf_strict))
23611     {
23612       /* Don't add if the CU has no associated code.  */
23613       if (text_section_used)
23614         {
23615           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23616           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23617         }
23618     }
23619   else
23620     {
23621       unsigned fde_idx = 0;
23622       bool range_list_added = false;
23623
23624       if (text_section_used)
23625         add_ranges_by_labels (comp_unit_die (), text_section_label,
23626                               text_end_label, &range_list_added);
23627       if (cold_text_section_used)
23628         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23629                               cold_end_label, &range_list_added);
23630
23631       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23632         {
23633           dw_fde_ref fde = &fde_table[fde_idx];
23634
23635           if (!fde->in_std_section)
23636             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23637                                   fde->dw_fde_end, &range_list_added);
23638           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23639             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
23640                                   fde->dw_fde_second_end, &range_list_added);
23641         }
23642
23643       if (range_list_added)
23644         {
23645           /* We need to give .debug_loc and .debug_ranges an appropriate
23646              "base address".  Use zero so that these addresses become
23647              absolute.  Historically, we've emitted the unexpected
23648              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23649              Emit both to give time for other tools to adapt.  */
23650           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23651           if (! dwarf_strict && dwarf_version < 4)
23652             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23653
23654           add_ranges (NULL);
23655         }
23656     }
23657
23658   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23659     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23660                     debug_line_section_label);
23661
23662   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23663     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23664
23665   if (have_location_lists)
23666     optimize_location_lists (comp_unit_die ());
23667
23668   /* Output all of the compilation units.  We put the main one last so that
23669      the offsets are available to output_pubnames.  */
23670   for (node = limbo_die_list; node; node = node->next)
23671     output_comp_unit (node->die, 0);
23672
23673   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23674   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23675     {
23676       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23677
23678       /* Don't output duplicate types.  */
23679       if (*slot != HTAB_EMPTY_ENTRY)
23680         continue;
23681
23682       /* Add a pointer to the line table for the main compilation unit
23683          so that the debugger can make sense of DW_AT_decl_file
23684          attributes.  */
23685       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23686         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23687                         debug_line_section_label);
23688
23689       output_comdat_type_unit (ctnode);
23690       *slot = ctnode;
23691     }
23692   htab_delete (comdat_type_table);
23693
23694   /* Output the main compilation unit if non-empty or if .debug_macinfo
23695      will be emitted.  */
23696   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23697
23698   /* Output the abbreviation table.  */
23699   switch_to_section (debug_abbrev_section);
23700   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23701   output_abbrev_section ();
23702
23703   /* Output location list section if necessary.  */
23704   if (have_location_lists)
23705     {
23706       /* Output the location lists info.  */
23707       switch_to_section (debug_loc_section);
23708       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23709                                    DEBUG_LOC_SECTION_LABEL, 0);
23710       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23711       output_location_lists (comp_unit_die ());
23712     }
23713
23714   /* Output public names table if necessary.  */
23715   if (!VEC_empty (pubname_entry, pubname_table))
23716     {
23717       gcc_assert (info_section_emitted);
23718       switch_to_section (debug_pubnames_section);
23719       output_pubnames (pubname_table);
23720     }
23721
23722   /* Output public types table if necessary.  */
23723   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23724      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23725      simply won't look for the section.  */
23726   if (!VEC_empty (pubname_entry, pubtype_table))
23727     {
23728       bool empty = false;
23729       
23730       if (flag_eliminate_unused_debug_types)
23731         {
23732           /* The pubtypes table might be emptied by pruning unused items.  */
23733           unsigned i;
23734           pubname_ref p;
23735           empty = true;
23736           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23737             if (p->die->die_offset != 0)
23738               {
23739                 empty = false;
23740                 break;
23741               }
23742         }
23743       if (!empty)
23744         {
23745           gcc_assert (info_section_emitted);
23746           switch_to_section (debug_pubtypes_section);
23747           output_pubnames (pubtype_table);
23748         }
23749     }
23750
23751   /* Output the address range information.  We only put functions in the
23752      arange table, so don't write it out if we don't have any.  */
23753   if (info_section_emitted)
23754     {
23755       unsigned long aranges_length = size_of_aranges ();
23756
23757       /* Empty .debug_aranges would contain just header and
23758          terminating 0,0.  */
23759       if (aranges_length
23760           != (unsigned long) (DWARF_ARANGES_HEADER_SIZE
23761                               + 2 * DWARF2_ADDR_SIZE))
23762         {
23763           switch_to_section (debug_aranges_section);
23764           output_aranges (aranges_length);
23765         }
23766     }
23767
23768   /* Output ranges section if necessary.  */
23769   if (ranges_table_in_use)
23770     {
23771       switch_to_section (debug_ranges_section);
23772       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23773       output_ranges ();
23774     }
23775
23776   /* Output the source line correspondence table.  We must do this
23777      even if there is no line information.  Otherwise, on an empty
23778      translation unit, we will generate a present, but empty,
23779      .debug_info section.  IRIX 6.5 `nm' will then complain when
23780      examining the file.  This is done late so that any filenames
23781      used by the debug_info section are marked as 'used'.  */
23782   switch_to_section (debug_line_section);
23783   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23784   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23785     output_line_info ();
23786
23787   /* Have to end the macro section.  */
23788   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23789     {
23790       switch_to_section (debug_macinfo_section);
23791       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23792       if (!VEC_empty (macinfo_entry, macinfo_table))
23793         output_macinfo ();
23794       dw2_asm_output_data (1, 0, "End compilation unit");
23795     }
23796
23797   /* If we emitted any DW_FORM_strp form attribute, output the string
23798      table too.  */
23799   if (debug_str_hash)
23800     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23801 }
23802
23803 #include "gt-dwarf2out.h"