OSDN Git Service

PR debug/48853
[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   enum dwarf_call_frame_info dw_cfi_opc;
271   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
272     dw_cfi_oprnd1;
273   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
274     dw_cfi_oprnd2;
275 }
276 dw_cfi_node;
277
278 DEF_VEC_P (dw_cfi_ref);
279 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
280 DEF_VEC_ALLOC_P (dw_cfi_ref, gc);
281
282 typedef VEC(dw_cfi_ref, gc) *cfi_vec;
283
284 /* This is how we define the location of the CFA. We use to handle it
285    as REG + OFFSET all the time,  but now it can be more complex.
286    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
287    Instead of passing around REG and OFFSET, we pass a copy
288    of this structure.  */
289 typedef struct cfa_loc {
290   HOST_WIDE_INT offset;
291   HOST_WIDE_INT base_offset;
292   unsigned int reg;
293   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
294   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
295 } dw_cfa_location;
296
297 /* All call frame descriptions (FDE's) in the GCC generated DWARF
298    refer to a single Common Information Entry (CIE), defined at
299    the beginning of the .debug_frame section.  This use of a single
300    CIE obviates the need to keep track of multiple CIE's
301    in the DWARF generation routines below.  */
302
303 typedef struct GTY(()) dw_fde_struct {
304   tree decl;
305   const char *dw_fde_begin;
306   const char *dw_fde_current_label;
307   const char *dw_fde_end;
308   const char *dw_fde_vms_end_prologue;
309   const char *dw_fde_vms_begin_epilogue;
310   const char *dw_fde_second_begin;
311   const char *dw_fde_second_end;
312   cfi_vec dw_fde_cfi;
313   int dw_fde_switch_cfi_index; /* Last CFI before switching sections.  */
314   HOST_WIDE_INT stack_realignment;
315   unsigned funcdef_number;
316   /* Dynamic realign argument pointer register.  */
317   unsigned int drap_reg;
318   /* Virtual dynamic realign argument pointer register.  */
319   unsigned int vdrap_reg;
320   /* These 3 flags are copied from rtl_data in function.h.  */
321   unsigned all_throwers_are_sibcalls : 1;
322   unsigned uses_eh_lsda : 1;
323   unsigned nothrow : 1;
324   /* Whether we did stack realign in this call frame.  */
325   unsigned stack_realign : 1;
326   /* Whether dynamic realign argument pointer register has been saved.  */
327   unsigned drap_reg_saved: 1;
328   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
329   unsigned in_std_section : 1;
330   /* True iff dw_fde_second_begin label is in text_section or
331      cold_text_section.  */
332   unsigned second_in_std_section : 1;
333 }
334 dw_fde_node;
335
336 /* Maximum size (in bytes) of an artificially generated label.  */
337 #define MAX_ARTIFICIAL_LABEL_BYTES      30
338
339 /* The size of addresses as they appear in the Dwarf 2 data.
340    Some architectures use word addresses to refer to code locations,
341    but Dwarf 2 info always uses byte addresses.  On such machines,
342    Dwarf 2 addresses need to be larger than the architecture's
343    pointers.  */
344 #ifndef DWARF2_ADDR_SIZE
345 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
346 #endif
347
348 /* The size in bytes of a DWARF field indicating an offset or length
349    relative to a debug info section, specified to be 4 bytes in the
350    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
351    as PTR_SIZE.  */
352
353 #ifndef DWARF_OFFSET_SIZE
354 #define DWARF_OFFSET_SIZE 4
355 #endif
356
357 /* The size in bytes of a DWARF 4 type signature.  */
358
359 #ifndef DWARF_TYPE_SIGNATURE_SIZE
360 #define DWARF_TYPE_SIGNATURE_SIZE 8
361 #endif
362
363 /* According to the (draft) DWARF 3 specification, the initial length
364    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
365    bytes are 0xffffffff, followed by the length stored in the next 8
366    bytes.
367
368    However, the SGI/MIPS ABI uses an initial length which is equal to
369    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
370
371 #ifndef DWARF_INITIAL_LENGTH_SIZE
372 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
373 #endif
374
375 /* Round SIZE up to the nearest BOUNDARY.  */
376 #define DWARF_ROUND(SIZE,BOUNDARY) \
377   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
378
379 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
380 #ifndef DWARF_CIE_DATA_ALIGNMENT
381 #ifdef STACK_GROWS_DOWNWARD
382 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
383 #else
384 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
385 #endif
386 #endif
387
388 /* CIE identifier.  */
389 #if HOST_BITS_PER_WIDE_INT >= 64
390 #define DWARF_CIE_ID \
391   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
392 #else
393 #define DWARF_CIE_ID DW_CIE_ID
394 #endif
395
396 /* A pointer to the base of a table that contains frame description
397    information for each routine.  */
398 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
399
400 /* Number of elements currently allocated for fde_table.  */
401 static GTY(()) unsigned fde_table_allocated;
402
403 /* Number of elements in fde_table currently in use.  */
404 static GTY(()) unsigned fde_table_in_use;
405
406 /* Size (in elements) of increments by which we may expand the
407    fde_table.  */
408 #define FDE_TABLE_INCREMENT 256
409
410 /* Get the current fde_table entry we should use.  */
411
412 static inline dw_fde_ref
413 current_fde (void)
414 {
415   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
416 }
417
418 /* A vector of call frame insns for the CIE.  */
419 static GTY(()) cfi_vec cie_cfi_vec;
420
421 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
422    attribute that accelerates the lookup of the FDE associated
423    with the subprogram.  This variable holds the table index of the FDE
424    associated with the current function (body) definition.  */
425 static unsigned current_funcdef_fde;
426
427 struct GTY(()) indirect_string_node {
428   const char *str;
429   unsigned int refcount;
430   enum dwarf_form form;
431   char *label;
432 };
433
434 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
435
436 static GTY(()) int dw2_string_counter;
437 static GTY(()) unsigned long dwarf2out_cfi_label_num;
438
439 /* True if the compilation unit places functions in more than one section.  */
440 static GTY(()) bool have_multiple_function_sections = false;
441
442 /* Whether the default text and cold text sections have been used at all.  */
443
444 static GTY(()) bool text_section_used = false;
445 static GTY(()) bool cold_text_section_used = false;
446
447 /* The default cold text section.  */
448 static GTY(()) section *cold_text_section;
449
450 /* Forward declarations for functions defined in this file.  */
451
452 static char *stripattributes (const char *);
453 static const char *dwarf_cfi_name (unsigned);
454 static dw_cfi_ref new_cfi (void);
455 static void add_cfi (cfi_vec *, dw_cfi_ref);
456 static void add_fde_cfi (const char *, dw_cfi_ref);
457 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
458 static void lookup_cfa (dw_cfa_location *);
459 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
460 static void initial_return_save (rtx);
461 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
462                                           HOST_WIDE_INT);
463 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
464 static void output_cfi_directive (dw_cfi_ref);
465 static void output_call_frame_info (int);
466 static void dwarf2out_note_section_used (void);
467 static bool clobbers_queued_reg_save (const_rtx);
468 static void dwarf2out_frame_debug_expr (rtx, const char *);
469
470 /* Support for complex CFA locations.  */
471 static void output_cfa_loc (dw_cfi_ref, int);
472 static void output_cfa_loc_raw (dw_cfi_ref);
473 static void get_cfa_from_loc_descr (dw_cfa_location *,
474                                     struct dw_loc_descr_struct *);
475 static struct dw_loc_descr_struct *build_cfa_loc
476   (dw_cfa_location *, HOST_WIDE_INT);
477 static struct dw_loc_descr_struct *build_cfa_aligned_loc
478   (HOST_WIDE_INT, HOST_WIDE_INT);
479 static void def_cfa_1 (const char *, dw_cfa_location *);
480 static struct dw_loc_descr_struct *mem_loc_descriptor
481   (rtx, enum machine_mode mode, enum machine_mode mem_mode,
482    enum var_init_status);
483
484 /* How to start an assembler comment.  */
485 #ifndef ASM_COMMENT_START
486 #define ASM_COMMENT_START ";#"
487 #endif
488
489 /* Data and reference forms for relocatable data.  */
490 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
491 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
492
493 #ifndef DEBUG_FRAME_SECTION
494 #define DEBUG_FRAME_SECTION     ".debug_frame"
495 #endif
496
497 #ifndef FUNC_BEGIN_LABEL
498 #define FUNC_BEGIN_LABEL        "LFB"
499 #endif
500
501 #ifndef FUNC_END_LABEL
502 #define FUNC_END_LABEL          "LFE"
503 #endif
504
505 #ifndef PROLOGUE_END_LABEL
506 #define PROLOGUE_END_LABEL      "LPE"
507 #endif
508
509 #ifndef EPILOGUE_BEGIN_LABEL
510 #define EPILOGUE_BEGIN_LABEL    "LEB"
511 #endif
512
513 #ifndef FRAME_BEGIN_LABEL
514 #define FRAME_BEGIN_LABEL       "Lframe"
515 #endif
516 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
517 #define CIE_END_LABEL           "LECIE"
518 #define FDE_LABEL               "LSFDE"
519 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
520 #define FDE_END_LABEL           "LEFDE"
521 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
522 #define LINE_NUMBER_END_LABEL   "LELT"
523 #define LN_PROLOG_AS_LABEL      "LASLTP"
524 #define LN_PROLOG_END_LABEL     "LELTP"
525 #define DIE_LABEL_PREFIX        "DW"
526
527 /* The DWARF 2 CFA column which tracks the return address.  Normally this
528    is the column for PC, or the first column after all of the hard
529    registers.  */
530 #ifndef DWARF_FRAME_RETURN_COLUMN
531 #ifdef PC_REGNUM
532 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
533 #else
534 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
535 #endif
536 #endif
537
538 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
539    default, we just provide columns for all registers.  */
540 #ifndef DWARF_FRAME_REGNUM
541 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
542 #endif
543 \f
544 /* Match the base name of a file to the base name of a compilation unit. */
545
546 static int
547 matches_main_base (const char *path)
548 {
549   /* Cache the last query. */
550   static const char *last_path = NULL;
551   static int last_match = 0;
552   if (path != last_path)
553     {
554       const char *base;
555       int length = base_of_path (path, &base);
556       last_path = path;
557       last_match = (length == main_input_baselength
558                     && memcmp (base, main_input_basename, length) == 0);
559     }
560   return last_match;
561 }
562
563 #ifdef DEBUG_DEBUG_STRUCT
564
565 static int
566 dump_struct_debug (tree type, enum debug_info_usage usage,
567                    enum debug_struct_file criterion, int generic,
568                    int matches, int result)
569 {
570   /* Find the type name. */
571   tree type_decl = TYPE_STUB_DECL (type);
572   tree t = type_decl;
573   const char *name = 0;
574   if (TREE_CODE (t) == TYPE_DECL)
575     t = DECL_NAME (t);
576   if (t)
577     name = IDENTIFIER_POINTER (t);
578
579   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
580            criterion,
581            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
582            matches ? "bas" : "hdr",
583            generic ? "gen" : "ord",
584            usage == DINFO_USAGE_DFN ? ";" :
585              usage == DINFO_USAGE_DIR_USE ? "." : "*",
586            result,
587            (void*) type_decl, name);
588   return result;
589 }
590 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
591   dump_struct_debug (type, usage, criterion, generic, matches, result)
592
593 #else
594
595 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
596   (result)
597
598 #endif
599
600 static bool
601 should_emit_struct_debug (tree type, enum debug_info_usage usage)
602 {
603   enum debug_struct_file criterion;
604   tree type_decl;
605   bool generic = lang_hooks.types.generic_p (type);
606
607   if (generic)
608     criterion = debug_struct_generic[usage];
609   else
610     criterion = debug_struct_ordinary[usage];
611
612   if (criterion == DINFO_STRUCT_FILE_NONE)
613     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
614   if (criterion == DINFO_STRUCT_FILE_ANY)
615     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
616
617   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
618
619   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
620     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
621
622   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
623     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
624   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
625 }
626 \f
627 /* Hook used by __throw.  */
628
629 rtx
630 expand_builtin_dwarf_sp_column (void)
631 {
632   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
633   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
634 }
635
636 /* Return a pointer to a copy of the section string name S with all
637    attributes stripped off, and an asterisk prepended (for assemble_name).  */
638
639 static inline char *
640 stripattributes (const char *s)
641 {
642   char *stripped = XNEWVEC (char, strlen (s) + 2);
643   char *p = stripped;
644
645   *p++ = '*';
646
647   while (*s && *s != ',')
648     *p++ = *s++;
649
650   *p = '\0';
651   return stripped;
652 }
653
654 /* MEM is a memory reference for the register size table, each element of
655    which has mode MODE.  Initialize column C as a return address column.  */
656
657 static void
658 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
659 {
660   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
661   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
662   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
663 }
664
665 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
666
667 static inline HOST_WIDE_INT
668 div_data_align (HOST_WIDE_INT off)
669 {
670   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
671   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
672   return r;
673 }
674
675 /* Return true if we need a signed version of a given opcode
676    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
677
678 static inline bool
679 need_data_align_sf_opcode (HOST_WIDE_INT off)
680 {
681   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
682 }
683
684 /* Generate code to initialize the register size table.  */
685
686 void
687 expand_builtin_init_dwarf_reg_sizes (tree address)
688 {
689   unsigned int i;
690   enum machine_mode mode = TYPE_MODE (char_type_node);
691   rtx addr = expand_normal (address);
692   rtx mem = gen_rtx_MEM (BLKmode, addr);
693   bool wrote_return_column = false;
694
695   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
696     {
697       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
698
699       if (rnum < DWARF_FRAME_REGISTERS)
700         {
701           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
702           enum machine_mode save_mode = reg_raw_mode[i];
703           HOST_WIDE_INT size;
704
705           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
706             save_mode = choose_hard_reg_mode (i, 1, true);
707           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
708             {
709               if (save_mode == VOIDmode)
710                 continue;
711               wrote_return_column = true;
712             }
713           size = GET_MODE_SIZE (save_mode);
714           if (offset < 0)
715             continue;
716
717           emit_move_insn (adjust_address (mem, mode, offset),
718                           gen_int_mode (size, mode));
719         }
720     }
721
722   if (!wrote_return_column)
723     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
724
725 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
726   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
727 #endif
728
729   targetm.init_dwarf_reg_sizes_extra (address);
730 }
731
732 /* Convert a DWARF call frame info. operation to its string name */
733
734 static const char *
735 dwarf_cfi_name (unsigned int cfi_opc)
736 {
737   switch (cfi_opc)
738     {
739     case DW_CFA_advance_loc:
740       return "DW_CFA_advance_loc";
741     case DW_CFA_offset:
742       return "DW_CFA_offset";
743     case DW_CFA_restore:
744       return "DW_CFA_restore";
745     case DW_CFA_nop:
746       return "DW_CFA_nop";
747     case DW_CFA_set_loc:
748       return "DW_CFA_set_loc";
749     case DW_CFA_advance_loc1:
750       return "DW_CFA_advance_loc1";
751     case DW_CFA_advance_loc2:
752       return "DW_CFA_advance_loc2";
753     case DW_CFA_advance_loc4:
754       return "DW_CFA_advance_loc4";
755     case DW_CFA_offset_extended:
756       return "DW_CFA_offset_extended";
757     case DW_CFA_restore_extended:
758       return "DW_CFA_restore_extended";
759     case DW_CFA_undefined:
760       return "DW_CFA_undefined";
761     case DW_CFA_same_value:
762       return "DW_CFA_same_value";
763     case DW_CFA_register:
764       return "DW_CFA_register";
765     case DW_CFA_remember_state:
766       return "DW_CFA_remember_state";
767     case DW_CFA_restore_state:
768       return "DW_CFA_restore_state";
769     case DW_CFA_def_cfa:
770       return "DW_CFA_def_cfa";
771     case DW_CFA_def_cfa_register:
772       return "DW_CFA_def_cfa_register";
773     case DW_CFA_def_cfa_offset:
774       return "DW_CFA_def_cfa_offset";
775
776     /* DWARF 3 */
777     case DW_CFA_def_cfa_expression:
778       return "DW_CFA_def_cfa_expression";
779     case DW_CFA_expression:
780       return "DW_CFA_expression";
781     case DW_CFA_offset_extended_sf:
782       return "DW_CFA_offset_extended_sf";
783     case DW_CFA_def_cfa_sf:
784       return "DW_CFA_def_cfa_sf";
785     case DW_CFA_def_cfa_offset_sf:
786       return "DW_CFA_def_cfa_offset_sf";
787
788     /* SGI/MIPS specific */
789     case DW_CFA_MIPS_advance_loc8:
790       return "DW_CFA_MIPS_advance_loc8";
791
792     /* GNU extensions */
793     case DW_CFA_GNU_window_save:
794       return "DW_CFA_GNU_window_save";
795     case DW_CFA_GNU_args_size:
796       return "DW_CFA_GNU_args_size";
797     case DW_CFA_GNU_negative_offset_extended:
798       return "DW_CFA_GNU_negative_offset_extended";
799
800     default:
801       return "DW_CFA_<unknown>";
802     }
803 }
804
805 /* Return a pointer to a newly allocated Call Frame Instruction.  */
806
807 static inline dw_cfi_ref
808 new_cfi (void)
809 {
810   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
811
812   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
813   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
814
815   return cfi;
816 }
817
818 /* Add a Call Frame Instruction to list of instructions.  */
819
820 static inline void
821 add_cfi (cfi_vec *vec, dw_cfi_ref cfi)
822 {
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   VEC_safe_push (dw_cfi_ref, gc, *vec, cfi);
845 }
846
847 /* Generate a new label for the CFI info to refer to.  FORCE is true
848    if a label needs to be output even when using .cfi_* directives.  */
849
850 char *
851 dwarf2out_cfi_label (bool force)
852 {
853   static char label[20];
854
855   if (!force && dwarf2out_do_cfi_asm ())
856     {
857       /* In this case, we will be emitting the asm directive instead of
858          the label, so just return a placeholder to keep the rest of the
859          interfaces happy.  */
860       strcpy (label, "<do not output>");
861     }
862   else
863     {
864       int num = dwarf2out_cfi_label_num++;
865       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
866       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
867     }
868
869   return label;
870 }
871
872 /* True if remember_state should be emitted before following CFI directive.  */
873 static bool emit_cfa_remember;
874
875 /* True if any CFI directives were emitted at the current insn.  */
876 static bool any_cfis_emitted;
877
878 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
879    or to the CIE if LABEL is NULL.  */
880
881 static void
882 add_fde_cfi (const char *label, dw_cfi_ref cfi)
883 {
884   cfi_vec *vec;
885
886   if (cie_cfi_vec == NULL)
887     cie_cfi_vec = VEC_alloc (dw_cfi_ref, gc, 20);
888
889   vec = &cie_cfi_vec;
890
891   if (emit_cfa_remember)
892     {
893       dw_cfi_ref cfi_remember;
894
895       /* Emit the state save.  */
896       emit_cfa_remember = false;
897       cfi_remember = new_cfi ();
898       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
899       add_fde_cfi (label, cfi_remember);
900     }
901
902   if (dwarf2out_do_cfi_asm ())
903     {
904       if (label)
905         {
906           dw_fde_ref fde = current_fde ();
907
908           gcc_assert (fde != NULL);
909
910           /* We still have to add the cfi to the list so that lookup_cfa
911              works later on.  When -g2 and above we even need to force
912              emitting of CFI labels and add to list a DW_CFA_set_loc for
913              convert_cfa_to_fb_loc_list purposes.  If we're generating
914              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
915              convert_cfa_to_fb_loc_list.  */
916           if (dwarf_version == 2
917               && debug_info_level > DINFO_LEVEL_TERSE
918               && (write_symbols == DWARF2_DEBUG
919                   || write_symbols == VMS_AND_DWARF2_DEBUG))
920             {
921               switch (cfi->dw_cfi_opc)
922                 {
923                 case DW_CFA_def_cfa_offset:
924                 case DW_CFA_def_cfa_offset_sf:
925                 case DW_CFA_def_cfa_register:
926                 case DW_CFA_def_cfa:
927                 case DW_CFA_def_cfa_sf:
928                 case DW_CFA_def_cfa_expression:
929                 case DW_CFA_restore_state:
930                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
931                     label = dwarf2out_cfi_label (true);
932
933                   if (fde->dw_fde_current_label == NULL
934                       || strcmp (label, fde->dw_fde_current_label) != 0)
935                     {
936                       dw_cfi_ref xcfi;
937
938                       label = xstrdup (label);
939
940                       /* Set the location counter to the new label.  */
941                       xcfi = new_cfi ();
942                       /* It doesn't metter whether DW_CFA_set_loc
943                          or DW_CFA_advance_loc4 is added here, those aren't
944                          emitted into assembly, only looked up by
945                          convert_cfa_to_fb_loc_list.  */
946                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
947                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
948                       add_cfi (&fde->dw_fde_cfi, xcfi);
949                       fde->dw_fde_current_label = label;
950                     }
951                   break;
952                 default:
953                   break;
954                 }
955             }
956
957           output_cfi_directive (cfi);
958
959           vec = &fde->dw_fde_cfi;
960           any_cfis_emitted = true;
961         }
962       /* ??? If this is a CFI for the CIE, we don't emit.  This
963          assumes that the standard CIE contents that the assembler
964          uses matches the standard CIE contents that the compiler
965          uses.  This is probably a bad assumption.  I'm not quite
966          sure how to address this for now.  */
967     }
968   else if (label)
969     {
970       dw_fde_ref fde = current_fde ();
971
972       gcc_assert (fde != NULL);
973
974       if (*label == 0)
975         label = dwarf2out_cfi_label (false);
976
977       if (fde->dw_fde_current_label == NULL
978           || strcmp (label, fde->dw_fde_current_label) != 0)
979         {
980           dw_cfi_ref xcfi;
981
982           label = xstrdup (label);
983
984           /* Set the location counter to the new label.  */
985           xcfi = new_cfi ();
986           /* If we have a current label, advance from there, otherwise
987              set the location directly using set_loc.  */
988           xcfi->dw_cfi_opc = fde->dw_fde_current_label
989                              ? DW_CFA_advance_loc4
990                              : DW_CFA_set_loc;
991           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
992           add_cfi (&fde->dw_fde_cfi, xcfi);
993
994           fde->dw_fde_current_label = label;
995         }
996
997       vec = &fde->dw_fde_cfi;
998       any_cfis_emitted = true;
999     }
1000
1001   add_cfi (vec, cfi);
1002 }
1003
1004 /* Subroutine of lookup_cfa.  */
1005
1006 static void
1007 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1008 {
1009   switch (cfi->dw_cfi_opc)
1010     {
1011     case DW_CFA_def_cfa_offset:
1012     case DW_CFA_def_cfa_offset_sf:
1013       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1014       break;
1015     case DW_CFA_def_cfa_register:
1016       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1017       break;
1018     case DW_CFA_def_cfa:
1019     case DW_CFA_def_cfa_sf:
1020       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1021       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1022       break;
1023     case DW_CFA_def_cfa_expression:
1024       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1025       break;
1026
1027     case DW_CFA_remember_state:
1028       gcc_assert (!remember->in_use);
1029       *remember = *loc;
1030       remember->in_use = 1;
1031       break;
1032     case DW_CFA_restore_state:
1033       gcc_assert (remember->in_use);
1034       *loc = *remember;
1035       remember->in_use = 0;
1036       break;
1037
1038     default:
1039       break;
1040     }
1041 }
1042
1043 /* Find the previous value for the CFA.  */
1044
1045 static void
1046 lookup_cfa (dw_cfa_location *loc)
1047 {
1048   int ix;
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_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
1058     lookup_cfa_1 (cfi, loc, &remember);
1059
1060   fde = current_fde ();
1061   if (fde)
1062     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
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 /* Helper function to get mode of MEM's address.  */
2054
2055 static inline enum machine_mode
2056 get_address_mode (rtx mem)
2057 {
2058   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
2059   if (mode != VOIDmode)
2060     return mode;
2061   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
2062 }
2063
2064 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2065
2066 static void
2067 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2068 {
2069   rtx src, dest, span;
2070   dw_cfi_ref cfi = new_cfi ();
2071
2072   dest = SET_DEST (set);
2073   src = SET_SRC (set);
2074
2075   gcc_assert (REG_P (src));
2076   gcc_assert (MEM_P (dest));
2077
2078   span = targetm.dwarf_register_span (src);
2079   gcc_assert (!span);
2080
2081   cfi->dw_cfi_opc = DW_CFA_expression;
2082   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2083   cfi->dw_cfi_oprnd2.dw_cfi_loc
2084     = mem_loc_descriptor (XEXP (dest, 0), get_address_mode (dest),
2085                           GET_MODE (dest), VAR_INIT_STATUS_INITIALIZED);
2086
2087   /* ??? We'd like to use queue_reg_save, were the interface different,
2088      and, as above, we could manage flushing for epilogues.  */
2089   add_fde_cfi (label, cfi);
2090 }
2091
2092 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2093
2094 static void
2095 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2096 {
2097   dw_cfi_ref cfi = new_cfi ();
2098   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2099
2100   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2101   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2102
2103   add_fde_cfi (label, cfi);
2104 }
2105
2106 /* Record call frame debugging information for an expression EXPR,
2107    which either sets SP or FP (adjusting how we calculate the frame
2108    address) or saves a register to the stack or another register.
2109    LABEL indicates the address of EXPR.
2110
2111    This function encodes a state machine mapping rtxes to actions on
2112    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2113    users need not read the source code.
2114
2115   The High-Level Picture
2116
2117   Changes in the register we use to calculate the CFA: Currently we
2118   assume that if you copy the CFA register into another register, we
2119   should take the other one as the new CFA register; this seems to
2120   work pretty well.  If it's wrong for some target, it's simple
2121   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2122
2123   Changes in the register we use for saving registers to the stack:
2124   This is usually SP, but not always.  Again, we deduce that if you
2125   copy SP into another register (and SP is not the CFA register),
2126   then the new register is the one we will be using for register
2127   saves.  This also seems to work.
2128
2129   Register saves: There's not much guesswork about this one; if
2130   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2131   register save, and the register used to calculate the destination
2132   had better be the one we think we're using for this purpose.
2133   It's also assumed that a copy from a call-saved register to another
2134   register is saving that register if RTX_FRAME_RELATED_P is set on
2135   that instruction.  If the copy is from a call-saved register to
2136   the *same* register, that means that the register is now the same
2137   value as in the caller.
2138
2139   Except: If the register being saved is the CFA register, and the
2140   offset is nonzero, we are saving the CFA, so we assume we have to
2141   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2142   the intent is to save the value of SP from the previous frame.
2143
2144   In addition, if a register has previously been saved to a different
2145   register,
2146
2147   Invariants / Summaries of Rules
2148
2149   cfa          current rule for calculating the CFA.  It usually
2150                consists of a register and an offset.
2151   cfa_store    register used by prologue code to save things to the stack
2152                cfa_store.offset is the offset from the value of
2153                cfa_store.reg to the actual CFA
2154   cfa_temp     register holding an integral value.  cfa_temp.offset
2155                stores the value, which will be used to adjust the
2156                stack pointer.  cfa_temp is also used like cfa_store,
2157                to track stores to the stack via fp or a temp reg.
2158
2159   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2160                with cfa.reg as the first operand changes the cfa.reg and its
2161                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2162                cfa_temp.offset.
2163
2164   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2165                expression yielding a constant.  This sets cfa_temp.reg
2166                and cfa_temp.offset.
2167
2168   Rule 5:      Create a new register cfa_store used to save items to the
2169                stack.
2170
2171   Rules 10-14: Save a register to the stack.  Define offset as the
2172                difference of the original location and cfa_store's
2173                location (or cfa_temp's location if cfa_temp is used).
2174
2175   Rules 16-20: If AND operation happens on sp in prologue, we assume
2176                stack is realigned.  We will use a group of DW_OP_XXX
2177                expressions to represent the location of the stored
2178                register instead of CFA+offset.
2179
2180   The Rules
2181
2182   "{a,b}" indicates a choice of a xor b.
2183   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2184
2185   Rule 1:
2186   (set <reg1> <reg2>:cfa.reg)
2187   effects: cfa.reg = <reg1>
2188            cfa.offset unchanged
2189            cfa_temp.reg = <reg1>
2190            cfa_temp.offset = cfa.offset
2191
2192   Rule 2:
2193   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2194                               {<const_int>,<reg>:cfa_temp.reg}))
2195   effects: cfa.reg = sp if fp used
2196            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2197            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2198              if cfa_store.reg==sp
2199
2200   Rule 3:
2201   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2202   effects: cfa.reg = fp
2203            cfa_offset += +/- <const_int>
2204
2205   Rule 4:
2206   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2207   constraints: <reg1> != fp
2208                <reg1> != sp
2209   effects: cfa.reg = <reg1>
2210            cfa_temp.reg = <reg1>
2211            cfa_temp.offset = cfa.offset
2212
2213   Rule 5:
2214   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2215   constraints: <reg1> != fp
2216                <reg1> != sp
2217   effects: cfa_store.reg = <reg1>
2218            cfa_store.offset = cfa.offset - cfa_temp.offset
2219
2220   Rule 6:
2221   (set <reg> <const_int>)
2222   effects: cfa_temp.reg = <reg>
2223            cfa_temp.offset = <const_int>
2224
2225   Rule 7:
2226   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2227   effects: cfa_temp.reg = <reg1>
2228            cfa_temp.offset |= <const_int>
2229
2230   Rule 8:
2231   (set <reg> (high <exp>))
2232   effects: none
2233
2234   Rule 9:
2235   (set <reg> (lo_sum <exp> <const_int>))
2236   effects: cfa_temp.reg = <reg>
2237            cfa_temp.offset = <const_int>
2238
2239   Rule 10:
2240   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2241   effects: cfa_store.offset -= <const_int>
2242            cfa.offset = cfa_store.offset if cfa.reg == sp
2243            cfa.reg = sp
2244            cfa.base_offset = -cfa_store.offset
2245
2246   Rule 11:
2247   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2248   effects: cfa_store.offset += -/+ mode_size(mem)
2249            cfa.offset = cfa_store.offset if cfa.reg == sp
2250            cfa.reg = sp
2251            cfa.base_offset = -cfa_store.offset
2252
2253   Rule 12:
2254   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2255
2256        <reg2>)
2257   effects: cfa.reg = <reg1>
2258            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2259
2260   Rule 13:
2261   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2262   effects: cfa.reg = <reg1>
2263            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2264
2265   Rule 14:
2266   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2267   effects: cfa.reg = <reg1>
2268            cfa.base_offset = -cfa_temp.offset
2269            cfa_temp.offset -= mode_size(mem)
2270
2271   Rule 15:
2272   (set <reg> {unspec, unspec_volatile})
2273   effects: target-dependent
2274
2275   Rule 16:
2276   (set sp (and: sp <const_int>))
2277   constraints: cfa_store.reg == sp
2278   effects: current_fde.stack_realign = 1
2279            cfa_store.offset = 0
2280            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2281
2282   Rule 17:
2283   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2284   effects: cfa_store.offset += -/+ mode_size(mem)
2285
2286   Rule 18:
2287   (set (mem ({pre_inc, pre_dec} sp)) fp)
2288   constraints: fde->stack_realign == 1
2289   effects: cfa_store.offset = 0
2290            cfa.reg != HARD_FRAME_POINTER_REGNUM
2291
2292   Rule 19:
2293   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2294   constraints: fde->stack_realign == 1
2295                && cfa.offset == 0
2296                && cfa.indirect == 0
2297                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2298   effects: Use DW_CFA_def_cfa_expression to define cfa
2299            cfa.reg == fde->drap_reg  */
2300
2301 static void
2302 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2303 {
2304   rtx src, dest, span;
2305   HOST_WIDE_INT offset;
2306   dw_fde_ref fde;
2307
2308   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2309      the PARALLEL independently. The first element is always processed if
2310      it is a SET. This is for backward compatibility.   Other elements
2311      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2312      flag is set in them.  */
2313   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2314     {
2315       int par_index;
2316       int limit = XVECLEN (expr, 0);
2317       rtx elem;
2318
2319       /* PARALLELs have strict read-modify-write semantics, so we
2320          ought to evaluate every rvalue before changing any lvalue.
2321          It's cumbersome to do that in general, but there's an
2322          easy approximation that is enough for all current users:
2323          handle register saves before register assignments.  */
2324       if (GET_CODE (expr) == PARALLEL)
2325         for (par_index = 0; par_index < limit; par_index++)
2326           {
2327             elem = XVECEXP (expr, 0, par_index);
2328             if (GET_CODE (elem) == SET
2329                 && MEM_P (SET_DEST (elem))
2330                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2331               dwarf2out_frame_debug_expr (elem, label);
2332           }
2333
2334       for (par_index = 0; par_index < limit; par_index++)
2335         {
2336           elem = XVECEXP (expr, 0, par_index);
2337           if (GET_CODE (elem) == SET
2338               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2339               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2340             dwarf2out_frame_debug_expr (elem, label);
2341           else if (GET_CODE (elem) == SET
2342                    && par_index != 0
2343                    && !RTX_FRAME_RELATED_P (elem))
2344             {
2345               /* Stack adjustment combining might combine some post-prologue
2346                  stack adjustment into a prologue stack adjustment.  */
2347               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2348
2349               if (offset != 0)
2350                 dwarf2out_stack_adjust (offset, label);
2351             }
2352         }
2353       return;
2354     }
2355
2356   gcc_assert (GET_CODE (expr) == SET);
2357
2358   src = SET_SRC (expr);
2359   dest = SET_DEST (expr);
2360
2361   if (REG_P (src))
2362     {
2363       rtx rsi = reg_saved_in (src);
2364       if (rsi)
2365         src = rsi;
2366     }
2367
2368   fde = current_fde ();
2369
2370   switch (GET_CODE (dest))
2371     {
2372     case REG:
2373       switch (GET_CODE (src))
2374         {
2375           /* Setting FP from SP.  */
2376         case REG:
2377           if (cfa.reg == (unsigned) REGNO (src))
2378             {
2379               /* Rule 1 */
2380               /* Update the CFA rule wrt SP or FP.  Make sure src is
2381                  relative to the current CFA register.
2382
2383                  We used to require that dest be either SP or FP, but the
2384                  ARM copies SP to a temporary register, and from there to
2385                  FP.  So we just rely on the backends to only set
2386                  RTX_FRAME_RELATED_P on appropriate insns.  */
2387               cfa.reg = REGNO (dest);
2388               cfa_temp.reg = cfa.reg;
2389               cfa_temp.offset = cfa.offset;
2390             }
2391           else
2392             {
2393               /* Saving a register in a register.  */
2394               gcc_assert (!fixed_regs [REGNO (dest)]
2395                           /* For the SPARC and its register window.  */
2396                           || (DWARF_FRAME_REGNUM (REGNO (src))
2397                               == DWARF_FRAME_RETURN_COLUMN));
2398
2399               /* After stack is aligned, we can only save SP in FP
2400                  if drap register is used.  In this case, we have
2401                  to restore stack pointer with the CFA value and we
2402                  don't generate this DWARF information.  */
2403               if (fde
2404                   && fde->stack_realign
2405                   && REGNO (src) == STACK_POINTER_REGNUM)
2406                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2407                             && fde->drap_reg != INVALID_REGNUM
2408                             && cfa.reg != REGNO (src));
2409               else
2410                 queue_reg_save (label, src, dest, 0);
2411             }
2412           break;
2413
2414         case PLUS:
2415         case MINUS:
2416         case LO_SUM:
2417           if (dest == stack_pointer_rtx)
2418             {
2419               /* Rule 2 */
2420               /* Adjusting SP.  */
2421               switch (GET_CODE (XEXP (src, 1)))
2422                 {
2423                 case CONST_INT:
2424                   offset = INTVAL (XEXP (src, 1));
2425                   break;
2426                 case REG:
2427                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2428                               == cfa_temp.reg);
2429                   offset = cfa_temp.offset;
2430                   break;
2431                 default:
2432                   gcc_unreachable ();
2433                 }
2434
2435               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2436                 {
2437                   /* Restoring SP from FP in the epilogue.  */
2438                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2439                   cfa.reg = STACK_POINTER_REGNUM;
2440                 }
2441               else if (GET_CODE (src) == LO_SUM)
2442                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2443                 ;
2444               else
2445                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2446
2447               if (GET_CODE (src) != MINUS)
2448                 offset = -offset;
2449               if (cfa.reg == STACK_POINTER_REGNUM)
2450                 cfa.offset += offset;
2451               if (cfa_store.reg == STACK_POINTER_REGNUM)
2452                 cfa_store.offset += offset;
2453             }
2454           else if (dest == hard_frame_pointer_rtx)
2455             {
2456               /* Rule 3 */
2457               /* Either setting the FP from an offset of the SP,
2458                  or adjusting the FP */
2459               gcc_assert (frame_pointer_needed);
2460
2461               gcc_assert (REG_P (XEXP (src, 0))
2462                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2463                           && CONST_INT_P (XEXP (src, 1)));
2464               offset = INTVAL (XEXP (src, 1));
2465               if (GET_CODE (src) != MINUS)
2466                 offset = -offset;
2467               cfa.offset += offset;
2468               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2469             }
2470           else
2471             {
2472               gcc_assert (GET_CODE (src) != MINUS);
2473
2474               /* Rule 4 */
2475               if (REG_P (XEXP (src, 0))
2476                   && REGNO (XEXP (src, 0)) == cfa.reg
2477                   && CONST_INT_P (XEXP (src, 1)))
2478                 {
2479                   /* Setting a temporary CFA register that will be copied
2480                      into the FP later on.  */
2481                   offset = - INTVAL (XEXP (src, 1));
2482                   cfa.offset += offset;
2483                   cfa.reg = REGNO (dest);
2484                   /* Or used to save regs to the stack.  */
2485                   cfa_temp.reg = cfa.reg;
2486                   cfa_temp.offset = cfa.offset;
2487                 }
2488
2489               /* Rule 5 */
2490               else if (REG_P (XEXP (src, 0))
2491                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2492                        && XEXP (src, 1) == stack_pointer_rtx)
2493                 {
2494                   /* Setting a scratch register that we will use instead
2495                      of SP for saving registers to the stack.  */
2496                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2497                   cfa_store.reg = REGNO (dest);
2498                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2499                 }
2500
2501               /* Rule 9 */
2502               else if (GET_CODE (src) == LO_SUM
2503                        && CONST_INT_P (XEXP (src, 1)))
2504                 {
2505                   cfa_temp.reg = REGNO (dest);
2506                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2507                 }
2508               else
2509                 gcc_unreachable ();
2510             }
2511           break;
2512
2513           /* Rule 6 */
2514         case CONST_INT:
2515           cfa_temp.reg = REGNO (dest);
2516           cfa_temp.offset = INTVAL (src);
2517           break;
2518
2519           /* Rule 7 */
2520         case IOR:
2521           gcc_assert (REG_P (XEXP (src, 0))
2522                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2523                       && CONST_INT_P (XEXP (src, 1)));
2524
2525           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2526             cfa_temp.reg = REGNO (dest);
2527           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2528           break;
2529
2530           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2531              which will fill in all of the bits.  */
2532           /* Rule 8 */
2533         case HIGH:
2534           break;
2535
2536           /* Rule 15 */
2537         case UNSPEC:
2538         case UNSPEC_VOLATILE:
2539           gcc_assert (targetm.dwarf_handle_frame_unspec);
2540           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2541           return;
2542
2543           /* Rule 16 */
2544         case AND:
2545           /* If this AND operation happens on stack pointer in prologue,
2546              we assume the stack is realigned and we extract the
2547              alignment.  */
2548           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2549             {
2550               /* We interpret reg_save differently with stack_realign set.
2551                  Thus we must flush whatever we have queued first.  */
2552               dwarf2out_flush_queued_reg_saves ();
2553
2554               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2555               fde->stack_realign = 1;
2556               fde->stack_realignment = INTVAL (XEXP (src, 1));
2557               cfa_store.offset = 0;
2558
2559               if (cfa.reg != STACK_POINTER_REGNUM
2560                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2561                 fde->drap_reg = cfa.reg;
2562             }
2563           return;
2564
2565         default:
2566           gcc_unreachable ();
2567         }
2568
2569       def_cfa_1 (label, &cfa);
2570       break;
2571
2572     case MEM:
2573
2574       /* Saving a register to the stack.  Make sure dest is relative to the
2575          CFA register.  */
2576       switch (GET_CODE (XEXP (dest, 0)))
2577         {
2578           /* Rule 10 */
2579           /* With a push.  */
2580         case PRE_MODIFY:
2581           /* We can't handle variable size modifications.  */
2582           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2583                       == CONST_INT);
2584           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2585
2586           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2587                       && cfa_store.reg == STACK_POINTER_REGNUM);
2588
2589           cfa_store.offset += offset;
2590           if (cfa.reg == STACK_POINTER_REGNUM)
2591             cfa.offset = cfa_store.offset;
2592
2593           offset = -cfa_store.offset;
2594           break;
2595
2596           /* Rule 11 */
2597         case PRE_INC:
2598         case PRE_DEC:
2599         case POST_DEC:
2600           offset = GET_MODE_SIZE (GET_MODE (dest));
2601           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2602             offset = -offset;
2603
2604           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2605                        == STACK_POINTER_REGNUM)
2606                       && cfa_store.reg == STACK_POINTER_REGNUM);
2607
2608           cfa_store.offset += offset;
2609
2610           /* Rule 18: If stack is aligned, we will use FP as a
2611              reference to represent the address of the stored
2612              regiser.  */
2613           if (fde
2614               && fde->stack_realign
2615               && src == hard_frame_pointer_rtx)
2616             {
2617               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2618               cfa_store.offset = 0;
2619             }
2620
2621           if (cfa.reg == STACK_POINTER_REGNUM)
2622             cfa.offset = cfa_store.offset;
2623
2624           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2625             offset += -cfa_store.offset;
2626           else
2627             offset = -cfa_store.offset;
2628           break;
2629
2630           /* Rule 12 */
2631           /* With an offset.  */
2632         case PLUS:
2633         case MINUS:
2634         case LO_SUM:
2635           {
2636             int regno;
2637
2638             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2639                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2640             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2641             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2642               offset = -offset;
2643
2644             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2645
2646             if (cfa.reg == (unsigned) regno)
2647               offset -= cfa.offset;
2648             else if (cfa_store.reg == (unsigned) regno)
2649               offset -= cfa_store.offset;
2650             else
2651               {
2652                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2653                 offset -= cfa_temp.offset;
2654               }
2655           }
2656           break;
2657
2658           /* Rule 13 */
2659           /* Without an offset.  */
2660         case REG:
2661           {
2662             int regno = REGNO (XEXP (dest, 0));
2663
2664             if (cfa.reg == (unsigned) regno)
2665               offset = -cfa.offset;
2666             else if (cfa_store.reg == (unsigned) regno)
2667               offset = -cfa_store.offset;
2668             else
2669               {
2670                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2671                 offset = -cfa_temp.offset;
2672               }
2673           }
2674           break;
2675
2676           /* Rule 14 */
2677         case POST_INC:
2678           gcc_assert (cfa_temp.reg
2679                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2680           offset = -cfa_temp.offset;
2681           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2682           break;
2683
2684         default:
2685           gcc_unreachable ();
2686         }
2687
2688         /* Rule 17 */
2689         /* If the source operand of this MEM operation is not a
2690            register, basically the source is return address.  Here
2691            we only care how much stack grew and we don't save it.  */
2692       if (!REG_P (src))
2693         break;
2694
2695       if (REGNO (src) != STACK_POINTER_REGNUM
2696           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2697           && (unsigned) REGNO (src) == cfa.reg)
2698         {
2699           /* We're storing the current CFA reg into the stack.  */
2700
2701           if (cfa.offset == 0)
2702             {
2703               /* Rule 19 */
2704               /* If stack is aligned, putting CFA reg into stack means
2705                  we can no longer use reg + offset to represent CFA.
2706                  Here we use DW_CFA_def_cfa_expression instead.  The
2707                  result of this expression equals to the original CFA
2708                  value.  */
2709               if (fde
2710                   && fde->stack_realign
2711                   && cfa.indirect == 0
2712                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2713                 {
2714                   dw_cfa_location cfa_exp;
2715
2716                   gcc_assert (fde->drap_reg == cfa.reg);
2717
2718                   cfa_exp.indirect = 1;
2719                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2720                   cfa_exp.base_offset = offset;
2721                   cfa_exp.offset = 0;
2722
2723                   fde->drap_reg_saved = 1;
2724
2725                   def_cfa_1 (label, &cfa_exp);
2726                   break;
2727                 }
2728
2729               /* If the source register is exactly the CFA, assume
2730                  we're saving SP like any other register; this happens
2731                  on the ARM.  */
2732               def_cfa_1 (label, &cfa);
2733               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2734               break;
2735             }
2736           else
2737             {
2738               /* Otherwise, we'll need to look in the stack to
2739                  calculate the CFA.  */
2740               rtx x = XEXP (dest, 0);
2741
2742               if (!REG_P (x))
2743                 x = XEXP (x, 0);
2744               gcc_assert (REG_P (x));
2745
2746               cfa.reg = REGNO (x);
2747               cfa.base_offset = offset;
2748               cfa.indirect = 1;
2749               def_cfa_1 (label, &cfa);
2750               break;
2751             }
2752         }
2753
2754       def_cfa_1 (label, &cfa);
2755       {
2756         span = targetm.dwarf_register_span (src);
2757
2758         if (!span)
2759           queue_reg_save (label, src, NULL_RTX, offset);
2760         else
2761           {
2762             /* We have a PARALLEL describing where the contents of SRC
2763                live.  Queue register saves for each piece of the
2764                PARALLEL.  */
2765             int par_index;
2766             int limit;
2767             HOST_WIDE_INT span_offset = offset;
2768
2769             gcc_assert (GET_CODE (span) == PARALLEL);
2770
2771             limit = XVECLEN (span, 0);
2772             for (par_index = 0; par_index < limit; par_index++)
2773               {
2774                 rtx elem = XVECEXP (span, 0, par_index);
2775
2776                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2777                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2778               }
2779           }
2780       }
2781       break;
2782
2783     default:
2784       gcc_unreachable ();
2785     }
2786 }
2787
2788 /* Record call frame debugging information for INSN, which either
2789    sets SP or FP (adjusting how we calculate the frame address) or saves a
2790    register to the stack.  If INSN is NULL_RTX, initialize our state.
2791
2792    If AFTER_P is false, we're being called before the insn is emitted,
2793    otherwise after.  Call instructions get invoked twice.  */
2794
2795 void
2796 dwarf2out_frame_debug (rtx insn, bool after_p)
2797 {
2798   const char *label;
2799   rtx note, n;
2800   bool handled_one = false;
2801
2802   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2803     dwarf2out_flush_queued_reg_saves ();
2804
2805   if (!RTX_FRAME_RELATED_P (insn))
2806     {
2807       /* ??? This should be done unconditionally since stack adjustments
2808          matter if the stack pointer is not the CFA register anymore but
2809          is still used to save registers.  */
2810       if (!ACCUMULATE_OUTGOING_ARGS)
2811         dwarf2out_notice_stack_adjust (insn, after_p);
2812       return;
2813     }
2814
2815   label = dwarf2out_cfi_label (false);
2816   any_cfis_emitted = false;
2817
2818   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2819     switch (REG_NOTE_KIND (note))
2820       {
2821       case REG_FRAME_RELATED_EXPR:
2822         insn = XEXP (note, 0);
2823         goto found;
2824
2825       case REG_CFA_DEF_CFA:
2826         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2827         handled_one = true;
2828         break;
2829
2830       case REG_CFA_ADJUST_CFA:
2831         n = XEXP (note, 0);
2832         if (n == NULL)
2833           {
2834             n = PATTERN (insn);
2835             if (GET_CODE (n) == PARALLEL)
2836               n = XVECEXP (n, 0, 0);
2837           }
2838         dwarf2out_frame_debug_adjust_cfa (n, label);
2839         handled_one = true;
2840         break;
2841
2842       case REG_CFA_OFFSET:
2843         n = XEXP (note, 0);
2844         if (n == NULL)
2845           n = single_set (insn);
2846         dwarf2out_frame_debug_cfa_offset (n, label);
2847         handled_one = true;
2848         break;
2849
2850       case REG_CFA_REGISTER:
2851         n = XEXP (note, 0);
2852         if (n == NULL)
2853           {
2854             n = PATTERN (insn);
2855             if (GET_CODE (n) == PARALLEL)
2856               n = XVECEXP (n, 0, 0);
2857           }
2858         dwarf2out_frame_debug_cfa_register (n, label);
2859         handled_one = true;
2860         break;
2861
2862       case REG_CFA_EXPRESSION:
2863         n = XEXP (note, 0);
2864         if (n == NULL)
2865           n = single_set (insn);
2866         dwarf2out_frame_debug_cfa_expression (n, label);
2867         handled_one = true;
2868         break;
2869
2870       case REG_CFA_RESTORE:
2871         n = XEXP (note, 0);
2872         if (n == NULL)
2873           {
2874             n = PATTERN (insn);
2875             if (GET_CODE (n) == PARALLEL)
2876               n = XVECEXP (n, 0, 0);
2877             n = XEXP (n, 0);
2878           }
2879         dwarf2out_frame_debug_cfa_restore (n, label);
2880         handled_one = true;
2881         break;
2882
2883       case REG_CFA_SET_VDRAP:
2884         n = XEXP (note, 0);
2885         if (REG_P (n))
2886           {
2887             dw_fde_ref fde = current_fde ();
2888             if (fde)
2889               {
2890                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2891                 if (REG_P (n))
2892                   fde->vdrap_reg = REGNO (n);
2893               }
2894           }
2895         handled_one = true;
2896         break;
2897
2898       default:
2899         break;
2900       }
2901   if (handled_one)
2902     {
2903       if (any_cfis_emitted)
2904         dwarf2out_flush_queued_reg_saves ();
2905       return;
2906     }
2907
2908   insn = PATTERN (insn);
2909  found:
2910   dwarf2out_frame_debug_expr (insn, label);
2911
2912   /* Check again.  A parallel can save and update the same register.
2913      We could probably check just once, here, but this is safer than
2914      removing the check above.  */
2915   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2916     dwarf2out_flush_queued_reg_saves ();
2917 }
2918
2919 /* Called once at the start of final to initialize some data for the
2920    current function.  */
2921 void
2922 dwarf2out_frame_debug_init (void)
2923 {
2924   size_t i;
2925
2926   /* Flush any queued register saves.  */
2927   dwarf2out_flush_queued_reg_saves ();
2928
2929   /* Set up state for generating call frame debug info.  */
2930   lookup_cfa (&cfa);
2931   gcc_assert (cfa.reg
2932               == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2933
2934   cfa.reg = STACK_POINTER_REGNUM;
2935   cfa_store = cfa;
2936   cfa_temp.reg = -1;
2937   cfa_temp.offset = 0;
2938
2939   for (i = 0; i < num_regs_saved_in_regs; i++)
2940     {
2941       regs_saved_in_regs[i].orig_reg = NULL_RTX;
2942       regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2943     }
2944   num_regs_saved_in_regs = 0;
2945
2946   if (barrier_args_size)
2947     {
2948       XDELETEVEC (barrier_args_size);
2949       barrier_args_size = NULL;
2950     }
2951 }
2952
2953 /* Determine if we need to save and restore CFI information around this
2954    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2955    we do need to save/restore, then emit the save now, and insert a
2956    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2957
2958 void
2959 dwarf2out_cfi_begin_epilogue (rtx insn)
2960 {
2961   bool saw_frp = false;
2962   rtx i;
2963
2964   /* Scan forward to the return insn, noticing if there are possible
2965      frame related insns.  */
2966   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2967     {
2968       if (!INSN_P (i))
2969         continue;
2970
2971       /* Look for both regular and sibcalls to end the block.  */
2972       if (returnjump_p (i))
2973         break;
2974       if (CALL_P (i) && SIBLING_CALL_P (i))
2975         break;
2976
2977       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2978         {
2979           int idx;
2980           rtx seq = PATTERN (i);
2981
2982           if (returnjump_p (XVECEXP (seq, 0, 0)))
2983             break;
2984           if (CALL_P (XVECEXP (seq, 0, 0))
2985               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2986             break;
2987
2988           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2989             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2990               saw_frp = true;
2991         }
2992
2993       if (RTX_FRAME_RELATED_P (i))
2994         saw_frp = true;
2995     }
2996
2997   /* If the port doesn't emit epilogue unwind info, we don't need a
2998      save/restore pair.  */
2999   if (!saw_frp)
3000     return;
3001
3002   /* Otherwise, search forward to see if the return insn was the last
3003      basic block of the function.  If so, we don't need save/restore.  */
3004   gcc_assert (i != NULL);
3005   i = next_real_insn (i);
3006   if (i == NULL)
3007     return;
3008
3009   /* Insert the restore before that next real insn in the stream, and before
3010      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3011      properly nested.  This should be after any label or alignment.  This
3012      will be pushed into the CFI stream by the function below.  */
3013   while (1)
3014     {
3015       rtx p = PREV_INSN (i);
3016       if (!NOTE_P (p))
3017         break;
3018       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3019         break;
3020       i = p;
3021     }
3022   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3023
3024   emit_cfa_remember = true;
3025
3026   /* And emulate the state save.  */
3027   gcc_assert (!cfa_remember.in_use);
3028   cfa_remember = cfa;
3029   cfa_remember.in_use = 1;
3030 }
3031
3032 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3033    required.  */
3034
3035 void
3036 dwarf2out_frame_debug_restore_state (void)
3037 {
3038   dw_cfi_ref cfi = new_cfi ();
3039   const char *label = dwarf2out_cfi_label (false);
3040
3041   cfi->dw_cfi_opc = DW_CFA_restore_state;
3042   add_fde_cfi (label, cfi);
3043
3044   gcc_assert (cfa_remember.in_use);
3045   cfa = cfa_remember;
3046   cfa_remember.in_use = 0;
3047 }
3048
3049 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3050 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3051  (enum dwarf_call_frame_info cfi);
3052
3053 static enum dw_cfi_oprnd_type
3054 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3055 {
3056   switch (cfi)
3057     {
3058     case DW_CFA_nop:
3059     case DW_CFA_GNU_window_save:
3060     case DW_CFA_remember_state:
3061     case DW_CFA_restore_state:
3062       return dw_cfi_oprnd_unused;
3063
3064     case DW_CFA_set_loc:
3065     case DW_CFA_advance_loc1:
3066     case DW_CFA_advance_loc2:
3067     case DW_CFA_advance_loc4:
3068     case DW_CFA_MIPS_advance_loc8:
3069       return dw_cfi_oprnd_addr;
3070
3071     case DW_CFA_offset:
3072     case DW_CFA_offset_extended:
3073     case DW_CFA_def_cfa:
3074     case DW_CFA_offset_extended_sf:
3075     case DW_CFA_def_cfa_sf:
3076     case DW_CFA_restore:
3077     case DW_CFA_restore_extended:
3078     case DW_CFA_undefined:
3079     case DW_CFA_same_value:
3080     case DW_CFA_def_cfa_register:
3081     case DW_CFA_register:
3082     case DW_CFA_expression:
3083       return dw_cfi_oprnd_reg_num;
3084
3085     case DW_CFA_def_cfa_offset:
3086     case DW_CFA_GNU_args_size:
3087     case DW_CFA_def_cfa_offset_sf:
3088       return dw_cfi_oprnd_offset;
3089
3090     case DW_CFA_def_cfa_expression:
3091       return dw_cfi_oprnd_loc;
3092
3093     default:
3094       gcc_unreachable ();
3095     }
3096 }
3097
3098 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3099 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3100  (enum dwarf_call_frame_info cfi);
3101
3102 static enum dw_cfi_oprnd_type
3103 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3104 {
3105   switch (cfi)
3106     {
3107     case DW_CFA_def_cfa:
3108     case DW_CFA_def_cfa_sf:
3109     case DW_CFA_offset:
3110     case DW_CFA_offset_extended_sf:
3111     case DW_CFA_offset_extended:
3112       return dw_cfi_oprnd_offset;
3113
3114     case DW_CFA_register:
3115       return dw_cfi_oprnd_reg_num;
3116
3117     case DW_CFA_expression:
3118       return dw_cfi_oprnd_loc;
3119
3120     default:
3121       return dw_cfi_oprnd_unused;
3122     }
3123 }
3124
3125 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3126    switch to the data section instead, and write out a synthetic start label
3127    for collect2 the first time around.  */
3128
3129 static void
3130 switch_to_eh_frame_section (bool back)
3131 {
3132   tree label;
3133
3134 #ifdef EH_FRAME_SECTION_NAME
3135   if (eh_frame_section == 0)
3136     {
3137       int flags;
3138
3139       if (EH_TABLES_CAN_BE_READ_ONLY)
3140         {
3141           int fde_encoding;
3142           int per_encoding;
3143           int lsda_encoding;
3144
3145           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3146                                                        /*global=*/0);
3147           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3148                                                        /*global=*/1);
3149           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3150                                                         /*global=*/0);
3151           flags = ((! flag_pic
3152                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3153                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3154                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3155                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3156                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3157                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3158                    ? 0 : SECTION_WRITE);
3159         }
3160       else
3161         flags = SECTION_WRITE;
3162       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3163     }
3164 #endif /* EH_FRAME_SECTION_NAME */
3165
3166   if (eh_frame_section)
3167     switch_to_section (eh_frame_section);
3168   else
3169     {
3170       /* We have no special eh_frame section.  Put the information in
3171          the data section and emit special labels to guide collect2.  */
3172       switch_to_section (data_section);
3173
3174       if (!back)
3175         {
3176           label = get_file_function_name ("F");
3177           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3178           targetm.asm_out.globalize_label (asm_out_file,
3179                                            IDENTIFIER_POINTER (label));
3180           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3181         }
3182     }
3183 }
3184
3185 /* Switch [BACK] to the eh or debug frame table section, depending on
3186    FOR_EH.  */
3187
3188 static void
3189 switch_to_frame_table_section (int for_eh, bool back)
3190 {
3191   if (for_eh)
3192     switch_to_eh_frame_section (back);
3193   else
3194     {
3195       if (!debug_frame_section)
3196         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3197                                            SECTION_DEBUG, NULL);
3198       switch_to_section (debug_frame_section);
3199     }
3200 }
3201
3202 /* Output a Call Frame Information opcode and its operand(s).  */
3203
3204 static void
3205 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3206 {
3207   unsigned long r;
3208   HOST_WIDE_INT off;
3209
3210   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3211     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3212                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3213                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3214                          ((unsigned HOST_WIDE_INT)
3215                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3216   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3217     {
3218       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3219       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3220                            "DW_CFA_offset, column %#lx", r);
3221       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3222       dw2_asm_output_data_uleb128 (off, NULL);
3223     }
3224   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3225     {
3226       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3227       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3228                            "DW_CFA_restore, column %#lx", r);
3229     }
3230   else
3231     {
3232       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3233                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3234
3235       switch (cfi->dw_cfi_opc)
3236         {
3237         case DW_CFA_set_loc:
3238           if (for_eh)
3239             dw2_asm_output_encoded_addr_rtx (
3240                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3241                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3242                 false, NULL);
3243           else
3244             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3245                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3246           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3247           break;
3248
3249         case DW_CFA_advance_loc1:
3250           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3251                                 fde->dw_fde_current_label, NULL);
3252           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3253           break;
3254
3255         case DW_CFA_advance_loc2:
3256           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3257                                 fde->dw_fde_current_label, NULL);
3258           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3259           break;
3260
3261         case DW_CFA_advance_loc4:
3262           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3263                                 fde->dw_fde_current_label, NULL);
3264           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3265           break;
3266
3267         case DW_CFA_MIPS_advance_loc8:
3268           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3269                                 fde->dw_fde_current_label, NULL);
3270           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3271           break;
3272
3273         case DW_CFA_offset_extended:
3274           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3275           dw2_asm_output_data_uleb128 (r, NULL);
3276           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3277           dw2_asm_output_data_uleb128 (off, NULL);
3278           break;
3279
3280         case DW_CFA_def_cfa:
3281           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3282           dw2_asm_output_data_uleb128 (r, NULL);
3283           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3284           break;
3285
3286         case DW_CFA_offset_extended_sf:
3287           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3288           dw2_asm_output_data_uleb128 (r, NULL);
3289           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3290           dw2_asm_output_data_sleb128 (off, NULL);
3291           break;
3292
3293         case DW_CFA_def_cfa_sf:
3294           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3295           dw2_asm_output_data_uleb128 (r, NULL);
3296           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3297           dw2_asm_output_data_sleb128 (off, NULL);
3298           break;
3299
3300         case DW_CFA_restore_extended:
3301         case DW_CFA_undefined:
3302         case DW_CFA_same_value:
3303         case DW_CFA_def_cfa_register:
3304           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3305           dw2_asm_output_data_uleb128 (r, NULL);
3306           break;
3307
3308         case DW_CFA_register:
3309           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3310           dw2_asm_output_data_uleb128 (r, NULL);
3311           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3312           dw2_asm_output_data_uleb128 (r, NULL);
3313           break;
3314
3315         case DW_CFA_def_cfa_offset:
3316         case DW_CFA_GNU_args_size:
3317           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3318           break;
3319
3320         case DW_CFA_def_cfa_offset_sf:
3321           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3322           dw2_asm_output_data_sleb128 (off, NULL);
3323           break;
3324
3325         case DW_CFA_GNU_window_save:
3326           break;
3327
3328         case DW_CFA_def_cfa_expression:
3329         case DW_CFA_expression:
3330           output_cfa_loc (cfi, for_eh);
3331           break;
3332
3333         case DW_CFA_GNU_negative_offset_extended:
3334           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3335           gcc_unreachable ();
3336
3337         default:
3338           break;
3339         }
3340     }
3341 }
3342
3343 /* Similar, but do it via assembler directives instead.  */
3344
3345 static void
3346 output_cfi_directive (dw_cfi_ref cfi)
3347 {
3348   unsigned long r, r2;
3349
3350   switch (cfi->dw_cfi_opc)
3351     {
3352     case DW_CFA_advance_loc:
3353     case DW_CFA_advance_loc1:
3354     case DW_CFA_advance_loc2:
3355     case DW_CFA_advance_loc4:
3356     case DW_CFA_MIPS_advance_loc8:
3357     case DW_CFA_set_loc:
3358       /* Should only be created by add_fde_cfi in a code path not
3359          followed when emitting via directives.  The assembler is
3360          going to take care of this for us.  */
3361       gcc_unreachable ();
3362
3363     case DW_CFA_offset:
3364     case DW_CFA_offset_extended:
3365     case DW_CFA_offset_extended_sf:
3366       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3367       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3368                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3369       break;
3370
3371     case DW_CFA_restore:
3372     case DW_CFA_restore_extended:
3373       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3374       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3375       break;
3376
3377     case DW_CFA_undefined:
3378       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3379       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3380       break;
3381
3382     case DW_CFA_same_value:
3383       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3384       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3385       break;
3386
3387     case DW_CFA_def_cfa:
3388     case DW_CFA_def_cfa_sf:
3389       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3390       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3391                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3392       break;
3393
3394     case DW_CFA_def_cfa_register:
3395       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3396       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3397       break;
3398
3399     case DW_CFA_register:
3400       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3401       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3402       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3403       break;
3404
3405     case DW_CFA_def_cfa_offset:
3406     case DW_CFA_def_cfa_offset_sf:
3407       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3408                HOST_WIDE_INT_PRINT_DEC"\n",
3409                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3410       break;
3411
3412     case DW_CFA_remember_state:
3413       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3414       break;
3415     case DW_CFA_restore_state:
3416       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3417       break;
3418
3419     case DW_CFA_GNU_args_size:
3420       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3421       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3422       if (flag_debug_asm)
3423         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3424                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3425       fputc ('\n', asm_out_file);
3426       break;
3427
3428     case DW_CFA_GNU_window_save:
3429       fprintf (asm_out_file, "\t.cfi_window_save\n");
3430       break;
3431
3432     case DW_CFA_def_cfa_expression:
3433     case DW_CFA_expression:
3434       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3435       output_cfa_loc_raw (cfi);
3436       fputc ('\n', asm_out_file);
3437       break;
3438
3439     default:
3440       gcc_unreachable ();
3441     }
3442 }
3443
3444 /* Output CFIs from VEC, up to index UPTO, to bring current FDE to the
3445    same state as after executing CFIs in CFI chain.  DO_CFI_ASM is
3446    true if .cfi_* directives shall be emitted, false otherwise.  If it
3447    is false, FDE and FOR_EH are the other arguments to pass to
3448    output_cfi.  */
3449
3450 static void
3451 output_cfis (cfi_vec vec, int upto, bool do_cfi_asm,
3452              dw_fde_ref fde, bool for_eh)
3453 {
3454   int ix;
3455   struct dw_cfi_struct cfi_buf;
3456   dw_cfi_ref cfi2;
3457   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3458   VEC(dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3459   unsigned int len, idx;
3460
3461   for (ix = 0; ix < upto + 1; ix++)
3462     {
3463       dw_cfi_ref cfi = ix < upto ? VEC_index (dw_cfi_ref, vec, ix) : NULL;
3464       switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3465         {
3466         case DW_CFA_advance_loc:
3467         case DW_CFA_advance_loc1:
3468         case DW_CFA_advance_loc2:
3469         case DW_CFA_advance_loc4:
3470         case DW_CFA_MIPS_advance_loc8:
3471         case DW_CFA_set_loc:
3472           /* All advances should be ignored.  */
3473           break;
3474         case DW_CFA_remember_state:
3475           {
3476             dw_cfi_ref args_size = cfi_args_size;
3477
3478             /* Skip everything between .cfi_remember_state and
3479                .cfi_restore_state.  */
3480             ix++;
3481             if (ix == upto)
3482               goto flush_all;
3483
3484             for (; ix < upto; ix++)
3485               {
3486                 cfi2 = VEC_index (dw_cfi_ref, vec, ix);
3487                 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3488                   break;
3489                 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3490                   args_size = cfi2;
3491                 else
3492                   gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3493               }
3494
3495             cfi_args_size = args_size;
3496             break;
3497           }
3498         case DW_CFA_GNU_args_size:
3499           cfi_args_size = cfi;
3500           break;
3501         case DW_CFA_GNU_window_save:
3502           goto flush_all;
3503         case DW_CFA_offset:
3504         case DW_CFA_offset_extended:
3505         case DW_CFA_offset_extended_sf:
3506         case DW_CFA_restore:
3507         case DW_CFA_restore_extended:
3508         case DW_CFA_undefined:
3509         case DW_CFA_same_value:
3510         case DW_CFA_register:
3511         case DW_CFA_val_offset:
3512         case DW_CFA_val_offset_sf:
3513         case DW_CFA_expression:
3514         case DW_CFA_val_expression:
3515         case DW_CFA_GNU_negative_offset_extended:
3516           if (VEC_length (dw_cfi_ref, regs)
3517               <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3518             VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3519                                    cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3520           VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
3521                        cfi);
3522           break;
3523         case DW_CFA_def_cfa:
3524         case DW_CFA_def_cfa_sf:
3525         case DW_CFA_def_cfa_expression:
3526           cfi_cfa = cfi;
3527           cfi_cfa_offset = cfi;
3528           break;
3529         case DW_CFA_def_cfa_register:
3530           cfi_cfa = cfi;
3531           break;
3532         case DW_CFA_def_cfa_offset:
3533         case DW_CFA_def_cfa_offset_sf:
3534           cfi_cfa_offset = cfi;
3535           break;
3536         case DW_CFA_nop:
3537           gcc_assert (cfi == NULL);
3538         flush_all:
3539           len = VEC_length (dw_cfi_ref, regs);
3540           for (idx = 0; idx < len; idx++)
3541             {
3542               cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3543               if (cfi2 != NULL
3544                   && cfi2->dw_cfi_opc != DW_CFA_restore
3545                   && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3546                 {
3547                   if (do_cfi_asm)
3548                     output_cfi_directive (cfi2);
3549                   else
3550                     output_cfi (cfi2, fde, for_eh);
3551                 }
3552             }
3553           if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3554             {
3555               gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3556               cfi_buf = *cfi_cfa;
3557               switch (cfi_cfa_offset->dw_cfi_opc)
3558                 {
3559                 case DW_CFA_def_cfa_offset:
3560                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3561                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3562                   break;
3563                 case DW_CFA_def_cfa_offset_sf:
3564                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3565                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3566                   break;
3567                 case DW_CFA_def_cfa:
3568                 case DW_CFA_def_cfa_sf:
3569                   cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3570                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3571                   break;
3572                 default:
3573                   gcc_unreachable ();
3574                 }
3575               cfi_cfa = &cfi_buf;
3576             }
3577           else if (cfi_cfa_offset)
3578             cfi_cfa = cfi_cfa_offset;
3579           if (cfi_cfa)
3580             {
3581               if (do_cfi_asm)
3582                 output_cfi_directive (cfi_cfa);
3583               else
3584                 output_cfi (cfi_cfa, fde, for_eh);
3585             }
3586           cfi_cfa = NULL;
3587           cfi_cfa_offset = NULL;
3588           if (cfi_args_size
3589               && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3590             {
3591               if (do_cfi_asm)
3592                 output_cfi_directive (cfi_args_size);
3593               else
3594                 output_cfi (cfi_args_size, fde, for_eh);
3595             }
3596           cfi_args_size = NULL;
3597           if (cfi == NULL)
3598             {
3599               VEC_free (dw_cfi_ref, heap, regs);
3600               return;
3601             }
3602           else if (do_cfi_asm)
3603             output_cfi_directive (cfi);
3604           else
3605             output_cfi (cfi, fde, for_eh);
3606           break;
3607         default:
3608           gcc_unreachable ();
3609         }
3610     }
3611 }
3612
3613 /* Like output_cfis, but emit all CFIs in the vector.  */
3614 static void
3615 output_all_cfis (cfi_vec vec, bool do_cfi_asm,
3616                  dw_fde_ref fde, bool for_eh)
3617 {
3618   output_cfis (vec, VEC_length (dw_cfi_ref, vec), do_cfi_asm, fde, for_eh);
3619 }
3620
3621 /* Output one FDE.  */
3622
3623 static void
3624 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3625             char *section_start_label, int fde_encoding, char *augmentation,
3626             bool any_lsda_needed, int lsda_encoding)
3627 {
3628   int ix;
3629   const char *begin, *end;
3630   static unsigned int j;
3631   char l1[20], l2[20];
3632   dw_cfi_ref cfi;
3633
3634   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3635                                      /* empty */ 0);
3636   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3637                                   for_eh + j);
3638   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3639   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3640   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3641     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3642                          " indicating 64-bit DWARF extension");
3643   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3644                         "FDE Length");
3645   ASM_OUTPUT_LABEL (asm_out_file, l1);
3646
3647   if (for_eh)
3648     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3649   else
3650     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3651                            debug_frame_section, "FDE CIE offset");
3652
3653   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3654   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3655
3656   if (for_eh)
3657     {
3658       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3659       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3660       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3661                                        "FDE initial location");
3662       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3663                             end, begin, "FDE address range");
3664     }
3665   else
3666     {
3667       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3668       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3669     }
3670
3671   if (augmentation[0])
3672     {
3673       if (any_lsda_needed)
3674         {
3675           int size = size_of_encoded_value (lsda_encoding);
3676
3677           if (lsda_encoding == DW_EH_PE_aligned)
3678             {
3679               int offset = (  4         /* Length */
3680                             + 4         /* CIE offset */
3681                             + 2 * size_of_encoded_value (fde_encoding)
3682                             + 1         /* Augmentation size */ );
3683               int pad = -offset & (PTR_SIZE - 1);
3684
3685               size += pad;
3686               gcc_assert (size_of_uleb128 (size) == 1);
3687             }
3688
3689           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3690
3691           if (fde->uses_eh_lsda)
3692             {
3693               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3694                                            fde->funcdef_number);
3695               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3696                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3697                                                false,
3698                                                "Language Specific Data Area");
3699             }
3700           else
3701             {
3702               if (lsda_encoding == DW_EH_PE_aligned)
3703                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3704               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3705                                    "Language Specific Data Area (none)");
3706             }
3707         }
3708       else
3709         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3710     }
3711
3712   /* Loop through the Call Frame Instructions associated with
3713      this FDE.  */
3714   fde->dw_fde_current_label = begin;
3715   if (fde->dw_fde_second_begin == NULL)
3716     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3717       output_cfi (cfi, fde, for_eh);
3718   else if (!second)
3719     {
3720       if (fde->dw_fde_switch_cfi_index > 0)
3721         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3722           {
3723             if (ix == fde->dw_fde_switch_cfi_index)
3724               break;
3725             output_cfi (cfi, fde, for_eh);
3726           }
3727     }
3728   else
3729     {
3730       int i, from = 0;
3731       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
3732
3733       if (fde->dw_fde_switch_cfi_index > 0)
3734         {
3735           from = fde->dw_fde_switch_cfi_index;
3736           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
3737         }
3738       for (i = from; i < until; i++)
3739         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
3740                     fde, for_eh);
3741     }
3742
3743   /* If we are to emit a ref/link from function bodies to their frame tables,
3744      do it now.  This is typically performed to make sure that tables
3745      associated with functions are dragged with them and not discarded in
3746      garbage collecting links. We need to do this on a per function basis to
3747      cope with -ffunction-sections.  */
3748
3749 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3750   /* Switch to the function section, emit the ref to the tables, and
3751      switch *back* into the table section.  */
3752   switch_to_section (function_section (fde->decl));
3753   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3754   switch_to_frame_table_section (for_eh, true);
3755 #endif
3756
3757   /* Pad the FDE out to an address sized boundary.  */
3758   ASM_OUTPUT_ALIGN (asm_out_file,
3759                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3760   ASM_OUTPUT_LABEL (asm_out_file, l2);
3761
3762   j += 2;
3763 }
3764
3765 /* Return true if frame description entry FDE is needed for EH.  */
3766
3767 static bool
3768 fde_needed_for_eh_p (dw_fde_ref fde)
3769 {
3770   if (flag_asynchronous_unwind_tables)
3771     return true;
3772
3773   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3774     return true;
3775
3776   if (fde->uses_eh_lsda)
3777     return true;
3778
3779   /* If exceptions are enabled, we have collected nothrow info.  */
3780   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3781     return false;
3782
3783   return true;
3784 }
3785
3786 /* Output the call frame information used to record information
3787    that relates to calculating the frame pointer, and records the
3788    location of saved registers.  */
3789
3790 static void
3791 output_call_frame_info (int for_eh)
3792 {
3793   unsigned int i;
3794   dw_fde_ref fde;
3795   dw_cfi_ref cfi;
3796   char l1[20], l2[20], section_start_label[20];
3797   bool any_lsda_needed = false;
3798   char augmentation[6];
3799   int augmentation_size;
3800   int fde_encoding = DW_EH_PE_absptr;
3801   int per_encoding = DW_EH_PE_absptr;
3802   int lsda_encoding = DW_EH_PE_absptr;
3803   int return_reg;
3804   rtx personality = NULL;
3805   int dw_cie_version;
3806
3807   /* Don't emit a CIE if there won't be any FDEs.  */
3808   if (fde_table_in_use == 0)
3809     return;
3810
3811   /* Nothing to do if the assembler's doing it all.  */
3812   if (dwarf2out_do_cfi_asm ())
3813     return;
3814
3815   /* If we don't have any functions we'll want to unwind out of, don't emit
3816      any EH unwind information.  If we make FDEs linkonce, we may have to
3817      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3818      want to avoid having an FDE kept around when the function it refers to
3819      is discarded.  Example where this matters: a primary function template
3820      in C++ requires EH information, an explicit specialization doesn't.  */
3821   if (for_eh)
3822     {
3823       bool any_eh_needed = false;
3824
3825       for (i = 0; i < fde_table_in_use; i++)
3826         if (fde_table[i].uses_eh_lsda)
3827           any_eh_needed = any_lsda_needed = true;
3828         else if (fde_needed_for_eh_p (&fde_table[i]))
3829           any_eh_needed = true;
3830         else if (TARGET_USES_WEAK_UNWIND_INFO)
3831           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3832                                              1, 1);
3833
3834       if (!any_eh_needed)
3835         return;
3836     }
3837
3838   /* We're going to be generating comments, so turn on app.  */
3839   if (flag_debug_asm)
3840     app_enable ();
3841
3842   /* Switch to the proper frame section, first time.  */
3843   switch_to_frame_table_section (for_eh, false);
3844
3845   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3846   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3847
3848   /* Output the CIE.  */
3849   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3850   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3851   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3852     dw2_asm_output_data (4, 0xffffffff,
3853       "Initial length escape value indicating 64-bit DWARF extension");
3854   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3855                         "Length of Common Information Entry");
3856   ASM_OUTPUT_LABEL (asm_out_file, l1);
3857
3858   /* Now that the CIE pointer is PC-relative for EH,
3859      use 0 to identify the CIE.  */
3860   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3861                        (for_eh ? 0 : DWARF_CIE_ID),
3862                        "CIE Identifier Tag");
3863
3864   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3865      use CIE version 1, unless that would produce incorrect results
3866      due to overflowing the return register column.  */
3867   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3868   dw_cie_version = 1;
3869   if (return_reg >= 256 || dwarf_version > 2)
3870     dw_cie_version = 3;
3871   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3872
3873   augmentation[0] = 0;
3874   augmentation_size = 0;
3875
3876   personality = current_unit_personality;
3877   if (for_eh)
3878     {
3879       char *p;
3880
3881       /* Augmentation:
3882          z      Indicates that a uleb128 is present to size the
3883                 augmentation section.
3884          L      Indicates the encoding (and thus presence) of
3885                 an LSDA pointer in the FDE augmentation.
3886          R      Indicates a non-default pointer encoding for
3887                 FDE code pointers.
3888          P      Indicates the presence of an encoding + language
3889                 personality routine in the CIE augmentation.  */
3890
3891       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3892       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3893       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3894
3895       p = augmentation + 1;
3896       if (personality)
3897         {
3898           *p++ = 'P';
3899           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3900           assemble_external_libcall (personality);
3901         }
3902       if (any_lsda_needed)
3903         {
3904           *p++ = 'L';
3905           augmentation_size += 1;
3906         }
3907       if (fde_encoding != DW_EH_PE_absptr)
3908         {
3909           *p++ = 'R';
3910           augmentation_size += 1;
3911         }
3912       if (p > augmentation + 1)
3913         {
3914           augmentation[0] = 'z';
3915           *p = '\0';
3916         }
3917
3918       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3919       if (personality && per_encoding == DW_EH_PE_aligned)
3920         {
3921           int offset = (  4             /* Length */
3922                         + 4             /* CIE Id */
3923                         + 1             /* CIE version */
3924                         + strlen (augmentation) + 1     /* Augmentation */
3925                         + size_of_uleb128 (1)           /* Code alignment */
3926                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3927                         + 1             /* RA column */
3928                         + 1             /* Augmentation size */
3929                         + 1             /* Personality encoding */ );
3930           int pad = -offset & (PTR_SIZE - 1);
3931
3932           augmentation_size += pad;
3933
3934           /* Augmentations should be small, so there's scarce need to
3935              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3936           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3937         }
3938     }
3939
3940   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3941   if (dw_cie_version >= 4)
3942     {
3943       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3944       dw2_asm_output_data (1, 0, "CIE Segment Size");
3945     }
3946   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3947   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3948                                "CIE Data Alignment Factor");
3949
3950   if (dw_cie_version == 1)
3951     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3952   else
3953     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3954
3955   if (augmentation[0])
3956     {
3957       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3958       if (personality)
3959         {
3960           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3961                                eh_data_format_name (per_encoding));
3962           dw2_asm_output_encoded_addr_rtx (per_encoding,
3963                                            personality,
3964                                            true, NULL);
3965         }
3966
3967       if (any_lsda_needed)
3968         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3969                              eh_data_format_name (lsda_encoding));
3970
3971       if (fde_encoding != DW_EH_PE_absptr)
3972         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3973                              eh_data_format_name (fde_encoding));
3974     }
3975
3976   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
3977     output_cfi (cfi, NULL, for_eh);
3978
3979   /* Pad the CIE out to an address sized boundary.  */
3980   ASM_OUTPUT_ALIGN (asm_out_file,
3981                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3982   ASM_OUTPUT_LABEL (asm_out_file, l2);
3983
3984   /* Loop through all of the FDE's.  */
3985   for (i = 0; i < fde_table_in_use; i++)
3986     {
3987       unsigned int k;
3988       fde = &fde_table[i];
3989
3990       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3991       if (for_eh && !fde_needed_for_eh_p (fde))
3992         continue;
3993
3994       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
3995         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3996                     augmentation, any_lsda_needed, lsda_encoding);
3997     }
3998
3999   if (for_eh && targetm.terminate_dw2_eh_frame_info)
4000     dw2_asm_output_data (4, 0, "End of Table");
4001 #ifdef MIPS_DEBUGGING_INFO
4002   /* Work around Irix 6 assembler bug whereby labels at the end of a section
4003      get a value of 0.  Putting .align 0 after the label fixes it.  */
4004   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4005 #endif
4006
4007   /* Turn off app to make assembly quicker.  */
4008   if (flag_debug_asm)
4009     app_disable ();
4010 }
4011
4012 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4013
4014 static void
4015 dwarf2out_do_cfi_startproc (bool second)
4016 {
4017   int enc;
4018   rtx ref;
4019   rtx personality = get_personality_function (current_function_decl);
4020
4021   fprintf (asm_out_file, "\t.cfi_startproc\n");
4022
4023   if (personality)
4024     {
4025       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4026       ref = personality;
4027
4028       /* ??? The GAS support isn't entirely consistent.  We have to
4029          handle indirect support ourselves, but PC-relative is done
4030          in the assembler.  Further, the assembler can't handle any
4031          of the weirder relocation types.  */
4032       if (enc & DW_EH_PE_indirect)
4033         ref = dw2_force_const_mem (ref, true);
4034
4035       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4036       output_addr_const (asm_out_file, ref);
4037       fputc ('\n', asm_out_file);
4038     }
4039
4040   if (crtl->uses_eh_lsda)
4041     {
4042       char lab[20];
4043
4044       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4045       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4046                                    current_function_funcdef_no);
4047       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4048       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4049
4050       if (enc & DW_EH_PE_indirect)
4051         ref = dw2_force_const_mem (ref, true);
4052
4053       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4054       output_addr_const (asm_out_file, ref);
4055       fputc ('\n', asm_out_file);
4056     }
4057 }
4058
4059 /* Output a marker (i.e. a label) for the beginning of a function, before
4060    the prologue.  */
4061
4062 void
4063 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4064                           const char *file ATTRIBUTE_UNUSED)
4065 {
4066   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4067   char * dup_label;
4068   dw_fde_ref fde;
4069   section *fnsec;
4070   bool do_frame;
4071
4072   current_function_func_begin_label = NULL;
4073
4074   do_frame = dwarf2out_do_frame ();
4075
4076   /* ??? current_function_func_begin_label is also used by except.c for
4077      call-site information.  We must emit this label if it might be used.  */
4078   if (!do_frame
4079       && (!flag_exceptions
4080           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4081     return;
4082
4083   fnsec = function_section (current_function_decl);
4084   switch_to_section (fnsec);
4085   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4086                                current_function_funcdef_no);
4087   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4088                           current_function_funcdef_no);
4089   dup_label = xstrdup (label);
4090   current_function_func_begin_label = dup_label;
4091
4092   /* We can elide the fde allocation if we're not emitting debug info.  */
4093   if (!do_frame)
4094     return;
4095
4096   /* Expand the fde table if necessary.  */
4097   if (fde_table_in_use == fde_table_allocated)
4098     {
4099       fde_table_allocated += FDE_TABLE_INCREMENT;
4100       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4101       memset (fde_table + fde_table_in_use, 0,
4102               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4103     }
4104
4105   /* Record the FDE associated with this function.  */
4106   current_funcdef_fde = fde_table_in_use;
4107
4108   /* Add the new FDE at the end of the fde_table.  */
4109   fde = &fde_table[fde_table_in_use++];
4110   fde->decl = current_function_decl;
4111   fde->dw_fde_begin = dup_label;
4112   fde->dw_fde_end = NULL;
4113   fde->dw_fde_current_label = dup_label;
4114   fde->dw_fde_second_begin = NULL;
4115   fde->dw_fde_second_end = NULL;
4116   fde->dw_fde_vms_end_prologue = NULL;
4117   fde->dw_fde_vms_begin_epilogue = NULL;
4118   fde->dw_fde_cfi = VEC_alloc (dw_cfi_ref, gc, 20);
4119   fde->dw_fde_switch_cfi_index = 0;
4120   fde->funcdef_number = current_function_funcdef_no;
4121   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4122   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4123   fde->nothrow = crtl->nothrow;
4124   fde->drap_reg = INVALID_REGNUM;
4125   fde->vdrap_reg = INVALID_REGNUM;
4126   fde->in_std_section = (fnsec == text_section
4127                          || (cold_text_section && fnsec == cold_text_section));
4128   fde->second_in_std_section = 0;
4129
4130   args_size = old_args_size = 0;
4131
4132   /* We only want to output line number information for the genuine dwarf2
4133      prologue case, not the eh frame case.  */
4134 #ifdef DWARF2_DEBUGGING_INFO
4135   if (file)
4136     dwarf2out_source_line (line, file, 0, true);
4137 #endif
4138
4139   if (dwarf2out_do_cfi_asm ())
4140     dwarf2out_do_cfi_startproc (false);
4141   else
4142     {
4143       rtx personality = get_personality_function (current_function_decl);
4144       if (!current_unit_personality)
4145         current_unit_personality = personality;
4146
4147       /* We cannot keep a current personality per function as without CFI
4148          asm, at the point where we emit the CFI data, there is no current
4149          function anymore.  */
4150       if (personality && current_unit_personality != personality)
4151         sorry ("multiple EH personalities are supported only with assemblers "
4152                "supporting .cfi_personality directive");
4153     }
4154 }
4155
4156 /* Output a marker (i.e. a label) for the end of the generated code
4157    for a function prologue.  This gets called *after* the prologue code has
4158    been generated.  */
4159
4160 void
4161 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4162                         const char *file ATTRIBUTE_UNUSED)
4163 {
4164   dw_fde_ref fde;
4165   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4166
4167   /* Output a label to mark the endpoint of the code generated for this
4168      function.  */
4169   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4170                                current_function_funcdef_no);
4171   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4172                           current_function_funcdef_no);
4173   fde = &fde_table[fde_table_in_use - 1];
4174   fde->dw_fde_vms_end_prologue = xstrdup (label);
4175 }
4176
4177 /* Output a marker (i.e. a label) for the beginning of the generated code
4178    for a function epilogue.  This gets called *before* the prologue code has
4179    been generated.  */
4180
4181 void
4182 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4183                           const char *file ATTRIBUTE_UNUSED)
4184 {
4185   dw_fde_ref fde;
4186   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4187
4188   fde = &fde_table[fde_table_in_use - 1];
4189   if (fde->dw_fde_vms_begin_epilogue)
4190     return;
4191
4192   /* Output a label to mark the endpoint of the code generated for this
4193      function.  */
4194   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4195                                current_function_funcdef_no);
4196   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4197                           current_function_funcdef_no);
4198   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4199 }
4200
4201 /* Output a marker (i.e. a label) for the absolute end of the generated code
4202    for a function definition.  This gets called *after* the epilogue code has
4203    been generated.  */
4204
4205 void
4206 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4207                         const char *file ATTRIBUTE_UNUSED)
4208 {
4209   dw_fde_ref fde;
4210   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4211
4212   last_var_location_insn = NULL_RTX;
4213
4214   if (dwarf2out_do_cfi_asm ())
4215     fprintf (asm_out_file, "\t.cfi_endproc\n");
4216
4217   /* Output a label to mark the endpoint of the code generated for this
4218      function.  */
4219   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4220                                current_function_funcdef_no);
4221   ASM_OUTPUT_LABEL (asm_out_file, label);
4222   fde = current_fde ();
4223   gcc_assert (fde != NULL);
4224   if (fde->dw_fde_second_begin == NULL)
4225     fde->dw_fde_end = xstrdup (label);
4226 }
4227
4228 void
4229 dwarf2out_frame_init (void)
4230 {
4231   /* Allocate the initial hunk of the fde_table.  */
4232   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4233   fde_table_allocated = FDE_TABLE_INCREMENT;
4234   fde_table_in_use = 0;
4235
4236   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4237      sake of lookup_cfa.  */
4238
4239   /* On entry, the Canonical Frame Address is at SP.  */
4240   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4241
4242   if (targetm.debug_unwind_info () == UI_DWARF2
4243       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4244     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4245 }
4246
4247 void
4248 dwarf2out_frame_finish (void)
4249 {
4250   /* Output call frame information.  */
4251   if (targetm.debug_unwind_info () == UI_DWARF2)
4252     output_call_frame_info (0);
4253
4254   /* Output another copy for the unwinder.  */
4255   if ((flag_unwind_tables || flag_exceptions)
4256       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4257     output_call_frame_info (1);
4258 }
4259
4260 /* Note that the current function section is being used for code.  */
4261
4262 static void
4263 dwarf2out_note_section_used (void)
4264 {
4265   section *sec = current_function_section ();
4266   if (sec == text_section)
4267     text_section_used = true;
4268   else if (sec == cold_text_section)
4269     cold_text_section_used = true;
4270 }
4271
4272 static void var_location_switch_text_section (void);
4273 static void set_cur_line_info_table (section *);
4274
4275 void
4276 dwarf2out_switch_text_section (void)
4277 {
4278   section *sect;
4279   dw_fde_ref fde = current_fde ();
4280
4281   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4282
4283   if (!in_cold_section_p)
4284     {
4285       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4286       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4287       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4288     }
4289   else
4290     {
4291       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4292       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4293       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4294     }
4295   have_multiple_function_sections = true;
4296
4297   /* Reset the current label on switching text sections, so that we
4298      don't attempt to advance_loc4 between labels in different sections.  */
4299   fde->dw_fde_current_label = NULL;
4300
4301   /* There is no need to mark used sections when not debugging.  */
4302   if (cold_text_section != NULL)
4303     dwarf2out_note_section_used ();
4304
4305   if (dwarf2out_do_cfi_asm ())
4306     fprintf (asm_out_file, "\t.cfi_endproc\n");
4307
4308   /* Now do the real section switch.  */
4309   sect = current_function_section ();
4310   switch_to_section (sect);
4311
4312   fde->second_in_std_section
4313     = (sect == text_section
4314        || (cold_text_section && sect == cold_text_section));
4315
4316   if (dwarf2out_do_cfi_asm ())
4317     {
4318       dwarf2out_do_cfi_startproc (true);
4319       /* As this is a different FDE, insert all current CFI instructions
4320          again.  */
4321       output_all_cfis (fde->dw_fde_cfi, true, fde, true);
4322     }
4323   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
4324   var_location_switch_text_section ();
4325
4326   set_cur_line_info_table (sect);
4327 }
4328 \f
4329 /* And now, the subset of the debugging information support code necessary
4330    for emitting location expressions.  */
4331
4332 /* Data about a single source file.  */
4333 struct GTY(()) dwarf_file_data {
4334   const char * filename;
4335   int emitted_number;
4336 };
4337
4338 typedef struct dw_val_struct *dw_val_ref;
4339 typedef struct die_struct *dw_die_ref;
4340 typedef const struct die_struct *const_dw_die_ref;
4341 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4342 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4343
4344 typedef struct GTY(()) deferred_locations_struct
4345 {
4346   tree variable;
4347   dw_die_ref die;
4348 } deferred_locations;
4349
4350 DEF_VEC_O(deferred_locations);
4351 DEF_VEC_ALLOC_O(deferred_locations,gc);
4352
4353 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4354
4355 DEF_VEC_P(dw_die_ref);
4356 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4357
4358 /* Each DIE may have a series of attribute/value pairs.  Values
4359    can take on several forms.  The forms that are used in this
4360    implementation are listed below.  */
4361
4362 enum dw_val_class
4363 {
4364   dw_val_class_addr,
4365   dw_val_class_offset,
4366   dw_val_class_loc,
4367   dw_val_class_loc_list,
4368   dw_val_class_range_list,
4369   dw_val_class_const,
4370   dw_val_class_unsigned_const,
4371   dw_val_class_const_double,
4372   dw_val_class_vec,
4373   dw_val_class_flag,
4374   dw_val_class_die_ref,
4375   dw_val_class_fde_ref,
4376   dw_val_class_lbl_id,
4377   dw_val_class_lineptr,
4378   dw_val_class_str,
4379   dw_val_class_macptr,
4380   dw_val_class_file,
4381   dw_val_class_data8,
4382   dw_val_class_decl_ref,
4383   dw_val_class_vms_delta
4384 };
4385
4386 /* Describe a floating point constant value, or a vector constant value.  */
4387
4388 typedef struct GTY(()) dw_vec_struct {
4389   unsigned char * GTY((length ("%h.length"))) array;
4390   unsigned length;
4391   unsigned elt_size;
4392 }
4393 dw_vec_const;
4394
4395 /* The dw_val_node describes an attribute's value, as it is
4396    represented internally.  */
4397
4398 typedef struct GTY(()) dw_val_struct {
4399   enum dw_val_class val_class;
4400   union dw_val_struct_union
4401     {
4402       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4403       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4404       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4405       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4406       HOST_WIDE_INT GTY ((default)) val_int;
4407       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4408       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4409       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4410       struct dw_val_die_union
4411         {
4412           dw_die_ref die;
4413           int external;
4414         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4415       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4416       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4417       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4418       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4419       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4420       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4421       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4422       struct dw_val_vms_delta_union
4423         {
4424           char * lbl1;
4425           char * lbl2;
4426         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4427     }
4428   GTY ((desc ("%1.val_class"))) v;
4429 }
4430 dw_val_node;
4431
4432 /* Locations in memory are described using a sequence of stack machine
4433    operations.  */
4434
4435 typedef struct GTY(()) dw_loc_descr_struct {
4436   dw_loc_descr_ref dw_loc_next;
4437   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4438   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4439      from DW_OP_addr with a dtp-relative symbol relocation.  */
4440   unsigned int dtprel : 1;
4441   int dw_loc_addr;
4442   dw_val_node dw_loc_oprnd1;
4443   dw_val_node dw_loc_oprnd2;
4444 }
4445 dw_loc_descr_node;
4446
4447 /* Location lists are ranges + location descriptions for that range,
4448    so you can track variables that are in different places over
4449    their entire life.  */
4450 typedef struct GTY(()) dw_loc_list_struct {
4451   dw_loc_list_ref dw_loc_next;
4452   const char *begin; /* Label for begin address of range */
4453   const char *end;  /* Label for end address of range */
4454   char *ll_symbol; /* Label for beginning of location list.
4455                       Only on head of list */
4456   const char *section; /* Section this loclist is relative to */
4457   dw_loc_descr_ref expr;
4458   hashval_t hash;
4459   /* True if all addresses in this and subsequent lists are known to be
4460      resolved.  */
4461   bool resolved_addr;
4462   /* True if this list has been replaced by dw_loc_next.  */
4463   bool replaced;
4464   bool emitted;
4465 } dw_loc_list_node;
4466
4467 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4468
4469 /* Convert a DWARF stack opcode into its string name.  */
4470
4471 static const char *
4472 dwarf_stack_op_name (unsigned int op)
4473 {
4474   switch (op)
4475     {
4476     case DW_OP_addr:
4477       return "DW_OP_addr";
4478     case DW_OP_deref:
4479       return "DW_OP_deref";
4480     case DW_OP_const1u:
4481       return "DW_OP_const1u";
4482     case DW_OP_const1s:
4483       return "DW_OP_const1s";
4484     case DW_OP_const2u:
4485       return "DW_OP_const2u";
4486     case DW_OP_const2s:
4487       return "DW_OP_const2s";
4488     case DW_OP_const4u:
4489       return "DW_OP_const4u";
4490     case DW_OP_const4s:
4491       return "DW_OP_const4s";
4492     case DW_OP_const8u:
4493       return "DW_OP_const8u";
4494     case DW_OP_const8s:
4495       return "DW_OP_const8s";
4496     case DW_OP_constu:
4497       return "DW_OP_constu";
4498     case DW_OP_consts:
4499       return "DW_OP_consts";
4500     case DW_OP_dup:
4501       return "DW_OP_dup";
4502     case DW_OP_drop:
4503       return "DW_OP_drop";
4504     case DW_OP_over:
4505       return "DW_OP_over";
4506     case DW_OP_pick:
4507       return "DW_OP_pick";
4508     case DW_OP_swap:
4509       return "DW_OP_swap";
4510     case DW_OP_rot:
4511       return "DW_OP_rot";
4512     case DW_OP_xderef:
4513       return "DW_OP_xderef";
4514     case DW_OP_abs:
4515       return "DW_OP_abs";
4516     case DW_OP_and:
4517       return "DW_OP_and";
4518     case DW_OP_div:
4519       return "DW_OP_div";
4520     case DW_OP_minus:
4521       return "DW_OP_minus";
4522     case DW_OP_mod:
4523       return "DW_OP_mod";
4524     case DW_OP_mul:
4525       return "DW_OP_mul";
4526     case DW_OP_neg:
4527       return "DW_OP_neg";
4528     case DW_OP_not:
4529       return "DW_OP_not";
4530     case DW_OP_or:
4531       return "DW_OP_or";
4532     case DW_OP_plus:
4533       return "DW_OP_plus";
4534     case DW_OP_plus_uconst:
4535       return "DW_OP_plus_uconst";
4536     case DW_OP_shl:
4537       return "DW_OP_shl";
4538     case DW_OP_shr:
4539       return "DW_OP_shr";
4540     case DW_OP_shra:
4541       return "DW_OP_shra";
4542     case DW_OP_xor:
4543       return "DW_OP_xor";
4544     case DW_OP_bra:
4545       return "DW_OP_bra";
4546     case DW_OP_eq:
4547       return "DW_OP_eq";
4548     case DW_OP_ge:
4549       return "DW_OP_ge";
4550     case DW_OP_gt:
4551       return "DW_OP_gt";
4552     case DW_OP_le:
4553       return "DW_OP_le";
4554     case DW_OP_lt:
4555       return "DW_OP_lt";
4556     case DW_OP_ne:
4557       return "DW_OP_ne";
4558     case DW_OP_skip:
4559       return "DW_OP_skip";
4560     case DW_OP_lit0:
4561       return "DW_OP_lit0";
4562     case DW_OP_lit1:
4563       return "DW_OP_lit1";
4564     case DW_OP_lit2:
4565       return "DW_OP_lit2";
4566     case DW_OP_lit3:
4567       return "DW_OP_lit3";
4568     case DW_OP_lit4:
4569       return "DW_OP_lit4";
4570     case DW_OP_lit5:
4571       return "DW_OP_lit5";
4572     case DW_OP_lit6:
4573       return "DW_OP_lit6";
4574     case DW_OP_lit7:
4575       return "DW_OP_lit7";
4576     case DW_OP_lit8:
4577       return "DW_OP_lit8";
4578     case DW_OP_lit9:
4579       return "DW_OP_lit9";
4580     case DW_OP_lit10:
4581       return "DW_OP_lit10";
4582     case DW_OP_lit11:
4583       return "DW_OP_lit11";
4584     case DW_OP_lit12:
4585       return "DW_OP_lit12";
4586     case DW_OP_lit13:
4587       return "DW_OP_lit13";
4588     case DW_OP_lit14:
4589       return "DW_OP_lit14";
4590     case DW_OP_lit15:
4591       return "DW_OP_lit15";
4592     case DW_OP_lit16:
4593       return "DW_OP_lit16";
4594     case DW_OP_lit17:
4595       return "DW_OP_lit17";
4596     case DW_OP_lit18:
4597       return "DW_OP_lit18";
4598     case DW_OP_lit19:
4599       return "DW_OP_lit19";
4600     case DW_OP_lit20:
4601       return "DW_OP_lit20";
4602     case DW_OP_lit21:
4603       return "DW_OP_lit21";
4604     case DW_OP_lit22:
4605       return "DW_OP_lit22";
4606     case DW_OP_lit23:
4607       return "DW_OP_lit23";
4608     case DW_OP_lit24:
4609       return "DW_OP_lit24";
4610     case DW_OP_lit25:
4611       return "DW_OP_lit25";
4612     case DW_OP_lit26:
4613       return "DW_OP_lit26";
4614     case DW_OP_lit27:
4615       return "DW_OP_lit27";
4616     case DW_OP_lit28:
4617       return "DW_OP_lit28";
4618     case DW_OP_lit29:
4619       return "DW_OP_lit29";
4620     case DW_OP_lit30:
4621       return "DW_OP_lit30";
4622     case DW_OP_lit31:
4623       return "DW_OP_lit31";
4624     case DW_OP_reg0:
4625       return "DW_OP_reg0";
4626     case DW_OP_reg1:
4627       return "DW_OP_reg1";
4628     case DW_OP_reg2:
4629       return "DW_OP_reg2";
4630     case DW_OP_reg3:
4631       return "DW_OP_reg3";
4632     case DW_OP_reg4:
4633       return "DW_OP_reg4";
4634     case DW_OP_reg5:
4635       return "DW_OP_reg5";
4636     case DW_OP_reg6:
4637       return "DW_OP_reg6";
4638     case DW_OP_reg7:
4639       return "DW_OP_reg7";
4640     case DW_OP_reg8:
4641       return "DW_OP_reg8";
4642     case DW_OP_reg9:
4643       return "DW_OP_reg9";
4644     case DW_OP_reg10:
4645       return "DW_OP_reg10";
4646     case DW_OP_reg11:
4647       return "DW_OP_reg11";
4648     case DW_OP_reg12:
4649       return "DW_OP_reg12";
4650     case DW_OP_reg13:
4651       return "DW_OP_reg13";
4652     case DW_OP_reg14:
4653       return "DW_OP_reg14";
4654     case DW_OP_reg15:
4655       return "DW_OP_reg15";
4656     case DW_OP_reg16:
4657       return "DW_OP_reg16";
4658     case DW_OP_reg17:
4659       return "DW_OP_reg17";
4660     case DW_OP_reg18:
4661       return "DW_OP_reg18";
4662     case DW_OP_reg19:
4663       return "DW_OP_reg19";
4664     case DW_OP_reg20:
4665       return "DW_OP_reg20";
4666     case DW_OP_reg21:
4667       return "DW_OP_reg21";
4668     case DW_OP_reg22:
4669       return "DW_OP_reg22";
4670     case DW_OP_reg23:
4671       return "DW_OP_reg23";
4672     case DW_OP_reg24:
4673       return "DW_OP_reg24";
4674     case DW_OP_reg25:
4675       return "DW_OP_reg25";
4676     case DW_OP_reg26:
4677       return "DW_OP_reg26";
4678     case DW_OP_reg27:
4679       return "DW_OP_reg27";
4680     case DW_OP_reg28:
4681       return "DW_OP_reg28";
4682     case DW_OP_reg29:
4683       return "DW_OP_reg29";
4684     case DW_OP_reg30:
4685       return "DW_OP_reg30";
4686     case DW_OP_reg31:
4687       return "DW_OP_reg31";
4688     case DW_OP_breg0:
4689       return "DW_OP_breg0";
4690     case DW_OP_breg1:
4691       return "DW_OP_breg1";
4692     case DW_OP_breg2:
4693       return "DW_OP_breg2";
4694     case DW_OP_breg3:
4695       return "DW_OP_breg3";
4696     case DW_OP_breg4:
4697       return "DW_OP_breg4";
4698     case DW_OP_breg5:
4699       return "DW_OP_breg5";
4700     case DW_OP_breg6:
4701       return "DW_OP_breg6";
4702     case DW_OP_breg7:
4703       return "DW_OP_breg7";
4704     case DW_OP_breg8:
4705       return "DW_OP_breg8";
4706     case DW_OP_breg9:
4707       return "DW_OP_breg9";
4708     case DW_OP_breg10:
4709       return "DW_OP_breg10";
4710     case DW_OP_breg11:
4711       return "DW_OP_breg11";
4712     case DW_OP_breg12:
4713       return "DW_OP_breg12";
4714     case DW_OP_breg13:
4715       return "DW_OP_breg13";
4716     case DW_OP_breg14:
4717       return "DW_OP_breg14";
4718     case DW_OP_breg15:
4719       return "DW_OP_breg15";
4720     case DW_OP_breg16:
4721       return "DW_OP_breg16";
4722     case DW_OP_breg17:
4723       return "DW_OP_breg17";
4724     case DW_OP_breg18:
4725       return "DW_OP_breg18";
4726     case DW_OP_breg19:
4727       return "DW_OP_breg19";
4728     case DW_OP_breg20:
4729       return "DW_OP_breg20";
4730     case DW_OP_breg21:
4731       return "DW_OP_breg21";
4732     case DW_OP_breg22:
4733       return "DW_OP_breg22";
4734     case DW_OP_breg23:
4735       return "DW_OP_breg23";
4736     case DW_OP_breg24:
4737       return "DW_OP_breg24";
4738     case DW_OP_breg25:
4739       return "DW_OP_breg25";
4740     case DW_OP_breg26:
4741       return "DW_OP_breg26";
4742     case DW_OP_breg27:
4743       return "DW_OP_breg27";
4744     case DW_OP_breg28:
4745       return "DW_OP_breg28";
4746     case DW_OP_breg29:
4747       return "DW_OP_breg29";
4748     case DW_OP_breg30:
4749       return "DW_OP_breg30";
4750     case DW_OP_breg31:
4751       return "DW_OP_breg31";
4752     case DW_OP_regx:
4753       return "DW_OP_regx";
4754     case DW_OP_fbreg:
4755       return "DW_OP_fbreg";
4756     case DW_OP_bregx:
4757       return "DW_OP_bregx";
4758     case DW_OP_piece:
4759       return "DW_OP_piece";
4760     case DW_OP_deref_size:
4761       return "DW_OP_deref_size";
4762     case DW_OP_xderef_size:
4763       return "DW_OP_xderef_size";
4764     case DW_OP_nop:
4765       return "DW_OP_nop";
4766
4767     case DW_OP_push_object_address:
4768       return "DW_OP_push_object_address";
4769     case DW_OP_call2:
4770       return "DW_OP_call2";
4771     case DW_OP_call4:
4772       return "DW_OP_call4";
4773     case DW_OP_call_ref:
4774       return "DW_OP_call_ref";
4775     case DW_OP_implicit_value:
4776       return "DW_OP_implicit_value";
4777     case DW_OP_stack_value:
4778       return "DW_OP_stack_value";
4779     case DW_OP_form_tls_address:
4780       return "DW_OP_form_tls_address";
4781     case DW_OP_call_frame_cfa:
4782       return "DW_OP_call_frame_cfa";
4783     case DW_OP_bit_piece:
4784       return "DW_OP_bit_piece";
4785
4786     case DW_OP_GNU_push_tls_address:
4787       return "DW_OP_GNU_push_tls_address";
4788     case DW_OP_GNU_uninit:
4789       return "DW_OP_GNU_uninit";
4790     case DW_OP_GNU_encoded_addr:
4791       return "DW_OP_GNU_encoded_addr";
4792     case DW_OP_GNU_implicit_pointer:
4793       return "DW_OP_GNU_implicit_pointer";
4794     case DW_OP_GNU_entry_value:
4795       return "DW_OP_GNU_entry_value";
4796     case DW_OP_GNU_const_type:
4797       return "DW_OP_GNU_const_type";
4798     case DW_OP_GNU_regval_type:
4799       return "DW_OP_GNU_regval_type";
4800     case DW_OP_GNU_deref_type:
4801       return "DW_OP_GNU_deref_type";
4802     case DW_OP_GNU_convert:
4803       return "DW_OP_GNU_convert";
4804     case DW_OP_GNU_reinterpret:
4805       return "DW_OP_GNU_reinterpret";
4806
4807     default:
4808       return "OP_<unknown>";
4809     }
4810 }
4811
4812 /* Return a pointer to a newly allocated location description.  Location
4813    descriptions are simple expression terms that can be strung
4814    together to form more complicated location (address) descriptions.  */
4815
4816 static inline dw_loc_descr_ref
4817 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4818                unsigned HOST_WIDE_INT oprnd2)
4819 {
4820   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4821
4822   descr->dw_loc_opc = op;
4823   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4824   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4825   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4826   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4827
4828   return descr;
4829 }
4830
4831 /* Return a pointer to a newly allocated location description for
4832    REG and OFFSET.  */
4833
4834 static inline dw_loc_descr_ref
4835 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4836 {
4837   if (reg <= 31)
4838     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4839                           offset, 0);
4840   else
4841     return new_loc_descr (DW_OP_bregx, reg, offset);
4842 }
4843
4844 /* Add a location description term to a location description expression.  */
4845
4846 static inline void
4847 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4848 {
4849   dw_loc_descr_ref *d;
4850
4851   /* Find the end of the chain.  */
4852   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4853     ;
4854
4855   *d = descr;
4856 }
4857
4858 /* Add a constant OFFSET to a location expression.  */
4859
4860 static void
4861 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4862 {
4863   dw_loc_descr_ref loc;
4864   HOST_WIDE_INT *p;
4865
4866   gcc_assert (*list_head != NULL);
4867
4868   if (!offset)
4869     return;
4870
4871   /* Find the end of the chain.  */
4872   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4873     ;
4874
4875   p = NULL;
4876   if (loc->dw_loc_opc == DW_OP_fbreg
4877       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4878     p = &loc->dw_loc_oprnd1.v.val_int;
4879   else if (loc->dw_loc_opc == DW_OP_bregx)
4880     p = &loc->dw_loc_oprnd2.v.val_int;
4881
4882   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4883      offset.  Don't optimize if an signed integer overflow would happen.  */
4884   if (p != NULL
4885       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4886           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4887     *p += offset;
4888
4889   else if (offset > 0)
4890     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4891
4892   else
4893     {
4894       loc->dw_loc_next = int_loc_descriptor (-offset);
4895       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4896     }
4897 }
4898
4899 /* Add a constant OFFSET to a location list.  */
4900
4901 static void
4902 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4903 {
4904   dw_loc_list_ref d;
4905   for (d = list_head; d != NULL; d = d->dw_loc_next)
4906     loc_descr_plus_const (&d->expr, offset);
4907 }
4908
4909 #define DWARF_REF_SIZE  \
4910   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4911
4912 static unsigned long size_of_locs (dw_loc_descr_ref);
4913 static unsigned long int get_base_type_offset (dw_die_ref);
4914
4915 /* Return the size of a location descriptor.  */
4916
4917 static unsigned long
4918 size_of_loc_descr (dw_loc_descr_ref loc)
4919 {
4920   unsigned long size = 1;
4921
4922   switch (loc->dw_loc_opc)
4923     {
4924     case DW_OP_addr:
4925       size += DWARF2_ADDR_SIZE;
4926       break;
4927     case DW_OP_const1u:
4928     case DW_OP_const1s:
4929       size += 1;
4930       break;
4931     case DW_OP_const2u:
4932     case DW_OP_const2s:
4933       size += 2;
4934       break;
4935     case DW_OP_const4u:
4936     case DW_OP_const4s:
4937       size += 4;
4938       break;
4939     case DW_OP_const8u:
4940     case DW_OP_const8s:
4941       size += 8;
4942       break;
4943     case DW_OP_constu:
4944       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4945       break;
4946     case DW_OP_consts:
4947       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4948       break;
4949     case DW_OP_pick:
4950       size += 1;
4951       break;
4952     case DW_OP_plus_uconst:
4953       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4954       break;
4955     case DW_OP_skip:
4956     case DW_OP_bra:
4957       size += 2;
4958       break;
4959     case DW_OP_breg0:
4960     case DW_OP_breg1:
4961     case DW_OP_breg2:
4962     case DW_OP_breg3:
4963     case DW_OP_breg4:
4964     case DW_OP_breg5:
4965     case DW_OP_breg6:
4966     case DW_OP_breg7:
4967     case DW_OP_breg8:
4968     case DW_OP_breg9:
4969     case DW_OP_breg10:
4970     case DW_OP_breg11:
4971     case DW_OP_breg12:
4972     case DW_OP_breg13:
4973     case DW_OP_breg14:
4974     case DW_OP_breg15:
4975     case DW_OP_breg16:
4976     case DW_OP_breg17:
4977     case DW_OP_breg18:
4978     case DW_OP_breg19:
4979     case DW_OP_breg20:
4980     case DW_OP_breg21:
4981     case DW_OP_breg22:
4982     case DW_OP_breg23:
4983     case DW_OP_breg24:
4984     case DW_OP_breg25:
4985     case DW_OP_breg26:
4986     case DW_OP_breg27:
4987     case DW_OP_breg28:
4988     case DW_OP_breg29:
4989     case DW_OP_breg30:
4990     case DW_OP_breg31:
4991       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4992       break;
4993     case DW_OP_regx:
4994       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4995       break;
4996     case DW_OP_fbreg:
4997       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4998       break;
4999     case DW_OP_bregx:
5000       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5001       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5002       break;
5003     case DW_OP_piece:
5004       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5005       break;
5006     case DW_OP_bit_piece:
5007       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5008       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5009       break;
5010     case DW_OP_deref_size:
5011     case DW_OP_xderef_size:
5012       size += 1;
5013       break;
5014     case DW_OP_call2:
5015       size += 2;
5016       break;
5017     case DW_OP_call4:
5018       size += 4;
5019       break;
5020     case DW_OP_call_ref:
5021       size += DWARF_REF_SIZE;
5022       break;
5023     case DW_OP_implicit_value:
5024       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5025               + loc->dw_loc_oprnd1.v.val_unsigned;
5026       break;
5027     case DW_OP_GNU_implicit_pointer:
5028       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5029       break;
5030     case DW_OP_GNU_entry_value:
5031       {
5032         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5033         size += size_of_uleb128 (op_size) + op_size;
5034         break;
5035       }
5036     case DW_OP_GNU_const_type:
5037       {
5038         unsigned long o
5039           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5040         size += size_of_uleb128 (o) + 1;
5041         switch (loc->dw_loc_oprnd2.val_class)
5042           {
5043           case dw_val_class_vec:
5044             size += loc->dw_loc_oprnd2.v.val_vec.length
5045                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
5046             break;
5047           case dw_val_class_const:
5048             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5049             break;
5050           case dw_val_class_const_double:
5051             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5052             break;
5053           default:
5054             gcc_unreachable ();
5055           }
5056         break;
5057       }
5058     case DW_OP_GNU_regval_type:
5059       {
5060         unsigned long o
5061           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5062         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5063                 + size_of_uleb128 (o);
5064       }
5065       break;
5066     case DW_OP_GNU_deref_type:
5067       {
5068         unsigned long o
5069           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5070         size += 1 + size_of_uleb128 (o);
5071       }
5072       break;
5073     case DW_OP_GNU_convert:
5074     case DW_OP_GNU_reinterpret:
5075       {
5076         unsigned long o
5077           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5078         size += size_of_uleb128 (o);
5079       }
5080     default:
5081       break;
5082     }
5083
5084   return size;
5085 }
5086
5087 /* Return the size of a series of location descriptors.  */
5088
5089 static unsigned long
5090 size_of_locs (dw_loc_descr_ref loc)
5091 {
5092   dw_loc_descr_ref l;
5093   unsigned long size;
5094
5095   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5096      field, to avoid writing to a PCH file.  */
5097   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5098     {
5099       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5100         break;
5101       size += size_of_loc_descr (l);
5102     }
5103   if (! l)
5104     return size;
5105
5106   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5107     {
5108       l->dw_loc_addr = size;
5109       size += size_of_loc_descr (l);
5110     }
5111
5112   return size;
5113 }
5114
5115 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5116 static void get_ref_die_offset_label (char *, dw_die_ref);
5117 static void output_loc_sequence (dw_loc_descr_ref, int);
5118
5119 /* Output location description stack opcode's operands (if any).
5120    The for_eh_or_skip parameter controls whether register numbers are
5121    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5122    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5123    info).  This should be suppressed for the cases that have not been converted
5124    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5125
5126 static void
5127 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5128 {
5129   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5130   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5131
5132   switch (loc->dw_loc_opc)
5133     {
5134 #ifdef DWARF2_DEBUGGING_INFO
5135     case DW_OP_const2u:
5136     case DW_OP_const2s:
5137       dw2_asm_output_data (2, val1->v.val_int, NULL);
5138       break;
5139     case DW_OP_const4u:
5140       if (loc->dtprel)
5141         {
5142           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5143           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5144                                                val1->v.val_addr);
5145           fputc ('\n', asm_out_file);
5146           break;
5147         }
5148       /* FALLTHRU */
5149     case DW_OP_const4s:
5150       dw2_asm_output_data (4, val1->v.val_int, NULL);
5151       break;
5152     case DW_OP_const8u:
5153       if (loc->dtprel)
5154         {
5155           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5156           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5157                                                val1->v.val_addr);
5158           fputc ('\n', asm_out_file);
5159           break;
5160         }
5161       /* FALLTHRU */
5162     case DW_OP_const8s:
5163       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5164       dw2_asm_output_data (8, val1->v.val_int, NULL);
5165       break;
5166     case DW_OP_skip:
5167     case DW_OP_bra:
5168       {
5169         int offset;
5170
5171         gcc_assert (val1->val_class == dw_val_class_loc);
5172         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5173
5174         dw2_asm_output_data (2, offset, NULL);
5175       }
5176       break;
5177     case DW_OP_implicit_value:
5178       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5179       switch (val2->val_class)
5180         {
5181         case dw_val_class_const:
5182           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5183           break;
5184         case dw_val_class_vec:
5185           {
5186             unsigned int elt_size = val2->v.val_vec.elt_size;
5187             unsigned int len = val2->v.val_vec.length;
5188             unsigned int i;
5189             unsigned char *p;
5190
5191             if (elt_size > sizeof (HOST_WIDE_INT))
5192               {
5193                 elt_size /= 2;
5194                 len *= 2;
5195               }
5196             for (i = 0, p = val2->v.val_vec.array;
5197                  i < len;
5198                  i++, p += elt_size)
5199               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5200                                    "fp or vector constant word %u", i);
5201           }
5202           break;
5203         case dw_val_class_const_double:
5204           {
5205             unsigned HOST_WIDE_INT first, second;
5206
5207             if (WORDS_BIG_ENDIAN)
5208               {
5209                 first = val2->v.val_double.high;
5210                 second = val2->v.val_double.low;
5211               }
5212             else
5213               {
5214                 first = val2->v.val_double.low;
5215                 second = val2->v.val_double.high;
5216               }
5217             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5218                                  first, NULL);
5219             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5220                                  second, NULL);
5221           }
5222           break;
5223         case dw_val_class_addr:
5224           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5225           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5226           break;
5227         default:
5228           gcc_unreachable ();
5229         }
5230       break;
5231 #else
5232     case DW_OP_const2u:
5233     case DW_OP_const2s:
5234     case DW_OP_const4u:
5235     case DW_OP_const4s:
5236     case DW_OP_const8u:
5237     case DW_OP_const8s:
5238     case DW_OP_skip:
5239     case DW_OP_bra:
5240     case DW_OP_implicit_value:
5241       /* We currently don't make any attempt to make sure these are
5242          aligned properly like we do for the main unwind info, so
5243          don't support emitting things larger than a byte if we're
5244          only doing unwinding.  */
5245       gcc_unreachable ();
5246 #endif
5247     case DW_OP_const1u:
5248     case DW_OP_const1s:
5249       dw2_asm_output_data (1, val1->v.val_int, NULL);
5250       break;
5251     case DW_OP_constu:
5252       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5253       break;
5254     case DW_OP_consts:
5255       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5256       break;
5257     case DW_OP_pick:
5258       dw2_asm_output_data (1, val1->v.val_int, NULL);
5259       break;
5260     case DW_OP_plus_uconst:
5261       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5262       break;
5263     case DW_OP_breg0:
5264     case DW_OP_breg1:
5265     case DW_OP_breg2:
5266     case DW_OP_breg3:
5267     case DW_OP_breg4:
5268     case DW_OP_breg5:
5269     case DW_OP_breg6:
5270     case DW_OP_breg7:
5271     case DW_OP_breg8:
5272     case DW_OP_breg9:
5273     case DW_OP_breg10:
5274     case DW_OP_breg11:
5275     case DW_OP_breg12:
5276     case DW_OP_breg13:
5277     case DW_OP_breg14:
5278     case DW_OP_breg15:
5279     case DW_OP_breg16:
5280     case DW_OP_breg17:
5281     case DW_OP_breg18:
5282     case DW_OP_breg19:
5283     case DW_OP_breg20:
5284     case DW_OP_breg21:
5285     case DW_OP_breg22:
5286     case DW_OP_breg23:
5287     case DW_OP_breg24:
5288     case DW_OP_breg25:
5289     case DW_OP_breg26:
5290     case DW_OP_breg27:
5291     case DW_OP_breg28:
5292     case DW_OP_breg29:
5293     case DW_OP_breg30:
5294     case DW_OP_breg31:
5295       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5296       break;
5297     case DW_OP_regx:
5298       {
5299         unsigned r = val1->v.val_unsigned;
5300         if (for_eh_or_skip >= 0)
5301           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5302         gcc_assert (size_of_uleb128 (r) 
5303                     == size_of_uleb128 (val1->v.val_unsigned));
5304         dw2_asm_output_data_uleb128 (r, NULL);  
5305       }
5306       break;
5307     case DW_OP_fbreg:
5308       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5309       break;
5310     case DW_OP_bregx:
5311       {
5312         unsigned r = val1->v.val_unsigned;
5313         if (for_eh_or_skip >= 0)
5314           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5315         gcc_assert (size_of_uleb128 (r) 
5316                     == size_of_uleb128 (val1->v.val_unsigned));
5317         dw2_asm_output_data_uleb128 (r, NULL);  
5318         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5319       }
5320       break;
5321     case DW_OP_piece:
5322       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5323       break;
5324     case DW_OP_bit_piece:
5325       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5326       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5327       break;
5328     case DW_OP_deref_size:
5329     case DW_OP_xderef_size:
5330       dw2_asm_output_data (1, val1->v.val_int, NULL);
5331       break;
5332
5333     case DW_OP_addr:
5334       if (loc->dtprel)
5335         {
5336           if (targetm.asm_out.output_dwarf_dtprel)
5337             {
5338               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5339                                                    DWARF2_ADDR_SIZE,
5340                                                    val1->v.val_addr);
5341               fputc ('\n', asm_out_file);
5342             }
5343           else
5344             gcc_unreachable ();
5345         }
5346       else
5347         {
5348 #ifdef DWARF2_DEBUGGING_INFO
5349           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5350 #else
5351           gcc_unreachable ();
5352 #endif
5353         }
5354       break;
5355
5356     case DW_OP_GNU_implicit_pointer:
5357       {
5358         char label[MAX_ARTIFICIAL_LABEL_BYTES
5359                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5360         gcc_assert (val1->val_class == dw_val_class_die_ref);
5361         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5362         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5363         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5364       }
5365       break;
5366
5367     case DW_OP_GNU_entry_value:
5368       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5369       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5370       break;
5371
5372     case DW_OP_GNU_const_type:
5373       {
5374         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
5375         gcc_assert (o);
5376         dw2_asm_output_data_uleb128 (o, NULL);
5377         switch (val2->val_class)
5378           {
5379           case dw_val_class_const:
5380             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5381             dw2_asm_output_data (1, l, NULL);
5382             dw2_asm_output_data (l, val2->v.val_int, NULL);
5383             break;
5384           case dw_val_class_vec:
5385             {
5386               unsigned int elt_size = val2->v.val_vec.elt_size;
5387               unsigned int len = val2->v.val_vec.length;
5388               unsigned int i;
5389               unsigned char *p;
5390
5391               l = len * elt_size;
5392               dw2_asm_output_data (1, l, NULL);
5393               if (elt_size > sizeof (HOST_WIDE_INT))
5394                 {
5395                   elt_size /= 2;
5396                   len *= 2;
5397                 }
5398               for (i = 0, p = val2->v.val_vec.array;
5399                    i < len;
5400                    i++, p += elt_size)
5401                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5402                                      "fp or vector constant word %u", i);
5403             }
5404             break;
5405           case dw_val_class_const_double:
5406             {
5407               unsigned HOST_WIDE_INT first, second;
5408               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5409
5410               dw2_asm_output_data (1, 2 * l, NULL);
5411               if (WORDS_BIG_ENDIAN)
5412                 {
5413                   first = val2->v.val_double.high;
5414                   second = val2->v.val_double.low;
5415                 }
5416               else
5417                 {
5418                   first = val2->v.val_double.low;
5419                   second = val2->v.val_double.high;
5420                 }
5421               dw2_asm_output_data (l, first, NULL);
5422               dw2_asm_output_data (l, second, NULL);
5423             }
5424             break;
5425           default:
5426             gcc_unreachable ();
5427           }
5428       }
5429       break;
5430     case DW_OP_GNU_regval_type:
5431       {
5432         unsigned r = val1->v.val_unsigned;
5433         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5434         gcc_assert (o);
5435         if (for_eh_or_skip >= 0)
5436           {
5437             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5438             gcc_assert (size_of_uleb128 (r)
5439                         == size_of_uleb128 (val1->v.val_unsigned));
5440           }
5441         dw2_asm_output_data_uleb128 (r, NULL);
5442         dw2_asm_output_data_uleb128 (o, NULL);
5443       }
5444       break;
5445     case DW_OP_GNU_deref_type:
5446       {
5447         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5448         gcc_assert (o);
5449         dw2_asm_output_data (1, val1->v.val_int, NULL);
5450         dw2_asm_output_data_uleb128 (o, NULL);
5451       }
5452       break;
5453     case DW_OP_GNU_convert:
5454     case DW_OP_GNU_reinterpret:
5455       {
5456         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
5457         gcc_assert (o);
5458         dw2_asm_output_data_uleb128 (o, NULL);
5459       }
5460       break;
5461
5462     default:
5463       /* Other codes have no operands.  */
5464       break;
5465     }
5466 }
5467
5468 /* Output a sequence of location operations.  
5469    The for_eh_or_skip parameter controls whether register numbers are
5470    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5471    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5472    info).  This should be suppressed for the cases that have not been converted
5473    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5474
5475 static void
5476 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5477 {
5478   for (; loc != NULL; loc = loc->dw_loc_next)
5479     {
5480       enum dwarf_location_atom opc = loc->dw_loc_opc;
5481       /* Output the opcode.  */
5482       if (for_eh_or_skip >= 0 
5483           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5484         {
5485           unsigned r = (opc - DW_OP_breg0);
5486           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5487           gcc_assert (r <= 31);
5488           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5489         }
5490       else if (for_eh_or_skip >= 0 
5491                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5492         {
5493           unsigned r = (opc - DW_OP_reg0);
5494           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5495           gcc_assert (r <= 31);
5496           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5497         }
5498
5499       dw2_asm_output_data (1, opc,
5500                              "%s", dwarf_stack_op_name (opc));
5501
5502       /* Output the operand(s) (if any).  */
5503       output_loc_operands (loc, for_eh_or_skip);
5504     }
5505 }
5506
5507 /* Output location description stack opcode's operands (if any).
5508    The output is single bytes on a line, suitable for .cfi_escape.  */
5509
5510 static void
5511 output_loc_operands_raw (dw_loc_descr_ref loc)
5512 {
5513   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5514   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5515
5516   switch (loc->dw_loc_opc)
5517     {
5518     case DW_OP_addr:
5519     case DW_OP_implicit_value:
5520       /* We cannot output addresses in .cfi_escape, only bytes.  */
5521       gcc_unreachable ();
5522
5523     case DW_OP_const1u:
5524     case DW_OP_const1s:
5525     case DW_OP_pick:
5526     case DW_OP_deref_size:
5527     case DW_OP_xderef_size:
5528       fputc (',', asm_out_file);
5529       dw2_asm_output_data_raw (1, val1->v.val_int);
5530       break;
5531
5532     case DW_OP_const2u:
5533     case DW_OP_const2s:
5534       fputc (',', asm_out_file);
5535       dw2_asm_output_data_raw (2, val1->v.val_int);
5536       break;
5537
5538     case DW_OP_const4u:
5539     case DW_OP_const4s:
5540       fputc (',', asm_out_file);
5541       dw2_asm_output_data_raw (4, val1->v.val_int);
5542       break;
5543
5544     case DW_OP_const8u:
5545     case DW_OP_const8s:
5546       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5547       fputc (',', asm_out_file);
5548       dw2_asm_output_data_raw (8, val1->v.val_int);
5549       break;
5550
5551     case DW_OP_skip:
5552     case DW_OP_bra:
5553       {
5554         int offset;
5555
5556         gcc_assert (val1->val_class == dw_val_class_loc);
5557         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5558
5559         fputc (',', asm_out_file);
5560         dw2_asm_output_data_raw (2, offset);
5561       }
5562       break;
5563
5564     case DW_OP_regx:
5565       {
5566         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5567         gcc_assert (size_of_uleb128 (r) 
5568                     == size_of_uleb128 (val1->v.val_unsigned));
5569         fputc (',', asm_out_file);
5570         dw2_asm_output_data_uleb128_raw (r);
5571       }
5572       break;
5573       
5574     case DW_OP_constu:
5575     case DW_OP_plus_uconst:
5576     case DW_OP_piece:
5577       fputc (',', asm_out_file);
5578       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5579       break;
5580
5581     case DW_OP_bit_piece:
5582       fputc (',', asm_out_file);
5583       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5584       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5585       break;
5586
5587     case DW_OP_consts:
5588     case DW_OP_breg0:
5589     case DW_OP_breg1:
5590     case DW_OP_breg2:
5591     case DW_OP_breg3:
5592     case DW_OP_breg4:
5593     case DW_OP_breg5:
5594     case DW_OP_breg6:
5595     case DW_OP_breg7:
5596     case DW_OP_breg8:
5597     case DW_OP_breg9:
5598     case DW_OP_breg10:
5599     case DW_OP_breg11:
5600     case DW_OP_breg12:
5601     case DW_OP_breg13:
5602     case DW_OP_breg14:
5603     case DW_OP_breg15:
5604     case DW_OP_breg16:
5605     case DW_OP_breg17:
5606     case DW_OP_breg18:
5607     case DW_OP_breg19:
5608     case DW_OP_breg20:
5609     case DW_OP_breg21:
5610     case DW_OP_breg22:
5611     case DW_OP_breg23:
5612     case DW_OP_breg24:
5613     case DW_OP_breg25:
5614     case DW_OP_breg26:
5615     case DW_OP_breg27:
5616     case DW_OP_breg28:
5617     case DW_OP_breg29:
5618     case DW_OP_breg30:
5619     case DW_OP_breg31:
5620     case DW_OP_fbreg:
5621       fputc (',', asm_out_file);
5622       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5623       break;
5624
5625     case DW_OP_bregx:
5626       {
5627         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5628         gcc_assert (size_of_uleb128 (r) 
5629                     == size_of_uleb128 (val1->v.val_unsigned));
5630         fputc (',', asm_out_file);
5631         dw2_asm_output_data_uleb128_raw (r);
5632         fputc (',', asm_out_file);
5633         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5634       }
5635       break;
5636
5637     case DW_OP_GNU_implicit_pointer:
5638     case DW_OP_GNU_entry_value:
5639     case DW_OP_GNU_const_type:
5640     case DW_OP_GNU_regval_type:
5641     case DW_OP_GNU_deref_type:
5642     case DW_OP_GNU_convert:
5643     case DW_OP_GNU_reinterpret:
5644       gcc_unreachable ();
5645       break;
5646
5647     default:
5648       /* Other codes have no operands.  */
5649       break;
5650     }
5651 }
5652
5653 static void
5654 output_loc_sequence_raw (dw_loc_descr_ref loc)
5655 {
5656   while (1)
5657     {
5658       enum dwarf_location_atom opc = loc->dw_loc_opc;
5659       /* Output the opcode.  */
5660       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5661         {
5662           unsigned r = (opc - DW_OP_breg0);
5663           r = DWARF2_FRAME_REG_OUT (r, 1);
5664           gcc_assert (r <= 31);
5665           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5666         }
5667       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5668         {
5669           unsigned r = (opc - DW_OP_reg0);
5670           r = DWARF2_FRAME_REG_OUT (r, 1);
5671           gcc_assert (r <= 31);
5672           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5673         }
5674       /* Output the opcode.  */
5675       fprintf (asm_out_file, "%#x", opc);
5676       output_loc_operands_raw (loc);
5677
5678       if (!loc->dw_loc_next)
5679         break;
5680       loc = loc->dw_loc_next;
5681
5682       fputc (',', asm_out_file);
5683     }
5684 }
5685
5686 /* This routine will generate the correct assembly data for a location
5687    description based on a cfi entry with a complex address.  */
5688
5689 static void
5690 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5691 {
5692   dw_loc_descr_ref loc;
5693   unsigned long size;
5694
5695   if (cfi->dw_cfi_opc == DW_CFA_expression)
5696     {
5697       unsigned r = 
5698         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5699       dw2_asm_output_data (1, r, NULL);
5700       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5701     }
5702   else
5703     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5704
5705   /* Output the size of the block.  */
5706   size = size_of_locs (loc);
5707   dw2_asm_output_data_uleb128 (size, NULL);
5708
5709   /* Now output the operations themselves.  */
5710   output_loc_sequence (loc, for_eh);
5711 }
5712
5713 /* Similar, but used for .cfi_escape.  */
5714
5715 static void
5716 output_cfa_loc_raw (dw_cfi_ref cfi)
5717 {
5718   dw_loc_descr_ref loc;
5719   unsigned long size;
5720
5721   if (cfi->dw_cfi_opc == DW_CFA_expression)
5722     {
5723       unsigned r = 
5724         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5725       fprintf (asm_out_file, "%#x,", r);
5726       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5727     }
5728   else
5729     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5730
5731   /* Output the size of the block.  */
5732   size = size_of_locs (loc);
5733   dw2_asm_output_data_uleb128_raw (size);
5734   fputc (',', asm_out_file);
5735
5736   /* Now output the operations themselves.  */
5737   output_loc_sequence_raw (loc);
5738 }
5739
5740 /* This function builds a dwarf location descriptor sequence from a
5741    dw_cfa_location, adding the given OFFSET to the result of the
5742    expression.  */
5743
5744 static struct dw_loc_descr_struct *
5745 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5746 {
5747   struct dw_loc_descr_struct *head, *tmp;
5748
5749   offset += cfa->offset;
5750
5751   if (cfa->indirect)
5752     {
5753       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5754       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5755       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5756       add_loc_descr (&head, tmp);
5757       if (offset != 0)
5758         {
5759           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5760           add_loc_descr (&head, tmp);
5761         }
5762     }
5763   else
5764     head = new_reg_loc_descr (cfa->reg, offset);
5765
5766   return head;
5767 }
5768
5769 /* This function builds a dwarf location descriptor sequence for
5770    the address at OFFSET from the CFA when stack is aligned to
5771    ALIGNMENT byte.  */
5772
5773 static struct dw_loc_descr_struct *
5774 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5775 {
5776   struct dw_loc_descr_struct *head;
5777   unsigned int dwarf_fp
5778     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5779
5780  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5781   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5782     {
5783       head = new_reg_loc_descr (dwarf_fp, 0);
5784       add_loc_descr (&head, int_loc_descriptor (alignment));
5785       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5786       loc_descr_plus_const (&head, offset);
5787     }
5788   else
5789     head = new_reg_loc_descr (dwarf_fp, offset);
5790   return head;
5791 }
5792
5793 /* This function fills in aa dw_cfa_location structure from a dwarf location
5794    descriptor sequence.  */
5795
5796 static void
5797 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5798 {
5799   struct dw_loc_descr_struct *ptr;
5800   cfa->offset = 0;
5801   cfa->base_offset = 0;
5802   cfa->indirect = 0;
5803   cfa->reg = -1;
5804
5805   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5806     {
5807       enum dwarf_location_atom op = ptr->dw_loc_opc;
5808
5809       switch (op)
5810         {
5811         case DW_OP_reg0:
5812         case DW_OP_reg1:
5813         case DW_OP_reg2:
5814         case DW_OP_reg3:
5815         case DW_OP_reg4:
5816         case DW_OP_reg5:
5817         case DW_OP_reg6:
5818         case DW_OP_reg7:
5819         case DW_OP_reg8:
5820         case DW_OP_reg9:
5821         case DW_OP_reg10:
5822         case DW_OP_reg11:
5823         case DW_OP_reg12:
5824         case DW_OP_reg13:
5825         case DW_OP_reg14:
5826         case DW_OP_reg15:
5827         case DW_OP_reg16:
5828         case DW_OP_reg17:
5829         case DW_OP_reg18:
5830         case DW_OP_reg19:
5831         case DW_OP_reg20:
5832         case DW_OP_reg21:
5833         case DW_OP_reg22:
5834         case DW_OP_reg23:
5835         case DW_OP_reg24:
5836         case DW_OP_reg25:
5837         case DW_OP_reg26:
5838         case DW_OP_reg27:
5839         case DW_OP_reg28:
5840         case DW_OP_reg29:
5841         case DW_OP_reg30:
5842         case DW_OP_reg31:
5843           cfa->reg = op - DW_OP_reg0;
5844           break;
5845         case DW_OP_regx:
5846           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5847           break;
5848         case DW_OP_breg0:
5849         case DW_OP_breg1:
5850         case DW_OP_breg2:
5851         case DW_OP_breg3:
5852         case DW_OP_breg4:
5853         case DW_OP_breg5:
5854         case DW_OP_breg6:
5855         case DW_OP_breg7:
5856         case DW_OP_breg8:
5857         case DW_OP_breg9:
5858         case DW_OP_breg10:
5859         case DW_OP_breg11:
5860         case DW_OP_breg12:
5861         case DW_OP_breg13:
5862         case DW_OP_breg14:
5863         case DW_OP_breg15:
5864         case DW_OP_breg16:
5865         case DW_OP_breg17:
5866         case DW_OP_breg18:
5867         case DW_OP_breg19:
5868         case DW_OP_breg20:
5869         case DW_OP_breg21:
5870         case DW_OP_breg22:
5871         case DW_OP_breg23:
5872         case DW_OP_breg24:
5873         case DW_OP_breg25:
5874         case DW_OP_breg26:
5875         case DW_OP_breg27:
5876         case DW_OP_breg28:
5877         case DW_OP_breg29:
5878         case DW_OP_breg30:
5879         case DW_OP_breg31:
5880           cfa->reg = op - DW_OP_breg0;
5881           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5882           break;
5883         case DW_OP_bregx:
5884           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5885           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5886           break;
5887         case DW_OP_deref:
5888           cfa->indirect = 1;
5889           break;
5890         case DW_OP_plus_uconst:
5891           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5892           break;
5893         default:
5894           internal_error ("DW_LOC_OP %s not implemented",
5895                           dwarf_stack_op_name (ptr->dw_loc_opc));
5896         }
5897     }
5898 }
5899 \f
5900 /* And now, the support for symbolic debugging information.  */
5901
5902 /* .debug_str support.  */
5903 static int output_indirect_string (void **, void *);
5904
5905 static void dwarf2out_init (const char *);
5906 static void dwarf2out_finish (const char *);
5907 static void dwarf2out_assembly_start (void);
5908 static void dwarf2out_define (unsigned int, const char *);
5909 static void dwarf2out_undef (unsigned int, const char *);
5910 static void dwarf2out_start_source_file (unsigned, const char *);
5911 static void dwarf2out_end_source_file (unsigned);
5912 static void dwarf2out_function_decl (tree);
5913 static void dwarf2out_begin_block (unsigned, unsigned);
5914 static void dwarf2out_end_block (unsigned, unsigned);
5915 static bool dwarf2out_ignore_block (const_tree);
5916 static void dwarf2out_global_decl (tree);
5917 static void dwarf2out_type_decl (tree, int);
5918 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5919 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5920                                                  dw_die_ref);
5921 static void dwarf2out_abstract_function (tree);
5922 static void dwarf2out_var_location (rtx);
5923 static void dwarf2out_begin_function (tree);
5924 static void dwarf2out_set_name (tree, tree);
5925
5926 /* The debug hooks structure.  */
5927
5928 const struct gcc_debug_hooks dwarf2_debug_hooks =
5929 {
5930   dwarf2out_init,
5931   dwarf2out_finish,
5932   dwarf2out_assembly_start,
5933   dwarf2out_define,
5934   dwarf2out_undef,
5935   dwarf2out_start_source_file,
5936   dwarf2out_end_source_file,
5937   dwarf2out_begin_block,
5938   dwarf2out_end_block,
5939   dwarf2out_ignore_block,
5940   dwarf2out_source_line,
5941   dwarf2out_begin_prologue,
5942 #if VMS_DEBUGGING_INFO
5943   dwarf2out_vms_end_prologue,
5944   dwarf2out_vms_begin_epilogue,
5945 #else
5946   debug_nothing_int_charstar,
5947   debug_nothing_int_charstar,
5948 #endif
5949   dwarf2out_end_epilogue,
5950   dwarf2out_begin_function,
5951   debug_nothing_int,            /* end_function */
5952   dwarf2out_function_decl,      /* function_decl */
5953   dwarf2out_global_decl,
5954   dwarf2out_type_decl,          /* type_decl */
5955   dwarf2out_imported_module_or_decl,
5956   debug_nothing_tree,           /* deferred_inline_function */
5957   /* The DWARF 2 backend tries to reduce debugging bloat by not
5958      emitting the abstract description of inline functions until
5959      something tries to reference them.  */
5960   dwarf2out_abstract_function,  /* outlining_inline_function */
5961   debug_nothing_rtx,            /* label */
5962   debug_nothing_int,            /* handle_pch */
5963   dwarf2out_var_location,
5964   dwarf2out_switch_text_section,
5965   dwarf2out_set_name,
5966   1,                            /* start_end_main_source_file */
5967   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5968 };
5969 \f
5970 /* NOTE: In the comments in this file, many references are made to
5971    "Debugging Information Entries".  This term is abbreviated as `DIE'
5972    throughout the remainder of this file.  */
5973
5974 /* An internal representation of the DWARF output is built, and then
5975    walked to generate the DWARF debugging info.  The walk of the internal
5976    representation is done after the entire program has been compiled.
5977    The types below are used to describe the internal representation.  */
5978
5979 /* Whether to put type DIEs into their own section .debug_types instead
5980    of making them part of the .debug_info section.  Only supported for
5981    Dwarf V4 or higher and the user didn't disable them through
5982    -fno-debug-types-section.  It is more efficient to put them in a
5983    separate comdat sections since the linker will then be able to
5984    remove duplicates.  But not all tools support .debug_types sections
5985    yet.  */
5986
5987 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
5988
5989 /* Various DIE's use offsets relative to the beginning of the
5990    .debug_info section to refer to each other.  */
5991
5992 typedef long int dw_offset;
5993
5994 /* Define typedefs here to avoid circular dependencies.  */
5995
5996 typedef struct dw_attr_struct *dw_attr_ref;
5997 typedef struct dw_line_info_struct *dw_line_info_ref;
5998 typedef struct pubname_struct *pubname_ref;
5999 typedef struct dw_ranges_struct *dw_ranges_ref;
6000 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
6001 typedef struct comdat_type_struct *comdat_type_node_ref;
6002
6003 /* The entries in the line_info table more-or-less mirror the opcodes
6004    that are used in the real dwarf line table.  Arrays of these entries
6005    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
6006    supported.  */
6007
6008 enum dw_line_info_opcode {
6009   /* Emit DW_LNE_set_address; the operand is the label index.  */
6010   LI_set_address,
6011
6012   /* Emit a row to the matrix with the given line.  This may be done
6013      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
6014      special opcodes.  */
6015   LI_set_line,
6016
6017   /* Emit a DW_LNS_set_file.  */
6018   LI_set_file,
6019
6020   /* Emit a DW_LNS_set_column.  */
6021   LI_set_column,
6022
6023   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
6024   LI_negate_stmt,
6025
6026   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
6027   LI_set_prologue_end,
6028   LI_set_epilogue_begin,
6029
6030   /* Emit a DW_LNE_set_discriminator.  */
6031   LI_set_discriminator
6032 };
6033
6034 typedef struct GTY(()) dw_line_info_struct {
6035   enum dw_line_info_opcode opcode;
6036   unsigned int val;
6037 } dw_line_info_entry;
6038
6039 DEF_VEC_O(dw_line_info_entry);
6040 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
6041
6042 typedef struct GTY(()) dw_line_info_table_struct {
6043   /* The label that marks the end of this section.  */
6044   const char *end_label;
6045
6046   /* The values for the last row of the matrix, as collected in the table.
6047      These are used to minimize the changes to the next row.  */
6048   unsigned int file_num;
6049   unsigned int line_num;
6050   unsigned int column_num;
6051   int discrim_num;
6052   bool is_stmt;
6053   bool in_use;
6054
6055   VEC(dw_line_info_entry, gc) *entries;
6056 } dw_line_info_table;
6057
6058 typedef dw_line_info_table *dw_line_info_table_p;
6059
6060 DEF_VEC_P(dw_line_info_table_p);
6061 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
6062
6063 /* Each DIE attribute has a field specifying the attribute kind,
6064    a link to the next attribute in the chain, and an attribute value.
6065    Attributes are typically linked below the DIE they modify.  */
6066
6067 typedef struct GTY(()) dw_attr_struct {
6068   enum dwarf_attribute dw_attr;
6069   dw_val_node dw_attr_val;
6070 }
6071 dw_attr_node;
6072
6073 DEF_VEC_O(dw_attr_node);
6074 DEF_VEC_ALLOC_O(dw_attr_node,gc);
6075
6076 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
6077    The children of each node form a circular list linked by
6078    die_sib.  die_child points to the node *before* the "first" child node.  */
6079
6080 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
6081   union die_symbol_or_type_node
6082     {
6083       char * GTY ((tag ("0"))) die_symbol;
6084       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
6085     }
6086   GTY ((desc ("use_debug_types"))) die_id;
6087   VEC(dw_attr_node,gc) * die_attr;
6088   dw_die_ref die_parent;
6089   dw_die_ref die_child;
6090   dw_die_ref die_sib;
6091   dw_die_ref die_definition; /* ref from a specification to its definition */
6092   dw_offset die_offset;
6093   unsigned long die_abbrev;
6094   int die_mark;
6095   /* Die is used and must not be pruned as unused.  */
6096   int die_perennial_p;
6097   unsigned int decl_id;
6098   enum dwarf_tag die_tag;
6099 }
6100 die_node;
6101
6102 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
6103 #define FOR_EACH_CHILD(die, c, expr) do {       \
6104   c = die->die_child;                           \
6105   if (c) do {                                   \
6106     c = c->die_sib;                             \
6107     expr;                                       \
6108   } while (c != die->die_child);                \
6109 } while (0)
6110
6111 /* The pubname structure */
6112
6113 typedef struct GTY(()) pubname_struct {
6114   dw_die_ref die;
6115   const char *name;
6116 }
6117 pubname_entry;
6118
6119 DEF_VEC_O(pubname_entry);
6120 DEF_VEC_ALLOC_O(pubname_entry, gc);
6121
6122 struct GTY(()) dw_ranges_struct {
6123   /* If this is positive, it's a block number, otherwise it's a
6124      bitwise-negated index into dw_ranges_by_label.  */
6125   int num;
6126 };
6127
6128 /* A structure to hold a macinfo entry.  */
6129
6130 typedef struct GTY(()) macinfo_struct {
6131   unsigned HOST_WIDE_INT code;
6132   unsigned HOST_WIDE_INT lineno;
6133   const char *info;
6134 }
6135 macinfo_entry;
6136
6137 DEF_VEC_O(macinfo_entry);
6138 DEF_VEC_ALLOC_O(macinfo_entry, gc);
6139
6140 struct GTY(()) dw_ranges_by_label_struct {
6141   const char *begin;
6142   const char *end;
6143 };
6144
6145 /* The comdat type node structure.  */
6146 typedef struct GTY(()) comdat_type_struct
6147 {
6148   dw_die_ref root_die;
6149   dw_die_ref type_die;
6150   char signature[DWARF_TYPE_SIGNATURE_SIZE];
6151   struct comdat_type_struct *next;
6152 }
6153 comdat_type_node;
6154
6155 /* The limbo die list structure.  */
6156 typedef struct GTY(()) limbo_die_struct {
6157   dw_die_ref die;
6158   tree created_for;
6159   struct limbo_die_struct *next;
6160 }
6161 limbo_die_node;
6162
6163 typedef struct skeleton_chain_struct
6164 {
6165   dw_die_ref old_die;
6166   dw_die_ref new_die;
6167   struct skeleton_chain_struct *parent;
6168 }
6169 skeleton_chain_node;
6170
6171 /* How to start an assembler comment.  */
6172 #ifndef ASM_COMMENT_START
6173 #define ASM_COMMENT_START ";#"
6174 #endif
6175
6176 /* Define a macro which returns nonzero for a TYPE_DECL which was
6177    implicitly generated for a tagged type.
6178
6179    Note that unlike the gcc front end (which generates a NULL named
6180    TYPE_DECL node for each complete tagged type, each array type, and
6181    each function type node created) the g++ front end generates a
6182    _named_ TYPE_DECL node for each tagged type node created.
6183    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
6184    generate a DW_TAG_typedef DIE for them.  */
6185
6186 #define TYPE_DECL_IS_STUB(decl)                         \
6187   (DECL_NAME (decl) == NULL_TREE                        \
6188    || (DECL_ARTIFICIAL (decl)                           \
6189        && is_tagged_type (TREE_TYPE (decl))             \
6190        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
6191            /* This is necessary for stub decls that     \
6192               appear in nested inline functions.  */    \
6193            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6194                && (decl_ultimate_origin (decl)          \
6195                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6196
6197 /* Information concerning the compilation unit's programming
6198    language, and compiler version.  */
6199
6200 /* Fixed size portion of the DWARF compilation unit header.  */
6201 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6202   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6203
6204 /* Fixed size portion of the DWARF comdat type unit header.  */
6205 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6206   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6207    + DWARF_OFFSET_SIZE)
6208
6209 /* Fixed size portion of public names info.  */
6210 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6211
6212 /* Fixed size portion of the address range info.  */
6213 #define DWARF_ARANGES_HEADER_SIZE                                       \
6214   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6215                 DWARF2_ADDR_SIZE * 2)                                   \
6216    - DWARF_INITIAL_LENGTH_SIZE)
6217
6218 /* Size of padding portion in the address range info.  It must be
6219    aligned to twice the pointer size.  */
6220 #define DWARF_ARANGES_PAD_SIZE \
6221   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6222                 DWARF2_ADDR_SIZE * 2)                              \
6223    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6224
6225 /* Use assembler line directives if available.  */
6226 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6227 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6228 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6229 #else
6230 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6231 #endif
6232 #endif
6233
6234 /* Minimum line offset in a special line info. opcode.
6235    This value was chosen to give a reasonable range of values.  */
6236 #define DWARF_LINE_BASE  -10
6237
6238 /* First special line opcode - leave room for the standard opcodes.  */
6239 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
6240
6241 /* Range of line offsets in a special line info. opcode.  */
6242 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6243
6244 /* Flag that indicates the initial value of the is_stmt_start flag.
6245    In the present implementation, we do not mark any lines as
6246    the beginning of a source statement, because that information
6247    is not made available by the GCC front-end.  */
6248 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6249
6250 /* Maximum number of operations per instruction bundle.  */
6251 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6252 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6253 #endif
6254
6255 /* This location is used by calc_die_sizes() to keep track
6256    the offset of each DIE within the .debug_info section.  */
6257 static unsigned long next_die_offset;
6258
6259 /* Record the root of the DIE's built for the current compilation unit.  */
6260 static GTY(()) dw_die_ref single_comp_unit_die;
6261
6262 /* A list of type DIEs that have been separated into comdat sections.  */
6263 static GTY(()) comdat_type_node *comdat_type_list;
6264
6265 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6266 static GTY(()) limbo_die_node *limbo_die_list;
6267
6268 /* A list of DIEs for which we may have to generate
6269    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6270 static GTY(()) limbo_die_node *deferred_asm_name;
6271
6272 /* Filenames referenced by this compilation unit.  */
6273 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6274
6275 /* A hash table of references to DIE's that describe declarations.
6276    The key is a DECL_UID() which is a unique number identifying each decl.  */
6277 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6278
6279 /* A hash table of references to DIE's that describe COMMON blocks.
6280    The key is DECL_UID() ^ die_parent.  */
6281 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6282
6283 typedef struct GTY(()) die_arg_entry_struct {
6284     dw_die_ref die;
6285     tree arg;
6286 } die_arg_entry;
6287
6288 DEF_VEC_O(die_arg_entry);
6289 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6290
6291 /* Node of the variable location list.  */
6292 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6293   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6294      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6295      in mode of the EXPR_LIST node and first EXPR_LIST operand
6296      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6297      location or NULL for padding.  For larger bitsizes,
6298      mode is 0 and first operand is a CONCAT with bitsize
6299      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6300      NULL as second operand.  */
6301   rtx GTY (()) loc;
6302   const char * GTY (()) label;
6303   struct var_loc_node * GTY (()) next;
6304 };
6305
6306 /* Variable location list.  */
6307 struct GTY (()) var_loc_list_def {
6308   struct var_loc_node * GTY (()) first;
6309
6310   /* Pointer to the last but one or last element of the
6311      chained list.  If the list is empty, both first and
6312      last are NULL, if the list contains just one node
6313      or the last node certainly is not redundant, it points
6314      to the last node, otherwise points to the last but one.
6315      Do not mark it for GC because it is marked through the chain.  */
6316   struct var_loc_node * GTY ((skip ("%h"))) last;
6317
6318   /* Pointer to the last element before section switch,
6319      if NULL, either sections weren't switched or first
6320      is after section switch.  */
6321   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6322
6323   /* DECL_UID of the variable decl.  */
6324   unsigned int decl_id;
6325 };
6326 typedef struct var_loc_list_def var_loc_list;
6327
6328 /* Call argument location list.  */
6329 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6330   rtx GTY (()) call_arg_loc_note;
6331   const char * GTY (()) label;
6332   tree GTY (()) block;
6333   bool tail_call_p;
6334   rtx GTY (()) symbol_ref;
6335   struct call_arg_loc_node * GTY (()) next;
6336 };
6337
6338
6339 /* Table of decl location linked lists.  */
6340 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6341
6342 /* Head and tail of call_arg_loc chain.  */
6343 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6344 static struct call_arg_loc_node *call_arg_loc_last;
6345
6346 /* Number of call sites in the current function.  */
6347 static int call_site_count = -1;
6348 /* Number of tail call sites in the current function.  */
6349 static int tail_call_site_count = -1;
6350
6351 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6352    DIEs.  */
6353 static VEC (dw_die_ref, heap) *block_map;
6354
6355 /* A cached location list.  */
6356 struct GTY (()) cached_dw_loc_list_def {
6357   /* The DECL_UID of the decl that this entry describes.  */
6358   unsigned int decl_id;
6359
6360   /* The cached location list.  */
6361   dw_loc_list_ref loc_list;
6362 };
6363 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6364
6365 /* Table of cached location lists.  */
6366 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6367
6368 /* A pointer to the base of a list of references to DIE's that
6369    are uniquely identified by their tag, presence/absence of
6370    children DIE's, and list of attribute/value pairs.  */
6371 static GTY((length ("abbrev_die_table_allocated")))
6372   dw_die_ref *abbrev_die_table;
6373
6374 /* Number of elements currently allocated for abbrev_die_table.  */
6375 static GTY(()) unsigned abbrev_die_table_allocated;
6376
6377 /* Number of elements in type_die_table currently in use.  */
6378 static GTY(()) unsigned abbrev_die_table_in_use;
6379
6380 /* Size (in elements) of increments by which we may expand the
6381    abbrev_die_table.  */
6382 #define ABBREV_DIE_TABLE_INCREMENT 256
6383
6384 /* A global counter for generating labels for line number data.  */
6385 static unsigned int line_info_label_num;
6386
6387 /* The current table to which we should emit line number information
6388    for the current function.  This will be set up at the beginning of
6389    assembly for the function.  */
6390 static dw_line_info_table *cur_line_info_table;
6391
6392 /* The two default tables of line number info.  */
6393 static GTY(()) dw_line_info_table *text_section_line_info;
6394 static GTY(()) dw_line_info_table *cold_text_section_line_info;
6395
6396 /* The set of all non-default tables of line number info.  */
6397 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
6398
6399 /* A flag to tell pubnames/types export if there is an info section to
6400    refer to.  */
6401 static bool info_section_emitted;
6402
6403 /* A pointer to the base of a table that contains a list of publicly
6404    accessible names.  */
6405 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6406
6407 /* A pointer to the base of a table that contains a list of publicly
6408    accessible types.  */
6409 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6410
6411 /* A pointer to the base of a table that contains a list of macro
6412    defines/undefines (and file start/end markers).  */
6413 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6414
6415 /* Array of dies for which we should generate .debug_ranges info.  */
6416 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6417
6418 /* Number of elements currently allocated for ranges_table.  */
6419 static GTY(()) unsigned ranges_table_allocated;
6420
6421 /* Number of elements in ranges_table currently in use.  */
6422 static GTY(()) unsigned ranges_table_in_use;
6423
6424 /* Array of pairs of labels referenced in ranges_table.  */
6425 static GTY ((length ("ranges_by_label_allocated")))
6426      dw_ranges_by_label_ref ranges_by_label;
6427
6428 /* Number of elements currently allocated for ranges_by_label.  */
6429 static GTY(()) unsigned ranges_by_label_allocated;
6430
6431 /* Number of elements in ranges_by_label currently in use.  */
6432 static GTY(()) unsigned ranges_by_label_in_use;
6433
6434 /* Size (in elements) of increments by which we may expand the
6435    ranges_table.  */
6436 #define RANGES_TABLE_INCREMENT 64
6437
6438 /* Whether we have location lists that need outputting */
6439 static GTY(()) bool have_location_lists;
6440
6441 /* Unique label counter.  */
6442 static GTY(()) unsigned int loclabel_num;
6443
6444 /* Unique label counter for point-of-call tables.  */
6445 static GTY(()) unsigned int poc_label_num;
6446
6447 /* Record whether the function being analyzed contains inlined functions.  */
6448 static int current_function_has_inlines;
6449
6450 /* The last file entry emitted by maybe_emit_file().  */
6451 static GTY(()) struct dwarf_file_data * last_emitted_file;
6452
6453 /* Number of internal labels generated by gen_internal_sym().  */
6454 static GTY(()) int label_num;
6455
6456 /* Cached result of previous call to lookup_filename.  */
6457 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6458
6459 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6460
6461 /* Instances of generic types for which we need to generate debug
6462    info that describe their generic parameters and arguments. That
6463    generation needs to happen once all types are properly laid out so
6464    we do it at the end of compilation.  */
6465 static GTY(()) VEC(tree,gc) *generic_type_instances;
6466
6467 /* Offset from the "steady-state frame pointer" to the frame base,
6468    within the current function.  */
6469 static HOST_WIDE_INT frame_pointer_fb_offset;
6470
6471 static VEC (dw_die_ref, heap) *base_types;
6472
6473 /* Forward declarations for functions defined in this file.  */
6474
6475 static int is_pseudo_reg (const_rtx);
6476 static tree type_main_variant (tree);
6477 static int is_tagged_type (const_tree);
6478 static const char *dwarf_tag_name (unsigned);
6479 static const char *dwarf_attr_name (unsigned);
6480 static const char *dwarf_form_name (unsigned);
6481 static tree decl_ultimate_origin (const_tree);
6482 static tree decl_class_context (tree);
6483 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6484 static inline enum dw_val_class AT_class (dw_attr_ref);
6485 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6486 static inline unsigned AT_flag (dw_attr_ref);
6487 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6488 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6489 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6490 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6491 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6492                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6493 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6494                                unsigned int, unsigned char *);
6495 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6496 static hashval_t debug_str_do_hash (const void *);
6497 static int debug_str_eq (const void *, const void *);
6498 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6499 static inline const char *AT_string (dw_attr_ref);
6500 static enum dwarf_form AT_string_form (dw_attr_ref);
6501 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6502 static void add_AT_specification (dw_die_ref, dw_die_ref);
6503 static inline dw_die_ref AT_ref (dw_attr_ref);
6504 static inline int AT_ref_external (dw_attr_ref);
6505 static inline void set_AT_ref_external (dw_attr_ref, int);
6506 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6507 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6508 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6509 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6510                              dw_loc_list_ref);
6511 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6512 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6513 static inline rtx AT_addr (dw_attr_ref);
6514 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6515 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6516 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6517 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6518                            unsigned HOST_WIDE_INT);
6519 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6520                                unsigned long);
6521 static inline const char *AT_lbl (dw_attr_ref);
6522 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6523 static const char *get_AT_low_pc (dw_die_ref);
6524 static const char *get_AT_hi_pc (dw_die_ref);
6525 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6526 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6527 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6528 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6529 static bool is_cxx (void);
6530 static bool is_fortran (void);
6531 static bool is_ada (void);
6532 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6533 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6534 static void add_child_die (dw_die_ref, dw_die_ref);
6535 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6536 static dw_die_ref lookup_type_die (tree);
6537 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6538 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6539 static void equate_type_number_to_die (tree, dw_die_ref);
6540 static hashval_t decl_die_table_hash (const void *);
6541 static int decl_die_table_eq (const void *, const void *);
6542 static dw_die_ref lookup_decl_die (tree);
6543 static hashval_t common_block_die_table_hash (const void *);
6544 static int common_block_die_table_eq (const void *, const void *);
6545 static hashval_t decl_loc_table_hash (const void *);
6546 static int decl_loc_table_eq (const void *, const void *);
6547 static var_loc_list *lookup_decl_loc (const_tree);
6548 static void equate_decl_number_to_die (tree, dw_die_ref);
6549 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6550 static void print_spaces (FILE *);
6551 static void print_die (dw_die_ref, FILE *);
6552 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6553 static dw_die_ref pop_compile_unit (dw_die_ref);
6554 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6555 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6556 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6557 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6558 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6559 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6560 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6561                                    struct md5_ctx *, int *);
6562 struct checksum_attributes;
6563 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6564 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6565 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6566 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6567 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6568 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6569 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6570 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6571 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6572 static void compute_section_prefix (dw_die_ref);
6573 static int is_type_die (dw_die_ref);
6574 static int is_comdat_die (dw_die_ref);
6575 static int is_symbol_die (dw_die_ref);
6576 static void assign_symbol_names (dw_die_ref);
6577 static void break_out_includes (dw_die_ref);
6578 static int is_declaration_die (dw_die_ref);
6579 static int should_move_die_to_comdat (dw_die_ref);
6580 static dw_die_ref clone_as_declaration (dw_die_ref);
6581 static dw_die_ref clone_die (dw_die_ref);
6582 static dw_die_ref clone_tree (dw_die_ref);
6583 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6584 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6585 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6586 static dw_die_ref generate_skeleton (dw_die_ref);
6587 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6588                                                          dw_die_ref);
6589 static void break_out_comdat_types (dw_die_ref);
6590 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6591 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6592 static void copy_decls_for_unworthy_types (dw_die_ref);
6593
6594 static hashval_t htab_cu_hash (const void *);
6595 static int htab_cu_eq (const void *, const void *);
6596 static void htab_cu_del (void *);
6597 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6598 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6599 static void add_sibling_attributes (dw_die_ref);
6600 static void build_abbrev_table (dw_die_ref);
6601 static void output_location_lists (dw_die_ref);
6602 static int constant_size (unsigned HOST_WIDE_INT);
6603 static unsigned long size_of_die (dw_die_ref);
6604 static void calc_die_sizes (dw_die_ref);
6605 static void calc_base_type_die_sizes (void);
6606 static void mark_dies (dw_die_ref);
6607 static void unmark_dies (dw_die_ref);
6608 static void unmark_all_dies (dw_die_ref);
6609 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6610 static unsigned long size_of_aranges (void);
6611 static enum dwarf_form value_format (dw_attr_ref);
6612 static void output_value_format (dw_attr_ref);
6613 static void output_abbrev_section (void);
6614 static void output_die_symbol (dw_die_ref);
6615 static void output_die (dw_die_ref);
6616 static void output_compilation_unit_header (void);
6617 static void output_comp_unit (dw_die_ref, int);
6618 static void output_comdat_type_unit (comdat_type_node *);
6619 static const char *dwarf2_name (tree, int);
6620 static void add_pubname (tree, dw_die_ref);
6621 static void add_pubname_string (const char *, dw_die_ref);
6622 static void add_pubtype (tree, dw_die_ref);
6623 static void output_pubnames (VEC (pubname_entry,gc) *);
6624 static void output_aranges (unsigned long);
6625 static unsigned int add_ranges_num (int);
6626 static unsigned int add_ranges (const_tree);
6627 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6628                                   bool *);
6629 static void output_ranges (void);
6630 static dw_line_info_table *new_line_info_table (void);
6631 static void output_line_info (void);
6632 static void output_file_names (void);
6633 static dw_die_ref base_type_die (tree);
6634 static int is_base_type (tree);
6635 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6636 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6637 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6638 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6639 static int type_is_enum (const_tree);
6640 static unsigned int dbx_reg_number (const_rtx);
6641 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6642 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6643 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6644                                                 enum var_init_status);
6645 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6646                                                      enum var_init_status);
6647 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6648                                          enum var_init_status);
6649 static int is_based_loc (const_rtx);
6650 static int resolve_one_addr (rtx *, void *);
6651 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6652                                                enum var_init_status);
6653 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6654                                         enum var_init_status);
6655 static dw_loc_list_ref loc_list_from_tree (tree, int);
6656 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6657 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6658 static tree field_type (const_tree);
6659 static unsigned int simple_type_align_in_bits (const_tree);
6660 static unsigned int simple_decl_align_in_bits (const_tree);
6661 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6662 static HOST_WIDE_INT field_byte_offset (const_tree);
6663 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6664                                          dw_loc_list_ref);
6665 static void add_data_member_location_attribute (dw_die_ref, tree);
6666 static bool add_const_value_attribute (dw_die_ref, rtx);
6667 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6668 static void insert_double (double_int, unsigned char *);
6669 static void insert_float (const_rtx, unsigned char *);
6670 static rtx rtl_for_decl_location (tree);
6671 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6672                                                    enum dwarf_attribute);
6673 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6674 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6675 static void add_name_attribute (dw_die_ref, const char *);
6676 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
6677 static void add_comp_dir_attribute (dw_die_ref);
6678 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6679 static void add_subscript_info (dw_die_ref, tree, bool);
6680 static void add_byte_size_attribute (dw_die_ref, tree);
6681 static void add_bit_offset_attribute (dw_die_ref, tree);
6682 static void add_bit_size_attribute (dw_die_ref, tree);
6683 static void add_prototyped_attribute (dw_die_ref, tree);
6684 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6685 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6686 static void add_src_coords_attributes (dw_die_ref, tree);
6687 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6688 static void push_decl_scope (tree);
6689 static void pop_decl_scope (void);
6690 static dw_die_ref scope_die_for (tree, dw_die_ref);
6691 static inline int local_scope_p (dw_die_ref);
6692 static inline int class_scope_p (dw_die_ref);
6693 static inline int class_or_namespace_scope_p (dw_die_ref);
6694 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6695 static void add_calling_convention_attribute (dw_die_ref, tree);
6696 static const char *type_tag (const_tree);
6697 static tree member_declared_type (const_tree);
6698 #if 0
6699 static const char *decl_start_label (tree);
6700 #endif
6701 static void gen_array_type_die (tree, dw_die_ref);
6702 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6703 #if 0
6704 static void gen_entry_point_die (tree, dw_die_ref);
6705 #endif
6706 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6707 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6708 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6709 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6710 static void gen_formal_types_die (tree, dw_die_ref);
6711 static void gen_subprogram_die (tree, dw_die_ref);
6712 static void gen_variable_die (tree, tree, dw_die_ref);
6713 static void gen_const_die (tree, dw_die_ref);
6714 static void gen_label_die (tree, dw_die_ref);
6715 static void gen_lexical_block_die (tree, dw_die_ref, int);
6716 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6717 static void gen_field_die (tree, dw_die_ref);
6718 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6719 static dw_die_ref gen_compile_unit_die (const char *);
6720 static void gen_inheritance_die (tree, tree, dw_die_ref);
6721 static void gen_member_die (tree, dw_die_ref);
6722 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6723                                                 enum debug_info_usage);
6724 static void gen_subroutine_type_die (tree, dw_die_ref);
6725 static void gen_typedef_die (tree, dw_die_ref);
6726 static void gen_type_die (tree, dw_die_ref);
6727 static void gen_block_die (tree, dw_die_ref, int);
6728 static void decls_for_scope (tree, dw_die_ref, int);
6729 static inline int is_redundant_typedef (const_tree);
6730 static bool is_naming_typedef_decl (const_tree);
6731 static inline dw_die_ref get_context_die (tree);
6732 static void gen_namespace_die (tree, dw_die_ref);
6733 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6734 static dw_die_ref force_decl_die (tree);
6735 static dw_die_ref force_type_die (tree);
6736 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6737 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6738 static struct dwarf_file_data * lookup_filename (const char *);
6739 static void retry_incomplete_types (void);
6740 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6741 static void gen_generic_params_dies (tree);
6742 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6743 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6744 static void splice_child_die (dw_die_ref, dw_die_ref);
6745 static int file_info_cmp (const void *, const void *);
6746 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6747                                      const char *, const char *);
6748 static void output_loc_list (dw_loc_list_ref);
6749 static char *gen_internal_sym (const char *);
6750
6751 static void prune_unmark_dies (dw_die_ref);
6752 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6753 static void prune_unused_types_mark (dw_die_ref, int);
6754 static void prune_unused_types_walk (dw_die_ref);
6755 static void prune_unused_types_walk_attribs (dw_die_ref);
6756 static void prune_unused_types_prune (dw_die_ref);
6757 static void prune_unused_types (void);
6758 static int maybe_emit_file (struct dwarf_file_data *fd);
6759 static inline const char *AT_vms_delta1 (dw_attr_ref);
6760 static inline const char *AT_vms_delta2 (dw_attr_ref);
6761 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6762                                      const char *, const char *);
6763 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6764 static void gen_remaining_tmpl_value_param_die_attribute (void);
6765 static bool generic_type_p (tree);
6766 static void schedule_generic_params_dies_gen (tree t);
6767 static void gen_scheduled_generic_parms_dies (void);
6768
6769 /* Section names used to hold DWARF debugging information.  */
6770 #ifndef DEBUG_INFO_SECTION
6771 #define DEBUG_INFO_SECTION      ".debug_info"
6772 #endif
6773 #ifndef DEBUG_ABBREV_SECTION
6774 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6775 #endif
6776 #ifndef DEBUG_ARANGES_SECTION
6777 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6778 #endif
6779 #ifndef DEBUG_MACINFO_SECTION
6780 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6781 #endif
6782 #ifndef DEBUG_LINE_SECTION
6783 #define DEBUG_LINE_SECTION      ".debug_line"
6784 #endif
6785 #ifndef DEBUG_LOC_SECTION
6786 #define DEBUG_LOC_SECTION       ".debug_loc"
6787 #endif
6788 #ifndef DEBUG_PUBNAMES_SECTION
6789 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6790 #endif
6791 #ifndef DEBUG_PUBTYPES_SECTION
6792 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6793 #endif
6794 #ifndef DEBUG_STR_SECTION
6795 #define DEBUG_STR_SECTION       ".debug_str"
6796 #endif
6797 #ifndef DEBUG_RANGES_SECTION
6798 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6799 #endif
6800
6801 /* Standard ELF section names for compiled code and data.  */
6802 #ifndef TEXT_SECTION_NAME
6803 #define TEXT_SECTION_NAME       ".text"
6804 #endif
6805
6806 /* Section flags for .debug_str section.  */
6807 #define DEBUG_STR_SECTION_FLAGS \
6808   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6809    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6810    : SECTION_DEBUG)
6811
6812 /* Labels we insert at beginning sections we can reference instead of
6813    the section names themselves.  */
6814
6815 #ifndef TEXT_SECTION_LABEL
6816 #define TEXT_SECTION_LABEL              "Ltext"
6817 #endif
6818 #ifndef COLD_TEXT_SECTION_LABEL
6819 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6820 #endif
6821 #ifndef DEBUG_LINE_SECTION_LABEL
6822 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6823 #endif
6824 #ifndef DEBUG_INFO_SECTION_LABEL
6825 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6826 #endif
6827 #ifndef DEBUG_ABBREV_SECTION_LABEL
6828 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6829 #endif
6830 #ifndef DEBUG_LOC_SECTION_LABEL
6831 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6832 #endif
6833 #ifndef DEBUG_RANGES_SECTION_LABEL
6834 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6835 #endif
6836 #ifndef DEBUG_MACINFO_SECTION_LABEL
6837 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6838 #endif
6839
6840
6841 /* Definitions of defaults for formats and names of various special
6842    (artificial) labels which may be generated within this file (when the -g
6843    options is used and DWARF2_DEBUGGING_INFO is in effect.
6844    If necessary, these may be overridden from within the tm.h file, but
6845    typically, overriding these defaults is unnecessary.  */
6846
6847 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6848 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6849 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6850 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6851 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6852 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6853 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6854 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6855 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6856 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6857
6858 #ifndef TEXT_END_LABEL
6859 #define TEXT_END_LABEL          "Letext"
6860 #endif
6861 #ifndef COLD_END_LABEL
6862 #define COLD_END_LABEL          "Letext_cold"
6863 #endif
6864 #ifndef BLOCK_BEGIN_LABEL
6865 #define BLOCK_BEGIN_LABEL       "LBB"
6866 #endif
6867 #ifndef BLOCK_END_LABEL
6868 #define BLOCK_END_LABEL         "LBE"
6869 #endif
6870 #ifndef LINE_CODE_LABEL
6871 #define LINE_CODE_LABEL         "LM"
6872 #endif
6873
6874 \f
6875 /* Return the root of the DIE's built for the current compilation unit.  */
6876 static dw_die_ref
6877 comp_unit_die (void)
6878 {
6879   if (!single_comp_unit_die)
6880     single_comp_unit_die = gen_compile_unit_die (NULL);
6881   return single_comp_unit_die;
6882 }
6883
6884 /* We allow a language front-end to designate a function that is to be
6885    called to "demangle" any name before it is put into a DIE.  */
6886
6887 static const char *(*demangle_name_func) (const char *);
6888
6889 void
6890 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6891 {
6892   demangle_name_func = func;
6893 }
6894
6895 /* Test if rtl node points to a pseudo register.  */
6896
6897 static inline int
6898 is_pseudo_reg (const_rtx rtl)
6899 {
6900   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6901           || (GET_CODE (rtl) == SUBREG
6902               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6903 }
6904
6905 /* Return a reference to a type, with its const and volatile qualifiers
6906    removed.  */
6907
6908 static inline tree
6909 type_main_variant (tree type)
6910 {
6911   type = TYPE_MAIN_VARIANT (type);
6912
6913   /* ??? There really should be only one main variant among any group of
6914      variants of a given type (and all of the MAIN_VARIANT values for all
6915      members of the group should point to that one type) but sometimes the C
6916      front-end messes this up for array types, so we work around that bug
6917      here.  */
6918   if (TREE_CODE (type) == ARRAY_TYPE)
6919     while (type != TYPE_MAIN_VARIANT (type))
6920       type = TYPE_MAIN_VARIANT (type);
6921
6922   return type;
6923 }
6924
6925 /* Return nonzero if the given type node represents a tagged type.  */
6926
6927 static inline int
6928 is_tagged_type (const_tree type)
6929 {
6930   enum tree_code code = TREE_CODE (type);
6931
6932   return (code == RECORD_TYPE || code == UNION_TYPE
6933           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6934 }
6935
6936 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6937
6938 static void
6939 get_ref_die_offset_label (char *label, dw_die_ref ref)
6940 {
6941   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6942 }
6943
6944 /* Return die_offset of a DIE reference to a base type.  */
6945
6946 static unsigned long int
6947 get_base_type_offset (dw_die_ref ref)
6948 {
6949   if (ref->die_offset)
6950     return ref->die_offset;
6951   if (comp_unit_die ()->die_abbrev)
6952     {
6953       calc_base_type_die_sizes ();
6954       gcc_assert (ref->die_offset);
6955     }
6956   return ref->die_offset;
6957 }
6958
6959 /* Convert a DIE tag into its string name.  */
6960
6961 static const char *
6962 dwarf_tag_name (unsigned int tag)
6963 {
6964   switch (tag)
6965     {
6966     case DW_TAG_padding:
6967       return "DW_TAG_padding";
6968     case DW_TAG_array_type:
6969       return "DW_TAG_array_type";
6970     case DW_TAG_class_type:
6971       return "DW_TAG_class_type";
6972     case DW_TAG_entry_point:
6973       return "DW_TAG_entry_point";
6974     case DW_TAG_enumeration_type:
6975       return "DW_TAG_enumeration_type";
6976     case DW_TAG_formal_parameter:
6977       return "DW_TAG_formal_parameter";
6978     case DW_TAG_imported_declaration:
6979       return "DW_TAG_imported_declaration";
6980     case DW_TAG_label:
6981       return "DW_TAG_label";
6982     case DW_TAG_lexical_block:
6983       return "DW_TAG_lexical_block";
6984     case DW_TAG_member:
6985       return "DW_TAG_member";
6986     case DW_TAG_pointer_type:
6987       return "DW_TAG_pointer_type";
6988     case DW_TAG_reference_type:
6989       return "DW_TAG_reference_type";
6990     case DW_TAG_compile_unit:
6991       return "DW_TAG_compile_unit";
6992     case DW_TAG_string_type:
6993       return "DW_TAG_string_type";
6994     case DW_TAG_structure_type:
6995       return "DW_TAG_structure_type";
6996     case DW_TAG_subroutine_type:
6997       return "DW_TAG_subroutine_type";
6998     case DW_TAG_typedef:
6999       return "DW_TAG_typedef";
7000     case DW_TAG_union_type:
7001       return "DW_TAG_union_type";
7002     case DW_TAG_unspecified_parameters:
7003       return "DW_TAG_unspecified_parameters";
7004     case DW_TAG_variant:
7005       return "DW_TAG_variant";
7006     case DW_TAG_common_block:
7007       return "DW_TAG_common_block";
7008     case DW_TAG_common_inclusion:
7009       return "DW_TAG_common_inclusion";
7010     case DW_TAG_inheritance:
7011       return "DW_TAG_inheritance";
7012     case DW_TAG_inlined_subroutine:
7013       return "DW_TAG_inlined_subroutine";
7014     case DW_TAG_module:
7015       return "DW_TAG_module";
7016     case DW_TAG_ptr_to_member_type:
7017       return "DW_TAG_ptr_to_member_type";
7018     case DW_TAG_set_type:
7019       return "DW_TAG_set_type";
7020     case DW_TAG_subrange_type:
7021       return "DW_TAG_subrange_type";
7022     case DW_TAG_with_stmt:
7023       return "DW_TAG_with_stmt";
7024     case DW_TAG_access_declaration:
7025       return "DW_TAG_access_declaration";
7026     case DW_TAG_base_type:
7027       return "DW_TAG_base_type";
7028     case DW_TAG_catch_block:
7029       return "DW_TAG_catch_block";
7030     case DW_TAG_const_type:
7031       return "DW_TAG_const_type";
7032     case DW_TAG_constant:
7033       return "DW_TAG_constant";
7034     case DW_TAG_enumerator:
7035       return "DW_TAG_enumerator";
7036     case DW_TAG_file_type:
7037       return "DW_TAG_file_type";
7038     case DW_TAG_friend:
7039       return "DW_TAG_friend";
7040     case DW_TAG_namelist:
7041       return "DW_TAG_namelist";
7042     case DW_TAG_namelist_item:
7043       return "DW_TAG_namelist_item";
7044     case DW_TAG_packed_type:
7045       return "DW_TAG_packed_type";
7046     case DW_TAG_subprogram:
7047       return "DW_TAG_subprogram";
7048     case DW_TAG_template_type_param:
7049       return "DW_TAG_template_type_param";
7050     case DW_TAG_template_value_param:
7051       return "DW_TAG_template_value_param";
7052     case DW_TAG_thrown_type:
7053       return "DW_TAG_thrown_type";
7054     case DW_TAG_try_block:
7055       return "DW_TAG_try_block";
7056     case DW_TAG_variant_part:
7057       return "DW_TAG_variant_part";
7058     case DW_TAG_variable:
7059       return "DW_TAG_variable";
7060     case DW_TAG_volatile_type:
7061       return "DW_TAG_volatile_type";
7062     case DW_TAG_dwarf_procedure:
7063       return "DW_TAG_dwarf_procedure";
7064     case DW_TAG_restrict_type:
7065       return "DW_TAG_restrict_type";
7066     case DW_TAG_interface_type:
7067       return "DW_TAG_interface_type";
7068     case DW_TAG_namespace:
7069       return "DW_TAG_namespace";
7070     case DW_TAG_imported_module:
7071       return "DW_TAG_imported_module";
7072     case DW_TAG_unspecified_type:
7073       return "DW_TAG_unspecified_type";
7074     case DW_TAG_partial_unit:
7075       return "DW_TAG_partial_unit";
7076     case DW_TAG_imported_unit:
7077       return "DW_TAG_imported_unit";
7078     case DW_TAG_condition:
7079       return "DW_TAG_condition";
7080     case DW_TAG_shared_type:
7081       return "DW_TAG_shared_type";
7082     case DW_TAG_type_unit:
7083       return "DW_TAG_type_unit";
7084     case DW_TAG_rvalue_reference_type:
7085       return "DW_TAG_rvalue_reference_type";
7086     case DW_TAG_template_alias:
7087       return "DW_TAG_template_alias";
7088     case DW_TAG_GNU_template_parameter_pack:
7089       return "DW_TAG_GNU_template_parameter_pack";
7090     case DW_TAG_GNU_formal_parameter_pack:
7091       return "DW_TAG_GNU_formal_parameter_pack";
7092     case DW_TAG_MIPS_loop:
7093       return "DW_TAG_MIPS_loop";
7094     case DW_TAG_format_label:
7095       return "DW_TAG_format_label";
7096     case DW_TAG_function_template:
7097       return "DW_TAG_function_template";
7098     case DW_TAG_class_template:
7099       return "DW_TAG_class_template";
7100     case DW_TAG_GNU_BINCL:
7101       return "DW_TAG_GNU_BINCL";
7102     case DW_TAG_GNU_EINCL:
7103       return "DW_TAG_GNU_EINCL";
7104     case DW_TAG_GNU_template_template_param:
7105       return "DW_TAG_GNU_template_template_param";
7106     case DW_TAG_GNU_call_site:
7107       return "DW_TAG_GNU_call_site";
7108     case DW_TAG_GNU_call_site_parameter:
7109       return "DW_TAG_GNU_call_site_parameter";
7110     default:
7111       return "DW_TAG_<unknown>";
7112     }
7113 }
7114
7115 /* Convert a DWARF attribute code into its string name.  */
7116
7117 static const char *
7118 dwarf_attr_name (unsigned int attr)
7119 {
7120   switch (attr)
7121     {
7122     case DW_AT_sibling:
7123       return "DW_AT_sibling";
7124     case DW_AT_location:
7125       return "DW_AT_location";
7126     case DW_AT_name:
7127       return "DW_AT_name";
7128     case DW_AT_ordering:
7129       return "DW_AT_ordering";
7130     case DW_AT_subscr_data:
7131       return "DW_AT_subscr_data";
7132     case DW_AT_byte_size:
7133       return "DW_AT_byte_size";
7134     case DW_AT_bit_offset:
7135       return "DW_AT_bit_offset";
7136     case DW_AT_bit_size:
7137       return "DW_AT_bit_size";
7138     case DW_AT_element_list:
7139       return "DW_AT_element_list";
7140     case DW_AT_stmt_list:
7141       return "DW_AT_stmt_list";
7142     case DW_AT_low_pc:
7143       return "DW_AT_low_pc";
7144     case DW_AT_high_pc:
7145       return "DW_AT_high_pc";
7146     case DW_AT_language:
7147       return "DW_AT_language";
7148     case DW_AT_member:
7149       return "DW_AT_member";
7150     case DW_AT_discr:
7151       return "DW_AT_discr";
7152     case DW_AT_discr_value:
7153       return "DW_AT_discr_value";
7154     case DW_AT_visibility:
7155       return "DW_AT_visibility";
7156     case DW_AT_import:
7157       return "DW_AT_import";
7158     case DW_AT_string_length:
7159       return "DW_AT_string_length";
7160     case DW_AT_common_reference:
7161       return "DW_AT_common_reference";
7162     case DW_AT_comp_dir:
7163       return "DW_AT_comp_dir";
7164     case DW_AT_const_value:
7165       return "DW_AT_const_value";
7166     case DW_AT_containing_type:
7167       return "DW_AT_containing_type";
7168     case DW_AT_default_value:
7169       return "DW_AT_default_value";
7170     case DW_AT_inline:
7171       return "DW_AT_inline";
7172     case DW_AT_is_optional:
7173       return "DW_AT_is_optional";
7174     case DW_AT_lower_bound:
7175       return "DW_AT_lower_bound";
7176     case DW_AT_producer:
7177       return "DW_AT_producer";
7178     case DW_AT_prototyped:
7179       return "DW_AT_prototyped";
7180     case DW_AT_return_addr:
7181       return "DW_AT_return_addr";
7182     case DW_AT_start_scope:
7183       return "DW_AT_start_scope";
7184     case DW_AT_bit_stride:
7185       return "DW_AT_bit_stride";
7186     case DW_AT_upper_bound:
7187       return "DW_AT_upper_bound";
7188     case DW_AT_abstract_origin:
7189       return "DW_AT_abstract_origin";
7190     case DW_AT_accessibility:
7191       return "DW_AT_accessibility";
7192     case DW_AT_address_class:
7193       return "DW_AT_address_class";
7194     case DW_AT_artificial:
7195       return "DW_AT_artificial";
7196     case DW_AT_base_types:
7197       return "DW_AT_base_types";
7198     case DW_AT_calling_convention:
7199       return "DW_AT_calling_convention";
7200     case DW_AT_count:
7201       return "DW_AT_count";
7202     case DW_AT_data_member_location:
7203       return "DW_AT_data_member_location";
7204     case DW_AT_decl_column:
7205       return "DW_AT_decl_column";
7206     case DW_AT_decl_file:
7207       return "DW_AT_decl_file";
7208     case DW_AT_decl_line:
7209       return "DW_AT_decl_line";
7210     case DW_AT_declaration:
7211       return "DW_AT_declaration";
7212     case DW_AT_discr_list:
7213       return "DW_AT_discr_list";
7214     case DW_AT_encoding:
7215       return "DW_AT_encoding";
7216     case DW_AT_external:
7217       return "DW_AT_external";
7218     case DW_AT_explicit:
7219       return "DW_AT_explicit";
7220     case DW_AT_frame_base:
7221       return "DW_AT_frame_base";
7222     case DW_AT_friend:
7223       return "DW_AT_friend";
7224     case DW_AT_identifier_case:
7225       return "DW_AT_identifier_case";
7226     case DW_AT_macro_info:
7227       return "DW_AT_macro_info";
7228     case DW_AT_namelist_items:
7229       return "DW_AT_namelist_items";
7230     case DW_AT_priority:
7231       return "DW_AT_priority";
7232     case DW_AT_segment:
7233       return "DW_AT_segment";
7234     case DW_AT_specification:
7235       return "DW_AT_specification";
7236     case DW_AT_static_link:
7237       return "DW_AT_static_link";
7238     case DW_AT_type:
7239       return "DW_AT_type";
7240     case DW_AT_use_location:
7241       return "DW_AT_use_location";
7242     case DW_AT_variable_parameter:
7243       return "DW_AT_variable_parameter";
7244     case DW_AT_virtuality:
7245       return "DW_AT_virtuality";
7246     case DW_AT_vtable_elem_location:
7247       return "DW_AT_vtable_elem_location";
7248
7249     case DW_AT_allocated:
7250       return "DW_AT_allocated";
7251     case DW_AT_associated:
7252       return "DW_AT_associated";
7253     case DW_AT_data_location:
7254       return "DW_AT_data_location";
7255     case DW_AT_byte_stride:
7256       return "DW_AT_byte_stride";
7257     case DW_AT_entry_pc:
7258       return "DW_AT_entry_pc";
7259     case DW_AT_use_UTF8:
7260       return "DW_AT_use_UTF8";
7261     case DW_AT_extension:
7262       return "DW_AT_extension";
7263     case DW_AT_ranges:
7264       return "DW_AT_ranges";
7265     case DW_AT_trampoline:
7266       return "DW_AT_trampoline";
7267     case DW_AT_call_column:
7268       return "DW_AT_call_column";
7269     case DW_AT_call_file:
7270       return "DW_AT_call_file";
7271     case DW_AT_call_line:
7272       return "DW_AT_call_line";
7273     case DW_AT_object_pointer:
7274       return "DW_AT_object_pointer";
7275
7276     case DW_AT_signature:
7277       return "DW_AT_signature";
7278     case DW_AT_main_subprogram:
7279       return "DW_AT_main_subprogram";
7280     case DW_AT_data_bit_offset:
7281       return "DW_AT_data_bit_offset";
7282     case DW_AT_const_expr:
7283       return "DW_AT_const_expr";
7284     case DW_AT_enum_class:
7285       return "DW_AT_enum_class";
7286     case DW_AT_linkage_name:
7287       return "DW_AT_linkage_name";
7288
7289     case DW_AT_MIPS_fde:
7290       return "DW_AT_MIPS_fde";
7291     case DW_AT_MIPS_loop_begin:
7292       return "DW_AT_MIPS_loop_begin";
7293     case DW_AT_MIPS_tail_loop_begin:
7294       return "DW_AT_MIPS_tail_loop_begin";
7295     case DW_AT_MIPS_epilog_begin:
7296       return "DW_AT_MIPS_epilog_begin";
7297 #if VMS_DEBUGGING_INFO
7298     case DW_AT_HP_prologue:
7299       return "DW_AT_HP_prologue";
7300 #else
7301     case DW_AT_MIPS_loop_unroll_factor:
7302       return "DW_AT_MIPS_loop_unroll_factor";
7303 #endif
7304     case DW_AT_MIPS_software_pipeline_depth:
7305       return "DW_AT_MIPS_software_pipeline_depth";
7306     case DW_AT_MIPS_linkage_name:
7307       return "DW_AT_MIPS_linkage_name";
7308 #if VMS_DEBUGGING_INFO
7309     case DW_AT_HP_epilogue:
7310       return "DW_AT_HP_epilogue";
7311 #else
7312     case DW_AT_MIPS_stride:
7313       return "DW_AT_MIPS_stride";
7314 #endif
7315     case DW_AT_MIPS_abstract_name:
7316       return "DW_AT_MIPS_abstract_name";
7317     case DW_AT_MIPS_clone_origin:
7318       return "DW_AT_MIPS_clone_origin";
7319     case DW_AT_MIPS_has_inlines:
7320       return "DW_AT_MIPS_has_inlines";
7321
7322     case DW_AT_sf_names:
7323       return "DW_AT_sf_names";
7324     case DW_AT_src_info:
7325       return "DW_AT_src_info";
7326     case DW_AT_mac_info:
7327       return "DW_AT_mac_info";
7328     case DW_AT_src_coords:
7329       return "DW_AT_src_coords";
7330     case DW_AT_body_begin:
7331       return "DW_AT_body_begin";
7332     case DW_AT_body_end:
7333       return "DW_AT_body_end";
7334
7335     case DW_AT_GNU_vector:
7336       return "DW_AT_GNU_vector";
7337     case DW_AT_GNU_guarded_by:
7338       return "DW_AT_GNU_guarded_by";
7339     case DW_AT_GNU_pt_guarded_by:
7340       return "DW_AT_GNU_pt_guarded_by";
7341     case DW_AT_GNU_guarded:
7342       return "DW_AT_GNU_guarded";
7343     case DW_AT_GNU_pt_guarded:
7344       return "DW_AT_GNU_pt_guarded";
7345     case DW_AT_GNU_locks_excluded:
7346       return "DW_AT_GNU_locks_excluded";
7347     case DW_AT_GNU_exclusive_locks_required:
7348       return "DW_AT_GNU_exclusive_locks_required";
7349     case DW_AT_GNU_shared_locks_required:
7350       return "DW_AT_GNU_shared_locks_required";
7351     case DW_AT_GNU_odr_signature:
7352       return "DW_AT_GNU_odr_signature";
7353     case DW_AT_GNU_template_name:
7354       return "DW_AT_GNU_template_name";
7355     case DW_AT_GNU_call_site_value:
7356       return "DW_AT_GNU_call_site_value";
7357     case DW_AT_GNU_call_site_data_value:
7358       return "DW_AT_GNU_call_site_data_value";
7359     case DW_AT_GNU_call_site_target:
7360       return "DW_AT_GNU_call_site_target";
7361     case DW_AT_GNU_call_site_target_clobbered:
7362       return "DW_AT_GNU_call_site_target_clobbered";
7363     case DW_AT_GNU_tail_call:
7364       return "DW_AT_GNU_tail_call";
7365     case DW_AT_GNU_all_tail_call_sites:
7366       return "DW_AT_GNU_all_tail_call_sites";
7367     case DW_AT_GNU_all_call_sites:
7368       return "DW_AT_GNU_all_call_sites";
7369     case DW_AT_GNU_all_source_call_sites:
7370       return "DW_AT_GNU_all_source_call_sites";
7371
7372     case DW_AT_GNAT_descriptive_type:
7373       return "DW_AT_GNAT_descriptive_type";
7374
7375     case DW_AT_VMS_rtnbeg_pd_address:
7376       return "DW_AT_VMS_rtnbeg_pd_address";
7377
7378     default:
7379       return "DW_AT_<unknown>";
7380     }
7381 }
7382
7383 /* Convert a DWARF value form code into its string name.  */
7384
7385 static const char *
7386 dwarf_form_name (unsigned int form)
7387 {
7388   switch (form)
7389     {
7390     case DW_FORM_addr:
7391       return "DW_FORM_addr";
7392     case DW_FORM_block2:
7393       return "DW_FORM_block2";
7394     case DW_FORM_block4:
7395       return "DW_FORM_block4";
7396     case DW_FORM_data2:
7397       return "DW_FORM_data2";
7398     case DW_FORM_data4:
7399       return "DW_FORM_data4";
7400     case DW_FORM_data8:
7401       return "DW_FORM_data8";
7402     case DW_FORM_string:
7403       return "DW_FORM_string";
7404     case DW_FORM_block:
7405       return "DW_FORM_block";
7406     case DW_FORM_block1:
7407       return "DW_FORM_block1";
7408     case DW_FORM_data1:
7409       return "DW_FORM_data1";
7410     case DW_FORM_flag:
7411       return "DW_FORM_flag";
7412     case DW_FORM_sdata:
7413       return "DW_FORM_sdata";
7414     case DW_FORM_strp:
7415       return "DW_FORM_strp";
7416     case DW_FORM_udata:
7417       return "DW_FORM_udata";
7418     case DW_FORM_ref_addr:
7419       return "DW_FORM_ref_addr";
7420     case DW_FORM_ref1:
7421       return "DW_FORM_ref1";
7422     case DW_FORM_ref2:
7423       return "DW_FORM_ref2";
7424     case DW_FORM_ref4:
7425       return "DW_FORM_ref4";
7426     case DW_FORM_ref8:
7427       return "DW_FORM_ref8";
7428     case DW_FORM_ref_udata:
7429       return "DW_FORM_ref_udata";
7430     case DW_FORM_indirect:
7431       return "DW_FORM_indirect";
7432     case DW_FORM_sec_offset:
7433       return "DW_FORM_sec_offset";
7434     case DW_FORM_exprloc:
7435       return "DW_FORM_exprloc";
7436     case DW_FORM_flag_present:
7437       return "DW_FORM_flag_present";
7438     case DW_FORM_ref_sig8:
7439       return "DW_FORM_ref_sig8";
7440     default:
7441       return "DW_FORM_<unknown>";
7442     }
7443 }
7444 \f
7445 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7446    instance of an inlined instance of a decl which is local to an inline
7447    function, so we have to trace all of the way back through the origin chain
7448    to find out what sort of node actually served as the original seed for the
7449    given block.  */
7450
7451 static tree
7452 decl_ultimate_origin (const_tree decl)
7453 {
7454   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7455     return NULL_TREE;
7456
7457   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7458      nodes in the function to point to themselves; ignore that if
7459      we're trying to output the abstract instance of this function.  */
7460   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7461     return NULL_TREE;
7462
7463   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7464      most distant ancestor, this should never happen.  */
7465   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7466
7467   return DECL_ABSTRACT_ORIGIN (decl);
7468 }
7469
7470 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7471    of a virtual function may refer to a base class, so we check the 'this'
7472    parameter.  */
7473
7474 static tree
7475 decl_class_context (tree decl)
7476 {
7477   tree context = NULL_TREE;
7478
7479   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7480     context = DECL_CONTEXT (decl);
7481   else
7482     context = TYPE_MAIN_VARIANT
7483       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7484
7485   if (context && !TYPE_P (context))
7486     context = NULL_TREE;
7487
7488   return context;
7489 }
7490 \f
7491 /* Add an attribute/value pair to a DIE.  */
7492
7493 static inline void
7494 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7495 {
7496   /* Maybe this should be an assert?  */
7497   if (die == NULL)
7498     return;
7499
7500   if (die->die_attr == NULL)
7501     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7502   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7503 }
7504
7505 static inline enum dw_val_class
7506 AT_class (dw_attr_ref a)
7507 {
7508   return a->dw_attr_val.val_class;
7509 }
7510
7511 /* Add a flag value attribute to a DIE.  */
7512
7513 static inline void
7514 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7515 {
7516   dw_attr_node attr;
7517
7518   attr.dw_attr = attr_kind;
7519   attr.dw_attr_val.val_class = dw_val_class_flag;
7520   attr.dw_attr_val.v.val_flag = flag;
7521   add_dwarf_attr (die, &attr);
7522 }
7523
7524 static inline unsigned
7525 AT_flag (dw_attr_ref a)
7526 {
7527   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7528   return a->dw_attr_val.v.val_flag;
7529 }
7530
7531 /* Add a signed integer attribute value to a DIE.  */
7532
7533 static inline void
7534 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7535 {
7536   dw_attr_node attr;
7537
7538   attr.dw_attr = attr_kind;
7539   attr.dw_attr_val.val_class = dw_val_class_const;
7540   attr.dw_attr_val.v.val_int = int_val;
7541   add_dwarf_attr (die, &attr);
7542 }
7543
7544 static inline HOST_WIDE_INT
7545 AT_int (dw_attr_ref a)
7546 {
7547   gcc_assert (a && AT_class (a) == dw_val_class_const);
7548   return a->dw_attr_val.v.val_int;
7549 }
7550
7551 /* Add an unsigned integer attribute value to a DIE.  */
7552
7553 static inline void
7554 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7555                  unsigned HOST_WIDE_INT unsigned_val)
7556 {
7557   dw_attr_node attr;
7558
7559   attr.dw_attr = attr_kind;
7560   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7561   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7562   add_dwarf_attr (die, &attr);
7563 }
7564
7565 static inline unsigned HOST_WIDE_INT
7566 AT_unsigned (dw_attr_ref a)
7567 {
7568   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7569   return a->dw_attr_val.v.val_unsigned;
7570 }
7571
7572 /* Add an unsigned double integer attribute value to a DIE.  */
7573
7574 static inline void
7575 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7576                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7577 {
7578   dw_attr_node attr;
7579
7580   attr.dw_attr = attr_kind;
7581   attr.dw_attr_val.val_class = dw_val_class_const_double;
7582   attr.dw_attr_val.v.val_double.high = high;
7583   attr.dw_attr_val.v.val_double.low = low;
7584   add_dwarf_attr (die, &attr);
7585 }
7586
7587 /* Add a floating point attribute value to a DIE and return it.  */
7588
7589 static inline void
7590 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7591             unsigned int length, unsigned int elt_size, unsigned char *array)
7592 {
7593   dw_attr_node attr;
7594
7595   attr.dw_attr = attr_kind;
7596   attr.dw_attr_val.val_class = dw_val_class_vec;
7597   attr.dw_attr_val.v.val_vec.length = length;
7598   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7599   attr.dw_attr_val.v.val_vec.array = array;
7600   add_dwarf_attr (die, &attr);
7601 }
7602
7603 /* Add an 8-byte data attribute value to a DIE.  */
7604
7605 static inline void
7606 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7607               unsigned char data8[8])
7608 {
7609   dw_attr_node attr;
7610
7611   attr.dw_attr = attr_kind;
7612   attr.dw_attr_val.val_class = dw_val_class_data8;
7613   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7614   add_dwarf_attr (die, &attr);
7615 }
7616
7617 /* Hash and equality functions for debug_str_hash.  */
7618
7619 static hashval_t
7620 debug_str_do_hash (const void *x)
7621 {
7622   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7623 }
7624
7625 static int
7626 debug_str_eq (const void *x1, const void *x2)
7627 {
7628   return strcmp ((((const struct indirect_string_node *)x1)->str),
7629                  (const char *)x2) == 0;
7630 }
7631
7632 /* Add STR to the indirect string hash table.  */
7633
7634 static struct indirect_string_node *
7635 find_AT_string (const char *str)
7636 {
7637   struct indirect_string_node *node;
7638   void **slot;
7639
7640   if (! debug_str_hash)
7641     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7642                                       debug_str_eq, NULL);
7643
7644   slot = htab_find_slot_with_hash (debug_str_hash, str,
7645                                    htab_hash_string (str), INSERT);
7646   if (*slot == NULL)
7647     {
7648       node = ggc_alloc_cleared_indirect_string_node ();
7649       node->str = ggc_strdup (str);
7650       *slot = node;
7651     }
7652   else
7653     node = (struct indirect_string_node *) *slot;
7654
7655   node->refcount++;
7656   return node;
7657 }
7658
7659 /* Add a string attribute value to a DIE.  */
7660
7661 static inline void
7662 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7663 {
7664   dw_attr_node attr;
7665   struct indirect_string_node *node;
7666
7667   node = find_AT_string (str);
7668
7669   attr.dw_attr = attr_kind;
7670   attr.dw_attr_val.val_class = dw_val_class_str;
7671   attr.dw_attr_val.v.val_str = node;
7672   add_dwarf_attr (die, &attr);
7673 }
7674
7675 static inline const char *
7676 AT_string (dw_attr_ref a)
7677 {
7678   gcc_assert (a && AT_class (a) == dw_val_class_str);
7679   return a->dw_attr_val.v.val_str->str;
7680 }
7681
7682 /* Find out whether a string should be output inline in DIE
7683    or out-of-line in .debug_str section.  */
7684
7685 static enum dwarf_form
7686 AT_string_form (dw_attr_ref a)
7687 {
7688   struct indirect_string_node *node;
7689   unsigned int len;
7690   char label[32];
7691
7692   gcc_assert (a && AT_class (a) == dw_val_class_str);
7693
7694   node = a->dw_attr_val.v.val_str;
7695   if (node->form)
7696     return node->form;
7697
7698   len = strlen (node->str) + 1;
7699
7700   /* If the string is shorter or equal to the size of the reference, it is
7701      always better to put it inline.  */
7702   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7703     return node->form = DW_FORM_string;
7704
7705   /* If we cannot expect the linker to merge strings in .debug_str
7706      section, only put it into .debug_str if it is worth even in this
7707      single module.  */
7708   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7709       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7710       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7711     return node->form = DW_FORM_string;
7712
7713   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7714   ++dw2_string_counter;
7715   node->label = xstrdup (label);
7716
7717   return node->form = DW_FORM_strp;
7718 }
7719
7720 /* Add a DIE reference attribute value to a DIE.  */
7721
7722 static inline void
7723 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7724 {
7725   dw_attr_node attr;
7726
7727 #ifdef ENABLE_CHECKING
7728   gcc_assert (targ_die != NULL);
7729 #else
7730   /* With LTO we can end up trying to reference something we didn't create
7731      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7732   if (targ_die == NULL)
7733     return;
7734 #endif
7735
7736   attr.dw_attr = attr_kind;
7737   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7738   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7739   attr.dw_attr_val.v.val_die_ref.external = 0;
7740   add_dwarf_attr (die, &attr);
7741 }
7742
7743 /* Add an AT_specification attribute to a DIE, and also make the back
7744    pointer from the specification to the definition.  */
7745
7746 static inline void
7747 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7748 {
7749   add_AT_die_ref (die, DW_AT_specification, targ_die);
7750   gcc_assert (!targ_die->die_definition);
7751   targ_die->die_definition = die;
7752 }
7753
7754 static inline dw_die_ref
7755 AT_ref (dw_attr_ref a)
7756 {
7757   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7758   return a->dw_attr_val.v.val_die_ref.die;
7759 }
7760
7761 static inline int
7762 AT_ref_external (dw_attr_ref a)
7763 {
7764   if (a && AT_class (a) == dw_val_class_die_ref)
7765     return a->dw_attr_val.v.val_die_ref.external;
7766
7767   return 0;
7768 }
7769
7770 static inline void
7771 set_AT_ref_external (dw_attr_ref a, int i)
7772 {
7773   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7774   a->dw_attr_val.v.val_die_ref.external = i;
7775 }
7776
7777 /* Add an FDE reference attribute value to a DIE.  */
7778
7779 static inline void
7780 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7781 {
7782   dw_attr_node attr;
7783
7784   attr.dw_attr = attr_kind;
7785   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7786   attr.dw_attr_val.v.val_fde_index = targ_fde;
7787   add_dwarf_attr (die, &attr);
7788 }
7789
7790 /* Add a location description attribute value to a DIE.  */
7791
7792 static inline void
7793 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7794 {
7795   dw_attr_node attr;
7796
7797   attr.dw_attr = attr_kind;
7798   attr.dw_attr_val.val_class = dw_val_class_loc;
7799   attr.dw_attr_val.v.val_loc = loc;
7800   add_dwarf_attr (die, &attr);
7801 }
7802
7803 static inline dw_loc_descr_ref
7804 AT_loc (dw_attr_ref a)
7805 {
7806   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7807   return a->dw_attr_val.v.val_loc;
7808 }
7809
7810 static inline void
7811 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7812 {
7813   dw_attr_node attr;
7814
7815   attr.dw_attr = attr_kind;
7816   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7817   attr.dw_attr_val.v.val_loc_list = loc_list;
7818   add_dwarf_attr (die, &attr);
7819   have_location_lists = true;
7820 }
7821
7822 static inline dw_loc_list_ref
7823 AT_loc_list (dw_attr_ref a)
7824 {
7825   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7826   return a->dw_attr_val.v.val_loc_list;
7827 }
7828
7829 static inline dw_loc_list_ref *
7830 AT_loc_list_ptr (dw_attr_ref a)
7831 {
7832   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7833   return &a->dw_attr_val.v.val_loc_list;
7834 }
7835
7836 /* Add an address constant attribute value to a DIE.  */
7837
7838 static inline void
7839 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7840 {
7841   dw_attr_node attr;
7842
7843   attr.dw_attr = attr_kind;
7844   attr.dw_attr_val.val_class = dw_val_class_addr;
7845   attr.dw_attr_val.v.val_addr = addr;
7846   add_dwarf_attr (die, &attr);
7847 }
7848
7849 /* Get the RTX from to an address DIE attribute.  */
7850
7851 static inline rtx
7852 AT_addr (dw_attr_ref a)
7853 {
7854   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7855   return a->dw_attr_val.v.val_addr;
7856 }
7857
7858 /* Add a file attribute value to a DIE.  */
7859
7860 static inline void
7861 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7862              struct dwarf_file_data *fd)
7863 {
7864   dw_attr_node attr;
7865
7866   attr.dw_attr = attr_kind;
7867   attr.dw_attr_val.val_class = dw_val_class_file;
7868   attr.dw_attr_val.v.val_file = fd;
7869   add_dwarf_attr (die, &attr);
7870 }
7871
7872 /* Get the dwarf_file_data from a file DIE attribute.  */
7873
7874 static inline struct dwarf_file_data *
7875 AT_file (dw_attr_ref a)
7876 {
7877   gcc_assert (a && AT_class (a) == dw_val_class_file);
7878   return a->dw_attr_val.v.val_file;
7879 }
7880
7881 /* Add a vms delta attribute value to a DIE.  */
7882
7883 static inline void
7884 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7885                   const char *lbl1, const char *lbl2)
7886 {
7887   dw_attr_node attr;
7888
7889   attr.dw_attr = attr_kind;
7890   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7891   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7892   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7893   add_dwarf_attr (die, &attr);
7894 }
7895
7896 /* Add a label identifier attribute value to a DIE.  */
7897
7898 static inline void
7899 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7900 {
7901   dw_attr_node attr;
7902
7903   attr.dw_attr = attr_kind;
7904   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7905   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7906   add_dwarf_attr (die, &attr);
7907 }
7908
7909 /* Add a section offset attribute value to a DIE, an offset into the
7910    debug_line section.  */
7911
7912 static inline void
7913 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7914                 const char *label)
7915 {
7916   dw_attr_node attr;
7917
7918   attr.dw_attr = attr_kind;
7919   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7920   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7921   add_dwarf_attr (die, &attr);
7922 }
7923
7924 /* Add a section offset attribute value to a DIE, an offset into the
7925    debug_macinfo section.  */
7926
7927 static inline void
7928 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7929                const char *label)
7930 {
7931   dw_attr_node attr;
7932
7933   attr.dw_attr = attr_kind;
7934   attr.dw_attr_val.val_class = dw_val_class_macptr;
7935   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7936   add_dwarf_attr (die, &attr);
7937 }
7938
7939 /* Add an offset attribute value to a DIE.  */
7940
7941 static inline void
7942 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7943                unsigned HOST_WIDE_INT offset)
7944 {
7945   dw_attr_node attr;
7946
7947   attr.dw_attr = attr_kind;
7948   attr.dw_attr_val.val_class = dw_val_class_offset;
7949   attr.dw_attr_val.v.val_offset = offset;
7950   add_dwarf_attr (die, &attr);
7951 }
7952
7953 /* Add an range_list attribute value to a DIE.  */
7954
7955 static void
7956 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7957                    long unsigned int offset)
7958 {
7959   dw_attr_node attr;
7960
7961   attr.dw_attr = attr_kind;
7962   attr.dw_attr_val.val_class = dw_val_class_range_list;
7963   attr.dw_attr_val.v.val_offset = offset;
7964   add_dwarf_attr (die, &attr);
7965 }
7966
7967 /* Return the start label of a delta attribute.  */
7968
7969 static inline const char *
7970 AT_vms_delta1 (dw_attr_ref a)
7971 {
7972   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7973   return a->dw_attr_val.v.val_vms_delta.lbl1;
7974 }
7975
7976 /* Return the end label of a delta attribute.  */
7977
7978 static inline const char *
7979 AT_vms_delta2 (dw_attr_ref a)
7980 {
7981   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7982   return a->dw_attr_val.v.val_vms_delta.lbl2;
7983 }
7984
7985 static inline const char *
7986 AT_lbl (dw_attr_ref a)
7987 {
7988   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7989                     || AT_class (a) == dw_val_class_lineptr
7990                     || AT_class (a) == dw_val_class_macptr));
7991   return a->dw_attr_val.v.val_lbl_id;
7992 }
7993
7994 /* Get the attribute of type attr_kind.  */
7995
7996 static dw_attr_ref
7997 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7998 {
7999   dw_attr_ref a;
8000   unsigned ix;
8001   dw_die_ref spec = NULL;
8002
8003   if (! die)
8004     return NULL;
8005
8006   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8007     if (a->dw_attr == attr_kind)
8008       return a;
8009     else if (a->dw_attr == DW_AT_specification
8010              || a->dw_attr == DW_AT_abstract_origin)
8011       spec = AT_ref (a);
8012
8013   if (spec)
8014     return get_AT (spec, attr_kind);
8015
8016   return NULL;
8017 }
8018
8019 /* Return the "low pc" attribute value, typically associated with a subprogram
8020    DIE.  Return null if the "low pc" attribute is either not present, or if it
8021    cannot be represented as an assembler label identifier.  */
8022
8023 static inline const char *
8024 get_AT_low_pc (dw_die_ref die)
8025 {
8026   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
8027
8028   return a ? AT_lbl (a) : NULL;
8029 }
8030
8031 /* Return the "high pc" attribute value, typically associated with a subprogram
8032    DIE.  Return null if the "high pc" attribute is either not present, or if it
8033    cannot be represented as an assembler label identifier.  */
8034
8035 static inline const char *
8036 get_AT_hi_pc (dw_die_ref die)
8037 {
8038   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
8039
8040   return a ? AT_lbl (a) : NULL;
8041 }
8042
8043 /* Return the value of the string attribute designated by ATTR_KIND, or
8044    NULL if it is not present.  */
8045
8046 static inline const char *
8047 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
8048 {
8049   dw_attr_ref a = get_AT (die, attr_kind);
8050
8051   return a ? AT_string (a) : NULL;
8052 }
8053
8054 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
8055    if it is not present.  */
8056
8057 static inline int
8058 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
8059 {
8060   dw_attr_ref a = get_AT (die, attr_kind);
8061
8062   return a ? AT_flag (a) : 0;
8063 }
8064
8065 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
8066    if it is not present.  */
8067
8068 static inline unsigned
8069 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
8070 {
8071   dw_attr_ref a = get_AT (die, attr_kind);
8072
8073   return a ? AT_unsigned (a) : 0;
8074 }
8075
8076 static inline dw_die_ref
8077 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
8078 {
8079   dw_attr_ref a = get_AT (die, attr_kind);
8080
8081   return a ? AT_ref (a) : NULL;
8082 }
8083
8084 static inline struct dwarf_file_data *
8085 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
8086 {
8087   dw_attr_ref a = get_AT (die, attr_kind);
8088
8089   return a ? AT_file (a) : NULL;
8090 }
8091
8092 /* Return TRUE if the language is C++.  */
8093
8094 static inline bool
8095 is_cxx (void)
8096 {
8097   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8098
8099   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
8100 }
8101
8102 /* Return TRUE if the language is Fortran.  */
8103
8104 static inline bool
8105 is_fortran (void)
8106 {
8107   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8108
8109   return (lang == DW_LANG_Fortran77
8110           || lang == DW_LANG_Fortran90
8111           || lang == DW_LANG_Fortran95);
8112 }
8113
8114 /* Return TRUE if the language is Ada.  */
8115
8116 static inline bool
8117 is_ada (void)
8118 {
8119   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8120
8121   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
8122 }
8123
8124 /* Remove the specified attribute if present.  */
8125
8126 static void
8127 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8128 {
8129   dw_attr_ref a;
8130   unsigned ix;
8131
8132   if (! die)
8133     return;
8134
8135   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8136     if (a->dw_attr == attr_kind)
8137       {
8138         if (AT_class (a) == dw_val_class_str)
8139           if (a->dw_attr_val.v.val_str->refcount)
8140             a->dw_attr_val.v.val_str->refcount--;
8141
8142         /* VEC_ordered_remove should help reduce the number of abbrevs
8143            that are needed.  */
8144         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
8145         return;
8146       }
8147 }
8148
8149 /* Remove CHILD from its parent.  PREV must have the property that
8150    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
8151
8152 static void
8153 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
8154 {
8155   gcc_assert (child->die_parent == prev->die_parent);
8156   gcc_assert (prev->die_sib == child);
8157   if (prev == child)
8158     {
8159       gcc_assert (child->die_parent->die_child == child);
8160       prev = NULL;
8161     }
8162   else
8163     prev->die_sib = child->die_sib;
8164   if (child->die_parent->die_child == child)
8165     child->die_parent->die_child = prev;
8166 }
8167
8168 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
8169    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
8170
8171 static void
8172 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8173 {
8174   dw_die_ref parent = old_child->die_parent;
8175
8176   gcc_assert (parent == prev->die_parent);
8177   gcc_assert (prev->die_sib == old_child);
8178
8179   new_child->die_parent = parent;
8180   if (prev == old_child)
8181     {
8182       gcc_assert (parent->die_child == old_child);
8183       new_child->die_sib = new_child;
8184     }
8185   else
8186     {
8187       prev->die_sib = new_child;
8188       new_child->die_sib = old_child->die_sib;
8189     }
8190   if (old_child->die_parent->die_child == old_child)
8191     old_child->die_parent->die_child = new_child;
8192 }
8193
8194 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8195
8196 static void
8197 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8198 {
8199   dw_die_ref c;
8200   new_parent->die_child = old_parent->die_child;
8201   old_parent->die_child = NULL;
8202   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8203 }
8204
8205 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8206    matches TAG.  */
8207
8208 static void
8209 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8210 {
8211   dw_die_ref c;
8212
8213   c = die->die_child;
8214   if (c) do {
8215     dw_die_ref prev = c;
8216     c = c->die_sib;
8217     while (c->die_tag == tag)
8218       {
8219         remove_child_with_prev (c, prev);
8220         /* Might have removed every child.  */
8221         if (c == c->die_sib)
8222           return;
8223         c = c->die_sib;
8224       }
8225   } while (c != die->die_child);
8226 }
8227
8228 /* Add a CHILD_DIE as the last child of DIE.  */
8229
8230 static void
8231 add_child_die (dw_die_ref die, dw_die_ref child_die)
8232 {
8233   /* FIXME this should probably be an assert.  */
8234   if (! die || ! child_die)
8235     return;
8236   gcc_assert (die != child_die);
8237
8238   child_die->die_parent = die;
8239   if (die->die_child)
8240     {
8241       child_die->die_sib = die->die_child->die_sib;
8242       die->die_child->die_sib = child_die;
8243     }
8244   else
8245     child_die->die_sib = child_die;
8246   die->die_child = child_die;
8247 }
8248
8249 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8250    is the specification, to the end of PARENT's list of children.
8251    This is done by removing and re-adding it.  */
8252
8253 static void
8254 splice_child_die (dw_die_ref parent, dw_die_ref child)
8255 {
8256   dw_die_ref p;
8257
8258   /* We want the declaration DIE from inside the class, not the
8259      specification DIE at toplevel.  */
8260   if (child->die_parent != parent)
8261     {
8262       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8263
8264       if (tmp)
8265         child = tmp;
8266     }
8267
8268   gcc_assert (child->die_parent == parent
8269               || (child->die_parent
8270                   == get_AT_ref (parent, DW_AT_specification)));
8271
8272   for (p = child->die_parent->die_child; ; p = p->die_sib)
8273     if (p->die_sib == child)
8274       {
8275         remove_child_with_prev (child, p);
8276         break;
8277       }
8278
8279   add_child_die (parent, child);
8280 }
8281
8282 /* Return a pointer to a newly created DIE node.  */
8283
8284 static inline dw_die_ref
8285 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8286 {
8287   dw_die_ref die = ggc_alloc_cleared_die_node ();
8288
8289   die->die_tag = tag_value;
8290
8291   if (parent_die != NULL)
8292     add_child_die (parent_die, die);
8293   else
8294     {
8295       limbo_die_node *limbo_node;
8296
8297       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8298       limbo_node->die = die;
8299       limbo_node->created_for = t;
8300       limbo_node->next = limbo_die_list;
8301       limbo_die_list = limbo_node;
8302     }
8303
8304   return die;
8305 }
8306
8307 /* Return the DIE associated with the given type specifier.  */
8308
8309 static inline dw_die_ref
8310 lookup_type_die (tree type)
8311 {
8312   return TYPE_SYMTAB_DIE (type);
8313 }
8314
8315 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8316    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8317    anonymous type instead the one of the naming typedef.  */
8318
8319 static inline dw_die_ref
8320 strip_naming_typedef (tree type, dw_die_ref type_die)
8321 {
8322   if (type
8323       && TREE_CODE (type) == RECORD_TYPE
8324       && type_die
8325       && type_die->die_tag == DW_TAG_typedef
8326       && is_naming_typedef_decl (TYPE_NAME (type)))
8327     type_die = get_AT_ref (type_die, DW_AT_type);
8328   return type_die;
8329 }
8330
8331 /* Like lookup_type_die, but if type is an anonymous type named by a
8332    typedef[1], return the DIE of the anonymous type instead the one of
8333    the naming typedef.  This is because in gen_typedef_die, we did
8334    equate the anonymous struct named by the typedef with the DIE of
8335    the naming typedef. So by default, lookup_type_die on an anonymous
8336    struct yields the DIE of the naming typedef.
8337
8338    [1]: Read the comment of is_naming_typedef_decl to learn about what
8339    a naming typedef is.  */
8340
8341 static inline dw_die_ref
8342 lookup_type_die_strip_naming_typedef (tree type)
8343 {
8344   dw_die_ref die = lookup_type_die (type);
8345   return strip_naming_typedef (type, die);
8346 }
8347
8348 /* Equate a DIE to a given type specifier.  */
8349
8350 static inline void
8351 equate_type_number_to_die (tree type, dw_die_ref type_die)
8352 {
8353   TYPE_SYMTAB_DIE (type) = type_die;
8354 }
8355
8356 /* Returns a hash value for X (which really is a die_struct).  */
8357
8358 static hashval_t
8359 decl_die_table_hash (const void *x)
8360 {
8361   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8362 }
8363
8364 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8365
8366 static int
8367 decl_die_table_eq (const void *x, const void *y)
8368 {
8369   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8370 }
8371
8372 /* Return the DIE associated with a given declaration.  */
8373
8374 static inline dw_die_ref
8375 lookup_decl_die (tree decl)
8376 {
8377   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8378 }
8379
8380 /* Returns a hash value for X (which really is a var_loc_list).  */
8381
8382 static hashval_t
8383 decl_loc_table_hash (const void *x)
8384 {
8385   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8386 }
8387
8388 /* Return nonzero if decl_id of var_loc_list X is the same as
8389    UID of decl *Y.  */
8390
8391 static int
8392 decl_loc_table_eq (const void *x, const void *y)
8393 {
8394   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8395 }
8396
8397 /* Return the var_loc list associated with a given declaration.  */
8398
8399 static inline var_loc_list *
8400 lookup_decl_loc (const_tree decl)
8401 {
8402   if (!decl_loc_table)
8403     return NULL;
8404   return (var_loc_list *)
8405     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8406 }
8407
8408 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8409
8410 static hashval_t
8411 cached_dw_loc_list_table_hash (const void *x)
8412 {
8413   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8414 }
8415
8416 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8417    UID of decl *Y.  */
8418
8419 static int
8420 cached_dw_loc_list_table_eq (const void *x, const void *y)
8421 {
8422   return (((const cached_dw_loc_list *) x)->decl_id
8423           == DECL_UID ((const_tree) y));
8424 }
8425
8426 /* Equate a DIE to a particular declaration.  */
8427
8428 static void
8429 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8430 {
8431   unsigned int decl_id = DECL_UID (decl);
8432   void **slot;
8433
8434   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8435   *slot = decl_die;
8436   decl_die->decl_id = decl_id;
8437 }
8438
8439 /* Return how many bits covers PIECE EXPR_LIST.  */
8440
8441 static int
8442 decl_piece_bitsize (rtx piece)
8443 {
8444   int ret = (int) GET_MODE (piece);
8445   if (ret)
8446     return ret;
8447   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8448               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8449   return INTVAL (XEXP (XEXP (piece, 0), 0));
8450 }
8451
8452 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8453
8454 static rtx *
8455 decl_piece_varloc_ptr (rtx piece)
8456 {
8457   if ((int) GET_MODE (piece))
8458     return &XEXP (piece, 0);
8459   else
8460     return &XEXP (XEXP (piece, 0), 1);
8461 }
8462
8463 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8464    Next is the chain of following piece nodes.  */
8465
8466 static rtx
8467 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8468 {
8469   if (bitsize <= (int) MAX_MACHINE_MODE)
8470     return alloc_EXPR_LIST (bitsize, loc_note, next);
8471   else
8472     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8473                                                GEN_INT (bitsize),
8474                                                loc_note), next);
8475 }
8476
8477 /* Return rtx that should be stored into loc field for
8478    LOC_NOTE and BITPOS/BITSIZE.  */
8479
8480 static rtx
8481 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8482                       HOST_WIDE_INT bitsize)
8483 {
8484   if (bitsize != -1)
8485     {
8486       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8487       if (bitpos != 0)
8488         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8489     }
8490   return loc_note;
8491 }
8492
8493 /* This function either modifies location piece list *DEST in
8494    place (if SRC and INNER is NULL), or copies location piece list
8495    *SRC to *DEST while modifying it.  Location BITPOS is modified
8496    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8497    not copied and if needed some padding around it is added.
8498    When modifying in place, DEST should point to EXPR_LIST where
8499    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8500    to the start of the whole list and INNER points to the EXPR_LIST
8501    where earlier pieces cover PIECE_BITPOS bits.  */
8502
8503 static void
8504 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8505                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8506                    HOST_WIDE_INT bitsize, rtx loc_note)
8507 {
8508   int diff;
8509   bool copy = inner != NULL;
8510
8511   if (copy)
8512     {
8513       /* First copy all nodes preceeding the current bitpos.  */
8514       while (src != inner)
8515         {
8516           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8517                                    decl_piece_bitsize (*src), NULL_RTX);
8518           dest = &XEXP (*dest, 1);
8519           src = &XEXP (*src, 1);
8520         }
8521     }
8522   /* Add padding if needed.  */
8523   if (bitpos != piece_bitpos)
8524     {
8525       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8526                                copy ? NULL_RTX : *dest);
8527       dest = &XEXP (*dest, 1);
8528     }
8529   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8530     {
8531       gcc_assert (!copy);
8532       /* A piece with correct bitpos and bitsize already exist,
8533          just update the location for it and return.  */
8534       *decl_piece_varloc_ptr (*dest) = loc_note;
8535       return;
8536     }
8537   /* Add the piece that changed.  */
8538   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8539   dest = &XEXP (*dest, 1);
8540   /* Skip over pieces that overlap it.  */
8541   diff = bitpos - piece_bitpos + bitsize;
8542   if (!copy)
8543     src = dest;
8544   while (diff > 0 && *src)
8545     {
8546       rtx piece = *src;
8547       diff -= decl_piece_bitsize (piece);
8548       if (copy)
8549         src = &XEXP (piece, 1);
8550       else
8551         {
8552           *src = XEXP (piece, 1);
8553           free_EXPR_LIST_node (piece);
8554         }
8555     }
8556   /* Add padding if needed.  */
8557   if (diff < 0 && *src)
8558     {
8559       if (!copy)
8560         dest = src;
8561       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8562       dest = &XEXP (*dest, 1);
8563     }
8564   if (!copy)
8565     return;
8566   /* Finally copy all nodes following it.  */
8567   while (*src)
8568     {
8569       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8570                                decl_piece_bitsize (*src), NULL_RTX);
8571       dest = &XEXP (*dest, 1);
8572       src = &XEXP (*src, 1);
8573     }
8574 }
8575
8576 /* Add a variable location node to the linked list for DECL.  */
8577
8578 static struct var_loc_node *
8579 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8580 {
8581   unsigned int decl_id;
8582   var_loc_list *temp;
8583   void **slot;
8584   struct var_loc_node *loc = NULL;
8585   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8586
8587   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8588     {
8589       tree realdecl = DECL_DEBUG_EXPR (decl);
8590       if (realdecl && handled_component_p (realdecl))
8591         {
8592           HOST_WIDE_INT maxsize;
8593           tree innerdecl;
8594           innerdecl
8595             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8596           if (!DECL_P (innerdecl)
8597               || DECL_IGNORED_P (innerdecl)
8598               || TREE_STATIC (innerdecl)
8599               || bitsize <= 0
8600               || bitpos + bitsize > 256
8601               || bitsize != maxsize)
8602             return NULL;
8603           decl = innerdecl;
8604         }
8605     }
8606
8607   decl_id = DECL_UID (decl);
8608   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8609   if (*slot == NULL)
8610     {
8611       temp = ggc_alloc_cleared_var_loc_list ();
8612       temp->decl_id = decl_id;
8613       *slot = temp;
8614     }
8615   else
8616     temp = (var_loc_list *) *slot;
8617
8618   if (temp->last)
8619     {
8620       struct var_loc_node *last = temp->last, *unused = NULL;
8621       rtx *piece_loc = NULL, last_loc_note;
8622       int piece_bitpos = 0;
8623       if (last->next)
8624         {
8625           last = last->next;
8626           gcc_assert (last->next == NULL);
8627         }
8628       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8629         {
8630           piece_loc = &last->loc;
8631           do
8632             {
8633               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8634               if (piece_bitpos + cur_bitsize > bitpos)
8635                 break;
8636               piece_bitpos += cur_bitsize;
8637               piece_loc = &XEXP (*piece_loc, 1);
8638             }
8639           while (*piece_loc);
8640         }
8641       /* TEMP->LAST here is either pointer to the last but one or
8642          last element in the chained list, LAST is pointer to the
8643          last element.  */
8644       if (label && strcmp (last->label, label) == 0)
8645         {
8646           /* For SRA optimized variables if there weren't any real
8647              insns since last note, just modify the last node.  */
8648           if (piece_loc != NULL)
8649             {
8650               adjust_piece_list (piece_loc, NULL, NULL,
8651                                  bitpos, piece_bitpos, bitsize, loc_note);
8652               return NULL;
8653             }
8654           /* If the last note doesn't cover any instructions, remove it.  */
8655           if (temp->last != last)
8656             {
8657               temp->last->next = NULL;
8658               unused = last;
8659               last = temp->last;
8660               gcc_assert (strcmp (last->label, label) != 0);
8661             }
8662           else
8663             {
8664               gcc_assert (temp->first == temp->last);
8665               memset (temp->last, '\0', sizeof (*temp->last));
8666               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8667               return temp->last;
8668             }
8669         }
8670       if (bitsize == -1 && NOTE_P (last->loc))
8671         last_loc_note = last->loc;
8672       else if (piece_loc != NULL
8673                && *piece_loc != NULL_RTX
8674                && piece_bitpos == bitpos
8675                && decl_piece_bitsize (*piece_loc) == bitsize)
8676         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8677       else
8678         last_loc_note = NULL_RTX;
8679       /* If the current location is the same as the end of the list,
8680          and either both or neither of the locations is uninitialized,
8681          we have nothing to do.  */
8682       if (last_loc_note == NULL_RTX
8683           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8684                             NOTE_VAR_LOCATION_LOC (loc_note)))
8685           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8686                != NOTE_VAR_LOCATION_STATUS (loc_note))
8687               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8688                    == VAR_INIT_STATUS_UNINITIALIZED)
8689                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8690                       == VAR_INIT_STATUS_UNINITIALIZED))))
8691         {
8692           /* Add LOC to the end of list and update LAST.  If the last
8693              element of the list has been removed above, reuse its
8694              memory for the new node, otherwise allocate a new one.  */
8695           if (unused)
8696             {
8697               loc = unused;
8698               memset (loc, '\0', sizeof (*loc));
8699             }
8700           else
8701             loc = ggc_alloc_cleared_var_loc_node ();
8702           if (bitsize == -1 || piece_loc == NULL)
8703             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8704           else
8705             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8706                                bitpos, piece_bitpos, bitsize, loc_note);
8707           last->next = loc;
8708           /* Ensure TEMP->LAST will point either to the new last but one
8709              element of the chain, or to the last element in it.  */
8710           if (last != temp->last)
8711             temp->last = last;
8712         }
8713       else if (unused)
8714         ggc_free (unused);
8715     }
8716   else
8717     {
8718       loc = ggc_alloc_cleared_var_loc_node ();
8719       temp->first = loc;
8720       temp->last = loc;
8721       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8722     }
8723   return loc;
8724 }
8725 \f
8726 /* Keep track of the number of spaces used to indent the
8727    output of the debugging routines that print the structure of
8728    the DIE internal representation.  */
8729 static int print_indent;
8730
8731 /* Indent the line the number of spaces given by print_indent.  */
8732
8733 static inline void
8734 print_spaces (FILE *outfile)
8735 {
8736   fprintf (outfile, "%*s", print_indent, "");
8737 }
8738
8739 /* Print a type signature in hex.  */
8740
8741 static inline void
8742 print_signature (FILE *outfile, char *sig)
8743 {
8744   int i;
8745
8746   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8747     fprintf (outfile, "%02x", sig[i] & 0xff);
8748 }
8749
8750 /* Print the information associated with a given DIE, and its children.
8751    This routine is a debugging aid only.  */
8752
8753 static void
8754 print_die (dw_die_ref die, FILE *outfile)
8755 {
8756   dw_attr_ref a;
8757   dw_die_ref c;
8758   unsigned ix;
8759
8760   print_spaces (outfile);
8761   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8762            die->die_offset, dwarf_tag_name (die->die_tag),
8763            (void*) die);
8764   print_spaces (outfile);
8765   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8766   fprintf (outfile, " offset: %ld", die->die_offset);
8767   fprintf (outfile, " mark: %d\n", die->die_mark);
8768
8769   if (use_debug_types && die->die_id.die_type_node)
8770     {
8771       print_spaces (outfile);
8772       fprintf (outfile, "  signature: ");
8773       print_signature (outfile, die->die_id.die_type_node->signature);
8774       fprintf (outfile, "\n");
8775     }
8776
8777   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8778     {
8779       print_spaces (outfile);
8780       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8781
8782       switch (AT_class (a))
8783         {
8784         case dw_val_class_addr:
8785           fprintf (outfile, "address");
8786           break;
8787         case dw_val_class_offset:
8788           fprintf (outfile, "offset");
8789           break;
8790         case dw_val_class_loc:
8791           fprintf (outfile, "location descriptor");
8792           break;
8793         case dw_val_class_loc_list:
8794           fprintf (outfile, "location list -> label:%s",
8795                    AT_loc_list (a)->ll_symbol);
8796           break;
8797         case dw_val_class_range_list:
8798           fprintf (outfile, "range list");
8799           break;
8800         case dw_val_class_const:
8801           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8802           break;
8803         case dw_val_class_unsigned_const:
8804           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8805           break;
8806         case dw_val_class_const_double:
8807           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8808                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8809                    a->dw_attr_val.v.val_double.high,
8810                    a->dw_attr_val.v.val_double.low);
8811           break;
8812         case dw_val_class_vec:
8813           fprintf (outfile, "floating-point or vector constant");
8814           break;
8815         case dw_val_class_flag:
8816           fprintf (outfile, "%u", AT_flag (a));
8817           break;
8818         case dw_val_class_die_ref:
8819           if (AT_ref (a) != NULL)
8820             {
8821               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8822                 {
8823                   fprintf (outfile, "die -> signature: ");
8824                   print_signature (outfile,
8825                                    AT_ref (a)->die_id.die_type_node->signature);
8826                 }
8827               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8828                 fprintf (outfile, "die -> label: %s",
8829                          AT_ref (a)->die_id.die_symbol);
8830               else
8831                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8832               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8833             }
8834           else
8835             fprintf (outfile, "die -> <null>");
8836           break;
8837         case dw_val_class_vms_delta:
8838           fprintf (outfile, "delta: @slotcount(%s-%s)",
8839                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8840           break;
8841         case dw_val_class_lbl_id:
8842         case dw_val_class_lineptr:
8843         case dw_val_class_macptr:
8844           fprintf (outfile, "label: %s", AT_lbl (a));
8845           break;
8846         case dw_val_class_str:
8847           if (AT_string (a) != NULL)
8848             fprintf (outfile, "\"%s\"", AT_string (a));
8849           else
8850             fprintf (outfile, "<null>");
8851           break;
8852         case dw_val_class_file:
8853           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8854                    AT_file (a)->emitted_number);
8855           break;
8856         case dw_val_class_data8:
8857           {
8858             int i;
8859
8860             for (i = 0; i < 8; i++)
8861               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8862             break;
8863           }
8864         default:
8865           break;
8866         }
8867
8868       fprintf (outfile, "\n");
8869     }
8870
8871   if (die->die_child != NULL)
8872     {
8873       print_indent += 4;
8874       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8875       print_indent -= 4;
8876     }
8877   if (print_indent == 0)
8878     fprintf (outfile, "\n");
8879 }
8880
8881 /* Print the information collected for a given DIE.  */
8882
8883 DEBUG_FUNCTION void
8884 debug_dwarf_die (dw_die_ref die)
8885 {
8886   print_die (die, stderr);
8887 }
8888
8889 /* Print all DWARF information collected for the compilation unit.
8890    This routine is a debugging aid only.  */
8891
8892 DEBUG_FUNCTION void
8893 debug_dwarf (void)
8894 {
8895   print_indent = 0;
8896   print_die (comp_unit_die (), stderr);
8897 }
8898 \f
8899 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8900    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8901    DIE that marks the start of the DIEs for this include file.  */
8902
8903 static dw_die_ref
8904 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8905 {
8906   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8907   dw_die_ref new_unit = gen_compile_unit_die (filename);
8908
8909   new_unit->die_sib = old_unit;
8910   return new_unit;
8911 }
8912
8913 /* Close an include-file CU and reopen the enclosing one.  */
8914
8915 static dw_die_ref
8916 pop_compile_unit (dw_die_ref old_unit)
8917 {
8918   dw_die_ref new_unit = old_unit->die_sib;
8919
8920   old_unit->die_sib = NULL;
8921   return new_unit;
8922 }
8923
8924 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8925 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8926
8927 /* Calculate the checksum of a location expression.  */
8928
8929 static inline void
8930 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8931 {
8932   int tem;
8933
8934   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8935   CHECKSUM (tem);
8936   CHECKSUM (loc->dw_loc_oprnd1);
8937   CHECKSUM (loc->dw_loc_oprnd2);
8938 }
8939
8940 /* Calculate the checksum of an attribute.  */
8941
8942 static void
8943 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8944 {
8945   dw_loc_descr_ref loc;
8946   rtx r;
8947
8948   CHECKSUM (at->dw_attr);
8949
8950   /* We don't care that this was compiled with a different compiler
8951      snapshot; if the output is the same, that's what matters.  */
8952   if (at->dw_attr == DW_AT_producer)
8953     return;
8954
8955   switch (AT_class (at))
8956     {
8957     case dw_val_class_const:
8958       CHECKSUM (at->dw_attr_val.v.val_int);
8959       break;
8960     case dw_val_class_unsigned_const:
8961       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8962       break;
8963     case dw_val_class_const_double:
8964       CHECKSUM (at->dw_attr_val.v.val_double);
8965       break;
8966     case dw_val_class_vec:
8967       CHECKSUM (at->dw_attr_val.v.val_vec);
8968       break;
8969     case dw_val_class_flag:
8970       CHECKSUM (at->dw_attr_val.v.val_flag);
8971       break;
8972     case dw_val_class_str:
8973       CHECKSUM_STRING (AT_string (at));
8974       break;
8975
8976     case dw_val_class_addr:
8977       r = AT_addr (at);
8978       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8979       CHECKSUM_STRING (XSTR (r, 0));
8980       break;
8981
8982     case dw_val_class_offset:
8983       CHECKSUM (at->dw_attr_val.v.val_offset);
8984       break;
8985
8986     case dw_val_class_loc:
8987       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8988         loc_checksum (loc, ctx);
8989       break;
8990
8991     case dw_val_class_die_ref:
8992       die_checksum (AT_ref (at), ctx, mark);
8993       break;
8994
8995     case dw_val_class_fde_ref:
8996     case dw_val_class_vms_delta:
8997     case dw_val_class_lbl_id:
8998     case dw_val_class_lineptr:
8999     case dw_val_class_macptr:
9000       break;
9001
9002     case dw_val_class_file:
9003       CHECKSUM_STRING (AT_file (at)->filename);
9004       break;
9005
9006     case dw_val_class_data8:
9007       CHECKSUM (at->dw_attr_val.v.val_data8);
9008       break;
9009
9010     default:
9011       break;
9012     }
9013 }
9014
9015 /* Calculate the checksum of a DIE.  */
9016
9017 static void
9018 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9019 {
9020   dw_die_ref c;
9021   dw_attr_ref a;
9022   unsigned ix;
9023
9024   /* To avoid infinite recursion.  */
9025   if (die->die_mark)
9026     {
9027       CHECKSUM (die->die_mark);
9028       return;
9029     }
9030   die->die_mark = ++(*mark);
9031
9032   CHECKSUM (die->die_tag);
9033
9034   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9035     attr_checksum (a, ctx, mark);
9036
9037   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
9038 }
9039
9040 #undef CHECKSUM
9041 #undef CHECKSUM_STRING
9042
9043 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
9044 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
9045 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
9046 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
9047 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
9048 #define CHECKSUM_ATTR(FOO) \
9049   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
9050
9051 /* Calculate the checksum of a number in signed LEB128 format.  */
9052
9053 static void
9054 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
9055 {
9056   unsigned char byte;
9057   bool more;
9058
9059   while (1)
9060     {
9061       byte = (value & 0x7f);
9062       value >>= 7;
9063       more = !((value == 0 && (byte & 0x40) == 0)
9064                 || (value == -1 && (byte & 0x40) != 0));
9065       if (more)
9066         byte |= 0x80;
9067       CHECKSUM (byte);
9068       if (!more)
9069         break;
9070     }
9071 }
9072
9073 /* Calculate the checksum of a number in unsigned LEB128 format.  */
9074
9075 static void
9076 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
9077 {
9078   while (1)
9079     {
9080       unsigned char byte = (value & 0x7f);
9081       value >>= 7;
9082       if (value != 0)
9083         /* More bytes to follow.  */
9084         byte |= 0x80;
9085       CHECKSUM (byte);
9086       if (value == 0)
9087         break;
9088     }
9089 }
9090
9091 /* Checksum the context of the DIE.  This adds the names of any
9092    surrounding namespaces or structures to the checksum.  */
9093
9094 static void
9095 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
9096 {
9097   const char *name;
9098   dw_die_ref spec;
9099   int tag = die->die_tag;
9100
9101   if (tag != DW_TAG_namespace
9102       && tag != DW_TAG_structure_type
9103       && tag != DW_TAG_class_type)
9104     return;
9105
9106   name = get_AT_string (die, DW_AT_name);
9107
9108   spec = get_AT_ref (die, DW_AT_specification);
9109   if (spec != NULL)
9110     die = spec;
9111
9112   if (die->die_parent != NULL)
9113     checksum_die_context (die->die_parent, ctx);
9114
9115   CHECKSUM_ULEB128 ('C');
9116   CHECKSUM_ULEB128 (tag);
9117   if (name != NULL)
9118     CHECKSUM_STRING (name);
9119 }
9120
9121 /* Calculate the checksum of a location expression.  */
9122
9123 static inline void
9124 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
9125 {
9126   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
9127      were emitted as a DW_FORM_sdata instead of a location expression.  */
9128   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
9129     {
9130       CHECKSUM_ULEB128 (DW_FORM_sdata);
9131       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
9132       return;
9133     }
9134
9135   /* Otherwise, just checksum the raw location expression.  */
9136   while (loc != NULL)
9137     {
9138       CHECKSUM_ULEB128 (loc->dw_loc_opc);
9139       CHECKSUM (loc->dw_loc_oprnd1);
9140       CHECKSUM (loc->dw_loc_oprnd2);
9141       loc = loc->dw_loc_next;
9142     }
9143 }
9144
9145 /* Calculate the checksum of an attribute.  */
9146
9147 static void
9148 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
9149                        struct md5_ctx *ctx, int *mark)
9150 {
9151   dw_loc_descr_ref loc;
9152   rtx r;
9153
9154   if (AT_class (at) == dw_val_class_die_ref)
9155     {
9156       dw_die_ref target_die = AT_ref (at);
9157
9158       /* For pointer and reference types, we checksum only the (qualified)
9159          name of the target type (if there is a name).  For friend entries,
9160          we checksum only the (qualified) name of the target type or function.
9161          This allows the checksum to remain the same whether the target type
9162          is complete or not.  */
9163       if ((at->dw_attr == DW_AT_type
9164            && (tag == DW_TAG_pointer_type
9165                || tag == DW_TAG_reference_type
9166                || tag == DW_TAG_rvalue_reference_type
9167                || tag == DW_TAG_ptr_to_member_type))
9168           || (at->dw_attr == DW_AT_friend
9169               && tag == DW_TAG_friend))
9170         {
9171           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9172
9173           if (name_attr != NULL)
9174             {
9175               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9176
9177               if (decl == NULL)
9178                 decl = target_die;
9179               CHECKSUM_ULEB128 ('N');
9180               CHECKSUM_ULEB128 (at->dw_attr);
9181               if (decl->die_parent != NULL)
9182                 checksum_die_context (decl->die_parent, ctx);
9183               CHECKSUM_ULEB128 ('E');
9184               CHECKSUM_STRING (AT_string (name_attr));
9185               return;
9186             }
9187         }
9188
9189       /* For all other references to another DIE, we check to see if the
9190          target DIE has already been visited.  If it has, we emit a
9191          backward reference; if not, we descend recursively.  */
9192       if (target_die->die_mark > 0)
9193         {
9194           CHECKSUM_ULEB128 ('R');
9195           CHECKSUM_ULEB128 (at->dw_attr);
9196           CHECKSUM_ULEB128 (target_die->die_mark);
9197         }
9198       else
9199         {
9200           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9201
9202           if (decl == NULL)
9203             decl = target_die;
9204           target_die->die_mark = ++(*mark);
9205           CHECKSUM_ULEB128 ('T');
9206           CHECKSUM_ULEB128 (at->dw_attr);
9207           if (decl->die_parent != NULL)
9208             checksum_die_context (decl->die_parent, ctx);
9209           die_checksum_ordered (target_die, ctx, mark);
9210         }
9211       return;
9212     }
9213
9214   CHECKSUM_ULEB128 ('A');
9215   CHECKSUM_ULEB128 (at->dw_attr);
9216
9217   switch (AT_class (at))
9218     {
9219     case dw_val_class_const:
9220       CHECKSUM_ULEB128 (DW_FORM_sdata);
9221       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9222       break;
9223
9224     case dw_val_class_unsigned_const:
9225       CHECKSUM_ULEB128 (DW_FORM_sdata);
9226       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9227       break;
9228
9229     case dw_val_class_const_double:
9230       CHECKSUM_ULEB128 (DW_FORM_block);
9231       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9232       CHECKSUM (at->dw_attr_val.v.val_double);
9233       break;
9234
9235     case dw_val_class_vec:
9236       CHECKSUM_ULEB128 (DW_FORM_block);
9237       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9238       CHECKSUM (at->dw_attr_val.v.val_vec);
9239       break;
9240
9241     case dw_val_class_flag:
9242       CHECKSUM_ULEB128 (DW_FORM_flag);
9243       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9244       break;
9245
9246     case dw_val_class_str:
9247       CHECKSUM_ULEB128 (DW_FORM_string);
9248       CHECKSUM_STRING (AT_string (at));
9249       break;
9250
9251     case dw_val_class_addr:
9252       r = AT_addr (at);
9253       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9254       CHECKSUM_ULEB128 (DW_FORM_string);
9255       CHECKSUM_STRING (XSTR (r, 0));
9256       break;
9257
9258     case dw_val_class_offset:
9259       CHECKSUM_ULEB128 (DW_FORM_sdata);
9260       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9261       break;
9262
9263     case dw_val_class_loc:
9264       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9265         loc_checksum_ordered (loc, ctx);
9266       break;
9267
9268     case dw_val_class_fde_ref:
9269     case dw_val_class_lbl_id:
9270     case dw_val_class_lineptr:
9271     case dw_val_class_macptr:
9272       break;
9273
9274     case dw_val_class_file:
9275       CHECKSUM_ULEB128 (DW_FORM_string);
9276       CHECKSUM_STRING (AT_file (at)->filename);
9277       break;
9278
9279     case dw_val_class_data8:
9280       CHECKSUM (at->dw_attr_val.v.val_data8);
9281       break;
9282
9283     default:
9284       break;
9285     }
9286 }
9287
9288 struct checksum_attributes
9289 {
9290   dw_attr_ref at_name;
9291   dw_attr_ref at_type;
9292   dw_attr_ref at_friend;
9293   dw_attr_ref at_accessibility;
9294   dw_attr_ref at_address_class;
9295   dw_attr_ref at_allocated;
9296   dw_attr_ref at_artificial;
9297   dw_attr_ref at_associated;
9298   dw_attr_ref at_binary_scale;
9299   dw_attr_ref at_bit_offset;
9300   dw_attr_ref at_bit_size;
9301   dw_attr_ref at_bit_stride;
9302   dw_attr_ref at_byte_size;
9303   dw_attr_ref at_byte_stride;
9304   dw_attr_ref at_const_value;
9305   dw_attr_ref at_containing_type;
9306   dw_attr_ref at_count;
9307   dw_attr_ref at_data_location;
9308   dw_attr_ref at_data_member_location;
9309   dw_attr_ref at_decimal_scale;
9310   dw_attr_ref at_decimal_sign;
9311   dw_attr_ref at_default_value;
9312   dw_attr_ref at_digit_count;
9313   dw_attr_ref at_discr;
9314   dw_attr_ref at_discr_list;
9315   dw_attr_ref at_discr_value;
9316   dw_attr_ref at_encoding;
9317   dw_attr_ref at_endianity;
9318   dw_attr_ref at_explicit;
9319   dw_attr_ref at_is_optional;
9320   dw_attr_ref at_location;
9321   dw_attr_ref at_lower_bound;
9322   dw_attr_ref at_mutable;
9323   dw_attr_ref at_ordering;
9324   dw_attr_ref at_picture_string;
9325   dw_attr_ref at_prototyped;
9326   dw_attr_ref at_small;
9327   dw_attr_ref at_segment;
9328   dw_attr_ref at_string_length;
9329   dw_attr_ref at_threads_scaled;
9330   dw_attr_ref at_upper_bound;
9331   dw_attr_ref at_use_location;
9332   dw_attr_ref at_use_UTF8;
9333   dw_attr_ref at_variable_parameter;
9334   dw_attr_ref at_virtuality;
9335   dw_attr_ref at_visibility;
9336   dw_attr_ref at_vtable_elem_location;
9337 };
9338
9339 /* Collect the attributes that we will want to use for the checksum.  */
9340
9341 static void
9342 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9343 {
9344   dw_attr_ref a;
9345   unsigned ix;
9346
9347   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9348     {
9349       switch (a->dw_attr)
9350         {
9351         case DW_AT_name:
9352           attrs->at_name = a;
9353           break;
9354         case DW_AT_type:
9355           attrs->at_type = a;
9356           break;
9357         case DW_AT_friend:
9358           attrs->at_friend = a;
9359           break;
9360         case DW_AT_accessibility:
9361           attrs->at_accessibility = a;
9362           break;
9363         case DW_AT_address_class:
9364           attrs->at_address_class = a;
9365           break;
9366         case DW_AT_allocated:
9367           attrs->at_allocated = a;
9368           break;
9369         case DW_AT_artificial:
9370           attrs->at_artificial = a;
9371           break;
9372         case DW_AT_associated:
9373           attrs->at_associated = a;
9374           break;
9375         case DW_AT_binary_scale:
9376           attrs->at_binary_scale = a;
9377           break;
9378         case DW_AT_bit_offset:
9379           attrs->at_bit_offset = a;
9380           break;
9381         case DW_AT_bit_size:
9382           attrs->at_bit_size = a;
9383           break;
9384         case DW_AT_bit_stride:
9385           attrs->at_bit_stride = a;
9386           break;
9387         case DW_AT_byte_size:
9388           attrs->at_byte_size = a;
9389           break;
9390         case DW_AT_byte_stride:
9391           attrs->at_byte_stride = a;
9392           break;
9393         case DW_AT_const_value:
9394           attrs->at_const_value = a;
9395           break;
9396         case DW_AT_containing_type:
9397           attrs->at_containing_type = a;
9398           break;
9399         case DW_AT_count:
9400           attrs->at_count = a;
9401           break;
9402         case DW_AT_data_location:
9403           attrs->at_data_location = a;
9404           break;
9405         case DW_AT_data_member_location:
9406           attrs->at_data_member_location = a;
9407           break;
9408         case DW_AT_decimal_scale:
9409           attrs->at_decimal_scale = a;
9410           break;
9411         case DW_AT_decimal_sign:
9412           attrs->at_decimal_sign = a;
9413           break;
9414         case DW_AT_default_value:
9415           attrs->at_default_value = a;
9416           break;
9417         case DW_AT_digit_count:
9418           attrs->at_digit_count = a;
9419           break;
9420         case DW_AT_discr:
9421           attrs->at_discr = a;
9422           break;
9423         case DW_AT_discr_list:
9424           attrs->at_discr_list = a;
9425           break;
9426         case DW_AT_discr_value:
9427           attrs->at_discr_value = a;
9428           break;
9429         case DW_AT_encoding:
9430           attrs->at_encoding = a;
9431           break;
9432         case DW_AT_endianity:
9433           attrs->at_endianity = a;
9434           break;
9435         case DW_AT_explicit:
9436           attrs->at_explicit = a;
9437           break;
9438         case DW_AT_is_optional:
9439           attrs->at_is_optional = a;
9440           break;
9441         case DW_AT_location:
9442           attrs->at_location = a;
9443           break;
9444         case DW_AT_lower_bound:
9445           attrs->at_lower_bound = a;
9446           break;
9447         case DW_AT_mutable:
9448           attrs->at_mutable = a;
9449           break;
9450         case DW_AT_ordering:
9451           attrs->at_ordering = a;
9452           break;
9453         case DW_AT_picture_string:
9454           attrs->at_picture_string = a;
9455           break;
9456         case DW_AT_prototyped:
9457           attrs->at_prototyped = a;
9458           break;
9459         case DW_AT_small:
9460           attrs->at_small = a;
9461           break;
9462         case DW_AT_segment:
9463           attrs->at_segment = a;
9464           break;
9465         case DW_AT_string_length:
9466           attrs->at_string_length = a;
9467           break;
9468         case DW_AT_threads_scaled:
9469           attrs->at_threads_scaled = a;
9470           break;
9471         case DW_AT_upper_bound:
9472           attrs->at_upper_bound = a;
9473           break;
9474         case DW_AT_use_location:
9475           attrs->at_use_location = a;
9476           break;
9477         case DW_AT_use_UTF8:
9478           attrs->at_use_UTF8 = a;
9479           break;
9480         case DW_AT_variable_parameter:
9481           attrs->at_variable_parameter = a;
9482           break;
9483         case DW_AT_virtuality:
9484           attrs->at_virtuality = a;
9485           break;
9486         case DW_AT_visibility:
9487           attrs->at_visibility = a;
9488           break;
9489         case DW_AT_vtable_elem_location:
9490           attrs->at_vtable_elem_location = a;
9491           break;
9492         default:
9493           break;
9494         }
9495     }
9496 }
9497
9498 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9499
9500 static void
9501 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9502 {
9503   dw_die_ref c;
9504   dw_die_ref decl;
9505   struct checksum_attributes attrs;
9506
9507   CHECKSUM_ULEB128 ('D');
9508   CHECKSUM_ULEB128 (die->die_tag);
9509
9510   memset (&attrs, 0, sizeof (attrs));
9511
9512   decl = get_AT_ref (die, DW_AT_specification);
9513   if (decl != NULL)
9514     collect_checksum_attributes (&attrs, decl);
9515   collect_checksum_attributes (&attrs, die);
9516
9517   CHECKSUM_ATTR (attrs.at_name);
9518   CHECKSUM_ATTR (attrs.at_accessibility);
9519   CHECKSUM_ATTR (attrs.at_address_class);
9520   CHECKSUM_ATTR (attrs.at_allocated);
9521   CHECKSUM_ATTR (attrs.at_artificial);
9522   CHECKSUM_ATTR (attrs.at_associated);
9523   CHECKSUM_ATTR (attrs.at_binary_scale);
9524   CHECKSUM_ATTR (attrs.at_bit_offset);
9525   CHECKSUM_ATTR (attrs.at_bit_size);
9526   CHECKSUM_ATTR (attrs.at_bit_stride);
9527   CHECKSUM_ATTR (attrs.at_byte_size);
9528   CHECKSUM_ATTR (attrs.at_byte_stride);
9529   CHECKSUM_ATTR (attrs.at_const_value);
9530   CHECKSUM_ATTR (attrs.at_containing_type);
9531   CHECKSUM_ATTR (attrs.at_count);
9532   CHECKSUM_ATTR (attrs.at_data_location);
9533   CHECKSUM_ATTR (attrs.at_data_member_location);
9534   CHECKSUM_ATTR (attrs.at_decimal_scale);
9535   CHECKSUM_ATTR (attrs.at_decimal_sign);
9536   CHECKSUM_ATTR (attrs.at_default_value);
9537   CHECKSUM_ATTR (attrs.at_digit_count);
9538   CHECKSUM_ATTR (attrs.at_discr);
9539   CHECKSUM_ATTR (attrs.at_discr_list);
9540   CHECKSUM_ATTR (attrs.at_discr_value);
9541   CHECKSUM_ATTR (attrs.at_encoding);
9542   CHECKSUM_ATTR (attrs.at_endianity);
9543   CHECKSUM_ATTR (attrs.at_explicit);
9544   CHECKSUM_ATTR (attrs.at_is_optional);
9545   CHECKSUM_ATTR (attrs.at_location);
9546   CHECKSUM_ATTR (attrs.at_lower_bound);
9547   CHECKSUM_ATTR (attrs.at_mutable);
9548   CHECKSUM_ATTR (attrs.at_ordering);
9549   CHECKSUM_ATTR (attrs.at_picture_string);
9550   CHECKSUM_ATTR (attrs.at_prototyped);
9551   CHECKSUM_ATTR (attrs.at_small);
9552   CHECKSUM_ATTR (attrs.at_segment);
9553   CHECKSUM_ATTR (attrs.at_string_length);
9554   CHECKSUM_ATTR (attrs.at_threads_scaled);
9555   CHECKSUM_ATTR (attrs.at_upper_bound);
9556   CHECKSUM_ATTR (attrs.at_use_location);
9557   CHECKSUM_ATTR (attrs.at_use_UTF8);
9558   CHECKSUM_ATTR (attrs.at_variable_parameter);
9559   CHECKSUM_ATTR (attrs.at_virtuality);
9560   CHECKSUM_ATTR (attrs.at_visibility);
9561   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9562   CHECKSUM_ATTR (attrs.at_type);
9563   CHECKSUM_ATTR (attrs.at_friend);
9564
9565   /* Checksum the child DIEs, except for nested types and member functions.  */
9566   c = die->die_child;
9567   if (c) do {
9568     dw_attr_ref name_attr;
9569
9570     c = c->die_sib;
9571     name_attr = get_AT (c, DW_AT_name);
9572     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9573         && name_attr != NULL)
9574       {
9575         CHECKSUM_ULEB128 ('S');
9576         CHECKSUM_ULEB128 (c->die_tag);
9577         CHECKSUM_STRING (AT_string (name_attr));
9578       }
9579     else
9580       {
9581         /* Mark this DIE so it gets processed when unmarking.  */
9582         if (c->die_mark == 0)
9583           c->die_mark = -1;
9584         die_checksum_ordered (c, ctx, mark);
9585       }
9586   } while (c != die->die_child);
9587
9588   CHECKSUM_ULEB128 (0);
9589 }
9590
9591 #undef CHECKSUM
9592 #undef CHECKSUM_STRING
9593 #undef CHECKSUM_ATTR
9594 #undef CHECKSUM_LEB128
9595 #undef CHECKSUM_ULEB128
9596
9597 /* Generate the type signature for DIE.  This is computed by generating an
9598    MD5 checksum over the DIE's tag, its relevant attributes, and its
9599    children.  Attributes that are references to other DIEs are processed
9600    by recursion, using the MARK field to prevent infinite recursion.
9601    If the DIE is nested inside a namespace or another type, we also
9602    need to include that context in the signature.  The lower 64 bits
9603    of the resulting MD5 checksum comprise the signature.  */
9604
9605 static void
9606 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9607 {
9608   int mark;
9609   const char *name;
9610   unsigned char checksum[16];
9611   struct md5_ctx ctx;
9612   dw_die_ref decl;
9613
9614   name = get_AT_string (die, DW_AT_name);
9615   decl = get_AT_ref (die, DW_AT_specification);
9616
9617   /* First, compute a signature for just the type name (and its surrounding
9618      context, if any.  This is stored in the type unit DIE for link-time
9619      ODR (one-definition rule) checking.  */
9620
9621   if (is_cxx() && name != NULL)
9622     {
9623       md5_init_ctx (&ctx);
9624
9625       /* Checksum the names of surrounding namespaces and structures.  */
9626       if (decl != NULL && decl->die_parent != NULL)
9627         checksum_die_context (decl->die_parent, &ctx);
9628
9629       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9630       md5_process_bytes (name, strlen (name) + 1, &ctx);
9631       md5_finish_ctx (&ctx, checksum);
9632
9633       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9634     }
9635
9636   /* Next, compute the complete type signature.  */
9637
9638   md5_init_ctx (&ctx);
9639   mark = 1;
9640   die->die_mark = mark;
9641
9642   /* Checksum the names of surrounding namespaces and structures.  */
9643   if (decl != NULL && decl->die_parent != NULL)
9644     checksum_die_context (decl->die_parent, &ctx);
9645
9646   /* Checksum the DIE and its children.  */
9647   die_checksum_ordered (die, &ctx, &mark);
9648   unmark_all_dies (die);
9649   md5_finish_ctx (&ctx, checksum);
9650
9651   /* Store the signature in the type node and link the type DIE and the
9652      type node together.  */
9653   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9654           DWARF_TYPE_SIGNATURE_SIZE);
9655   die->die_id.die_type_node = type_node;
9656   type_node->type_die = die;
9657
9658   /* If the DIE is a specification, link its declaration to the type node
9659      as well.  */
9660   if (decl != NULL)
9661     decl->die_id.die_type_node = type_node;
9662 }
9663
9664 /* Do the location expressions look same?  */
9665 static inline int
9666 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9667 {
9668   return loc1->dw_loc_opc == loc2->dw_loc_opc
9669          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9670          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9671 }
9672
9673 /* Do the values look the same?  */
9674 static int
9675 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9676 {
9677   dw_loc_descr_ref loc1, loc2;
9678   rtx r1, r2;
9679
9680   if (v1->val_class != v2->val_class)
9681     return 0;
9682
9683   switch (v1->val_class)
9684     {
9685     case dw_val_class_const:
9686       return v1->v.val_int == v2->v.val_int;
9687     case dw_val_class_unsigned_const:
9688       return v1->v.val_unsigned == v2->v.val_unsigned;
9689     case dw_val_class_const_double:
9690       return v1->v.val_double.high == v2->v.val_double.high
9691              && v1->v.val_double.low == v2->v.val_double.low;
9692     case dw_val_class_vec:
9693       if (v1->v.val_vec.length != v2->v.val_vec.length
9694           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9695         return 0;
9696       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9697                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9698         return 0;
9699       return 1;
9700     case dw_val_class_flag:
9701       return v1->v.val_flag == v2->v.val_flag;
9702     case dw_val_class_str:
9703       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9704
9705     case dw_val_class_addr:
9706       r1 = v1->v.val_addr;
9707       r2 = v2->v.val_addr;
9708       if (GET_CODE (r1) != GET_CODE (r2))
9709         return 0;
9710       return !rtx_equal_p (r1, r2);
9711
9712     case dw_val_class_offset:
9713       return v1->v.val_offset == v2->v.val_offset;
9714
9715     case dw_val_class_loc:
9716       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9717            loc1 && loc2;
9718            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9719         if (!same_loc_p (loc1, loc2, mark))
9720           return 0;
9721       return !loc1 && !loc2;
9722
9723     case dw_val_class_die_ref:
9724       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9725
9726     case dw_val_class_fde_ref:
9727     case dw_val_class_vms_delta:
9728     case dw_val_class_lbl_id:
9729     case dw_val_class_lineptr:
9730     case dw_val_class_macptr:
9731       return 1;
9732
9733     case dw_val_class_file:
9734       return v1->v.val_file == v2->v.val_file;
9735
9736     case dw_val_class_data8:
9737       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9738
9739     default:
9740       return 1;
9741     }
9742 }
9743
9744 /* Do the attributes look the same?  */
9745
9746 static int
9747 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9748 {
9749   if (at1->dw_attr != at2->dw_attr)
9750     return 0;
9751
9752   /* We don't care that this was compiled with a different compiler
9753      snapshot; if the output is the same, that's what matters. */
9754   if (at1->dw_attr == DW_AT_producer)
9755     return 1;
9756
9757   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9758 }
9759
9760 /* Do the dies look the same?  */
9761
9762 static int
9763 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9764 {
9765   dw_die_ref c1, c2;
9766   dw_attr_ref a1;
9767   unsigned ix;
9768
9769   /* To avoid infinite recursion.  */
9770   if (die1->die_mark)
9771     return die1->die_mark == die2->die_mark;
9772   die1->die_mark = die2->die_mark = ++(*mark);
9773
9774   if (die1->die_tag != die2->die_tag)
9775     return 0;
9776
9777   if (VEC_length (dw_attr_node, die1->die_attr)
9778       != VEC_length (dw_attr_node, die2->die_attr))
9779     return 0;
9780
9781   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9782     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9783       return 0;
9784
9785   c1 = die1->die_child;
9786   c2 = die2->die_child;
9787   if (! c1)
9788     {
9789       if (c2)
9790         return 0;
9791     }
9792   else
9793     for (;;)
9794       {
9795         if (!same_die_p (c1, c2, mark))
9796           return 0;
9797         c1 = c1->die_sib;
9798         c2 = c2->die_sib;
9799         if (c1 == die1->die_child)
9800           {
9801             if (c2 == die2->die_child)
9802               break;
9803             else
9804               return 0;
9805           }
9806     }
9807
9808   return 1;
9809 }
9810
9811 /* Do the dies look the same?  Wrapper around same_die_p.  */
9812
9813 static int
9814 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9815 {
9816   int mark = 0;
9817   int ret = same_die_p (die1, die2, &mark);
9818
9819   unmark_all_dies (die1);
9820   unmark_all_dies (die2);
9821
9822   return ret;
9823 }
9824
9825 /* The prefix to attach to symbols on DIEs in the current comdat debug
9826    info section.  */
9827 static char *comdat_symbol_id;
9828
9829 /* The index of the current symbol within the current comdat CU.  */
9830 static unsigned int comdat_symbol_number;
9831
9832 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9833    children, and set comdat_symbol_id accordingly.  */
9834
9835 static void
9836 compute_section_prefix (dw_die_ref unit_die)
9837 {
9838   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9839   const char *base = die_name ? lbasename (die_name) : "anonymous";
9840   char *name = XALLOCAVEC (char, strlen (base) + 64);
9841   char *p;
9842   int i, mark;
9843   unsigned char checksum[16];
9844   struct md5_ctx ctx;
9845
9846   /* Compute the checksum of the DIE, then append part of it as hex digits to
9847      the name filename of the unit.  */
9848
9849   md5_init_ctx (&ctx);
9850   mark = 0;
9851   die_checksum (unit_die, &ctx, &mark);
9852   unmark_all_dies (unit_die);
9853   md5_finish_ctx (&ctx, checksum);
9854
9855   sprintf (name, "%s.", base);
9856   clean_symbol_name (name);
9857
9858   p = name + strlen (name);
9859   for (i = 0; i < 4; i++)
9860     {
9861       sprintf (p, "%.2x", checksum[i]);
9862       p += 2;
9863     }
9864
9865   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9866   comdat_symbol_number = 0;
9867 }
9868
9869 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9870
9871 static int
9872 is_type_die (dw_die_ref die)
9873 {
9874   switch (die->die_tag)
9875     {
9876     case DW_TAG_array_type:
9877     case DW_TAG_class_type:
9878     case DW_TAG_interface_type:
9879     case DW_TAG_enumeration_type:
9880     case DW_TAG_pointer_type:
9881     case DW_TAG_reference_type:
9882     case DW_TAG_rvalue_reference_type:
9883     case DW_TAG_string_type:
9884     case DW_TAG_structure_type:
9885     case DW_TAG_subroutine_type:
9886     case DW_TAG_union_type:
9887     case DW_TAG_ptr_to_member_type:
9888     case DW_TAG_set_type:
9889     case DW_TAG_subrange_type:
9890     case DW_TAG_base_type:
9891     case DW_TAG_const_type:
9892     case DW_TAG_file_type:
9893     case DW_TAG_packed_type:
9894     case DW_TAG_volatile_type:
9895     case DW_TAG_typedef:
9896       return 1;
9897     default:
9898       return 0;
9899     }
9900 }
9901
9902 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9903    Basically, we want to choose the bits that are likely to be shared between
9904    compilations (types) and leave out the bits that are specific to individual
9905    compilations (functions).  */
9906
9907 static int
9908 is_comdat_die (dw_die_ref c)
9909 {
9910   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9911      we do for stabs.  The advantage is a greater likelihood of sharing between
9912      objects that don't include headers in the same order (and therefore would
9913      put the base types in a different comdat).  jason 8/28/00 */
9914
9915   if (c->die_tag == DW_TAG_base_type)
9916     return 0;
9917
9918   if (c->die_tag == DW_TAG_pointer_type
9919       || c->die_tag == DW_TAG_reference_type
9920       || c->die_tag == DW_TAG_rvalue_reference_type
9921       || c->die_tag == DW_TAG_const_type
9922       || c->die_tag == DW_TAG_volatile_type)
9923     {
9924       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9925
9926       return t ? is_comdat_die (t) : 0;
9927     }
9928
9929   return is_type_die (c);
9930 }
9931
9932 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9933    compilation unit.  */
9934
9935 static int
9936 is_symbol_die (dw_die_ref c)
9937 {
9938   return (is_type_die (c)
9939           || is_declaration_die (c)
9940           || c->die_tag == DW_TAG_namespace
9941           || c->die_tag == DW_TAG_module);
9942 }
9943
9944 /* Returns true iff C is a compile-unit DIE.  */
9945
9946 static inline bool
9947 is_cu_die (dw_die_ref c)
9948 {
9949   return c && c->die_tag == DW_TAG_compile_unit;
9950 }
9951
9952 static char *
9953 gen_internal_sym (const char *prefix)
9954 {
9955   char buf[256];
9956
9957   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9958   return xstrdup (buf);
9959 }
9960
9961 /* Assign symbols to all worthy DIEs under DIE.  */
9962
9963 static void
9964 assign_symbol_names (dw_die_ref die)
9965 {
9966   dw_die_ref c;
9967
9968   if (is_symbol_die (die))
9969     {
9970       if (comdat_symbol_id)
9971         {
9972           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9973
9974           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9975                    comdat_symbol_id, comdat_symbol_number++);
9976           die->die_id.die_symbol = xstrdup (p);
9977         }
9978       else
9979         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9980     }
9981
9982   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9983 }
9984
9985 struct cu_hash_table_entry
9986 {
9987   dw_die_ref cu;
9988   unsigned min_comdat_num, max_comdat_num;
9989   struct cu_hash_table_entry *next;
9990 };
9991
9992 /* Routines to manipulate hash table of CUs.  */
9993 static hashval_t
9994 htab_cu_hash (const void *of)
9995 {
9996   const struct cu_hash_table_entry *const entry =
9997     (const struct cu_hash_table_entry *) of;
9998
9999   return htab_hash_string (entry->cu->die_id.die_symbol);
10000 }
10001
10002 static int
10003 htab_cu_eq (const void *of1, const void *of2)
10004 {
10005   const struct cu_hash_table_entry *const entry1 =
10006     (const struct cu_hash_table_entry *) of1;
10007   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10008
10009   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
10010 }
10011
10012 static void
10013 htab_cu_del (void *what)
10014 {
10015   struct cu_hash_table_entry *next,
10016     *entry = (struct cu_hash_table_entry *) what;
10017
10018   while (entry)
10019     {
10020       next = entry->next;
10021       free (entry);
10022       entry = next;
10023     }
10024 }
10025
10026 /* Check whether we have already seen this CU and set up SYM_NUM
10027    accordingly.  */
10028 static int
10029 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
10030 {
10031   struct cu_hash_table_entry dummy;
10032   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
10033
10034   dummy.max_comdat_num = 0;
10035
10036   slot = (struct cu_hash_table_entry **)
10037     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10038         INSERT);
10039   entry = *slot;
10040
10041   for (; entry; last = entry, entry = entry->next)
10042     {
10043       if (same_die_p_wrap (cu, entry->cu))
10044         break;
10045     }
10046
10047   if (entry)
10048     {
10049       *sym_num = entry->min_comdat_num;
10050       return 1;
10051     }
10052
10053   entry = XCNEW (struct cu_hash_table_entry);
10054   entry->cu = cu;
10055   entry->min_comdat_num = *sym_num = last->max_comdat_num;
10056   entry->next = *slot;
10057   *slot = entry;
10058
10059   return 0;
10060 }
10061
10062 /* Record SYM_NUM to record of CU in HTABLE.  */
10063 static void
10064 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
10065 {
10066   struct cu_hash_table_entry **slot, *entry;
10067
10068   slot = (struct cu_hash_table_entry **)
10069     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10070         NO_INSERT);
10071   entry = *slot;
10072
10073   entry->max_comdat_num = sym_num;
10074 }
10075
10076 /* Traverse the DIE (which is always comp_unit_die), and set up
10077    additional compilation units for each of the include files we see
10078    bracketed by BINCL/EINCL.  */
10079
10080 static void
10081 break_out_includes (dw_die_ref die)
10082 {
10083   dw_die_ref c;
10084   dw_die_ref unit = NULL;
10085   limbo_die_node *node, **pnode;
10086   htab_t cu_hash_table;
10087
10088   c = die->die_child;
10089   if (c) do {
10090     dw_die_ref prev = c;
10091     c = c->die_sib;
10092     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
10093            || (unit && is_comdat_die (c)))
10094       {
10095         dw_die_ref next = c->die_sib;
10096
10097         /* This DIE is for a secondary CU; remove it from the main one.  */
10098         remove_child_with_prev (c, prev);
10099
10100         if (c->die_tag == DW_TAG_GNU_BINCL)
10101           unit = push_new_compile_unit (unit, c);
10102         else if (c->die_tag == DW_TAG_GNU_EINCL)
10103           unit = pop_compile_unit (unit);
10104         else
10105           add_child_die (unit, c);
10106         c = next;
10107         if (c == die->die_child)
10108           break;
10109       }
10110   } while (c != die->die_child);
10111
10112 #if 0
10113   /* We can only use this in debugging, since the frontend doesn't check
10114      to make sure that we leave every include file we enter.  */
10115   gcc_assert (!unit);
10116 #endif
10117
10118   assign_symbol_names (die);
10119   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
10120   for (node = limbo_die_list, pnode = &limbo_die_list;
10121        node;
10122        node = node->next)
10123     {
10124       int is_dupl;
10125
10126       compute_section_prefix (node->die);
10127       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
10128                         &comdat_symbol_number);
10129       assign_symbol_names (node->die);
10130       if (is_dupl)
10131         *pnode = node->next;
10132       else
10133         {
10134           pnode = &node->next;
10135           record_comdat_symbol_number (node->die, cu_hash_table,
10136                 comdat_symbol_number);
10137         }
10138     }
10139   htab_delete (cu_hash_table);
10140 }
10141
10142 /* Return non-zero if this DIE is a declaration.  */
10143
10144 static int
10145 is_declaration_die (dw_die_ref die)
10146 {
10147   dw_attr_ref a;
10148   unsigned ix;
10149
10150   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10151     if (a->dw_attr == DW_AT_declaration)
10152       return 1;
10153
10154   return 0;
10155 }
10156
10157 /* Return non-zero if this DIE is nested inside a subprogram.  */
10158
10159 static int
10160 is_nested_in_subprogram (dw_die_ref die)
10161 {
10162   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
10163
10164   if (decl == NULL)
10165     decl = die;
10166   return local_scope_p (decl);
10167 }
10168
10169 /* Return non-zero if this is a type DIE that should be moved to a
10170    COMDAT .debug_types section.  */
10171
10172 static int
10173 should_move_die_to_comdat (dw_die_ref die)
10174 {
10175   switch (die->die_tag)
10176     {
10177     case DW_TAG_class_type:
10178     case DW_TAG_structure_type:
10179     case DW_TAG_enumeration_type:
10180     case DW_TAG_union_type:
10181       /* Don't move declarations, inlined instances, or types nested in a
10182          subprogram.  */
10183       if (is_declaration_die (die)
10184           || get_AT (die, DW_AT_abstract_origin)
10185           || is_nested_in_subprogram (die))
10186         return 0;
10187       return 1;
10188     case DW_TAG_array_type:
10189     case DW_TAG_interface_type:
10190     case DW_TAG_pointer_type:
10191     case DW_TAG_reference_type:
10192     case DW_TAG_rvalue_reference_type:
10193     case DW_TAG_string_type:
10194     case DW_TAG_subroutine_type:
10195     case DW_TAG_ptr_to_member_type:
10196     case DW_TAG_set_type:
10197     case DW_TAG_subrange_type:
10198     case DW_TAG_base_type:
10199     case DW_TAG_const_type:
10200     case DW_TAG_file_type:
10201     case DW_TAG_packed_type:
10202     case DW_TAG_volatile_type:
10203     case DW_TAG_typedef:
10204     default:
10205       return 0;
10206     }
10207 }
10208
10209 /* Make a clone of DIE.  */
10210
10211 static dw_die_ref
10212 clone_die (dw_die_ref die)
10213 {
10214   dw_die_ref clone;
10215   dw_attr_ref a;
10216   unsigned ix;
10217
10218   clone = ggc_alloc_cleared_die_node ();
10219   clone->die_tag = die->die_tag;
10220
10221   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10222     add_dwarf_attr (clone, a);
10223
10224   return clone;
10225 }
10226
10227 /* Make a clone of the tree rooted at DIE.  */
10228
10229 static dw_die_ref
10230 clone_tree (dw_die_ref die)
10231 {
10232   dw_die_ref c;
10233   dw_die_ref clone = clone_die (die);
10234
10235   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10236
10237   return clone;
10238 }
10239
10240 /* Make a clone of DIE as a declaration.  */
10241
10242 static dw_die_ref
10243 clone_as_declaration (dw_die_ref die)
10244 {
10245   dw_die_ref clone;
10246   dw_die_ref decl;
10247   dw_attr_ref a;
10248   unsigned ix;
10249
10250   /* If the DIE is already a declaration, just clone it.  */
10251   if (is_declaration_die (die))
10252     return clone_die (die);
10253
10254   /* If the DIE is a specification, just clone its declaration DIE.  */
10255   decl = get_AT_ref (die, DW_AT_specification);
10256   if (decl != NULL)
10257     return clone_die (decl);
10258
10259   clone = ggc_alloc_cleared_die_node ();
10260   clone->die_tag = die->die_tag;
10261
10262   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10263     {
10264       /* We don't want to copy over all attributes.
10265          For example we don't want DW_AT_byte_size because otherwise we will no
10266          longer have a declaration and GDB will treat it as a definition.  */
10267
10268       switch (a->dw_attr)
10269         {
10270         case DW_AT_artificial:
10271         case DW_AT_containing_type:
10272         case DW_AT_external:
10273         case DW_AT_name:
10274         case DW_AT_type:
10275         case DW_AT_virtuality:
10276         case DW_AT_linkage_name:
10277         case DW_AT_MIPS_linkage_name:
10278           add_dwarf_attr (clone, a);
10279           break;
10280         case DW_AT_byte_size:
10281         default:
10282           break;
10283         }
10284     }
10285
10286   if (die->die_id.die_type_node)
10287     add_AT_die_ref (clone, DW_AT_signature, die);
10288
10289   add_AT_flag (clone, DW_AT_declaration, 1);
10290   return clone;
10291 }
10292
10293 /* Copy the declaration context to the new compile unit DIE.  This includes
10294    any surrounding namespace or type declarations.  If the DIE has an
10295    AT_specification attribute, it also includes attributes and children
10296    attached to the specification.  */
10297
10298 static void
10299 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10300 {
10301   dw_die_ref decl;
10302   dw_die_ref new_decl;
10303
10304   decl = get_AT_ref (die, DW_AT_specification);
10305   if (decl == NULL)
10306     decl = die;
10307   else
10308     {
10309       unsigned ix;
10310       dw_die_ref c;
10311       dw_attr_ref a;
10312
10313       /* Copy the type node pointer from the new DIE to the original
10314          declaration DIE so we can forward references later.  */
10315       decl->die_id.die_type_node = die->die_id.die_type_node;
10316
10317       remove_AT (die, DW_AT_specification);
10318
10319       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10320         {
10321           if (a->dw_attr != DW_AT_name
10322               && a->dw_attr != DW_AT_declaration
10323               && a->dw_attr != DW_AT_external)
10324             add_dwarf_attr (die, a);
10325         }
10326
10327       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10328     }
10329
10330   if (decl->die_parent != NULL
10331       && decl->die_parent->die_tag != DW_TAG_compile_unit
10332       && decl->die_parent->die_tag != DW_TAG_type_unit)
10333     {
10334       new_decl = copy_ancestor_tree (unit, decl, NULL);
10335       if (new_decl != NULL)
10336         {
10337           remove_AT (new_decl, DW_AT_signature);
10338           add_AT_specification (die, new_decl);
10339         }
10340     }
10341 }
10342
10343 /* Generate the skeleton ancestor tree for the given NODE, then clone
10344    the DIE and add the clone into the tree.  */
10345
10346 static void
10347 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10348 {
10349   if (node->new_die != NULL)
10350     return;
10351
10352   node->new_die = clone_as_declaration (node->old_die);
10353
10354   if (node->parent != NULL)
10355     {
10356       generate_skeleton_ancestor_tree (node->parent);
10357       add_child_die (node->parent->new_die, node->new_die);
10358     }
10359 }
10360
10361 /* Generate a skeleton tree of DIEs containing any declarations that are
10362    found in the original tree.  We traverse the tree looking for declaration
10363    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10364
10365 static void
10366 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10367 {
10368   skeleton_chain_node node;
10369   dw_die_ref c;
10370   dw_die_ref first;
10371   dw_die_ref prev = NULL;
10372   dw_die_ref next = NULL;
10373
10374   node.parent = parent;
10375
10376   first = c = parent->old_die->die_child;
10377   if (c)
10378     next = c->die_sib;
10379   if (c) do {
10380     if (prev == NULL || prev->die_sib == c)
10381       prev = c;
10382     c = next;
10383     next = (c == first ? NULL : c->die_sib);
10384     node.old_die = c;
10385     node.new_die = NULL;
10386     if (is_declaration_die (c))
10387       {
10388         /* Clone the existing DIE, move the original to the skeleton
10389            tree (which is in the main CU), and put the clone, with
10390            all the original's children, where the original came from.  */
10391         dw_die_ref clone = clone_die (c);
10392         move_all_children (c, clone);
10393
10394         replace_child (c, clone, prev);
10395         generate_skeleton_ancestor_tree (parent);
10396         add_child_die (parent->new_die, c);
10397         node.new_die = c;
10398         c = clone;
10399       }
10400     generate_skeleton_bottom_up (&node);
10401   } while (next != NULL);
10402 }
10403
10404 /* Wrapper function for generate_skeleton_bottom_up.  */
10405
10406 static dw_die_ref
10407 generate_skeleton (dw_die_ref die)
10408 {
10409   skeleton_chain_node node;
10410
10411   node.old_die = die;
10412   node.new_die = NULL;
10413   node.parent = NULL;
10414
10415   /* If this type definition is nested inside another type,
10416      always leave at least a declaration in its place.  */
10417   if (die->die_parent != NULL && is_type_die (die->die_parent))
10418     node.new_die = clone_as_declaration (die);
10419
10420   generate_skeleton_bottom_up (&node);
10421   return node.new_die;
10422 }
10423
10424 /* Remove the DIE from its parent, possibly replacing it with a cloned
10425    declaration.  The original DIE will be moved to a new compile unit
10426    so that existing references to it follow it to the new location.  If
10427    any of the original DIE's descendants is a declaration, we need to
10428    replace the original DIE with a skeleton tree and move the
10429    declarations back into the skeleton tree.  */
10430
10431 static dw_die_ref
10432 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10433 {
10434   dw_die_ref skeleton;
10435
10436   skeleton = generate_skeleton (child);
10437   if (skeleton == NULL)
10438     remove_child_with_prev (child, prev);
10439   else
10440     {
10441       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10442       replace_child (child, skeleton, prev);
10443     }
10444
10445   return skeleton;
10446 }
10447
10448 /* Traverse the DIE and set up additional .debug_types sections for each
10449    type worthy of being placed in a COMDAT section.  */
10450
10451 static void
10452 break_out_comdat_types (dw_die_ref die)
10453 {
10454   dw_die_ref c;
10455   dw_die_ref first;
10456   dw_die_ref prev = NULL;
10457   dw_die_ref next = NULL;
10458   dw_die_ref unit = NULL;
10459
10460   first = c = die->die_child;
10461   if (c)
10462     next = c->die_sib;
10463   if (c) do {
10464     if (prev == NULL || prev->die_sib == c)
10465       prev = c;
10466     c = next;
10467     next = (c == first ? NULL : c->die_sib);
10468     if (should_move_die_to_comdat (c))
10469       {
10470         dw_die_ref replacement;
10471         comdat_type_node_ref type_node;
10472
10473         /* Create a new type unit DIE as the root for the new tree, and
10474            add it to the list of comdat types.  */
10475         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10476         add_AT_unsigned (unit, DW_AT_language,
10477                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10478         type_node = ggc_alloc_cleared_comdat_type_node ();
10479         type_node->root_die = unit;
10480         type_node->next = comdat_type_list;
10481         comdat_type_list = type_node;
10482
10483         /* Generate the type signature.  */
10484         generate_type_signature (c, type_node);
10485
10486         /* Copy the declaration context, attributes, and children of the
10487            declaration into the new compile unit DIE.  */
10488         copy_declaration_context (unit, c);
10489
10490         /* Remove this DIE from the main CU.  */
10491         replacement = remove_child_or_replace_with_skeleton (c, prev);
10492
10493         /* Break out nested types into their own type units.  */
10494         break_out_comdat_types (c);
10495
10496         /* Add the DIE to the new compunit.  */
10497         add_child_die (unit, c);
10498
10499         if (replacement != NULL)
10500           c = replacement;
10501       }
10502     else if (c->die_tag == DW_TAG_namespace
10503              || c->die_tag == DW_TAG_class_type
10504              || c->die_tag == DW_TAG_structure_type
10505              || c->die_tag == DW_TAG_union_type)
10506       {
10507         /* Look for nested types that can be broken out.  */
10508         break_out_comdat_types (c);
10509       }
10510   } while (next != NULL);
10511 }
10512
10513 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10514
10515 struct decl_table_entry
10516 {
10517   dw_die_ref orig;
10518   dw_die_ref copy;
10519 };
10520
10521 /* Routines to manipulate hash table of copied declarations.  */
10522
10523 static hashval_t
10524 htab_decl_hash (const void *of)
10525 {
10526   const struct decl_table_entry *const entry =
10527     (const struct decl_table_entry *) of;
10528
10529   return htab_hash_pointer (entry->orig);
10530 }
10531
10532 static int
10533 htab_decl_eq (const void *of1, const void *of2)
10534 {
10535   const struct decl_table_entry *const entry1 =
10536     (const struct decl_table_entry *) of1;
10537   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10538
10539   return entry1->orig == entry2;
10540 }
10541
10542 static void
10543 htab_decl_del (void *what)
10544 {
10545   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10546
10547   free (entry);
10548 }
10549
10550 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10551    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10552    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10553    to check if the ancestor has already been copied into UNIT.  */
10554
10555 static dw_die_ref
10556 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10557 {
10558   dw_die_ref parent = die->die_parent;
10559   dw_die_ref new_parent = unit;
10560   dw_die_ref copy;
10561   void **slot = NULL;
10562   struct decl_table_entry *entry = NULL;
10563
10564   if (decl_table)
10565     {
10566       /* Check if the entry has already been copied to UNIT.  */
10567       slot = htab_find_slot_with_hash (decl_table, die,
10568                                        htab_hash_pointer (die), INSERT);
10569       if (*slot != HTAB_EMPTY_ENTRY)
10570         {
10571           entry = (struct decl_table_entry *) *slot;
10572           return entry->copy;
10573         }
10574
10575       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10576       entry = XCNEW (struct decl_table_entry);
10577       entry->orig = die;
10578       entry->copy = NULL;
10579       *slot = entry;
10580     }
10581
10582   if (parent != NULL)
10583     {
10584       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10585       if (spec != NULL)
10586         parent = spec;
10587       if (parent->die_tag != DW_TAG_compile_unit
10588           && parent->die_tag != DW_TAG_type_unit)
10589         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10590     }
10591
10592   copy = clone_as_declaration (die);
10593   add_child_die (new_parent, copy);
10594
10595   if (decl_table != NULL)
10596     {
10597       /* Record the pointer to the copy.  */
10598       entry->copy = copy;
10599     }
10600
10601   return copy;
10602 }
10603
10604 /* Walk the DIE and its children, looking for references to incomplete
10605    or trivial types that are unmarked (i.e., that are not in the current
10606    type_unit).  */
10607
10608 static void
10609 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10610 {
10611   dw_die_ref c;
10612   dw_attr_ref a;
10613   unsigned ix;
10614
10615   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10616     {
10617       if (AT_class (a) == dw_val_class_die_ref)
10618         {
10619           dw_die_ref targ = AT_ref (a);
10620           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10621           void **slot;
10622           struct decl_table_entry *entry;
10623
10624           if (targ->die_mark != 0 || type_node != NULL)
10625             continue;
10626
10627           slot = htab_find_slot_with_hash (decl_table, targ,
10628                                            htab_hash_pointer (targ), INSERT);
10629
10630           if (*slot != HTAB_EMPTY_ENTRY)
10631             {
10632               /* TARG has already been copied, so we just need to
10633                  modify the reference to point to the copy.  */
10634               entry = (struct decl_table_entry *) *slot;
10635               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10636             }
10637           else
10638             {
10639               dw_die_ref parent = unit;
10640               dw_die_ref copy = clone_tree (targ);
10641
10642               /* Make sure the cloned tree is marked as part of the
10643                  type unit.  */
10644               mark_dies (copy);
10645
10646               /* Record in DECL_TABLE that TARG has been copied.
10647                  Need to do this now, before the recursive call,
10648                  because DECL_TABLE may be expanded and SLOT
10649                  would no longer be a valid pointer.  */
10650               entry = XCNEW (struct decl_table_entry);
10651               entry->orig = targ;
10652               entry->copy = copy;
10653               *slot = entry;
10654
10655               /* If TARG has surrounding context, copy its ancestor tree
10656                  into the new type unit.  */
10657               if (targ->die_parent != NULL
10658                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10659                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10660                 parent = copy_ancestor_tree (unit, targ->die_parent,
10661                                              decl_table);
10662
10663               add_child_die (parent, copy);
10664               a->dw_attr_val.v.val_die_ref.die = copy;
10665
10666               /* Make sure the newly-copied DIE is walked.  If it was
10667                  installed in a previously-added context, it won't
10668                  get visited otherwise.  */
10669               if (parent != unit)
10670                 {
10671                   /* Find the highest point of the newly-added tree,
10672                      mark each node along the way, and walk from there.  */
10673                   parent->die_mark = 1;
10674                   while (parent->die_parent
10675                          && parent->die_parent->die_mark == 0)
10676                     {
10677                       parent = parent->die_parent;
10678                       parent->die_mark = 1;
10679                     }
10680                   copy_decls_walk (unit, parent, decl_table);
10681                 }
10682             }
10683         }
10684     }
10685
10686   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10687 }
10688
10689 /* Copy declarations for "unworthy" types into the new comdat section.
10690    Incomplete types, modified types, and certain other types aren't broken
10691    out into comdat sections of their own, so they don't have a signature,
10692    and we need to copy the declaration into the same section so that we
10693    don't have an external reference.  */
10694
10695 static void
10696 copy_decls_for_unworthy_types (dw_die_ref unit)
10697 {
10698   htab_t decl_table;
10699
10700   mark_dies (unit);
10701   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10702   copy_decls_walk (unit, unit, decl_table);
10703   htab_delete (decl_table);
10704   unmark_dies (unit);
10705 }
10706
10707 /* Traverse the DIE and add a sibling attribute if it may have the
10708    effect of speeding up access to siblings.  To save some space,
10709    avoid generating sibling attributes for DIE's without children.  */
10710
10711 static void
10712 add_sibling_attributes (dw_die_ref die)
10713 {
10714   dw_die_ref c;
10715
10716   if (! die->die_child)
10717     return;
10718
10719   if (die->die_parent && die != die->die_parent->die_child)
10720     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10721
10722   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10723 }
10724
10725 /* Output all location lists for the DIE and its children.  */
10726
10727 static void
10728 output_location_lists (dw_die_ref die)
10729 {
10730   dw_die_ref c;
10731   dw_attr_ref a;
10732   unsigned ix;
10733
10734   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10735     if (AT_class (a) == dw_val_class_loc_list)
10736       output_loc_list (AT_loc_list (a));
10737
10738   FOR_EACH_CHILD (die, c, output_location_lists (c));
10739 }
10740
10741 /* The format of each DIE (and its attribute value pairs) is encoded in an
10742    abbreviation table.  This routine builds the abbreviation table and assigns
10743    a unique abbreviation id for each abbreviation entry.  The children of each
10744    die are visited recursively.  */
10745
10746 static void
10747 build_abbrev_table (dw_die_ref die)
10748 {
10749   unsigned long abbrev_id;
10750   unsigned int n_alloc;
10751   dw_die_ref c;
10752   dw_attr_ref a;
10753   unsigned ix;
10754
10755   /* Scan the DIE references, and mark as external any that refer to
10756      DIEs from other CUs (i.e. those which are not marked).  */
10757   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10758     if (AT_class (a) == dw_val_class_die_ref
10759         && AT_ref (a)->die_mark == 0)
10760       {
10761         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10762         set_AT_ref_external (a, 1);
10763       }
10764
10765   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10766     {
10767       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10768       dw_attr_ref die_a, abbrev_a;
10769       unsigned ix;
10770       bool ok = true;
10771
10772       if (abbrev->die_tag != die->die_tag)
10773         continue;
10774       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10775         continue;
10776
10777       if (VEC_length (dw_attr_node, abbrev->die_attr)
10778           != VEC_length (dw_attr_node, die->die_attr))
10779         continue;
10780
10781       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10782         {
10783           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10784           if ((abbrev_a->dw_attr != die_a->dw_attr)
10785               || (value_format (abbrev_a) != value_format (die_a)))
10786             {
10787               ok = false;
10788               break;
10789             }
10790         }
10791       if (ok)
10792         break;
10793     }
10794
10795   if (abbrev_id >= abbrev_die_table_in_use)
10796     {
10797       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10798         {
10799           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10800           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10801                                             n_alloc);
10802
10803           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10804                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10805           abbrev_die_table_allocated = n_alloc;
10806         }
10807
10808       ++abbrev_die_table_in_use;
10809       abbrev_die_table[abbrev_id] = die;
10810     }
10811
10812   die->die_abbrev = abbrev_id;
10813   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10814 }
10815 \f
10816 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10817
10818 static int
10819 constant_size (unsigned HOST_WIDE_INT value)
10820 {
10821   int log;
10822
10823   if (value == 0)
10824     log = 0;
10825   else
10826     log = floor_log2 (value);
10827
10828   log = log / 8;
10829   log = 1 << (floor_log2 (log) + 1);
10830
10831   return log;
10832 }
10833
10834 /* Return the size of a DIE as it is represented in the
10835    .debug_info section.  */
10836
10837 static unsigned long
10838 size_of_die (dw_die_ref die)
10839 {
10840   unsigned long size = 0;
10841   dw_attr_ref a;
10842   unsigned ix;
10843
10844   size += size_of_uleb128 (die->die_abbrev);
10845   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10846     {
10847       switch (AT_class (a))
10848         {
10849         case dw_val_class_addr:
10850           size += DWARF2_ADDR_SIZE;
10851           break;
10852         case dw_val_class_offset:
10853           size += DWARF_OFFSET_SIZE;
10854           break;
10855         case dw_val_class_loc:
10856           {
10857             unsigned long lsize = size_of_locs (AT_loc (a));
10858
10859             /* Block length.  */
10860             if (dwarf_version >= 4)
10861               size += size_of_uleb128 (lsize);
10862             else
10863               size += constant_size (lsize);
10864             size += lsize;
10865           }
10866           break;
10867         case dw_val_class_loc_list:
10868           size += DWARF_OFFSET_SIZE;
10869           break;
10870         case dw_val_class_range_list:
10871           size += DWARF_OFFSET_SIZE;
10872           break;
10873         case dw_val_class_const:
10874           size += size_of_sleb128 (AT_int (a));
10875           break;
10876         case dw_val_class_unsigned_const:
10877           size += constant_size (AT_unsigned (a));
10878           break;
10879         case dw_val_class_const_double:
10880           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10881           if (HOST_BITS_PER_WIDE_INT >= 64)
10882             size++; /* block */
10883           break;
10884         case dw_val_class_vec:
10885           size += constant_size (a->dw_attr_val.v.val_vec.length
10886                                  * a->dw_attr_val.v.val_vec.elt_size)
10887                   + a->dw_attr_val.v.val_vec.length
10888                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10889           break;
10890         case dw_val_class_flag:
10891           if (dwarf_version >= 4)
10892             /* Currently all add_AT_flag calls pass in 1 as last argument,
10893                so DW_FORM_flag_present can be used.  If that ever changes,
10894                we'll need to use DW_FORM_flag and have some optimization
10895                in build_abbrev_table that will change those to
10896                DW_FORM_flag_present if it is set to 1 in all DIEs using
10897                the same abbrev entry.  */
10898             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10899           else
10900             size += 1;
10901           break;
10902         case dw_val_class_die_ref:
10903           if (AT_ref_external (a))
10904             {
10905               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10906                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10907                  is sized by target address length, whereas in DWARF3
10908                  it's always sized as an offset.  */
10909               if (use_debug_types)
10910                 size += DWARF_TYPE_SIGNATURE_SIZE;
10911               else if (dwarf_version == 2)
10912                 size += DWARF2_ADDR_SIZE;
10913               else
10914                 size += DWARF_OFFSET_SIZE;
10915             }
10916           else
10917             size += DWARF_OFFSET_SIZE;
10918           break;
10919         case dw_val_class_fde_ref:
10920           size += DWARF_OFFSET_SIZE;
10921           break;
10922         case dw_val_class_lbl_id:
10923           size += DWARF2_ADDR_SIZE;
10924           break;
10925         case dw_val_class_lineptr:
10926         case dw_val_class_macptr:
10927           size += DWARF_OFFSET_SIZE;
10928           break;
10929         case dw_val_class_str:
10930           if (AT_string_form (a) == DW_FORM_strp)
10931             size += DWARF_OFFSET_SIZE;
10932           else
10933             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10934           break;
10935         case dw_val_class_file:
10936           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10937           break;
10938         case dw_val_class_data8:
10939           size += 8;
10940           break;
10941         case dw_val_class_vms_delta:
10942           size += DWARF_OFFSET_SIZE;
10943           break;
10944         default:
10945           gcc_unreachable ();
10946         }
10947     }
10948
10949   return size;
10950 }
10951
10952 /* Size the debugging information associated with a given DIE.  Visits the
10953    DIE's children recursively.  Updates the global variable next_die_offset, on
10954    each time through.  Uses the current value of next_die_offset to update the
10955    die_offset field in each DIE.  */
10956
10957 static void
10958 calc_die_sizes (dw_die_ref die)
10959 {
10960   dw_die_ref c;
10961
10962   gcc_assert (die->die_offset == 0
10963               || (unsigned long int) die->die_offset == next_die_offset);
10964   die->die_offset = next_die_offset;
10965   next_die_offset += size_of_die (die);
10966
10967   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10968
10969   if (die->die_child != NULL)
10970     /* Count the null byte used to terminate sibling lists.  */
10971     next_die_offset += 1;
10972 }
10973
10974 /* Size just the base type children at the start of the CU.
10975    This is needed because build_abbrev needs to size locs
10976    and sizing of type based stack ops needs to know die_offset
10977    values for the base types.  */
10978
10979 static void
10980 calc_base_type_die_sizes (void)
10981 {
10982   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
10983   unsigned int i;
10984   dw_die_ref base_type;
10985 #if ENABLE_ASSERT_CHECKING
10986   dw_die_ref prev = comp_unit_die ()->die_child;
10987 #endif
10988
10989   die_offset += size_of_die (comp_unit_die ());
10990   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
10991     {
10992 #if ENABLE_ASSERT_CHECKING
10993       gcc_assert (base_type->die_offset == 0
10994                   && prev->die_sib == base_type
10995                   && base_type->die_child == NULL
10996                   && base_type->die_abbrev);
10997       prev = base_type;
10998 #endif
10999       base_type->die_offset = die_offset;
11000       die_offset += size_of_die (base_type);
11001     }
11002 }
11003
11004 /* Set the marks for a die and its children.  We do this so
11005    that we know whether or not a reference needs to use FORM_ref_addr; only
11006    DIEs in the same CU will be marked.  We used to clear out the offset
11007    and use that as the flag, but ran into ordering problems.  */
11008
11009 static void
11010 mark_dies (dw_die_ref die)
11011 {
11012   dw_die_ref c;
11013
11014   gcc_assert (!die->die_mark);
11015
11016   die->die_mark = 1;
11017   FOR_EACH_CHILD (die, c, mark_dies (c));
11018 }
11019
11020 /* Clear the marks for a die and its children.  */
11021
11022 static void
11023 unmark_dies (dw_die_ref die)
11024 {
11025   dw_die_ref c;
11026
11027   if (! use_debug_types)
11028     gcc_assert (die->die_mark);
11029
11030   die->die_mark = 0;
11031   FOR_EACH_CHILD (die, c, unmark_dies (c));
11032 }
11033
11034 /* Clear the marks for a die, its children and referred dies.  */
11035
11036 static void
11037 unmark_all_dies (dw_die_ref die)
11038 {
11039   dw_die_ref c;
11040   dw_attr_ref a;
11041   unsigned ix;
11042
11043   if (!die->die_mark)
11044     return;
11045   die->die_mark = 0;
11046
11047   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
11048
11049   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11050     if (AT_class (a) == dw_val_class_die_ref)
11051       unmark_all_dies (AT_ref (a));
11052 }
11053
11054 /* Return the size of the .debug_pubnames or .debug_pubtypes table
11055    generated for the compilation unit.  */
11056
11057 static unsigned long
11058 size_of_pubnames (VEC (pubname_entry, gc) * names)
11059 {
11060   unsigned long size;
11061   unsigned i;
11062   pubname_ref p;
11063
11064   size = DWARF_PUBNAMES_HEADER_SIZE;
11065   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
11066     if (names != pubtype_table
11067         || p->die->die_offset != 0
11068         || !flag_eliminate_unused_debug_types)
11069       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
11070
11071   size += DWARF_OFFSET_SIZE;
11072   return size;
11073 }
11074
11075 /* Return the size of the information in the .debug_aranges section.  */
11076
11077 static unsigned long
11078 size_of_aranges (void)
11079 {
11080   unsigned long size;
11081
11082   size = DWARF_ARANGES_HEADER_SIZE;
11083
11084   /* Count the address/length pair for this compilation unit.  */
11085   if (text_section_used)
11086     size += 2 * DWARF2_ADDR_SIZE;
11087   if (cold_text_section_used)
11088     size += 2 * DWARF2_ADDR_SIZE;
11089   if (have_multiple_function_sections)
11090     {
11091       unsigned fde_idx = 0;
11092
11093       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11094         {
11095           dw_fde_ref fde = &fde_table[fde_idx];
11096
11097           if (!fde->in_std_section)
11098             size += 2 * DWARF2_ADDR_SIZE;
11099           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11100             size += 2 * DWARF2_ADDR_SIZE;
11101         }
11102     }
11103
11104   /* Count the two zero words used to terminated the address range table.  */
11105   size += 2 * DWARF2_ADDR_SIZE;
11106   return size;
11107 }
11108 \f
11109 /* Select the encoding of an attribute value.  */
11110
11111 static enum dwarf_form
11112 value_format (dw_attr_ref a)
11113 {
11114   switch (a->dw_attr_val.val_class)
11115     {
11116     case dw_val_class_addr:
11117       /* Only very few attributes allow DW_FORM_addr.  */
11118       switch (a->dw_attr)
11119         {
11120         case DW_AT_low_pc:
11121         case DW_AT_high_pc:
11122         case DW_AT_entry_pc:
11123         case DW_AT_trampoline:
11124           return DW_FORM_addr;
11125         default:
11126           break;
11127         }
11128       switch (DWARF2_ADDR_SIZE)
11129         {
11130         case 1:
11131           return DW_FORM_data1;
11132         case 2:
11133           return DW_FORM_data2;
11134         case 4:
11135           return DW_FORM_data4;
11136         case 8:
11137           return DW_FORM_data8;
11138         default:
11139           gcc_unreachable ();
11140         }
11141     case dw_val_class_range_list:
11142     case dw_val_class_loc_list:
11143       if (dwarf_version >= 4)
11144         return DW_FORM_sec_offset;
11145       /* FALLTHRU */
11146     case dw_val_class_vms_delta:
11147     case dw_val_class_offset:
11148       switch (DWARF_OFFSET_SIZE)
11149         {
11150         case 4:
11151           return DW_FORM_data4;
11152         case 8:
11153           return DW_FORM_data8;
11154         default:
11155           gcc_unreachable ();
11156         }
11157     case dw_val_class_loc:
11158       if (dwarf_version >= 4)
11159         return DW_FORM_exprloc;
11160       switch (constant_size (size_of_locs (AT_loc (a))))
11161         {
11162         case 1:
11163           return DW_FORM_block1;
11164         case 2:
11165           return DW_FORM_block2;
11166         default:
11167           gcc_unreachable ();
11168         }
11169     case dw_val_class_const:
11170       return DW_FORM_sdata;
11171     case dw_val_class_unsigned_const:
11172       switch (constant_size (AT_unsigned (a)))
11173         {
11174         case 1:
11175           return DW_FORM_data1;
11176         case 2:
11177           return DW_FORM_data2;
11178         case 4:
11179           return DW_FORM_data4;
11180         case 8:
11181           return DW_FORM_data8;
11182         default:
11183           gcc_unreachable ();
11184         }
11185     case dw_val_class_const_double:
11186       switch (HOST_BITS_PER_WIDE_INT)
11187         {
11188         case 8:
11189           return DW_FORM_data2;
11190         case 16:
11191           return DW_FORM_data4;
11192         case 32:
11193           return DW_FORM_data8;
11194         case 64:
11195         default:
11196           return DW_FORM_block1;
11197         }
11198     case dw_val_class_vec:
11199       switch (constant_size (a->dw_attr_val.v.val_vec.length
11200                              * a->dw_attr_val.v.val_vec.elt_size))
11201         {
11202         case 1:
11203           return DW_FORM_block1;
11204         case 2:
11205           return DW_FORM_block2;
11206         case 4:
11207           return DW_FORM_block4;
11208         default:
11209           gcc_unreachable ();
11210         }
11211     case dw_val_class_flag:
11212       if (dwarf_version >= 4)
11213         {
11214           /* Currently all add_AT_flag calls pass in 1 as last argument,
11215              so DW_FORM_flag_present can be used.  If that ever changes,
11216              we'll need to use DW_FORM_flag and have some optimization
11217              in build_abbrev_table that will change those to
11218              DW_FORM_flag_present if it is set to 1 in all DIEs using
11219              the same abbrev entry.  */
11220           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11221           return DW_FORM_flag_present;
11222         }
11223       return DW_FORM_flag;
11224     case dw_val_class_die_ref:
11225       if (AT_ref_external (a))
11226         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11227       else
11228         return DW_FORM_ref;
11229     case dw_val_class_fde_ref:
11230       return DW_FORM_data;
11231     case dw_val_class_lbl_id:
11232       return DW_FORM_addr;
11233     case dw_val_class_lineptr:
11234     case dw_val_class_macptr:
11235       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11236     case dw_val_class_str:
11237       return AT_string_form (a);
11238     case dw_val_class_file:
11239       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11240         {
11241         case 1:
11242           return DW_FORM_data1;
11243         case 2:
11244           return DW_FORM_data2;
11245         case 4:
11246           return DW_FORM_data4;
11247         default:
11248           gcc_unreachable ();
11249         }
11250
11251     case dw_val_class_data8:
11252       return DW_FORM_data8;
11253
11254     default:
11255       gcc_unreachable ();
11256     }
11257 }
11258
11259 /* Output the encoding of an attribute value.  */
11260
11261 static void
11262 output_value_format (dw_attr_ref a)
11263 {
11264   enum dwarf_form form = value_format (a);
11265
11266   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11267 }
11268
11269 /* Output the .debug_abbrev section which defines the DIE abbreviation
11270    table.  */
11271
11272 static void
11273 output_abbrev_section (void)
11274 {
11275   unsigned long abbrev_id;
11276
11277   if (abbrev_die_table_in_use == 1)
11278     return;
11279
11280   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11281     {
11282       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11283       unsigned ix;
11284       dw_attr_ref a_attr;
11285
11286       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11287       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11288                                    dwarf_tag_name (abbrev->die_tag));
11289
11290       if (abbrev->die_child != NULL)
11291         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11292       else
11293         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11294
11295       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11296            ix++)
11297         {
11298           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11299                                        dwarf_attr_name (a_attr->dw_attr));
11300           output_value_format (a_attr);
11301         }
11302
11303       dw2_asm_output_data (1, 0, NULL);
11304       dw2_asm_output_data (1, 0, NULL);
11305     }
11306
11307   /* Terminate the table.  */
11308   dw2_asm_output_data (1, 0, NULL);
11309 }
11310
11311 /* Output a symbol we can use to refer to this DIE from another CU.  */
11312
11313 static inline void
11314 output_die_symbol (dw_die_ref die)
11315 {
11316   char *sym = die->die_id.die_symbol;
11317
11318   if (sym == 0)
11319     return;
11320
11321   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11322     /* We make these global, not weak; if the target doesn't support
11323        .linkonce, it doesn't support combining the sections, so debugging
11324        will break.  */
11325     targetm.asm_out.globalize_label (asm_out_file, sym);
11326
11327   ASM_OUTPUT_LABEL (asm_out_file, sym);
11328 }
11329
11330 /* Return a new location list, given the begin and end range, and the
11331    expression.  */
11332
11333 static inline dw_loc_list_ref
11334 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11335               const char *section)
11336 {
11337   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11338
11339   retlist->begin = begin;
11340   retlist->end = end;
11341   retlist->expr = expr;
11342   retlist->section = section;
11343
11344   return retlist;
11345 }
11346
11347 /* Generate a new internal symbol for this location list node, if it
11348    hasn't got one yet.  */
11349
11350 static inline void
11351 gen_llsym (dw_loc_list_ref list)
11352 {
11353   gcc_assert (!list->ll_symbol);
11354   list->ll_symbol = gen_internal_sym ("LLST");
11355 }
11356
11357 /* Output the location list given to us.  */
11358
11359 static void
11360 output_loc_list (dw_loc_list_ref list_head)
11361 {
11362   dw_loc_list_ref curr = list_head;
11363
11364   if (list_head->emitted)
11365     return;
11366   list_head->emitted = true;
11367
11368   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11369
11370   /* Walk the location list, and output each range + expression.  */
11371   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11372     {
11373       unsigned long size;
11374       /* Don't output an entry that starts and ends at the same address.  */
11375       if (strcmp (curr->begin, curr->end) == 0)
11376         continue;
11377       if (!have_multiple_function_sections)
11378         {
11379           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11380                                 "Location list begin address (%s)",
11381                                 list_head->ll_symbol);
11382           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11383                                 "Location list end address (%s)",
11384                                 list_head->ll_symbol);
11385         }
11386       else
11387         {
11388           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11389                                "Location list begin address (%s)",
11390                                list_head->ll_symbol);
11391           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11392                                "Location list end address (%s)",
11393                                list_head->ll_symbol);
11394         }
11395       size = size_of_locs (curr->expr);
11396
11397       /* Output the block length for this list of location operations.  */
11398       gcc_assert (size <= 0xffff);
11399       dw2_asm_output_data (2, size, "%s", "Location expression size");
11400
11401       output_loc_sequence (curr->expr, -1);
11402     }
11403
11404   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11405                        "Location list terminator begin (%s)",
11406                        list_head->ll_symbol);
11407   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11408                        "Location list terminator end (%s)",
11409                        list_head->ll_symbol);
11410 }
11411
11412 /* Output a type signature.  */
11413
11414 static inline void
11415 output_signature (const char *sig, const char *name)
11416 {
11417   int i;
11418
11419   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11420     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11421 }
11422
11423 /* Output the DIE and its attributes.  Called recursively to generate
11424    the definitions of each child DIE.  */
11425
11426 static void
11427 output_die (dw_die_ref die)
11428 {
11429   dw_attr_ref a;
11430   dw_die_ref c;
11431   unsigned long size;
11432   unsigned ix;
11433
11434   /* If someone in another CU might refer to us, set up a symbol for
11435      them to point to.  */
11436   if (! use_debug_types && die->die_id.die_symbol)
11437     output_die_symbol (die);
11438
11439   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11440                                (unsigned long)die->die_offset,
11441                                dwarf_tag_name (die->die_tag));
11442
11443   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11444     {
11445       const char *name = dwarf_attr_name (a->dw_attr);
11446
11447       switch (AT_class (a))
11448         {
11449         case dw_val_class_addr:
11450           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11451           break;
11452
11453         case dw_val_class_offset:
11454           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11455                                "%s", name);
11456           break;
11457
11458         case dw_val_class_range_list:
11459           {
11460             char *p = strchr (ranges_section_label, '\0');
11461
11462             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11463                      a->dw_attr_val.v.val_offset);
11464             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11465                                    debug_ranges_section, "%s", name);
11466             *p = '\0';
11467           }
11468           break;
11469
11470         case dw_val_class_loc:
11471           size = size_of_locs (AT_loc (a));
11472
11473           /* Output the block length for this list of location operations.  */
11474           if (dwarf_version >= 4)
11475             dw2_asm_output_data_uleb128 (size, "%s", name);
11476           else
11477             dw2_asm_output_data (constant_size (size), size, "%s", name);
11478
11479           output_loc_sequence (AT_loc (a), -1);
11480           break;
11481
11482         case dw_val_class_const:
11483           /* ??? It would be slightly more efficient to use a scheme like is
11484              used for unsigned constants below, but gdb 4.x does not sign
11485              extend.  Gdb 5.x does sign extend.  */
11486           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11487           break;
11488
11489         case dw_val_class_unsigned_const:
11490           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11491                                AT_unsigned (a), "%s", name);
11492           break;
11493
11494         case dw_val_class_const_double:
11495           {
11496             unsigned HOST_WIDE_INT first, second;
11497
11498             if (HOST_BITS_PER_WIDE_INT >= 64)
11499               dw2_asm_output_data (1,
11500                                    2 * HOST_BITS_PER_WIDE_INT
11501                                    / HOST_BITS_PER_CHAR,
11502                                    NULL);
11503
11504             if (WORDS_BIG_ENDIAN)
11505               {
11506                 first = a->dw_attr_val.v.val_double.high;
11507                 second = a->dw_attr_val.v.val_double.low;
11508               }
11509             else
11510               {
11511                 first = a->dw_attr_val.v.val_double.low;
11512                 second = a->dw_attr_val.v.val_double.high;
11513               }
11514
11515             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11516                                  first, name);
11517             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11518                                  second, NULL);
11519           }
11520           break;
11521
11522         case dw_val_class_vec:
11523           {
11524             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11525             unsigned int len = a->dw_attr_val.v.val_vec.length;
11526             unsigned int i;
11527             unsigned char *p;
11528
11529             dw2_asm_output_data (constant_size (len * elt_size),
11530                                  len * elt_size, "%s", name);
11531             if (elt_size > sizeof (HOST_WIDE_INT))
11532               {
11533                 elt_size /= 2;
11534                 len *= 2;
11535               }
11536             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11537                  i < len;
11538                  i++, p += elt_size)
11539               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11540                                    "fp or vector constant word %u", i);
11541             break;
11542           }
11543
11544         case dw_val_class_flag:
11545           if (dwarf_version >= 4)
11546             {
11547               /* Currently all add_AT_flag calls pass in 1 as last argument,
11548                  so DW_FORM_flag_present can be used.  If that ever changes,
11549                  we'll need to use DW_FORM_flag and have some optimization
11550                  in build_abbrev_table that will change those to
11551                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11552                  the same abbrev entry.  */
11553               gcc_assert (AT_flag (a) == 1);
11554               if (flag_debug_asm)
11555                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11556                          ASM_COMMENT_START, name);
11557               break;
11558             }
11559           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11560           break;
11561
11562         case dw_val_class_loc_list:
11563           {
11564             char *sym = AT_loc_list (a)->ll_symbol;
11565
11566             gcc_assert (sym);
11567             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11568                                    "%s", name);
11569           }
11570           break;
11571
11572         case dw_val_class_die_ref:
11573           if (AT_ref_external (a))
11574             {
11575               if (use_debug_types)
11576                 {
11577                   comdat_type_node_ref type_node =
11578                     AT_ref (a)->die_id.die_type_node;
11579
11580                   gcc_assert (type_node);
11581                   output_signature (type_node->signature, name);
11582                 }
11583               else
11584                 {
11585                   char *sym = AT_ref (a)->die_id.die_symbol;
11586                   int size;
11587
11588                   gcc_assert (sym);
11589                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11590                      length, whereas in DWARF3 it's always sized as an
11591                      offset.  */
11592                   if (dwarf_version == 2)
11593                     size = DWARF2_ADDR_SIZE;
11594                   else
11595                     size = DWARF_OFFSET_SIZE;
11596                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11597                                          name);
11598                 }
11599             }
11600           else
11601             {
11602               gcc_assert (AT_ref (a)->die_offset);
11603               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11604                                    "%s", name);
11605             }
11606           break;
11607
11608         case dw_val_class_fde_ref:
11609           {
11610             char l1[20];
11611
11612             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11613                                          a->dw_attr_val.v.val_fde_index * 2);
11614             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11615                                    "%s", name);
11616           }
11617           break;
11618
11619         case dw_val_class_vms_delta:
11620           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11621                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11622                                     "%s", name);
11623           break;
11624
11625         case dw_val_class_lbl_id:
11626           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11627           break;
11628
11629         case dw_val_class_lineptr:
11630           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11631                                  debug_line_section, "%s", name);
11632           break;
11633
11634         case dw_val_class_macptr:
11635           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11636                                  debug_macinfo_section, "%s", name);
11637           break;
11638
11639         case dw_val_class_str:
11640           if (AT_string_form (a) == DW_FORM_strp)
11641             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11642                                    a->dw_attr_val.v.val_str->label,
11643                                    debug_str_section,
11644                                    "%s: \"%s\"", name, AT_string (a));
11645           else
11646             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11647           break;
11648
11649         case dw_val_class_file:
11650           {
11651             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11652
11653             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11654                                  a->dw_attr_val.v.val_file->filename);
11655             break;
11656           }
11657
11658         case dw_val_class_data8:
11659           {
11660             int i;
11661
11662             for (i = 0; i < 8; i++)
11663               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11664                                    i == 0 ? "%s" : NULL, name);
11665             break;
11666           }
11667
11668         default:
11669           gcc_unreachable ();
11670         }
11671     }
11672
11673   FOR_EACH_CHILD (die, c, output_die (c));
11674
11675   /* Add null byte to terminate sibling list.  */
11676   if (die->die_child != NULL)
11677     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11678                          (unsigned long) die->die_offset);
11679 }
11680
11681 /* Output the compilation unit that appears at the beginning of the
11682    .debug_info section, and precedes the DIE descriptions.  */
11683
11684 static void
11685 output_compilation_unit_header (void)
11686 {
11687   int ver = dwarf_version;
11688
11689   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11690     dw2_asm_output_data (4, 0xffffffff,
11691       "Initial length escape value indicating 64-bit DWARF extension");
11692   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11693                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11694                        "Length of Compilation Unit Info");
11695   dw2_asm_output_data (2, ver, "DWARF version number");
11696   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11697                          debug_abbrev_section,
11698                          "Offset Into Abbrev. Section");
11699   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11700 }
11701
11702 /* Output the compilation unit DIE and its children.  */
11703
11704 static void
11705 output_comp_unit (dw_die_ref die, int output_if_empty)
11706 {
11707   const char *secname;
11708   char *oldsym, *tmp;
11709
11710   /* Unless we are outputting main CU, we may throw away empty ones.  */
11711   if (!output_if_empty && die->die_child == NULL)
11712     return;
11713
11714   /* Even if there are no children of this DIE, we must output the information
11715      about the compilation unit.  Otherwise, on an empty translation unit, we
11716      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11717      will then complain when examining the file.  First mark all the DIEs in
11718      this CU so we know which get local refs.  */
11719   mark_dies (die);
11720
11721   build_abbrev_table (die);
11722
11723   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11724   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11725   calc_die_sizes (die);
11726
11727   oldsym = die->die_id.die_symbol;
11728   if (oldsym)
11729     {
11730       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11731
11732       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11733       secname = tmp;
11734       die->die_id.die_symbol = NULL;
11735       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11736     }
11737   else
11738     {
11739       switch_to_section (debug_info_section);
11740       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11741       info_section_emitted = true;
11742     }
11743
11744   /* Output debugging information.  */
11745   output_compilation_unit_header ();
11746   output_die (die);
11747
11748   /* Leave the marks on the main CU, so we can check them in
11749      output_pubnames.  */
11750   if (oldsym)
11751     {
11752       unmark_dies (die);
11753       die->die_id.die_symbol = oldsym;
11754     }
11755 }
11756
11757 /* Output a comdat type unit DIE and its children.  */
11758
11759 static void
11760 output_comdat_type_unit (comdat_type_node *node)
11761 {
11762   const char *secname;
11763   char *tmp;
11764   int i;
11765 #if defined (OBJECT_FORMAT_ELF)
11766   tree comdat_key;
11767 #endif
11768
11769   /* First mark all the DIEs in this CU so we know which get local refs.  */
11770   mark_dies (node->root_die);
11771
11772   build_abbrev_table (node->root_die);
11773
11774   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11775   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11776   calc_die_sizes (node->root_die);
11777
11778 #if defined (OBJECT_FORMAT_ELF)
11779   secname = ".debug_types";
11780   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11781   sprintf (tmp, "wt.");
11782   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11783     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11784   comdat_key = get_identifier (tmp);
11785   targetm.asm_out.named_section (secname,
11786                                  SECTION_DEBUG | SECTION_LINKONCE,
11787                                  comdat_key);
11788 #else
11789   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11790   sprintf (tmp, ".gnu.linkonce.wt.");
11791   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11792     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11793   secname = tmp;
11794   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11795 #endif
11796
11797   /* Output debugging information.  */
11798   output_compilation_unit_header ();
11799   output_signature (node->signature, "Type Signature");
11800   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11801                        "Offset to Type DIE");
11802   output_die (node->root_die);
11803
11804   unmark_dies (node->root_die);
11805 }
11806
11807 /* Return the DWARF2/3 pubname associated with a decl.  */
11808
11809 static const char *
11810 dwarf2_name (tree decl, int scope)
11811 {
11812   if (DECL_NAMELESS (decl))
11813     return NULL;
11814   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11815 }
11816
11817 /* Add a new entry to .debug_pubnames if appropriate.  */
11818
11819 static void
11820 add_pubname_string (const char *str, dw_die_ref die)
11821 {
11822   if (targetm.want_debug_pub_sections)
11823     {
11824       pubname_entry e;
11825
11826       e.die = die;
11827       e.name = xstrdup (str);
11828       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11829     }
11830 }
11831
11832 static void
11833 add_pubname (tree decl, dw_die_ref die)
11834 {
11835   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11836     {
11837       const char *name = dwarf2_name (decl, 1);
11838       if (name)
11839         add_pubname_string (name, die);
11840     }
11841 }
11842
11843 /* Add a new entry to .debug_pubtypes if appropriate.  */
11844
11845 static void
11846 add_pubtype (tree decl, dw_die_ref die)
11847 {
11848   pubname_entry e;
11849
11850   if (!targetm.want_debug_pub_sections)
11851     return;
11852
11853   e.name = NULL;
11854   if ((TREE_PUBLIC (decl)
11855        || is_cu_die (die->die_parent))
11856       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11857     {
11858       e.die = die;
11859       if (TYPE_P (decl))
11860         {
11861           if (TYPE_NAME (decl))
11862             {
11863               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11864                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11865               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11866                        && DECL_NAME (TYPE_NAME (decl)))
11867                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11868               else
11869                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11870             }
11871         }
11872       else
11873         {
11874           e.name = dwarf2_name (decl, 1);
11875           if (e.name)
11876             e.name = xstrdup (e.name);
11877         }
11878
11879       /* If we don't have a name for the type, there's no point in adding
11880          it to the table.  */
11881       if (e.name && e.name[0] != '\0')
11882         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11883     }
11884 }
11885
11886 /* Output the public names table used to speed up access to externally
11887    visible names; or the public types table used to find type definitions.  */
11888
11889 static void
11890 output_pubnames (VEC (pubname_entry, gc) * names)
11891 {
11892   unsigned i;
11893   unsigned long pubnames_length = size_of_pubnames (names);
11894   pubname_ref pub;
11895
11896   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11897     dw2_asm_output_data (4, 0xffffffff,
11898       "Initial length escape value indicating 64-bit DWARF extension");
11899   if (names == pubname_table)
11900     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11901                          "Length of Public Names Info");
11902   else
11903     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11904                          "Length of Public Type Names Info");
11905   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11906   dw2_asm_output_data (2, 2, "DWARF Version");
11907   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11908                          debug_info_section,
11909                          "Offset of Compilation Unit Info");
11910   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11911                        "Compilation Unit Length");
11912
11913   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11914     {
11915       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11916       if (names == pubname_table)
11917         gcc_assert (pub->die->die_mark);
11918
11919       if (names != pubtype_table
11920           || pub->die->die_offset != 0
11921           || !flag_eliminate_unused_debug_types)
11922         {
11923           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11924                                "DIE offset");
11925
11926           dw2_asm_output_nstring (pub->name, -1, "external name");
11927         }
11928     }
11929
11930   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11931 }
11932
11933 /* Output the information that goes into the .debug_aranges table.
11934    Namely, define the beginning and ending address range of the
11935    text section generated for this compilation unit.  */
11936
11937 static void
11938 output_aranges (unsigned long aranges_length)
11939 {
11940   unsigned i;
11941
11942   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11943     dw2_asm_output_data (4, 0xffffffff,
11944       "Initial length escape value indicating 64-bit DWARF extension");
11945   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11946                        "Length of Address Ranges Info");
11947   /* Version number for aranges is still 2, even in DWARF3.  */
11948   dw2_asm_output_data (2, 2, "DWARF Version");
11949   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11950                          debug_info_section,
11951                          "Offset of Compilation Unit Info");
11952   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11953   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11954
11955   /* We need to align to twice the pointer size here.  */
11956   if (DWARF_ARANGES_PAD_SIZE)
11957     {
11958       /* Pad using a 2 byte words so that padding is correct for any
11959          pointer size.  */
11960       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11961                            2 * DWARF2_ADDR_SIZE);
11962       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11963         dw2_asm_output_data (2, 0, NULL);
11964     }
11965
11966   /* It is necessary not to output these entries if the sections were
11967      not used; if the sections were not used, the length will be 0 and
11968      the address may end up as 0 if the section is discarded by ld
11969      --gc-sections, leaving an invalid (0, 0) entry that can be
11970      confused with the terminator.  */
11971   if (text_section_used)
11972     {
11973       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11974       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11975                             text_section_label, "Length");
11976     }
11977   if (cold_text_section_used)
11978     {
11979       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11980                            "Address");
11981       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11982                             cold_text_section_label, "Length");
11983     }
11984
11985   if (have_multiple_function_sections)
11986     {
11987       unsigned fde_idx = 0;
11988
11989       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11990         {
11991           dw_fde_ref fde = &fde_table[fde_idx];
11992
11993           if (!fde->in_std_section)
11994             {
11995               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11996                                    "Address");
11997               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11998                                     fde->dw_fde_begin, "Length");
11999             }
12000           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
12001             {
12002               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
12003                                    "Address");
12004               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
12005                                     fde->dw_fde_second_begin, "Length");
12006             }
12007         }
12008     }
12009
12010   /* Output the terminator words.  */
12011   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12012   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12013 }
12014
12015 /* Add a new entry to .debug_ranges.  Return the offset at which it
12016    was placed.  */
12017
12018 static unsigned int
12019 add_ranges_num (int num)
12020 {
12021   unsigned int in_use = ranges_table_in_use;
12022
12023   if (in_use == ranges_table_allocated)
12024     {
12025       ranges_table_allocated += RANGES_TABLE_INCREMENT;
12026       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
12027                                     ranges_table_allocated);
12028       memset (ranges_table + ranges_table_in_use, 0,
12029               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
12030     }
12031
12032   ranges_table[in_use].num = num;
12033   ranges_table_in_use = in_use + 1;
12034
12035   return in_use * 2 * DWARF2_ADDR_SIZE;
12036 }
12037
12038 /* Add a new entry to .debug_ranges corresponding to a block, or a
12039    range terminator if BLOCK is NULL.  */
12040
12041 static unsigned int
12042 add_ranges (const_tree block)
12043 {
12044   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
12045 }
12046
12047 /* Add a new entry to .debug_ranges corresponding to a pair of
12048    labels.  */
12049
12050 static void
12051 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
12052                       bool *added)
12053 {
12054   unsigned int in_use = ranges_by_label_in_use;
12055   unsigned int offset;
12056
12057   if (in_use == ranges_by_label_allocated)
12058     {
12059       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
12060       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
12061                                        ranges_by_label,
12062                                        ranges_by_label_allocated);
12063       memset (ranges_by_label + ranges_by_label_in_use, 0,
12064               RANGES_TABLE_INCREMENT
12065               * sizeof (struct dw_ranges_by_label_struct));
12066     }
12067
12068   ranges_by_label[in_use].begin = begin;
12069   ranges_by_label[in_use].end = end;
12070   ranges_by_label_in_use = in_use + 1;
12071
12072   offset = add_ranges_num (-(int)in_use - 1);
12073   if (!*added)
12074     {
12075       add_AT_range_list (die, DW_AT_ranges, offset);
12076       *added = true;
12077     }
12078 }
12079
12080 static void
12081 output_ranges (void)
12082 {
12083   unsigned i;
12084   static const char *const start_fmt = "Offset %#x";
12085   const char *fmt = start_fmt;
12086
12087   for (i = 0; i < ranges_table_in_use; i++)
12088     {
12089       int block_num = ranges_table[i].num;
12090
12091       if (block_num > 0)
12092         {
12093           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12094           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12095
12096           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12097           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12098
12099           /* If all code is in the text section, then the compilation
12100              unit base address defaults to DW_AT_low_pc, which is the
12101              base of the text section.  */
12102           if (!have_multiple_function_sections)
12103             {
12104               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12105                                     text_section_label,
12106                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12107               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12108                                     text_section_label, NULL);
12109             }
12110
12111           /* Otherwise, the compilation unit base address is zero,
12112              which allows us to use absolute addresses, and not worry
12113              about whether the target supports cross-section
12114              arithmetic.  */
12115           else
12116             {
12117               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12118                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12119               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12120             }
12121
12122           fmt = NULL;
12123         }
12124
12125       /* Negative block_num stands for an index into ranges_by_label.  */
12126       else if (block_num < 0)
12127         {
12128           int lab_idx = - block_num - 1;
12129
12130           if (!have_multiple_function_sections)
12131             {
12132               gcc_unreachable ();
12133 #if 0
12134               /* If we ever use add_ranges_by_labels () for a single
12135                  function section, all we have to do is to take out
12136                  the #if 0 above.  */
12137               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12138                                     ranges_by_label[lab_idx].begin,
12139                                     text_section_label,
12140                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12141               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12142                                     ranges_by_label[lab_idx].end,
12143                                     text_section_label, NULL);
12144 #endif
12145             }
12146           else
12147             {
12148               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12149                                    ranges_by_label[lab_idx].begin,
12150                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12151               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12152                                    ranges_by_label[lab_idx].end,
12153                                    NULL);
12154             }
12155         }
12156       else
12157         {
12158           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12159           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12160           fmt = start_fmt;
12161         }
12162     }
12163 }
12164
12165 /* Data structure containing information about input files.  */
12166 struct file_info
12167 {
12168   const char *path;     /* Complete file name.  */
12169   const char *fname;    /* File name part.  */
12170   int length;           /* Length of entire string.  */
12171   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
12172   int dir_idx;          /* Index in directory table.  */
12173 };
12174
12175 /* Data structure containing information about directories with source
12176    files.  */
12177 struct dir_info
12178 {
12179   const char *path;     /* Path including directory name.  */
12180   int length;           /* Path length.  */
12181   int prefix;           /* Index of directory entry which is a prefix.  */
12182   int count;            /* Number of files in this directory.  */
12183   int dir_idx;          /* Index of directory used as base.  */
12184 };
12185
12186 /* Callback function for file_info comparison.  We sort by looking at
12187    the directories in the path.  */
12188
12189 static int
12190 file_info_cmp (const void *p1, const void *p2)
12191 {
12192   const struct file_info *const s1 = (const struct file_info *) p1;
12193   const struct file_info *const s2 = (const struct file_info *) p2;
12194   const unsigned char *cp1;
12195   const unsigned char *cp2;
12196
12197   /* Take care of file names without directories.  We need to make sure that
12198      we return consistent values to qsort since some will get confused if
12199      we return the same value when identical operands are passed in opposite
12200      orders.  So if neither has a directory, return 0 and otherwise return
12201      1 or -1 depending on which one has the directory.  */
12202   if ((s1->path == s1->fname || s2->path == s2->fname))
12203     return (s2->path == s2->fname) - (s1->path == s1->fname);
12204
12205   cp1 = (const unsigned char *) s1->path;
12206   cp2 = (const unsigned char *) s2->path;
12207
12208   while (1)
12209     {
12210       ++cp1;
12211       ++cp2;
12212       /* Reached the end of the first path?  If so, handle like above.  */
12213       if ((cp1 == (const unsigned char *) s1->fname)
12214           || (cp2 == (const unsigned char *) s2->fname))
12215         return ((cp2 == (const unsigned char *) s2->fname)
12216                 - (cp1 == (const unsigned char *) s1->fname));
12217
12218       /* Character of current path component the same?  */
12219       else if (*cp1 != *cp2)
12220         return *cp1 - *cp2;
12221     }
12222 }
12223
12224 struct file_name_acquire_data
12225 {
12226   struct file_info *files;
12227   int used_files;
12228   int max_files;
12229 };
12230
12231 /* Traversal function for the hash table.  */
12232
12233 static int
12234 file_name_acquire (void ** slot, void *data)
12235 {
12236   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12237   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12238   struct file_info *fi;
12239   const char *f;
12240
12241   gcc_assert (fnad->max_files >= d->emitted_number);
12242
12243   if (! d->emitted_number)
12244     return 1;
12245
12246   gcc_assert (fnad->max_files != fnad->used_files);
12247
12248   fi = fnad->files + fnad->used_files++;
12249
12250   /* Skip all leading "./".  */
12251   f = d->filename;
12252   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12253     f += 2;
12254
12255   /* Create a new array entry.  */
12256   fi->path = f;
12257   fi->length = strlen (f);
12258   fi->file_idx = d;
12259
12260   /* Search for the file name part.  */
12261   f = strrchr (f, DIR_SEPARATOR);
12262 #if defined (DIR_SEPARATOR_2)
12263   {
12264     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12265
12266     if (g != NULL)
12267       {
12268         if (f == NULL || f < g)
12269           f = g;
12270       }
12271   }
12272 #endif
12273
12274   fi->fname = f == NULL ? fi->path : f + 1;
12275   return 1;
12276 }
12277
12278 /* Output the directory table and the file name table.  We try to minimize
12279    the total amount of memory needed.  A heuristic is used to avoid large
12280    slowdowns with many input files.  */
12281
12282 static void
12283 output_file_names (void)
12284 {
12285   struct file_name_acquire_data fnad;
12286   int numfiles;
12287   struct file_info *files;
12288   struct dir_info *dirs;
12289   int *saved;
12290   int *savehere;
12291   int *backmap;
12292   int ndirs;
12293   int idx_offset;
12294   int i;
12295
12296   if (!last_emitted_file)
12297     {
12298       dw2_asm_output_data (1, 0, "End directory table");
12299       dw2_asm_output_data (1, 0, "End file name table");
12300       return;
12301     }
12302
12303   numfiles = last_emitted_file->emitted_number;
12304
12305   /* Allocate the various arrays we need.  */
12306   files = XALLOCAVEC (struct file_info, numfiles);
12307   dirs = XALLOCAVEC (struct dir_info, numfiles);
12308
12309   fnad.files = files;
12310   fnad.used_files = 0;
12311   fnad.max_files = numfiles;
12312   htab_traverse (file_table, file_name_acquire, &fnad);
12313   gcc_assert (fnad.used_files == fnad.max_files);
12314
12315   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12316
12317   /* Find all the different directories used.  */
12318   dirs[0].path = files[0].path;
12319   dirs[0].length = files[0].fname - files[0].path;
12320   dirs[0].prefix = -1;
12321   dirs[0].count = 1;
12322   dirs[0].dir_idx = 0;
12323   files[0].dir_idx = 0;
12324   ndirs = 1;
12325
12326   for (i = 1; i < numfiles; i++)
12327     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12328         && memcmp (dirs[ndirs - 1].path, files[i].path,
12329                    dirs[ndirs - 1].length) == 0)
12330       {
12331         /* Same directory as last entry.  */
12332         files[i].dir_idx = ndirs - 1;
12333         ++dirs[ndirs - 1].count;
12334       }
12335     else
12336       {
12337         int j;
12338
12339         /* This is a new directory.  */
12340         dirs[ndirs].path = files[i].path;
12341         dirs[ndirs].length = files[i].fname - files[i].path;
12342         dirs[ndirs].count = 1;
12343         dirs[ndirs].dir_idx = ndirs;
12344         files[i].dir_idx = ndirs;
12345
12346         /* Search for a prefix.  */
12347         dirs[ndirs].prefix = -1;
12348         for (j = 0; j < ndirs; j++)
12349           if (dirs[j].length < dirs[ndirs].length
12350               && dirs[j].length > 1
12351               && (dirs[ndirs].prefix == -1
12352                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12353               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12354             dirs[ndirs].prefix = j;
12355
12356         ++ndirs;
12357       }
12358
12359   /* Now to the actual work.  We have to find a subset of the directories which
12360      allow expressing the file name using references to the directory table
12361      with the least amount of characters.  We do not do an exhaustive search
12362      where we would have to check out every combination of every single
12363      possible prefix.  Instead we use a heuristic which provides nearly optimal
12364      results in most cases and never is much off.  */
12365   saved = XALLOCAVEC (int, ndirs);
12366   savehere = XALLOCAVEC (int, ndirs);
12367
12368   memset (saved, '\0', ndirs * sizeof (saved[0]));
12369   for (i = 0; i < ndirs; i++)
12370     {
12371       int j;
12372       int total;
12373
12374       /* We can always save some space for the current directory.  But this
12375          does not mean it will be enough to justify adding the directory.  */
12376       savehere[i] = dirs[i].length;
12377       total = (savehere[i] - saved[i]) * dirs[i].count;
12378
12379       for (j = i + 1; j < ndirs; j++)
12380         {
12381           savehere[j] = 0;
12382           if (saved[j] < dirs[i].length)
12383             {
12384               /* Determine whether the dirs[i] path is a prefix of the
12385                  dirs[j] path.  */
12386               int k;
12387
12388               k = dirs[j].prefix;
12389               while (k != -1 && k != (int) i)
12390                 k = dirs[k].prefix;
12391
12392               if (k == (int) i)
12393                 {
12394                   /* Yes it is.  We can possibly save some memory by
12395                      writing the filenames in dirs[j] relative to
12396                      dirs[i].  */
12397                   savehere[j] = dirs[i].length;
12398                   total += (savehere[j] - saved[j]) * dirs[j].count;
12399                 }
12400             }
12401         }
12402
12403       /* Check whether we can save enough to justify adding the dirs[i]
12404          directory.  */
12405       if (total > dirs[i].length + 1)
12406         {
12407           /* It's worthwhile adding.  */
12408           for (j = i; j < ndirs; j++)
12409             if (savehere[j] > 0)
12410               {
12411                 /* Remember how much we saved for this directory so far.  */
12412                 saved[j] = savehere[j];
12413
12414                 /* Remember the prefix directory.  */
12415                 dirs[j].dir_idx = i;
12416               }
12417         }
12418     }
12419
12420   /* Emit the directory name table.  */
12421   idx_offset = dirs[0].length > 0 ? 1 : 0;
12422   for (i = 1 - idx_offset; i < ndirs; i++)
12423     dw2_asm_output_nstring (dirs[i].path,
12424                             dirs[i].length
12425                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12426                             "Directory Entry: %#x", i + idx_offset);
12427
12428   dw2_asm_output_data (1, 0, "End directory table");
12429
12430   /* We have to emit them in the order of emitted_number since that's
12431      used in the debug info generation.  To do this efficiently we
12432      generate a back-mapping of the indices first.  */
12433   backmap = XALLOCAVEC (int, numfiles);
12434   for (i = 0; i < numfiles; i++)
12435     backmap[files[i].file_idx->emitted_number - 1] = i;
12436
12437   /* Now write all the file names.  */
12438   for (i = 0; i < numfiles; i++)
12439     {
12440       int file_idx = backmap[i];
12441       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12442
12443 #ifdef VMS_DEBUGGING_INFO
12444 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12445
12446       /* Setting these fields can lead to debugger miscomparisons,
12447          but VMS Debug requires them to be set correctly.  */
12448
12449       int ver;
12450       long long cdt;
12451       long siz;
12452       int maxfilelen = strlen (files[file_idx].path)
12453                                + dirs[dir_idx].length
12454                                + MAX_VMS_VERSION_LEN + 1;
12455       char *filebuf = XALLOCAVEC (char, maxfilelen);
12456
12457       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12458       snprintf (filebuf, maxfilelen, "%s;%d",
12459                 files[file_idx].path + dirs[dir_idx].length, ver);
12460
12461       dw2_asm_output_nstring
12462         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12463
12464       /* Include directory index.  */
12465       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12466
12467       /* Modification time.  */
12468       dw2_asm_output_data_uleb128
12469         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12470           ? cdt : 0,
12471          NULL);
12472
12473       /* File length in bytes.  */
12474       dw2_asm_output_data_uleb128
12475         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12476           ? siz : 0,
12477          NULL);
12478 #else
12479       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12480                               "File Entry: %#x", (unsigned) i + 1);
12481
12482       /* Include directory index.  */
12483       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12484
12485       /* Modification time.  */
12486       dw2_asm_output_data_uleb128 (0, NULL);
12487
12488       /* File length in bytes.  */
12489       dw2_asm_output_data_uleb128 (0, NULL);
12490 #endif /* VMS_DEBUGGING_INFO */
12491     }
12492
12493   dw2_asm_output_data (1, 0, "End file name table");
12494 }
12495
12496
12497 /* Output one line number table into the .debug_line section.  */
12498
12499 static void
12500 output_one_line_info_table (dw_line_info_table *table)
12501 {
12502   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12503   unsigned int current_line = 1;
12504   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12505   dw_line_info_entry *ent;
12506   size_t i;
12507
12508   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
12509     {
12510       switch (ent->opcode)
12511         {
12512         case LI_set_address:
12513           /* ??? Unfortunately, we have little choice here currently, and
12514              must always use the most general form.  GCC does not know the
12515              address delta itself, so we can't use DW_LNS_advance_pc.  Many
12516              ports do have length attributes which will give an upper bound
12517              on the address range.  We could perhaps use length attributes
12518              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12519           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12520
12521           /* This can handle any delta.  This takes
12522              4+DWARF2_ADDR_SIZE bytes.  */
12523           dw2_asm_output_data (1, 0, "set address %s", line_label);
12524           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12525           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12526           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12527           break;
12528
12529         case LI_set_line:
12530           if (ent->val == current_line)
12531             {
12532               /* We still need to start a new row, so output a copy insn.  */
12533               dw2_asm_output_data (1, DW_LNS_copy,
12534                                    "copy line %u", current_line);
12535             }
12536           else
12537             {
12538               int line_offset = ent->val - current_line;
12539               int line_delta = line_offset - DWARF_LINE_BASE;
12540
12541               current_line = ent->val;
12542               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12543                 {
12544                   /* This can handle deltas from -10 to 234, using the current
12545                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12546                      This takes 1 byte.  */
12547                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12548                                        "line %u", current_line);
12549                 }
12550               else
12551                 {
12552                   /* This can handle any delta.  This takes at least 4 bytes,
12553                      depending on the value being encoded.  */
12554                   dw2_asm_output_data (1, DW_LNS_advance_line,
12555                                        "advance to line %u", current_line);
12556                   dw2_asm_output_data_sleb128 (line_offset, NULL);
12557                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
12558                 }
12559             }
12560           break;
12561
12562         case LI_set_file:
12563           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12564           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12565           break;
12566
12567         case LI_set_column:
12568           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12569           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12570           break;
12571
12572         case LI_negate_stmt:
12573           current_is_stmt = !current_is_stmt;
12574           dw2_asm_output_data (1, DW_LNS_negate_stmt,
12575                                "is_stmt %d", current_is_stmt);
12576           break;
12577
12578         case LI_set_prologue_end:
12579           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12580                                "set prologue end");
12581           break;
12582           
12583         case LI_set_epilogue_begin:
12584           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12585                                "set epilogue begin");
12586           break;
12587
12588         case LI_set_discriminator:
12589           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12590           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12591           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12592           dw2_asm_output_data_uleb128 (ent->val, NULL);
12593           break;
12594         }
12595     }
12596
12597   /* Emit debug info for the address of the end of the table.  */
12598   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12599   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12600   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12601   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12602
12603   dw2_asm_output_data (1, 0, "end sequence");
12604   dw2_asm_output_data_uleb128 (1, NULL);
12605   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12606 }
12607
12608 /* Output the source line number correspondence information.  This
12609    information goes into the .debug_line section.  */
12610
12611 static void
12612 output_line_info (void)
12613 {
12614   char l1[20], l2[20], p1[20], p2[20];
12615   int ver = dwarf_version;
12616   bool saw_one = false;
12617   int opc;
12618
12619   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12620   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12621   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12622   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12623
12624   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12625     dw2_asm_output_data (4, 0xffffffff,
12626       "Initial length escape value indicating 64-bit DWARF extension");
12627   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12628                         "Length of Source Line Info");
12629   ASM_OUTPUT_LABEL (asm_out_file, l1);
12630
12631   dw2_asm_output_data (2, ver, "DWARF Version");
12632   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12633   ASM_OUTPUT_LABEL (asm_out_file, p1);
12634
12635   /* Define the architecture-dependent minimum instruction length (in bytes).
12636      In this implementation of DWARF, this field is used for information
12637      purposes only.  Since GCC generates assembly language, we have no
12638      a priori knowledge of how many instruction bytes are generated for each
12639      source line, and therefore can use only the DW_LNE_set_address and
12640      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12641      this as '1', which is "correct enough" for all architectures,
12642      and don't let the target override.  */
12643   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12644
12645   if (ver >= 4)
12646     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12647                          "Maximum Operations Per Instruction");
12648   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12649                        "Default is_stmt_start flag");
12650   dw2_asm_output_data (1, DWARF_LINE_BASE,
12651                        "Line Base Value (Special Opcodes)");
12652   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12653                        "Line Range Value (Special Opcodes)");
12654   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12655                        "Special Opcode Base");
12656
12657   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12658     {
12659       int n_op_args;
12660       switch (opc)
12661         {
12662         case DW_LNS_advance_pc:
12663         case DW_LNS_advance_line:
12664         case DW_LNS_set_file:
12665         case DW_LNS_set_column:
12666         case DW_LNS_fixed_advance_pc:
12667         case DW_LNS_set_isa:
12668           n_op_args = 1;
12669           break;
12670         default:
12671           n_op_args = 0;
12672           break;
12673         }
12674
12675       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12676                            opc, n_op_args);
12677     }
12678
12679   /* Write out the information about the files we use.  */
12680   output_file_names ();
12681   ASM_OUTPUT_LABEL (asm_out_file, p2);
12682
12683   if (separate_line_info)
12684     {
12685       dw_line_info_table *table;
12686       size_t i;
12687
12688       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
12689         if (table->in_use)
12690           {
12691             output_one_line_info_table (table);
12692             saw_one = true;
12693           }
12694     }
12695   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12696     {
12697       output_one_line_info_table (cold_text_section_line_info);
12698       saw_one = true;
12699     }
12700
12701   /* ??? Some Darwin linkers crash on a .debug_line section with no
12702      sequences.  Further, merely a DW_LNE_end_sequence entry is not
12703      sufficient -- the address column must also be initialized.
12704      Make sure to output at least one set_address/end_sequence pair,
12705      choosing .text since that section is always present.  */
12706   if (text_section_line_info->in_use || !saw_one)
12707     output_one_line_info_table (text_section_line_info);
12708
12709   /* Output the marker for the end of the line number info.  */
12710   ASM_OUTPUT_LABEL (asm_out_file, l2);
12711 }
12712 \f
12713 /* Given a pointer to a tree node for some base type, return a pointer to
12714    a DIE that describes the given type.
12715
12716    This routine must only be called for GCC type nodes that correspond to
12717    Dwarf base (fundamental) types.  */
12718
12719 static dw_die_ref
12720 base_type_die (tree type)
12721 {
12722   dw_die_ref base_type_result;
12723   enum dwarf_type encoding;
12724
12725   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12726     return 0;
12727
12728   /* If this is a subtype that should not be emitted as a subrange type,
12729      use the base type.  See subrange_type_for_debug_p.  */
12730   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12731     type = TREE_TYPE (type);
12732
12733   switch (TREE_CODE (type))
12734     {
12735     case INTEGER_TYPE:
12736       if ((dwarf_version >= 4 || !dwarf_strict)
12737           && TYPE_NAME (type)
12738           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12739           && DECL_IS_BUILTIN (TYPE_NAME (type))
12740           && DECL_NAME (TYPE_NAME (type)))
12741         {
12742           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12743           if (strcmp (name, "char16_t") == 0
12744               || strcmp (name, "char32_t") == 0)
12745             {
12746               encoding = DW_ATE_UTF;
12747               break;
12748             }
12749         }
12750       if (TYPE_STRING_FLAG (type))
12751         {
12752           if (TYPE_UNSIGNED (type))
12753             encoding = DW_ATE_unsigned_char;
12754           else
12755             encoding = DW_ATE_signed_char;
12756         }
12757       else if (TYPE_UNSIGNED (type))
12758         encoding = DW_ATE_unsigned;
12759       else
12760         encoding = DW_ATE_signed;
12761       break;
12762
12763     case REAL_TYPE:
12764       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12765         {
12766           if (dwarf_version >= 3 || !dwarf_strict)
12767             encoding = DW_ATE_decimal_float;
12768           else
12769             encoding = DW_ATE_lo_user;
12770         }
12771       else
12772         encoding = DW_ATE_float;
12773       break;
12774
12775     case FIXED_POINT_TYPE:
12776       if (!(dwarf_version >= 3 || !dwarf_strict))
12777         encoding = DW_ATE_lo_user;
12778       else if (TYPE_UNSIGNED (type))
12779         encoding = DW_ATE_unsigned_fixed;
12780       else
12781         encoding = DW_ATE_signed_fixed;
12782       break;
12783
12784       /* Dwarf2 doesn't know anything about complex ints, so use
12785          a user defined type for it.  */
12786     case COMPLEX_TYPE:
12787       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12788         encoding = DW_ATE_complex_float;
12789       else
12790         encoding = DW_ATE_lo_user;
12791       break;
12792
12793     case BOOLEAN_TYPE:
12794       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12795       encoding = DW_ATE_boolean;
12796       break;
12797
12798     default:
12799       /* No other TREE_CODEs are Dwarf fundamental types.  */
12800       gcc_unreachable ();
12801     }
12802
12803   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12804
12805   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12806                    int_size_in_bytes (type));
12807   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12808
12809   return base_type_result;
12810 }
12811
12812 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12813    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12814
12815 static inline int
12816 is_base_type (tree type)
12817 {
12818   switch (TREE_CODE (type))
12819     {
12820     case ERROR_MARK:
12821     case VOID_TYPE:
12822     case INTEGER_TYPE:
12823     case REAL_TYPE:
12824     case FIXED_POINT_TYPE:
12825     case COMPLEX_TYPE:
12826     case BOOLEAN_TYPE:
12827       return 1;
12828
12829     case ARRAY_TYPE:
12830     case RECORD_TYPE:
12831     case UNION_TYPE:
12832     case QUAL_UNION_TYPE:
12833     case ENUMERAL_TYPE:
12834     case FUNCTION_TYPE:
12835     case METHOD_TYPE:
12836     case POINTER_TYPE:
12837     case REFERENCE_TYPE:
12838     case NULLPTR_TYPE:
12839     case OFFSET_TYPE:
12840     case LANG_TYPE:
12841     case VECTOR_TYPE:
12842       return 0;
12843
12844     default:
12845       gcc_unreachable ();
12846     }
12847
12848   return 0;
12849 }
12850
12851 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12852    node, return the size in bits for the type if it is a constant, or else
12853    return the alignment for the type if the type's size is not constant, or
12854    else return BITS_PER_WORD if the type actually turns out to be an
12855    ERROR_MARK node.  */
12856
12857 static inline unsigned HOST_WIDE_INT
12858 simple_type_size_in_bits (const_tree type)
12859 {
12860   if (TREE_CODE (type) == ERROR_MARK)
12861     return BITS_PER_WORD;
12862   else if (TYPE_SIZE (type) == NULL_TREE)
12863     return 0;
12864   else if (host_integerp (TYPE_SIZE (type), 1))
12865     return tree_low_cst (TYPE_SIZE (type), 1);
12866   else
12867     return TYPE_ALIGN (type);
12868 }
12869
12870 /* Similarly, but return a double_int instead of UHWI.  */
12871
12872 static inline double_int
12873 double_int_type_size_in_bits (const_tree type)
12874 {
12875   if (TREE_CODE (type) == ERROR_MARK)
12876     return uhwi_to_double_int (BITS_PER_WORD);
12877   else if (TYPE_SIZE (type) == NULL_TREE)
12878     return double_int_zero;
12879   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12880     return tree_to_double_int (TYPE_SIZE (type));
12881   else
12882     return uhwi_to_double_int (TYPE_ALIGN (type));
12883 }
12884
12885 /*  Given a pointer to a tree node for a subrange type, return a pointer
12886     to a DIE that describes the given type.  */
12887
12888 static dw_die_ref
12889 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12890 {
12891   dw_die_ref subrange_die;
12892   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12893
12894   if (context_die == NULL)
12895     context_die = comp_unit_die ();
12896
12897   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12898
12899   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12900     {
12901       /* The size of the subrange type and its base type do not match,
12902          so we need to generate a size attribute for the subrange type.  */
12903       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12904     }
12905
12906   if (low)
12907     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12908   if (high)
12909     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12910
12911   return subrange_die;
12912 }
12913
12914 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12915    entry that chains various modifiers in front of the given type.  */
12916
12917 static dw_die_ref
12918 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12919                    dw_die_ref context_die)
12920 {
12921   enum tree_code code = TREE_CODE (type);
12922   dw_die_ref mod_type_die;
12923   dw_die_ref sub_die = NULL;
12924   tree item_type = NULL;
12925   tree qualified_type;
12926   tree name, low, high;
12927
12928   if (code == ERROR_MARK)
12929     return NULL;
12930
12931   /* See if we already have the appropriately qualified variant of
12932      this type.  */
12933   qualified_type
12934     = get_qualified_type (type,
12935                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12936                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12937
12938   if (qualified_type == sizetype
12939       && TYPE_NAME (qualified_type)
12940       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12941     {
12942       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12943
12944       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12945                            && TYPE_PRECISION (t)
12946                            == TYPE_PRECISION (qualified_type)
12947                            && TYPE_UNSIGNED (t)
12948                            == TYPE_UNSIGNED (qualified_type));
12949       qualified_type = t;
12950     }
12951
12952   /* If we do, then we can just use its DIE, if it exists.  */
12953   if (qualified_type)
12954     {
12955       mod_type_die = lookup_type_die (qualified_type);
12956       if (mod_type_die)
12957         return mod_type_die;
12958     }
12959
12960   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12961
12962   /* Handle C typedef types.  */
12963   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12964       && !DECL_ARTIFICIAL (name))
12965     {
12966       tree dtype = TREE_TYPE (name);
12967
12968       if (qualified_type == dtype)
12969         {
12970           /* For a named type, use the typedef.  */
12971           gen_type_die (qualified_type, context_die);
12972           return lookup_type_die (qualified_type);
12973         }
12974       else if (is_const_type < TYPE_READONLY (dtype)
12975                || is_volatile_type < TYPE_VOLATILE (dtype)
12976                || (is_const_type <= TYPE_READONLY (dtype)
12977                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12978                    && DECL_ORIGINAL_TYPE (name) != type))
12979         /* cv-unqualified version of named type.  Just use the unnamed
12980            type to which it refers.  */
12981         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12982                                   is_const_type, is_volatile_type,
12983                                   context_die);
12984       /* Else cv-qualified version of named type; fall through.  */
12985     }
12986
12987   if (is_const_type
12988       /* If both is_const_type and is_volatile_type, prefer the path
12989          which leads to a qualified type.  */
12990       && (!is_volatile_type
12991           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12992           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12993     {
12994       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12995       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12996     }
12997   else if (is_volatile_type)
12998     {
12999       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
13000       sub_die = modified_type_die (type, is_const_type, 0, context_die);
13001     }
13002   else if (code == POINTER_TYPE)
13003     {
13004       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
13005       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13006                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13007       item_type = TREE_TYPE (type);
13008       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13009         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13010                          TYPE_ADDR_SPACE (item_type));
13011     }
13012   else if (code == REFERENCE_TYPE)
13013     {
13014       if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
13015         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
13016                                 type);
13017       else
13018         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
13019       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13020                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13021       item_type = TREE_TYPE (type);
13022       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13023         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13024                          TYPE_ADDR_SPACE (item_type));
13025     }
13026   else if (code == INTEGER_TYPE
13027            && TREE_TYPE (type) != NULL_TREE
13028            && subrange_type_for_debug_p (type, &low, &high))
13029     {
13030       mod_type_die = subrange_type_die (type, low, high, context_die);
13031       item_type = TREE_TYPE (type);
13032     }
13033   else if (is_base_type (type))
13034     mod_type_die = base_type_die (type);
13035   else
13036     {
13037       gen_type_die (type, context_die);
13038
13039       /* We have to get the type_main_variant here (and pass that to the
13040          `lookup_type_die' routine) because the ..._TYPE node we have
13041          might simply be a *copy* of some original type node (where the
13042          copy was created to help us keep track of typedef names) and
13043          that copy might have a different TYPE_UID from the original
13044          ..._TYPE node.  */
13045       if (TREE_CODE (type) != VECTOR_TYPE)
13046         return lookup_type_die (type_main_variant (type));
13047       else
13048         /* Vectors have the debugging information in the type,
13049            not the main variant.  */
13050         return lookup_type_die (type);
13051     }
13052
13053   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13054      don't output a DW_TAG_typedef, since there isn't one in the
13055      user's program; just attach a DW_AT_name to the type.
13056      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13057      if the base type already has the same name.  */
13058   if (name
13059       && ((TREE_CODE (name) != TYPE_DECL
13060            && (qualified_type == TYPE_MAIN_VARIANT (type)
13061                || (!is_const_type && !is_volatile_type)))
13062           || (TREE_CODE (name) == TYPE_DECL
13063               && TREE_TYPE (name) == qualified_type
13064               && DECL_NAME (name))))
13065     {
13066       if (TREE_CODE (name) == TYPE_DECL)
13067         /* Could just call add_name_and_src_coords_attributes here,
13068            but since this is a builtin type it doesn't have any
13069            useful source coordinates anyway.  */
13070         name = DECL_NAME (name);
13071       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13072       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13073     }
13074   /* This probably indicates a bug.  */
13075   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13076     add_name_attribute (mod_type_die, "__unknown__");
13077
13078   if (qualified_type)
13079     equate_type_number_to_die (qualified_type, mod_type_die);
13080
13081   if (item_type)
13082     /* We must do this after the equate_type_number_to_die call, in case
13083        this is a recursive type.  This ensures that the modified_type_die
13084        recursion will terminate even if the type is recursive.  Recursive
13085        types are possible in Ada.  */
13086     sub_die = modified_type_die (item_type,
13087                                  TYPE_READONLY (item_type),
13088                                  TYPE_VOLATILE (item_type),
13089                                  context_die);
13090
13091   if (sub_die != NULL)
13092     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13093
13094   return mod_type_die;
13095 }
13096
13097 /* Generate DIEs for the generic parameters of T.
13098    T must be either a generic type or a generic function.
13099    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13100
13101 static void
13102 gen_generic_params_dies (tree t)
13103 {
13104   tree parms, args;
13105   int parms_num, i;
13106   dw_die_ref die = NULL;
13107
13108   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13109     return;
13110
13111   if (TYPE_P (t))
13112     die = lookup_type_die (t);
13113   else if (DECL_P (t))
13114     die = lookup_decl_die (t);
13115
13116   gcc_assert (die);
13117
13118   parms = lang_hooks.get_innermost_generic_parms (t);
13119   if (!parms)
13120     /* T has no generic parameter. It means T is neither a generic type
13121        or function. End of story.  */
13122     return;
13123
13124   parms_num = TREE_VEC_LENGTH (parms);
13125   args = lang_hooks.get_innermost_generic_args (t);
13126   for (i = 0; i < parms_num; i++)
13127     {
13128       tree parm, arg, arg_pack_elems;
13129
13130       parm = TREE_VEC_ELT (parms, i);
13131       arg = TREE_VEC_ELT (args, i);
13132       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13133       gcc_assert (parm && TREE_VALUE (parm) && arg);
13134
13135       if (parm && TREE_VALUE (parm) && arg)
13136         {
13137           /* If PARM represents a template parameter pack,
13138              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13139              by DW_TAG_template_*_parameter DIEs for the argument
13140              pack elements of ARG. Note that ARG would then be
13141              an argument pack.  */
13142           if (arg_pack_elems)
13143             template_parameter_pack_die (TREE_VALUE (parm),
13144                                          arg_pack_elems,
13145                                          die);
13146           else
13147             generic_parameter_die (TREE_VALUE (parm), arg,
13148                                    true /* Emit DW_AT_name */, die);
13149         }
13150     }
13151 }
13152
13153 /* Create and return a DIE for PARM which should be
13154    the representation of a generic type parameter.
13155    For instance, in the C++ front end, PARM would be a template parameter.
13156    ARG is the argument to PARM.
13157    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13158    name of the PARM.
13159    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13160    as a child node.  */
13161
13162 static dw_die_ref
13163 generic_parameter_die (tree parm, tree arg,
13164                        bool emit_name_p,
13165                        dw_die_ref parent_die)
13166 {
13167   dw_die_ref tmpl_die = NULL;
13168   const char *name = NULL;
13169
13170   if (!parm || !DECL_NAME (parm) || !arg)
13171     return NULL;
13172
13173   /* We support non-type generic parameters and arguments,
13174      type generic parameters and arguments, as well as
13175      generic generic parameters (a.k.a. template template parameters in C++)
13176      and arguments.  */
13177   if (TREE_CODE (parm) == PARM_DECL)
13178     /* PARM is a nontype generic parameter  */
13179     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13180   else if (TREE_CODE (parm) == TYPE_DECL)
13181     /* PARM is a type generic parameter.  */
13182     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13183   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13184     /* PARM is a generic generic parameter.
13185        Its DIE is a GNU extension. It shall have a
13186        DW_AT_name attribute to represent the name of the template template
13187        parameter, and a DW_AT_GNU_template_name attribute to represent the
13188        name of the template template argument.  */
13189     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13190                         parent_die, parm);
13191   else
13192     gcc_unreachable ();
13193
13194   if (tmpl_die)
13195     {
13196       tree tmpl_type;
13197
13198       /* If PARM is a generic parameter pack, it means we are
13199          emitting debug info for a template argument pack element.
13200          In other terms, ARG is a template argument pack element.
13201          In that case, we don't emit any DW_AT_name attribute for
13202          the die.  */
13203       if (emit_name_p)
13204         {
13205           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13206           gcc_assert (name);
13207           add_AT_string (tmpl_die, DW_AT_name, name);
13208         }
13209
13210       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13211         {
13212           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13213              TMPL_DIE should have a child DW_AT_type attribute that is set
13214              to the type of the argument to PARM, which is ARG.
13215              If PARM is a type generic parameter, TMPL_DIE should have a
13216              child DW_AT_type that is set to ARG.  */
13217           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13218           add_type_attribute (tmpl_die, tmpl_type, 0,
13219                               TREE_THIS_VOLATILE (tmpl_type),
13220                               parent_die);
13221         }
13222       else
13223         {
13224           /* So TMPL_DIE is a DIE representing a
13225              a generic generic template parameter, a.k.a template template
13226              parameter in C++ and arg is a template.  */
13227
13228           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13229              to the name of the argument.  */
13230           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13231           if (name)
13232             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13233         }
13234
13235       if (TREE_CODE (parm) == PARM_DECL)
13236         /* So PARM is a non-type generic parameter.
13237            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13238            attribute of TMPL_DIE which value represents the value
13239            of ARG.
13240            We must be careful here:
13241            The value of ARG might reference some function decls.
13242            We might currently be emitting debug info for a generic
13243            type and types are emitted before function decls, we don't
13244            know if the function decls referenced by ARG will actually be
13245            emitted after cgraph computations.
13246            So must defer the generation of the DW_AT_const_value to
13247            after cgraph is ready.  */
13248         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13249     }
13250
13251   return tmpl_die;
13252 }
13253
13254 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13255    PARM_PACK must be a template parameter pack. The returned DIE
13256    will be child DIE of PARENT_DIE.  */
13257
13258 static dw_die_ref
13259 template_parameter_pack_die (tree parm_pack,
13260                              tree parm_pack_args,
13261                              dw_die_ref parent_die)
13262 {
13263   dw_die_ref die;
13264   int j;
13265
13266   gcc_assert (parent_die && parm_pack);
13267
13268   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13269   add_name_and_src_coords_attributes (die, parm_pack);
13270   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13271     generic_parameter_die (parm_pack,
13272                            TREE_VEC_ELT (parm_pack_args, j),
13273                            false /* Don't emit DW_AT_name */,
13274                            die);
13275   return die;
13276 }
13277
13278 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13279    an enumerated type.  */
13280
13281 static inline int
13282 type_is_enum (const_tree type)
13283 {
13284   return TREE_CODE (type) == ENUMERAL_TYPE;
13285 }
13286
13287 /* Return the DBX register number described by a given RTL node.  */
13288
13289 static unsigned int
13290 dbx_reg_number (const_rtx rtl)
13291 {
13292   unsigned regno = REGNO (rtl);
13293
13294   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13295
13296 #ifdef LEAF_REG_REMAP
13297   if (current_function_uses_only_leaf_regs)
13298     {
13299       int leaf_reg = LEAF_REG_REMAP (regno);
13300       if (leaf_reg != -1)
13301         regno = (unsigned) leaf_reg;
13302     }
13303 #endif
13304
13305   return DBX_REGISTER_NUMBER (regno);
13306 }
13307
13308 /* Optionally add a DW_OP_piece term to a location description expression.
13309    DW_OP_piece is only added if the location description expression already
13310    doesn't end with DW_OP_piece.  */
13311
13312 static void
13313 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13314 {
13315   dw_loc_descr_ref loc;
13316
13317   if (*list_head != NULL)
13318     {
13319       /* Find the end of the chain.  */
13320       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13321         ;
13322
13323       if (loc->dw_loc_opc != DW_OP_piece)
13324         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13325     }
13326 }
13327
13328 /* Return a location descriptor that designates a machine register or
13329    zero if there is none.  */
13330
13331 static dw_loc_descr_ref
13332 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13333 {
13334   rtx regs;
13335
13336   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13337     return 0;
13338
13339   /* We only use "frame base" when we're sure we're talking about the
13340      post-prologue local stack frame.  We do this by *not* running
13341      register elimination until this point, and recognizing the special
13342      argument pointer and soft frame pointer rtx's.
13343      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13344   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13345       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13346     {
13347       dw_loc_descr_ref result = NULL;
13348
13349       if (dwarf_version >= 4 || !dwarf_strict)
13350         {
13351           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13352                                        initialized);
13353           if (result)
13354             add_loc_descr (&result,
13355                            new_loc_descr (DW_OP_stack_value, 0, 0));
13356         }
13357       return result;
13358     }
13359
13360   regs = targetm.dwarf_register_span (rtl);
13361
13362   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13363     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13364   else
13365     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13366 }
13367
13368 /* Return a location descriptor that designates a machine register for
13369    a given hard register number.  */
13370
13371 static dw_loc_descr_ref
13372 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13373 {
13374   dw_loc_descr_ref reg_loc_descr;
13375
13376   if (regno <= 31)
13377     reg_loc_descr
13378       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13379   else
13380     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13381
13382   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13383     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13384
13385   return reg_loc_descr;
13386 }
13387
13388 /* Given an RTL of a register, return a location descriptor that
13389    designates a value that spans more than one register.  */
13390
13391 static dw_loc_descr_ref
13392 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13393                              enum var_init_status initialized)
13394 {
13395   int nregs, size, i;
13396   unsigned reg;
13397   dw_loc_descr_ref loc_result = NULL;
13398
13399   reg = REGNO (rtl);
13400 #ifdef LEAF_REG_REMAP
13401   if (current_function_uses_only_leaf_regs)
13402     {
13403       int leaf_reg = LEAF_REG_REMAP (reg);
13404       if (leaf_reg != -1)
13405         reg = (unsigned) leaf_reg;
13406     }
13407 #endif
13408   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13409   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13410
13411   /* Simple, contiguous registers.  */
13412   if (regs == NULL_RTX)
13413     {
13414       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13415
13416       loc_result = NULL;
13417       while (nregs--)
13418         {
13419           dw_loc_descr_ref t;
13420
13421           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13422                                       VAR_INIT_STATUS_INITIALIZED);
13423           add_loc_descr (&loc_result, t);
13424           add_loc_descr_op_piece (&loc_result, size);
13425           ++reg;
13426         }
13427       return loc_result;
13428     }
13429
13430   /* Now onto stupid register sets in non contiguous locations.  */
13431
13432   gcc_assert (GET_CODE (regs) == PARALLEL);
13433
13434   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13435   loc_result = NULL;
13436
13437   for (i = 0; i < XVECLEN (regs, 0); ++i)
13438     {
13439       dw_loc_descr_ref t;
13440
13441       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13442                                   VAR_INIT_STATUS_INITIALIZED);
13443       add_loc_descr (&loc_result, t);
13444       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13445       add_loc_descr_op_piece (&loc_result, size);
13446     }
13447
13448   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13449     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13450   return loc_result;
13451 }
13452
13453 /* Return a location descriptor that designates a constant.  */
13454
13455 static dw_loc_descr_ref
13456 int_loc_descriptor (HOST_WIDE_INT i)
13457 {
13458   enum dwarf_location_atom op;
13459
13460   /* Pick the smallest representation of a constant, rather than just
13461      defaulting to the LEB encoding.  */
13462   if (i >= 0)
13463     {
13464       if (i <= 31)
13465         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13466       else if (i <= 0xff)
13467         op = DW_OP_const1u;
13468       else if (i <= 0xffff)
13469         op = DW_OP_const2u;
13470       else if (HOST_BITS_PER_WIDE_INT == 32
13471                || i <= 0xffffffff)
13472         op = DW_OP_const4u;
13473       else
13474         op = DW_OP_constu;
13475     }
13476   else
13477     {
13478       if (i >= -0x80)
13479         op = DW_OP_const1s;
13480       else if (i >= -0x8000)
13481         op = DW_OP_const2s;
13482       else if (HOST_BITS_PER_WIDE_INT == 32
13483                || i >= -0x80000000)
13484         op = DW_OP_const4s;
13485       else
13486         op = DW_OP_consts;
13487     }
13488
13489   return new_loc_descr (op, i, 0);
13490 }
13491
13492 /* Return loc description representing "address" of integer value.
13493    This can appear only as toplevel expression.  */
13494
13495 static dw_loc_descr_ref
13496 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13497 {
13498   int litsize;
13499   dw_loc_descr_ref loc_result = NULL;
13500
13501   if (!(dwarf_version >= 4 || !dwarf_strict))
13502     return NULL;
13503
13504   if (i >= 0)
13505     {
13506       if (i <= 31)
13507         litsize = 1;
13508       else if (i <= 0xff)
13509         litsize = 2;
13510       else if (i <= 0xffff)
13511         litsize = 3;
13512       else if (HOST_BITS_PER_WIDE_INT == 32
13513                || i <= 0xffffffff)
13514         litsize = 5;
13515       else
13516         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13517     }
13518   else
13519     {
13520       if (i >= -0x80)
13521         litsize = 2;
13522       else if (i >= -0x8000)
13523         litsize = 3;
13524       else if (HOST_BITS_PER_WIDE_INT == 32
13525                || i >= -0x80000000)
13526         litsize = 5;
13527       else
13528         litsize = 1 + size_of_sleb128 (i);
13529     }
13530   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13531      is more compact.  For DW_OP_stack_value we need:
13532      litsize + 1 (DW_OP_stack_value)
13533      and for DW_OP_implicit_value:
13534      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13535   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13536     {
13537       loc_result = int_loc_descriptor (i);
13538       add_loc_descr (&loc_result,
13539                      new_loc_descr (DW_OP_stack_value, 0, 0));
13540       return loc_result;
13541     }
13542
13543   loc_result = new_loc_descr (DW_OP_implicit_value,
13544                               size, 0);
13545   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13546   loc_result->dw_loc_oprnd2.v.val_int = i;
13547   return loc_result;
13548 }
13549
13550 /* Return a location descriptor that designates a base+offset location.  */
13551
13552 static dw_loc_descr_ref
13553 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13554                  enum var_init_status initialized)
13555 {
13556   unsigned int regno;
13557   dw_loc_descr_ref result;
13558   dw_fde_ref fde = current_fde ();
13559
13560   /* We only use "frame base" when we're sure we're talking about the
13561      post-prologue local stack frame.  We do this by *not* running
13562      register elimination until this point, and recognizing the special
13563      argument pointer and soft frame pointer rtx's.  */
13564   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13565     {
13566       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13567
13568       if (elim != reg)
13569         {
13570           if (GET_CODE (elim) == PLUS)
13571             {
13572               offset += INTVAL (XEXP (elim, 1));
13573               elim = XEXP (elim, 0);
13574             }
13575           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13576                        && (elim == hard_frame_pointer_rtx
13577                            || elim == stack_pointer_rtx))
13578                       || elim == (frame_pointer_needed
13579                                   ? hard_frame_pointer_rtx
13580                                   : stack_pointer_rtx));
13581
13582           /* If drap register is used to align stack, use frame
13583              pointer + offset to access stack variables.  If stack
13584              is aligned without drap, use stack pointer + offset to
13585              access stack variables.  */
13586           if (crtl->stack_realign_tried
13587               && reg == frame_pointer_rtx)
13588             {
13589               int base_reg
13590                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13591                                       ? HARD_FRAME_POINTER_REGNUM
13592                                       : REGNO (elim));
13593               return new_reg_loc_descr (base_reg, offset);
13594             }
13595
13596           offset += frame_pointer_fb_offset;
13597           return new_loc_descr (DW_OP_fbreg, offset, 0);
13598         }
13599     }
13600   else if (!optimize
13601            && fde
13602            && (fde->drap_reg == REGNO (reg)
13603                || fde->vdrap_reg == REGNO (reg)))
13604     {
13605       /* Use cfa+offset to represent the location of arguments passed
13606          on the stack when drap is used to align stack.
13607          Only do this when not optimizing, for optimized code var-tracking
13608          is supposed to track where the arguments live and the register
13609          used as vdrap or drap in some spot might be used for something
13610          else in other part of the routine.  */
13611       return new_loc_descr (DW_OP_fbreg, offset, 0);
13612     }
13613
13614   regno = dbx_reg_number (reg);
13615   if (regno <= 31)
13616     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13617                             offset, 0);
13618   else
13619     result = new_loc_descr (DW_OP_bregx, regno, offset);
13620
13621   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13622     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13623
13624   return result;
13625 }
13626
13627 /* Return true if this RTL expression describes a base+offset calculation.  */
13628
13629 static inline int
13630 is_based_loc (const_rtx rtl)
13631 {
13632   return (GET_CODE (rtl) == PLUS
13633           && ((REG_P (XEXP (rtl, 0))
13634                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13635                && CONST_INT_P (XEXP (rtl, 1)))));
13636 }
13637
13638 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13639    failed.  */
13640
13641 static dw_loc_descr_ref
13642 tls_mem_loc_descriptor (rtx mem)
13643 {
13644   tree base;
13645   dw_loc_descr_ref loc_result;
13646
13647   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13648     return NULL;
13649
13650   base = get_base_address (MEM_EXPR (mem));
13651   if (base == NULL
13652       || TREE_CODE (base) != VAR_DECL
13653       || !DECL_THREAD_LOCAL_P (base))
13654     return NULL;
13655
13656   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13657   if (loc_result == NULL)
13658     return NULL;
13659
13660   if (INTVAL (MEM_OFFSET (mem)))
13661     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13662
13663   return loc_result;
13664 }
13665
13666 /* Output debug info about reason why we failed to expand expression as dwarf
13667    expression.  */
13668
13669 static void
13670 expansion_failed (tree expr, rtx rtl, char const *reason)
13671 {
13672   if (dump_file && (dump_flags & TDF_DETAILS))
13673     {
13674       fprintf (dump_file, "Failed to expand as dwarf: ");
13675       if (expr)
13676         print_generic_expr (dump_file, expr, dump_flags);
13677       if (rtl)
13678         {
13679           fprintf (dump_file, "\n");
13680           print_rtl (dump_file, rtl);
13681         }
13682       fprintf (dump_file, "\nReason: %s\n", reason);
13683     }
13684 }
13685
13686 /* Helper function for const_ok_for_output, called either directly
13687    or via for_each_rtx.  */
13688
13689 static int
13690 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13691 {
13692   rtx rtl = *rtlp;
13693
13694   if (GET_CODE (rtl) == UNSPEC)
13695     {
13696       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13697          we can't express it in the debug info.  */
13698 #ifdef ENABLE_CHECKING
13699       /* Don't complain about TLS UNSPECs, those are just too hard to
13700          delegitimize.  */
13701       if (XVECLEN (rtl, 0) != 1
13702           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13703           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13704           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13705           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13706         inform (current_function_decl
13707                 ? DECL_SOURCE_LOCATION (current_function_decl)
13708                 : UNKNOWN_LOCATION,
13709 #if NUM_UNSPEC_VALUES > 0
13710                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13711                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13712                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13713                 XINT (rtl, 1));
13714 #else
13715                 "non-delegitimized UNSPEC %d found in variable location",
13716                 XINT (rtl, 1));
13717 #endif
13718 #endif
13719       expansion_failed (NULL_TREE, rtl,
13720                         "UNSPEC hasn't been delegitimized.\n");
13721       return 1;
13722     }
13723
13724   if (GET_CODE (rtl) != SYMBOL_REF)
13725     return 0;
13726
13727   if (CONSTANT_POOL_ADDRESS_P (rtl))
13728     {
13729       bool marked;
13730       get_pool_constant_mark (rtl, &marked);
13731       /* If all references to this pool constant were optimized away,
13732          it was not output and thus we can't represent it.  */
13733       if (!marked)
13734         {
13735           expansion_failed (NULL_TREE, rtl,
13736                             "Constant was removed from constant pool.\n");
13737           return 1;
13738         }
13739     }
13740
13741   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13742     return 1;
13743
13744   /* Avoid references to external symbols in debug info, on several targets
13745      the linker might even refuse to link when linking a shared library,
13746      and in many other cases the relocations for .debug_info/.debug_loc are
13747      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13748      to be defined within the same shared library or executable are fine.  */
13749   if (SYMBOL_REF_EXTERNAL_P (rtl))
13750     {
13751       tree decl = SYMBOL_REF_DECL (rtl);
13752
13753       if (decl == NULL || !targetm.binds_local_p (decl))
13754         {
13755           expansion_failed (NULL_TREE, rtl,
13756                             "Symbol not defined in current TU.\n");
13757           return 1;
13758         }
13759     }
13760
13761   return 0;
13762 }
13763
13764 /* Return true if constant RTL can be emitted in DW_OP_addr or
13765    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13766    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13767
13768 static bool
13769 const_ok_for_output (rtx rtl)
13770 {
13771   if (GET_CODE (rtl) == SYMBOL_REF)
13772     return const_ok_for_output_1 (&rtl, NULL) == 0;
13773
13774   if (GET_CODE (rtl) == CONST)
13775     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13776
13777   return true;
13778 }
13779
13780 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13781    if possible, NULL otherwise.  */
13782
13783 static dw_die_ref
13784 base_type_for_mode (enum machine_mode mode, bool unsignedp)
13785 {
13786   dw_die_ref type_die;
13787   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13788
13789   if (type == NULL)
13790     return NULL;
13791   switch (TREE_CODE (type))
13792     {
13793     case INTEGER_TYPE:
13794     case REAL_TYPE:
13795       break;
13796     default:
13797       return NULL;
13798     }
13799   type_die = lookup_type_die (type);
13800   if (!type_die)
13801     type_die = modified_type_die (type, false, false, comp_unit_die ());
13802   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13803     return NULL;
13804   return type_die;
13805 }
13806
13807 /* The following routine converts the RTL for a variable or parameter
13808    (resident in memory) into an equivalent Dwarf representation of a
13809    mechanism for getting the address of that same variable onto the top of a
13810    hypothetical "address evaluation" stack.
13811
13812    When creating memory location descriptors, we are effectively transforming
13813    the RTL for a memory-resident object into its Dwarf postfix expression
13814    equivalent.  This routine recursively descends an RTL tree, turning
13815    it into Dwarf postfix code as it goes.
13816
13817    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13818
13819    MEM_MODE is the mode of the memory reference, needed to handle some
13820    autoincrement addressing modes.
13821
13822    Return 0 if we can't represent the location.  */
13823
13824 static dw_loc_descr_ref
13825 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13826                     enum machine_mode mem_mode,
13827                     enum var_init_status initialized)
13828 {
13829   dw_loc_descr_ref mem_loc_result = NULL;
13830   enum dwarf_location_atom op;
13831   dw_loc_descr_ref op0, op1;
13832
13833   if (mode == VOIDmode)
13834     mode = GET_MODE (rtl);
13835
13836   /* Note that for a dynamically sized array, the location we will generate a
13837      description of here will be the lowest numbered location which is
13838      actually within the array.  That's *not* necessarily the same as the
13839      zeroth element of the array.  */
13840
13841   rtl = targetm.delegitimize_address (rtl);
13842
13843   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13844     return NULL;
13845
13846   switch (GET_CODE (rtl))
13847     {
13848     case POST_INC:
13849     case POST_DEC:
13850     case POST_MODIFY:
13851       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13852
13853     case SUBREG:
13854       /* The case of a subreg may arise when we have a local (register)
13855          variable or a formal (register) parameter which doesn't quite fill
13856          up an entire register.  For now, just assume that it is
13857          legitimate to make the Dwarf info refer to the whole register which
13858          contains the given subreg.  */
13859       if (!subreg_lowpart_p (rtl))
13860         break;
13861       if (GET_MODE_CLASS (mode) == MODE_INT
13862           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
13863           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13864           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
13865         {
13866           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
13867                                                GET_MODE (SUBREG_REG (rtl)),
13868                                                mem_mode, initialized);
13869           break;
13870         }
13871       if (dwarf_strict)
13872         break;
13873       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
13874         break;
13875       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
13876           && (GET_MODE_CLASS (mode) != MODE_INT
13877               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
13878         break;
13879       else
13880         {
13881           dw_die_ref type_die;
13882           dw_loc_descr_ref cvt;
13883
13884           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
13885                                                GET_MODE (SUBREG_REG (rtl)),
13886                                                mem_mode, initialized);
13887           if (mem_loc_result == NULL)
13888             break;
13889           type_die = base_type_for_mode (mode, 0);
13890           if (type_die == NULL)
13891             {
13892               mem_loc_result = NULL;
13893               break;
13894             }
13895           if (GET_MODE_SIZE (mode)
13896               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
13897             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13898           else
13899             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13900           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13901           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13902           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13903           add_loc_descr (&mem_loc_result, cvt);
13904         }
13905       break;
13906
13907     case REG:
13908       if (GET_MODE_CLASS (mode) != MODE_INT
13909           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13910 #ifdef POINTERS_EXTEND_UNSIGNED
13911               && (mode != Pmode || mem_mode == VOIDmode)
13912 #endif
13913               ))
13914         {
13915           dw_die_ref type_die;
13916
13917           if (dwarf_strict)
13918             break;
13919           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13920             break;
13921           type_die = base_type_for_mode (mode, 0);
13922           if (type_die == NULL)
13923             break;
13924           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13925                                           dbx_reg_number (rtl), 0);
13926           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13927           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13928           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13929           break;
13930         }
13931       /* Whenever a register number forms a part of the description of the
13932          method for calculating the (dynamic) address of a memory resident
13933          object, DWARF rules require the register number be referred to as
13934          a "base register".  This distinction is not based in any way upon
13935          what category of register the hardware believes the given register
13936          belongs to.  This is strictly DWARF terminology we're dealing with
13937          here. Note that in cases where the location of a memory-resident
13938          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13939          OP_CONST (0)) the actual DWARF location descriptor that we generate
13940          may just be OP_BASEREG (basereg).  This may look deceptively like
13941          the object in question was allocated to a register (rather than in
13942          memory) so DWARF consumers need to be aware of the subtle
13943          distinction between OP_REG and OP_BASEREG.  */
13944       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13945         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13946       else if (stack_realign_drap
13947                && crtl->drap_reg
13948                && crtl->args.internal_arg_pointer == rtl
13949                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13950         {
13951           /* If RTL is internal_arg_pointer, which has been optimized
13952              out, use DRAP instead.  */
13953           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13954                                             VAR_INIT_STATUS_INITIALIZED);
13955         }
13956       break;
13957
13958     case SIGN_EXTEND:
13959     case ZERO_EXTEND:
13960       gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
13961       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13962                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13963       if (op0 == 0)
13964         break;
13965       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13966         {
13967           int shift = DWARF2_ADDR_SIZE
13968                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13969           shift *= BITS_PER_UNIT;
13970           if (GET_CODE (rtl) == SIGN_EXTEND)
13971             op = DW_OP_shra;
13972           else
13973             op = DW_OP_shr;
13974           mem_loc_result = op0;
13975           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13976           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13977           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13978           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13979         }
13980       else if (!dwarf_strict)
13981         {
13982           dw_die_ref type_die1, type_die2;
13983           dw_loc_descr_ref cvt;
13984
13985           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
13986                                           GET_CODE (rtl) == ZERO_EXTEND);
13987           if (type_die1 == NULL)
13988             break;
13989           type_die2 = base_type_for_mode (mode, 0);
13990           if (type_die2 == NULL)
13991             break;
13992           mem_loc_result = op0;
13993           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13994           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13995           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
13996           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13997           add_loc_descr (&mem_loc_result, cvt);
13998           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13999           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14000           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14001           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14002           add_loc_descr (&mem_loc_result, cvt);
14003         }
14004       break;
14005
14006     case MEM:
14007       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14008                                            get_address_mode (rtl), mode,
14009                                            VAR_INIT_STATUS_INITIALIZED);
14010       if (mem_loc_result == NULL)
14011         mem_loc_result = tls_mem_loc_descriptor (rtl);
14012       if (mem_loc_result != 0)
14013         {
14014           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14015               || GET_MODE_CLASS (mode) != MODE_INT)
14016             {
14017               dw_die_ref type_die;
14018               dw_loc_descr_ref deref;
14019
14020               if (dwarf_strict)
14021                 return NULL;
14022               type_die = base_type_for_mode (mode, 0);
14023               if (type_die == NULL)
14024                 return NULL;
14025               deref = new_loc_descr (DW_OP_GNU_deref_type,
14026                                      GET_MODE_SIZE (mode), 0);
14027               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14028               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14029               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14030               add_loc_descr (&mem_loc_result, deref);
14031             }
14032           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14033             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14034           else
14035             add_loc_descr (&mem_loc_result,
14036                            new_loc_descr (DW_OP_deref_size,
14037                                           GET_MODE_SIZE (mode), 0));
14038         }
14039       else
14040         {
14041           rtx new_rtl = avoid_constant_pool_reference (rtl);
14042           if (new_rtl != rtl)
14043             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
14044         }
14045       break;
14046
14047     case LO_SUM:
14048       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14049
14050     case LABEL_REF:
14051       /* Some ports can transform a symbol ref into a label ref, because
14052          the symbol ref is too far away and has to be dumped into a constant
14053          pool.  */
14054     case CONST:
14055     case SYMBOL_REF:
14056       if (GET_MODE_CLASS (mode) != MODE_INT
14057           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14058 #ifdef POINTERS_EXTEND_UNSIGNED
14059               && (mode != Pmode || mem_mode == VOIDmode)
14060 #endif
14061               ))
14062         break;
14063       if (GET_CODE (rtl) == SYMBOL_REF
14064           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14065         {
14066           dw_loc_descr_ref temp;
14067
14068           /* If this is not defined, we have no way to emit the data.  */
14069           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14070             break;
14071
14072           /* We used to emit DW_OP_addr here, but that's wrong, since
14073              DW_OP_addr should be relocated by the debug info consumer,
14074              while DW_OP_GNU_push_tls_address operand should not.  */
14075           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
14076                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
14077           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
14078           temp->dw_loc_oprnd1.v.val_addr = rtl;
14079           temp->dtprel = true;
14080
14081           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
14082           add_loc_descr (&mem_loc_result, temp);
14083
14084           break;
14085         }
14086
14087       if (!const_ok_for_output (rtl))
14088         break;
14089
14090     symref:
14091       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14092       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14093       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14094       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14095       break;
14096
14097     case CONCAT:
14098     case CONCATN:
14099     case VAR_LOCATION:
14100     case DEBUG_IMPLICIT_PTR:
14101       expansion_failed (NULL_TREE, rtl,
14102                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14103       return 0;
14104
14105     case ENTRY_VALUE:
14106       if (dwarf_strict)
14107         return NULL;
14108       if (REG_P (ENTRY_VALUE_EXP (rtl)))
14109         {
14110           if (GET_MODE_CLASS (mode) != MODE_INT
14111               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14112             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14113                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14114           else
14115             op0
14116               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
14117                                         VAR_INIT_STATUS_INITIALIZED);
14118         }
14119       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14120                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14121         {
14122           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14123                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14124           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14125             return NULL;
14126         }
14127       else
14128         gcc_unreachable ();
14129       if (op0 == NULL)
14130         return NULL;
14131       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
14132       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14133       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14134       return mem_loc_result;
14135
14136     case PRE_MODIFY:
14137       /* Extract the PLUS expression nested inside and fall into
14138          PLUS code below.  */
14139       rtl = XEXP (rtl, 1);
14140       goto plus;
14141
14142     case PRE_INC:
14143     case PRE_DEC:
14144       /* Turn these into a PLUS expression and fall into the PLUS code
14145          below.  */
14146       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14147                           GEN_INT (GET_CODE (rtl) == PRE_INC
14148                                    ? GET_MODE_UNIT_SIZE (mem_mode)
14149                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
14150
14151       /* ... fall through ...  */
14152
14153     case PLUS:
14154     plus:
14155       if (is_based_loc (rtl)
14156           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14157           && GET_MODE_CLASS (mode) == MODE_INT)
14158         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14159                                           INTVAL (XEXP (rtl, 1)),
14160                                           VAR_INIT_STATUS_INITIALIZED);
14161       else
14162         {
14163           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14164                                                VAR_INIT_STATUS_INITIALIZED);
14165           if (mem_loc_result == 0)
14166             break;
14167
14168           if (CONST_INT_P (XEXP (rtl, 1))
14169               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14170             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14171           else
14172             {
14173               dw_loc_descr_ref mem_loc_result2
14174                 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14175                                       VAR_INIT_STATUS_INITIALIZED);
14176               if (mem_loc_result2 == 0)
14177                 break;
14178               add_loc_descr (&mem_loc_result, mem_loc_result2);
14179               add_loc_descr (&mem_loc_result,
14180                              new_loc_descr (DW_OP_plus, 0, 0));
14181             }
14182         }
14183       break;
14184
14185     /* If a pseudo-reg is optimized away, it is possible for it to
14186        be replaced with a MEM containing a multiply or shift.  */
14187     case MINUS:
14188       op = DW_OP_minus;
14189       goto do_binop;
14190
14191     case MULT:
14192       op = DW_OP_mul;
14193       goto do_binop;
14194
14195     case DIV:
14196       op = DW_OP_div;
14197       goto do_binop;
14198
14199     case UMOD:
14200       op = DW_OP_mod;
14201       goto do_binop;
14202
14203     case ASHIFT:
14204       op = DW_OP_shl;
14205       goto do_shift;
14206
14207     case ASHIFTRT:
14208       op = DW_OP_shra;
14209       goto do_shift;
14210
14211     case LSHIFTRT:
14212       op = DW_OP_shr;
14213       goto do_shift;
14214
14215     do_shift:
14216       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14217                                 VAR_INIT_STATUS_INITIALIZED);
14218       op1 = mem_loc_descriptor (XEXP (rtl, 1),
14219                                 GET_MODE (XEXP (rtl, 1)) == VOIDmode
14220                                 ? mode : GET_MODE (XEXP (rtl, 1)), mem_mode,
14221                                 VAR_INIT_STATUS_INITIALIZED);
14222
14223       if (op0 == 0 || op1 == 0)
14224         break;
14225
14226       mem_loc_result = op0;
14227       add_loc_descr (&mem_loc_result, op1);
14228       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14229       break;
14230
14231     case AND:
14232       op = DW_OP_and;
14233       goto do_binop;
14234
14235     case IOR:
14236       op = DW_OP_or;
14237       goto do_binop;
14238
14239     case XOR:
14240       op = DW_OP_xor;
14241       goto do_binop;
14242
14243     do_binop:
14244       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14245                                 VAR_INIT_STATUS_INITIALIZED);
14246       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14247                                 VAR_INIT_STATUS_INITIALIZED);
14248
14249       if (op0 == 0 || op1 == 0)
14250         break;
14251
14252       mem_loc_result = op0;
14253       add_loc_descr (&mem_loc_result, op1);
14254       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14255       break;
14256
14257     case MOD:
14258       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14259                                 VAR_INIT_STATUS_INITIALIZED);
14260       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14261                                 VAR_INIT_STATUS_INITIALIZED);
14262
14263       if (op0 == 0 || op1 == 0)
14264         break;
14265
14266       mem_loc_result = op0;
14267       add_loc_descr (&mem_loc_result, op1);
14268       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14269       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14270       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14271       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14272       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14273       break;
14274
14275     case NOT:
14276       op = DW_OP_not;
14277       goto do_unop;
14278
14279     case ABS:
14280       op = DW_OP_abs;
14281       goto do_unop;
14282
14283     case NEG:
14284       op = DW_OP_neg;
14285       goto do_unop;
14286
14287     do_unop:
14288       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14289                                 VAR_INIT_STATUS_INITIALIZED);
14290
14291       if (op0 == 0)
14292         break;
14293
14294       mem_loc_result = op0;
14295       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14296       break;
14297
14298     case CONST_INT:
14299       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14300 #ifdef POINTERS_EXTEND_UNSIGNED
14301           || (mode == Pmode
14302               && mem_mode != VOIDmode
14303               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
14304 #endif
14305           )
14306         {
14307           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14308           break;
14309         }
14310       if (!dwarf_strict
14311           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
14312               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
14313         {
14314           dw_die_ref type_die = base_type_for_mode (mode, 0);
14315           if (type_die == NULL)
14316             return NULL;
14317           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
14318                                           INTVAL (rtl));
14319           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14320           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14321           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14322           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14323             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14324           else
14325             {
14326               mem_loc_result->dw_loc_oprnd2.val_class
14327                 = dw_val_class_const_double;
14328               mem_loc_result->dw_loc_oprnd2.v.val_double
14329                 = shwi_to_double_int (INTVAL (rtl));
14330             }
14331         }
14332       break;
14333
14334     case CONST_DOUBLE:
14335       if (!dwarf_strict)
14336         {
14337           dw_die_ref type_die;
14338
14339           /* Note that a CONST_DOUBLE rtx could represent either an integer
14340              or a floating-point constant.  A CONST_DOUBLE is used whenever
14341              the constant requires more than one word in order to be
14342              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14343           if (mode == VOIDmode
14344               || (GET_MODE (rtl) == VOIDmode
14345                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
14346             break;
14347           type_die = base_type_for_mode (mode, 0);
14348           if (type_die == NULL)
14349             return NULL;
14350           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
14351           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14352           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14353           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14354           if (SCALAR_FLOAT_MODE_P (mode))
14355             {
14356               unsigned int length = GET_MODE_SIZE (mode);
14357               unsigned char *array
14358                   = (unsigned char*) ggc_alloc_atomic (length);
14359
14360               insert_float (rtl, array);
14361               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14362               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14363               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14364               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14365             }
14366           else
14367             {
14368               mem_loc_result->dw_loc_oprnd2.val_class
14369                 = dw_val_class_const_double;
14370               mem_loc_result->dw_loc_oprnd2.v.val_double
14371                 = rtx_to_double_int (rtl);
14372             }
14373         }
14374       break;
14375
14376     case EQ:
14377       op = DW_OP_eq;
14378       goto do_scompare;
14379
14380     case GE:
14381       op = DW_OP_ge;
14382       goto do_scompare;
14383
14384     case GT:
14385       op = DW_OP_gt;
14386       goto do_scompare;
14387
14388     case LE:
14389       op = DW_OP_le;
14390       goto do_scompare;
14391
14392     case LT:
14393       op = DW_OP_lt;
14394       goto do_scompare;
14395
14396     case NE:
14397       op = DW_OP_ne;
14398       goto do_scompare;
14399
14400     do_scompare:
14401       {
14402         enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14403
14404         if (op_mode == VOIDmode)
14405           op_mode = GET_MODE (XEXP (rtl, 1));
14406         if (op_mode == VOIDmode)
14407           break;
14408
14409         if (dwarf_strict
14410             && (GET_MODE_CLASS (op_mode) != MODE_INT
14411                 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
14412           break;
14413
14414         op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14415                                   VAR_INIT_STATUS_INITIALIZED);
14416         op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14417                                   VAR_INIT_STATUS_INITIALIZED);
14418
14419         if (op0 == 0 || op1 == 0)
14420           break;
14421
14422         if (GET_MODE_CLASS (op_mode) == MODE_INT
14423             && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14424           {
14425             int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14426             shift *= BITS_PER_UNIT;
14427             /* For eq/ne, if the operands are known to be zero-extended,
14428                there is no need to do the fancy shifting up.  */
14429             if (op == DW_OP_eq || op == DW_OP_ne)
14430               {
14431                 dw_loc_descr_ref last0, last1;
14432                 for (last0 = op0;
14433                      last0->dw_loc_next != NULL;
14434                      last0 = last0->dw_loc_next)
14435                   ;
14436                 for (last1 = op1;
14437                      last1->dw_loc_next != NULL;
14438                      last1 = last1->dw_loc_next)
14439                   ;
14440                 /* deref_size zero extends, and for constants we can check
14441                    whether they are zero extended or not.  */
14442                 if (((last0->dw_loc_opc == DW_OP_deref_size
14443                       && last0->dw_loc_oprnd1.v.val_int
14444                          <= GET_MODE_SIZE (op_mode))
14445                      || (CONST_INT_P (XEXP (rtl, 0))
14446                          && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14447                              == (INTVAL (XEXP (rtl, 0))
14448                                  & GET_MODE_MASK (op_mode))))
14449                     && ((last1->dw_loc_opc == DW_OP_deref_size
14450                          && last1->dw_loc_oprnd1.v.val_int
14451                             <= GET_MODE_SIZE (op_mode))
14452                         || (CONST_INT_P (XEXP (rtl, 1))
14453                             && (unsigned HOST_WIDE_INT)
14454                                INTVAL (XEXP (rtl, 1))
14455                                == (INTVAL (XEXP (rtl, 1))
14456                                    & GET_MODE_MASK (op_mode)))))
14457                   goto do_compare;
14458               }
14459             add_loc_descr (&op0, int_loc_descriptor (shift));
14460             add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14461             if (CONST_INT_P (XEXP (rtl, 1)))
14462               op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14463             else
14464               {
14465                 add_loc_descr (&op1, int_loc_descriptor (shift));
14466                 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14467               }
14468           }
14469       }
14470
14471     do_compare:
14472       mem_loc_result = op0;
14473       add_loc_descr (&mem_loc_result, op1);
14474       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14475       if (STORE_FLAG_VALUE != 1)
14476         {
14477           add_loc_descr (&mem_loc_result,
14478                          int_loc_descriptor (STORE_FLAG_VALUE));
14479           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14480         }
14481       break;
14482
14483     case GEU:
14484       op = DW_OP_ge;
14485       goto do_ucompare;
14486
14487     case GTU:
14488       op = DW_OP_gt;
14489       goto do_ucompare;
14490
14491     case LEU:
14492       op = DW_OP_le;
14493       goto do_ucompare;
14494
14495     case LTU:
14496       op = DW_OP_lt;
14497       goto do_ucompare;
14498
14499     do_ucompare:
14500       {
14501         enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14502
14503         if (op_mode == VOIDmode)
14504           op_mode = GET_MODE (XEXP (rtl, 1));
14505         if (op_mode == VOIDmode)
14506           break;
14507         if (GET_MODE_CLASS (op_mode) != MODE_INT)
14508           break;
14509
14510         if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14511           break;
14512
14513         if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14514             break;
14515
14516         op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14517                                   VAR_INIT_STATUS_INITIALIZED);
14518         op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14519                                   VAR_INIT_STATUS_INITIALIZED);
14520
14521         if (op0 == 0 || op1 == 0)
14522           break;
14523
14524         if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14525           {
14526             HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14527             dw_loc_descr_ref last0, last1;
14528             for (last0 = op0;
14529                  last0->dw_loc_next != NULL;
14530                  last0 = last0->dw_loc_next)
14531               ;
14532             for (last1 = op1;
14533                  last1->dw_loc_next != NULL;
14534                  last1 = last1->dw_loc_next)
14535               ;
14536             if (CONST_INT_P (XEXP (rtl, 0)))
14537               op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14538             /* deref_size zero extends, so no need to mask it again.  */
14539             else if (last0->dw_loc_opc != DW_OP_deref_size
14540                      || last0->dw_loc_oprnd1.v.val_int
14541                         > GET_MODE_SIZE (op_mode))
14542               {
14543                 add_loc_descr (&op0, int_loc_descriptor (mask));
14544                 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14545               }
14546             if (CONST_INT_P (XEXP (rtl, 1)))
14547               op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14548             /* deref_size zero extends, so no need to mask it again.  */
14549             else if (last1->dw_loc_opc != DW_OP_deref_size
14550                      || last1->dw_loc_oprnd1.v.val_int
14551                         > GET_MODE_SIZE (op_mode))
14552               {
14553                 add_loc_descr (&op1, int_loc_descriptor (mask));
14554                 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14555               }
14556           }
14557         else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14558           {
14559             HOST_WIDE_INT bias = 1;
14560             bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14561             add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14562             if (CONST_INT_P (XEXP (rtl, 1)))
14563               op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14564                                         + INTVAL (XEXP (rtl, 1)));
14565             else
14566               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14567                                                   bias, 0));
14568           }
14569         else
14570           {
14571             dw_die_ref type_die = base_type_for_mode (mode, 1);
14572             dw_loc_descr_ref cvt;
14573
14574             if (type_die == NULL)
14575               break;
14576             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14577             cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14578             cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14579             cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14580             add_loc_descr (&op0, cvt);
14581             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14582             cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14583             cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14584             cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14585             add_loc_descr (&op1, cvt);
14586           }
14587       }
14588       goto do_compare;
14589
14590     case UMIN:
14591     case UMAX:
14592       if (GET_MODE_CLASS (mode) != MODE_INT)
14593         break;
14594       /* FALLTHRU */
14595     case SMIN:
14596     case SMAX:
14597       if (dwarf_strict
14598           && (GET_MODE_CLASS (mode) != MODE_INT
14599               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14600         break;
14601
14602       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14603                                 VAR_INIT_STATUS_INITIALIZED);
14604       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14605                                 VAR_INIT_STATUS_INITIALIZED);
14606
14607       if (op0 == 0 || op1 == 0)
14608         break;
14609
14610       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14611       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14612       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14613       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14614         {
14615           if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14616             {
14617               HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14618               add_loc_descr (&op0, int_loc_descriptor (mask));
14619               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14620               add_loc_descr (&op1, int_loc_descriptor (mask));
14621               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14622             }
14623           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14624             {
14625               HOST_WIDE_INT bias = 1;
14626               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14627               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14628               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14629             }
14630           else
14631             {
14632               dw_die_ref type_die = base_type_for_mode (mode, 1);
14633               dw_loc_descr_ref cvt;
14634
14635               if (type_die == NULL)
14636                 break;
14637               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14638               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14639               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14640               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14641               add_loc_descr (&op0, cvt);
14642               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14643               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14644               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14645               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14646               add_loc_descr (&op1, cvt);
14647             }
14648         }
14649       else if (GET_MODE_CLASS (mode) == MODE_INT
14650                && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14651         {
14652           int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode);
14653           shift *= BITS_PER_UNIT;
14654           add_loc_descr (&op0, int_loc_descriptor (shift));
14655           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14656           add_loc_descr (&op1, int_loc_descriptor (shift));
14657           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14658         }
14659
14660       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14661         op = DW_OP_lt;
14662       else
14663         op = DW_OP_gt;
14664       mem_loc_result = op0;
14665       add_loc_descr (&mem_loc_result, op1);
14666       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14667       {
14668         dw_loc_descr_ref bra_node, drop_node;
14669
14670         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14671         add_loc_descr (&mem_loc_result, bra_node);
14672         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14673         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14674         add_loc_descr (&mem_loc_result, drop_node);
14675         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14676         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14677       }
14678       break;
14679
14680     case ZERO_EXTRACT:
14681     case SIGN_EXTRACT:
14682       if (CONST_INT_P (XEXP (rtl, 1))
14683           && CONST_INT_P (XEXP (rtl, 2))
14684           && ((unsigned) INTVAL (XEXP (rtl, 1))
14685               + (unsigned) INTVAL (XEXP (rtl, 2))
14686               <= GET_MODE_BITSIZE (mode))
14687           && GET_MODE_CLASS (mode) == MODE_INT
14688           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14689           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14690         {
14691           int shift, size;
14692           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14693                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14694           if (op0 == 0)
14695             break;
14696           if (GET_CODE (rtl) == SIGN_EXTRACT)
14697             op = DW_OP_shra;
14698           else
14699             op = DW_OP_shr;
14700           mem_loc_result = op0;
14701           size = INTVAL (XEXP (rtl, 1));
14702           shift = INTVAL (XEXP (rtl, 2));
14703           if (BITS_BIG_ENDIAN)
14704             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14705                     - shift - size;
14706           if (shift + size != (int) DWARF2_ADDR_SIZE)
14707             {
14708               add_loc_descr (&mem_loc_result,
14709                              int_loc_descriptor (DWARF2_ADDR_SIZE
14710                                                  - shift - size));
14711               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14712             }
14713           if (size != (int) DWARF2_ADDR_SIZE)
14714             {
14715               add_loc_descr (&mem_loc_result,
14716                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14717               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14718             }
14719         }
14720       break;
14721
14722     case IF_THEN_ELSE:
14723       {
14724         dw_loc_descr_ref op2, bra_node, drop_node;
14725         op0 = mem_loc_descriptor (XEXP (rtl, 0),
14726                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
14727                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
14728                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
14729         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14730                                   VAR_INIT_STATUS_INITIALIZED);
14731         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14732                                   VAR_INIT_STATUS_INITIALIZED);
14733         if (op0 == NULL || op1 == NULL || op2 == NULL)
14734           break;
14735
14736         mem_loc_result = op1;
14737         add_loc_descr (&mem_loc_result, op2);
14738         add_loc_descr (&mem_loc_result, op0);
14739         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14740         add_loc_descr (&mem_loc_result, bra_node);
14741         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14742         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14743         add_loc_descr (&mem_loc_result, drop_node);
14744         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14745         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14746       }
14747       break;
14748
14749     case FLOAT_EXTEND:
14750     case FLOAT_TRUNCATE:
14751     case FLOAT:
14752     case UNSIGNED_FLOAT:
14753     case FIX:
14754     case UNSIGNED_FIX:
14755       if (!dwarf_strict)
14756         {
14757           dw_die_ref type_die;
14758           dw_loc_descr_ref cvt;
14759
14760           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14761                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14762           if (op0 == NULL)
14763             break;
14764           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
14765               && (GET_CODE (rtl) == UNSIGNED_FLOAT
14766                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14767                      <= DWARF2_ADDR_SIZE))
14768             {
14769               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14770                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
14771               if (type_die == NULL)
14772                 break;
14773               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14774               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14775               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14776               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14777               add_loc_descr (&op0, cvt);
14778             }
14779           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14780           if (type_die == NULL)
14781             break;
14782           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14783           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14784           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14785           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14786           add_loc_descr (&op0, cvt);
14787           if (GET_MODE_CLASS (mode) == MODE_INT
14788               && (GET_CODE (rtl) == UNSIGNED_FIX
14789                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14790             {
14791               enum machine_mode outer_mode = mode;
14792               if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14793                 {
14794                   outer_mode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
14795                                               MODE_INT, 0);
14796                   if (outer_mode == BLKmode
14797                       || GET_MODE_SIZE (outer_mode) != DWARF2_ADDR_SIZE)
14798                     break;
14799                 }
14800               type_die = base_type_for_mode (outer_mode, 0);
14801               if (type_die == NULL)
14802                 break;
14803               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14804               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14805               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14806               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14807               add_loc_descr (&op0, cvt);
14808             }
14809           mem_loc_result = op0;
14810         }
14811       break;
14812
14813     case COMPARE:
14814     case ROTATE:
14815     case ROTATERT:
14816     case TRUNCATE:
14817       /* In theory, we could implement the above.  */
14818       /* DWARF cannot represent the unsigned compare operations
14819          natively.  */
14820     case SS_MULT:
14821     case US_MULT:
14822     case SS_DIV:
14823     case US_DIV:
14824     case SS_PLUS:
14825     case US_PLUS:
14826     case SS_MINUS:
14827     case US_MINUS:
14828     case SS_NEG:
14829     case US_NEG:
14830     case SS_ABS:
14831     case SS_ASHIFT:
14832     case US_ASHIFT:
14833     case SS_TRUNCATE:
14834     case US_TRUNCATE:
14835     case UDIV:
14836     case UNORDERED:
14837     case ORDERED:
14838     case UNEQ:
14839     case UNGE:
14840     case UNGT:
14841     case UNLE:
14842     case UNLT:
14843     case LTGT:
14844     case FRACT_CONVERT:
14845     case UNSIGNED_FRACT_CONVERT:
14846     case SAT_FRACT:
14847     case UNSIGNED_SAT_FRACT:
14848     case SQRT:
14849     case BSWAP:
14850     case FFS:
14851     case CLZ:
14852     case CTZ:
14853     case POPCOUNT:
14854     case PARITY:
14855     case ASM_OPERANDS:
14856     case VEC_MERGE:
14857     case VEC_SELECT:
14858     case VEC_CONCAT:
14859     case VEC_DUPLICATE:
14860     case UNSPEC:
14861     case HIGH:
14862     case FMA:
14863     case STRICT_LOW_PART:
14864     case CONST_VECTOR:
14865     case CONST_FIXED:
14866       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14867          can't express it in the debug info.  This can happen e.g. with some
14868          TLS UNSPECs.  */
14869       break;
14870
14871     case CONST_STRING:
14872       resolve_one_addr (&rtl, NULL);
14873       goto symref;
14874
14875     default:
14876 #ifdef ENABLE_CHECKING
14877       print_rtl (stderr, rtl);
14878       gcc_unreachable ();
14879 #else
14880       break;
14881 #endif
14882     }
14883
14884   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14885     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14886
14887   return mem_loc_result;
14888 }
14889
14890 /* Return a descriptor that describes the concatenation of two locations.
14891    This is typically a complex variable.  */
14892
14893 static dw_loc_descr_ref
14894 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14895 {
14896   dw_loc_descr_ref cc_loc_result = NULL;
14897   dw_loc_descr_ref x0_ref
14898     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14899   dw_loc_descr_ref x1_ref
14900     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14901
14902   if (x0_ref == 0 || x1_ref == 0)
14903     return 0;
14904
14905   cc_loc_result = x0_ref;
14906   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14907
14908   add_loc_descr (&cc_loc_result, x1_ref);
14909   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14910
14911   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14912     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14913
14914   return cc_loc_result;
14915 }
14916
14917 /* Return a descriptor that describes the concatenation of N
14918    locations.  */
14919
14920 static dw_loc_descr_ref
14921 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14922 {
14923   unsigned int i;
14924   dw_loc_descr_ref cc_loc_result = NULL;
14925   unsigned int n = XVECLEN (concatn, 0);
14926
14927   for (i = 0; i < n; ++i)
14928     {
14929       dw_loc_descr_ref ref;
14930       rtx x = XVECEXP (concatn, 0, i);
14931
14932       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14933       if (ref == NULL)
14934         return NULL;
14935
14936       add_loc_descr (&cc_loc_result, ref);
14937       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14938     }
14939
14940   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14941     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14942
14943   return cc_loc_result;
14944 }
14945
14946 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14947    for DEBUG_IMPLICIT_PTR RTL.  */
14948
14949 static dw_loc_descr_ref
14950 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14951 {
14952   dw_loc_descr_ref ret;
14953   dw_die_ref ref;
14954
14955   if (dwarf_strict)
14956     return NULL;
14957   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14958               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14959               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14960   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14961   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14962   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14963   if (ref)
14964     {
14965       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14966       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14967       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14968     }
14969   else
14970     {
14971       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14972       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14973     }
14974   return ret;
14975 }
14976
14977 /* Output a proper Dwarf location descriptor for a variable or parameter
14978    which is either allocated in a register or in a memory location.  For a
14979    register, we just generate an OP_REG and the register number.  For a
14980    memory location we provide a Dwarf postfix expression describing how to
14981    generate the (dynamic) address of the object onto the address stack.
14982
14983    MODE is mode of the decl if this loc_descriptor is going to be used in
14984    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14985    allowed, VOIDmode otherwise.
14986
14987    If we don't know how to describe it, return 0.  */
14988
14989 static dw_loc_descr_ref
14990 loc_descriptor (rtx rtl, enum machine_mode mode,
14991                 enum var_init_status initialized)
14992 {
14993   dw_loc_descr_ref loc_result = NULL;
14994
14995   switch (GET_CODE (rtl))
14996     {
14997     case SUBREG:
14998       /* The case of a subreg may arise when we have a local (register)
14999          variable or a formal (register) parameter which doesn't quite fill
15000          up an entire register.  For now, just assume that it is
15001          legitimate to make the Dwarf info refer to the whole register which
15002          contains the given subreg.  */
15003       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15004         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
15005       else
15006         goto do_default;
15007       break;
15008
15009     case REG:
15010       loc_result = reg_loc_descriptor (rtl, initialized);
15011       break;
15012
15013     case MEM:
15014       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15015                                        GET_MODE (rtl), initialized);
15016       if (loc_result == NULL)
15017         loc_result = tls_mem_loc_descriptor (rtl);
15018       if (loc_result == NULL)
15019         {
15020           rtx new_rtl = avoid_constant_pool_reference (rtl);
15021           if (new_rtl != rtl)
15022             loc_result = loc_descriptor (new_rtl, mode, initialized);
15023         }
15024       break;
15025
15026     case CONCAT:
15027       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15028                                           initialized);
15029       break;
15030
15031     case CONCATN:
15032       loc_result = concatn_loc_descriptor (rtl, initialized);
15033       break;
15034
15035     case VAR_LOCATION:
15036       /* Single part.  */
15037       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15038         {
15039           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15040           if (GET_CODE (loc) == EXPR_LIST)
15041             loc = XEXP (loc, 0);
15042           loc_result = loc_descriptor (loc, mode, initialized);
15043           break;
15044         }
15045
15046       rtl = XEXP (rtl, 1);
15047       /* FALLTHRU */
15048
15049     case PARALLEL:
15050       {
15051         rtvec par_elems = XVEC (rtl, 0);
15052         int num_elem = GET_NUM_ELEM (par_elems);
15053         enum machine_mode mode;
15054         int i;
15055
15056         /* Create the first one, so we have something to add to.  */
15057         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15058                                      VOIDmode, initialized);
15059         if (loc_result == NULL)
15060           return NULL;
15061         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15062         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15063         for (i = 1; i < num_elem; i++)
15064           {
15065             dw_loc_descr_ref temp;
15066
15067             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15068                                    VOIDmode, initialized);
15069             if (temp == NULL)
15070               return NULL;
15071             add_loc_descr (&loc_result, temp);
15072             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15073             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15074           }
15075       }
15076       break;
15077
15078     case CONST_INT:
15079       if (mode != VOIDmode && mode != BLKmode)
15080         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15081                                                     INTVAL (rtl));
15082       break;
15083
15084     case CONST_DOUBLE:
15085       if (mode == VOIDmode)
15086         mode = GET_MODE (rtl);
15087
15088       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15089         {
15090           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15091
15092           /* Note that a CONST_DOUBLE rtx could represent either an integer
15093              or a floating-point constant.  A CONST_DOUBLE is used whenever
15094              the constant requires more than one word in order to be
15095              adequately represented.  We output CONST_DOUBLEs as blocks.  */
15096           loc_result = new_loc_descr (DW_OP_implicit_value,
15097                                       GET_MODE_SIZE (mode), 0);
15098           if (SCALAR_FLOAT_MODE_P (mode))
15099             {
15100               unsigned int length = GET_MODE_SIZE (mode);
15101               unsigned char *array
15102                   = (unsigned char*) ggc_alloc_atomic (length);
15103
15104               insert_float (rtl, array);
15105               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15106               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15107               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15108               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15109             }
15110           else
15111             {
15112               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15113               loc_result->dw_loc_oprnd2.v.val_double
15114                 = rtx_to_double_int (rtl);
15115             }
15116         }
15117       break;
15118
15119     case CONST_VECTOR:
15120       if (mode == VOIDmode)
15121         mode = GET_MODE (rtl);
15122
15123       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15124         {
15125           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15126           unsigned int length = CONST_VECTOR_NUNITS (rtl);
15127           unsigned char *array = (unsigned char *)
15128             ggc_alloc_atomic (length * elt_size);
15129           unsigned int i;
15130           unsigned char *p;
15131
15132           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15133           switch (GET_MODE_CLASS (mode))
15134             {
15135             case MODE_VECTOR_INT:
15136               for (i = 0, p = array; i < length; i++, p += elt_size)
15137                 {
15138                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15139                   double_int val = rtx_to_double_int (elt);
15140
15141                   if (elt_size <= sizeof (HOST_WIDE_INT))
15142                     insert_int (double_int_to_shwi (val), elt_size, p);
15143                   else
15144                     {
15145                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15146                       insert_double (val, p);
15147                     }
15148                 }
15149               break;
15150
15151             case MODE_VECTOR_FLOAT:
15152               for (i = 0, p = array; i < length; i++, p += elt_size)
15153                 {
15154                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15155                   insert_float (elt, p);
15156                 }
15157               break;
15158
15159             default:
15160               gcc_unreachable ();
15161             }
15162
15163           loc_result = new_loc_descr (DW_OP_implicit_value,
15164                                       length * elt_size, 0);
15165           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15166           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15167           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15168           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15169         }
15170       break;
15171
15172     case CONST:
15173       if (mode == VOIDmode
15174           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
15175           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
15176           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15177         {
15178           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15179           break;
15180         }
15181       /* FALLTHROUGH */
15182     case SYMBOL_REF:
15183       if (!const_ok_for_output (rtl))
15184         break;
15185     case LABEL_REF:
15186       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15187           && (dwarf_version >= 4 || !dwarf_strict))
15188         {
15189           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
15190           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
15191           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
15192           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15193           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15194         }
15195       break;
15196
15197     case DEBUG_IMPLICIT_PTR:
15198       loc_result = implicit_ptr_descriptor (rtl, 0);
15199       break;
15200
15201     case PLUS:
15202       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15203           && CONST_INT_P (XEXP (rtl, 1)))
15204         {
15205           loc_result
15206             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15207           break;
15208         }
15209       /* FALLTHRU */
15210     do_default:
15211     default:
15212       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
15213            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15214            && dwarf_version >= 4)
15215           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15216         {
15217           /* Value expression.  */
15218           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15219           if (loc_result)
15220             add_loc_descr (&loc_result,
15221                            new_loc_descr (DW_OP_stack_value, 0, 0));
15222         }
15223       break;
15224     }
15225
15226   return loc_result;
15227 }
15228
15229 /* We need to figure out what section we should use as the base for the
15230    address ranges where a given location is valid.
15231    1. If this particular DECL has a section associated with it, use that.
15232    2. If this function has a section associated with it, use that.
15233    3. Otherwise, use the text section.
15234    XXX: If you split a variable across multiple sections, we won't notice.  */
15235
15236 static const char *
15237 secname_for_decl (const_tree decl)
15238 {
15239   const char *secname;
15240
15241   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
15242     {
15243       tree sectree = DECL_SECTION_NAME (decl);
15244       secname = TREE_STRING_POINTER (sectree);
15245     }
15246   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15247     {
15248       tree sectree = DECL_SECTION_NAME (current_function_decl);
15249       secname = TREE_STRING_POINTER (sectree);
15250     }
15251   else if (cfun && in_cold_section_p)
15252     secname = crtl->subsections.cold_section_label;
15253   else
15254     secname = text_section_label;
15255
15256   return secname;
15257 }
15258
15259 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
15260
15261 static bool
15262 decl_by_reference_p (tree decl)
15263 {
15264   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15265            || TREE_CODE (decl) == VAR_DECL)
15266           && DECL_BY_REFERENCE (decl));
15267 }
15268
15269 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15270    for VARLOC.  */
15271
15272 static dw_loc_descr_ref
15273 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15274                enum var_init_status initialized)
15275 {
15276   int have_address = 0;
15277   dw_loc_descr_ref descr;
15278   enum machine_mode mode;
15279
15280   if (want_address != 2)
15281     {
15282       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15283       /* Single part.  */
15284       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15285         {
15286           varloc = PAT_VAR_LOCATION_LOC (varloc);
15287           if (GET_CODE (varloc) == EXPR_LIST)
15288             varloc = XEXP (varloc, 0);
15289           mode = GET_MODE (varloc);
15290           if (MEM_P (varloc))
15291             {
15292               rtx addr = XEXP (varloc, 0);
15293               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15294                                           mode, initialized);
15295               if (descr)
15296                 have_address = 1;
15297               else
15298                 {
15299                   rtx x = avoid_constant_pool_reference (varloc);
15300                   if (x != varloc)
15301                     descr = mem_loc_descriptor (x, mode, VOIDmode,
15302                                                 initialized);
15303                 }
15304             }
15305           else
15306             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15307         }
15308       else
15309         return 0;
15310     }
15311   else
15312     {
15313       if (GET_CODE (varloc) == VAR_LOCATION)
15314         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15315       else
15316         mode = DECL_MODE (loc);
15317       descr = loc_descriptor (varloc, mode, initialized);
15318       have_address = 1;
15319     }
15320
15321   if (!descr)
15322     return 0;
15323
15324   if (want_address == 2 && !have_address
15325       && (dwarf_version >= 4 || !dwarf_strict))
15326     {
15327       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15328         {
15329           expansion_failed (loc, NULL_RTX,
15330                             "DWARF address size mismatch");
15331           return 0;
15332         }
15333       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15334       have_address = 1;
15335     }
15336   /* Show if we can't fill the request for an address.  */
15337   if (want_address && !have_address)
15338     {
15339       expansion_failed (loc, NULL_RTX,
15340                         "Want address and only have value");
15341       return 0;
15342     }
15343
15344   /* If we've got an address and don't want one, dereference.  */
15345   if (!want_address && have_address)
15346     {
15347       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15348       enum dwarf_location_atom op;
15349
15350       if (size > DWARF2_ADDR_SIZE || size == -1)
15351         {
15352           expansion_failed (loc, NULL_RTX,
15353                             "DWARF address size mismatch");
15354           return 0;
15355         }
15356       else if (size == DWARF2_ADDR_SIZE)
15357         op = DW_OP_deref;
15358       else
15359         op = DW_OP_deref_size;
15360
15361       add_loc_descr (&descr, new_loc_descr (op, size, 0));
15362     }
15363
15364   return descr;
15365 }
15366
15367 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15368    if it is not possible.  */
15369
15370 static dw_loc_descr_ref
15371 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15372 {
15373   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15374     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15375   else if (dwarf_version >= 3 || !dwarf_strict)
15376     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15377   else
15378     return NULL;
15379 }
15380
15381 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15382    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
15383
15384 static dw_loc_descr_ref
15385 dw_sra_loc_expr (tree decl, rtx loc)
15386 {
15387   rtx p;
15388   unsigned int padsize = 0;
15389   dw_loc_descr_ref descr, *descr_tail;
15390   unsigned HOST_WIDE_INT decl_size;
15391   rtx varloc;
15392   enum var_init_status initialized;
15393
15394   if (DECL_SIZE (decl) == NULL
15395       || !host_integerp (DECL_SIZE (decl), 1))
15396     return NULL;
15397
15398   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
15399   descr = NULL;
15400   descr_tail = &descr;
15401
15402   for (p = loc; p; p = XEXP (p, 1))
15403     {
15404       unsigned int bitsize = decl_piece_bitsize (p);
15405       rtx loc_note = *decl_piece_varloc_ptr (p);
15406       dw_loc_descr_ref cur_descr;
15407       dw_loc_descr_ref *tail, last = NULL;
15408       unsigned int opsize = 0;
15409
15410       if (loc_note == NULL_RTX
15411           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15412         {
15413           padsize += bitsize;
15414           continue;
15415         }
15416       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15417       varloc = NOTE_VAR_LOCATION (loc_note);
15418       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15419       if (cur_descr == NULL)
15420         {
15421           padsize += bitsize;
15422           continue;
15423         }
15424
15425       /* Check that cur_descr either doesn't use
15426          DW_OP_*piece operations, or their sum is equal
15427          to bitsize.  Otherwise we can't embed it.  */
15428       for (tail = &cur_descr; *tail != NULL;
15429            tail = &(*tail)->dw_loc_next)
15430         if ((*tail)->dw_loc_opc == DW_OP_piece)
15431           {
15432             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
15433                       * BITS_PER_UNIT;
15434             last = *tail;
15435           }
15436         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
15437           {
15438             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
15439             last = *tail;
15440           }
15441
15442       if (last != NULL && opsize != bitsize)
15443         {
15444           padsize += bitsize;
15445           continue;
15446         }
15447
15448       /* If there is a hole, add DW_OP_*piece after empty DWARF
15449          expression, which means that those bits are optimized out.  */
15450       if (padsize)
15451         {
15452           if (padsize > decl_size)
15453             return NULL;
15454           decl_size -= padsize;
15455           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15456           if (*descr_tail == NULL)
15457             return NULL;
15458           descr_tail = &(*descr_tail)->dw_loc_next;
15459           padsize = 0;
15460         }
15461       *descr_tail = cur_descr;
15462       descr_tail = tail;
15463       if (bitsize > decl_size)
15464         return NULL;
15465       decl_size -= bitsize;
15466       if (last == NULL)
15467         {
15468           HOST_WIDE_INT offset = 0;
15469           if (GET_CODE (varloc) == VAR_LOCATION
15470               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15471             {
15472               varloc = PAT_VAR_LOCATION_LOC (varloc);
15473               if (GET_CODE (varloc) == EXPR_LIST)
15474                 varloc = XEXP (varloc, 0);
15475             }
15476           do 
15477             {
15478               if (GET_CODE (varloc) == CONST
15479                   || GET_CODE (varloc) == SIGN_EXTEND
15480                   || GET_CODE (varloc) == ZERO_EXTEND)
15481                 varloc = XEXP (varloc, 0);
15482               else if (GET_CODE (varloc) == SUBREG)
15483                 varloc = SUBREG_REG (varloc);
15484               else
15485                 break;
15486             }
15487           while (1);
15488           /* DW_OP_bit_size offset should be zero for register
15489              or implicit location descriptions and empty location
15490              descriptions, but for memory addresses needs big endian
15491              adjustment.  */
15492           if (MEM_P (varloc))
15493             {
15494               unsigned HOST_WIDE_INT memsize
15495                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15496               if (memsize != bitsize)
15497                 {
15498                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15499                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15500                     return NULL;
15501                   if (memsize < bitsize)
15502                     return NULL;
15503                   if (BITS_BIG_ENDIAN)
15504                     offset = memsize - bitsize;
15505                 }
15506             }
15507
15508           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15509           if (*descr_tail == NULL)
15510             return NULL;
15511           descr_tail = &(*descr_tail)->dw_loc_next;
15512         }
15513     }
15514
15515   /* If there were any non-empty expressions, add padding till the end of
15516      the decl.  */
15517   if (descr != NULL && decl_size != 0)
15518     {
15519       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15520       if (*descr_tail == NULL)
15521         return NULL;
15522     }
15523   return descr;
15524 }
15525
15526 /* Return the dwarf representation of the location list LOC_LIST of
15527    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15528    function.  */
15529
15530 static dw_loc_list_ref
15531 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15532 {
15533   const char *endname, *secname;
15534   rtx varloc;
15535   enum var_init_status initialized;
15536   struct var_loc_node *node;
15537   dw_loc_descr_ref descr;
15538   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15539   dw_loc_list_ref list = NULL;
15540   dw_loc_list_ref *listp = &list;
15541
15542   /* Now that we know what section we are using for a base,
15543      actually construct the list of locations.
15544      The first location information is what is passed to the
15545      function that creates the location list, and the remaining
15546      locations just get added on to that list.
15547      Note that we only know the start address for a location
15548      (IE location changes), so to build the range, we use
15549      the range [current location start, next location start].
15550      This means we have to special case the last node, and generate
15551      a range of [last location start, end of function label].  */
15552
15553   secname = secname_for_decl (decl);
15554
15555   for (node = loc_list->first; node; node = node->next)
15556     if (GET_CODE (node->loc) == EXPR_LIST
15557         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15558       {
15559         if (GET_CODE (node->loc) == EXPR_LIST)
15560           {
15561             /* This requires DW_OP_{,bit_}piece, which is not usable
15562                inside DWARF expressions.  */
15563             if (want_address != 2)
15564               continue;
15565             descr = dw_sra_loc_expr (decl, node->loc);
15566             if (descr == NULL)
15567               continue;
15568           }
15569         else
15570           {
15571             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15572             varloc = NOTE_VAR_LOCATION (node->loc);
15573             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15574           }
15575         if (descr)
15576           {
15577             bool range_across_switch = false;
15578             /* If section switch happens in between node->label
15579                and node->next->label (or end of function) and
15580                we can't emit it as a single entry list,
15581                emit two ranges, first one ending at the end
15582                of first partition and second one starting at the
15583                beginning of second partition.  */
15584             if (node == loc_list->last_before_switch
15585                 && (node != loc_list->first || loc_list->first->next)
15586                 && current_function_decl)
15587               {
15588                 endname = current_fde ()->dw_fde_end;
15589                 range_across_switch = true;
15590               }
15591             /* The variable has a location between NODE->LABEL and
15592                NODE->NEXT->LABEL.  */
15593             else if (node->next)
15594               endname = node->next->label;
15595             /* If the variable has a location at the last label
15596                it keeps its location until the end of function.  */
15597             else if (!current_function_decl)
15598               endname = text_end_label;
15599             else
15600               {
15601                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15602                                              current_function_funcdef_no);
15603                 endname = ggc_strdup (label_id);
15604               }
15605
15606             *listp = new_loc_list (descr, node->label, endname, secname);
15607             listp = &(*listp)->dw_loc_next;
15608
15609             if (range_across_switch)
15610               {
15611                 if (GET_CODE (node->loc) == EXPR_LIST)
15612                   descr = dw_sra_loc_expr (decl, node->loc);
15613                 else
15614                   {
15615                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15616                     varloc = NOTE_VAR_LOCATION (node->loc);
15617                     descr = dw_loc_list_1 (decl, varloc, want_address,
15618                                            initialized);
15619                   }
15620                 gcc_assert (descr);
15621                 /* The variable has a location between NODE->LABEL and
15622                    NODE->NEXT->LABEL.  */
15623                 if (node->next)
15624                   endname = node->next->label;
15625                 else
15626                   endname = current_fde ()->dw_fde_second_end;
15627                 *listp = new_loc_list (descr,
15628                                        current_fde ()->dw_fde_second_begin,
15629                                        endname, secname);
15630                 listp = &(*listp)->dw_loc_next;
15631               }
15632           }
15633       }
15634
15635   /* Try to avoid the overhead of a location list emitting a location
15636      expression instead, but only if we didn't have more than one
15637      location entry in the first place.  If some entries were not
15638      representable, we don't want to pretend a single entry that was
15639      applies to the entire scope in which the variable is
15640      available.  */
15641   if (list && loc_list->first->next)
15642     gen_llsym (list);
15643
15644   return list;
15645 }
15646
15647 /* Return if the loc_list has only single element and thus can be represented
15648    as location description.   */
15649
15650 static bool
15651 single_element_loc_list_p (dw_loc_list_ref list)
15652 {
15653   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15654   return !list->ll_symbol;
15655 }
15656
15657 /* To each location in list LIST add loc descr REF.  */
15658
15659 static void
15660 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15661 {
15662   dw_loc_descr_ref copy;
15663   add_loc_descr (&list->expr, ref);
15664   list = list->dw_loc_next;
15665   while (list)
15666     {
15667       copy = ggc_alloc_dw_loc_descr_node ();
15668       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15669       add_loc_descr (&list->expr, copy);
15670       while (copy->dw_loc_next)
15671         {
15672           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15673           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15674           copy->dw_loc_next = new_copy;
15675           copy = new_copy;
15676         }
15677       list = list->dw_loc_next;
15678     }
15679 }
15680
15681 /* Given two lists RET and LIST
15682    produce location list that is result of adding expression in LIST
15683    to expression in RET on each possition in program.
15684    Might be destructive on both RET and LIST.
15685
15686    TODO: We handle only simple cases of RET or LIST having at most one
15687    element. General case would inolve sorting the lists in program order
15688    and merging them that will need some additional work.
15689    Adding that will improve quality of debug info especially for SRA-ed
15690    structures.  */
15691
15692 static void
15693 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15694 {
15695   if (!list)
15696     return;
15697   if (!*ret)
15698     {
15699       *ret = list;
15700       return;
15701     }
15702   if (!list->dw_loc_next)
15703     {
15704       add_loc_descr_to_each (*ret, list->expr);
15705       return;
15706     }
15707   if (!(*ret)->dw_loc_next)
15708     {
15709       add_loc_descr_to_each (list, (*ret)->expr);
15710       *ret = list;
15711       return;
15712     }
15713   expansion_failed (NULL_TREE, NULL_RTX,
15714                     "Don't know how to merge two non-trivial"
15715                     " location lists.\n");
15716   *ret = NULL;
15717   return;
15718 }
15719
15720 /* LOC is constant expression.  Try a luck, look it up in constant
15721    pool and return its loc_descr of its address.  */
15722
15723 static dw_loc_descr_ref
15724 cst_pool_loc_descr (tree loc)
15725 {
15726   /* Get an RTL for this, if something has been emitted.  */
15727   rtx rtl = lookup_constant_def (loc);
15728
15729   if (!rtl || !MEM_P (rtl))
15730     {
15731       gcc_assert (!rtl);
15732       return 0;
15733     }
15734   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15735
15736   /* TODO: We might get more coverage if we was actually delaying expansion
15737      of all expressions till end of compilation when constant pools are fully
15738      populated.  */
15739   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15740     {
15741       expansion_failed (loc, NULL_RTX,
15742                         "CST value in contant pool but not marked.");
15743       return 0;
15744     }
15745   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15746                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15747 }
15748
15749 /* Return dw_loc_list representing address of addr_expr LOC
15750    by looking for innder INDIRECT_REF expression and turing it
15751    into simple arithmetics.  */
15752
15753 static dw_loc_list_ref
15754 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15755 {
15756   tree obj, offset;
15757   HOST_WIDE_INT bitsize, bitpos, bytepos;
15758   enum machine_mode mode;
15759   int volatilep;
15760   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15761   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15762
15763   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15764                              &bitsize, &bitpos, &offset, &mode,
15765                              &unsignedp, &volatilep, false);
15766   STRIP_NOPS (obj);
15767   if (bitpos % BITS_PER_UNIT)
15768     {
15769       expansion_failed (loc, NULL_RTX, "bitfield access");
15770       return 0;
15771     }
15772   if (!INDIRECT_REF_P (obj))
15773     {
15774       expansion_failed (obj,
15775                         NULL_RTX, "no indirect ref in inner refrence");
15776       return 0;
15777     }
15778   if (!offset && !bitpos)
15779     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15780   else if (toplev
15781            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15782            && (dwarf_version >= 4 || !dwarf_strict))
15783     {
15784       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15785       if (!list_ret)
15786         return 0;
15787       if (offset)
15788         {
15789           /* Variable offset.  */
15790           list_ret1 = loc_list_from_tree (offset, 0);
15791           if (list_ret1 == 0)
15792             return 0;
15793           add_loc_list (&list_ret, list_ret1);
15794           if (!list_ret)
15795             return 0;
15796           add_loc_descr_to_each (list_ret,
15797                                  new_loc_descr (DW_OP_plus, 0, 0));
15798         }
15799       bytepos = bitpos / BITS_PER_UNIT;
15800       if (bytepos > 0)
15801         add_loc_descr_to_each (list_ret,
15802                                new_loc_descr (DW_OP_plus_uconst,
15803                                               bytepos, 0));
15804       else if (bytepos < 0)
15805         loc_list_plus_const (list_ret, bytepos);
15806       add_loc_descr_to_each (list_ret,
15807                              new_loc_descr (DW_OP_stack_value, 0, 0));
15808     }
15809   return list_ret;
15810 }
15811
15812
15813 /* Generate Dwarf location list representing LOC.
15814    If WANT_ADDRESS is false, expression computing LOC will be computed
15815    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15816    if WANT_ADDRESS is 2, expression computing address useable in location
15817      will be returned (i.e. DW_OP_reg can be used
15818      to refer to register values).  */
15819
15820 static dw_loc_list_ref
15821 loc_list_from_tree (tree loc, int want_address)
15822 {
15823   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15824   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15825   int have_address = 0;
15826   enum dwarf_location_atom op;
15827
15828   /* ??? Most of the time we do not take proper care for sign/zero
15829      extending the values properly.  Hopefully this won't be a real
15830      problem...  */
15831
15832   switch (TREE_CODE (loc))
15833     {
15834     case ERROR_MARK:
15835       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15836       return 0;
15837
15838     case PLACEHOLDER_EXPR:
15839       /* This case involves extracting fields from an object to determine the
15840          position of other fields.  We don't try to encode this here.  The
15841          only user of this is Ada, which encodes the needed information using
15842          the names of types.  */
15843       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15844       return 0;
15845
15846     case CALL_EXPR:
15847       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15848       /* There are no opcodes for these operations.  */
15849       return 0;
15850
15851     case PREINCREMENT_EXPR:
15852     case PREDECREMENT_EXPR:
15853     case POSTINCREMENT_EXPR:
15854     case POSTDECREMENT_EXPR:
15855       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15856       /* There are no opcodes for these operations.  */
15857       return 0;
15858
15859     case ADDR_EXPR:
15860       /* If we already want an address, see if there is INDIRECT_REF inside
15861          e.g. for &this->field.  */
15862       if (want_address)
15863         {
15864           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15865                        (loc, want_address == 2);
15866           if (list_ret)
15867             have_address = 1;
15868           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15869                    && (ret = cst_pool_loc_descr (loc)))
15870             have_address = 1;
15871         }
15872         /* Otherwise, process the argument and look for the address.  */
15873       if (!list_ret && !ret)
15874         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15875       else
15876         {
15877           if (want_address)
15878             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15879           return NULL;
15880         }
15881       break;
15882
15883     case VAR_DECL:
15884       if (DECL_THREAD_LOCAL_P (loc))
15885         {
15886           rtx rtl;
15887           enum dwarf_location_atom first_op;
15888           enum dwarf_location_atom second_op;
15889           bool dtprel = false;
15890
15891           if (targetm.have_tls)
15892             {
15893               /* If this is not defined, we have no way to emit the
15894                  data.  */
15895               if (!targetm.asm_out.output_dwarf_dtprel)
15896                 return 0;
15897
15898                /* The way DW_OP_GNU_push_tls_address is specified, we
15899                   can only look up addresses of objects in the current
15900                   module.  We used DW_OP_addr as first op, but that's
15901                   wrong, because DW_OP_addr is relocated by the debug
15902                   info consumer, while DW_OP_GNU_push_tls_address
15903                   operand shouldn't be.  */
15904               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15905                 return 0;
15906               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15907               dtprel = true;
15908               second_op = DW_OP_GNU_push_tls_address;
15909             }
15910           else
15911             {
15912               if (!targetm.emutls.debug_form_tls_address
15913                   || !(dwarf_version >= 3 || !dwarf_strict))
15914                 return 0;
15915               /* We stuffed the control variable into the DECL_VALUE_EXPR
15916                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15917                  no longer appear in gimple code.  We used the control
15918                  variable in specific so that we could pick it up here.  */
15919               loc = DECL_VALUE_EXPR (loc);
15920               first_op = DW_OP_addr;
15921               second_op = DW_OP_form_tls_address;
15922             }
15923
15924           rtl = rtl_for_decl_location (loc);
15925           if (rtl == NULL_RTX)
15926             return 0;
15927
15928           if (!MEM_P (rtl))
15929             return 0;
15930           rtl = XEXP (rtl, 0);
15931           if (! CONSTANT_P (rtl))
15932             return 0;
15933
15934           ret = new_loc_descr (first_op, 0, 0);
15935           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15936           ret->dw_loc_oprnd1.v.val_addr = rtl;
15937           ret->dtprel = dtprel;
15938
15939           ret1 = new_loc_descr (second_op, 0, 0);
15940           add_loc_descr (&ret, ret1);
15941
15942           have_address = 1;
15943           break;
15944         }
15945       /* FALLTHRU */
15946
15947     case PARM_DECL:
15948     case RESULT_DECL:
15949       if (DECL_HAS_VALUE_EXPR_P (loc))
15950         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15951                                    want_address);
15952       /* FALLTHRU */
15953
15954     case FUNCTION_DECL:
15955       {
15956         rtx rtl;
15957         var_loc_list *loc_list = lookup_decl_loc (loc);
15958
15959         if (loc_list && loc_list->first)
15960           {
15961             list_ret = dw_loc_list (loc_list, loc, want_address);
15962             have_address = want_address != 0;
15963             break;
15964           }
15965         rtl = rtl_for_decl_location (loc);
15966         if (rtl == NULL_RTX)
15967           {
15968             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15969             return 0;
15970           }
15971         else if (CONST_INT_P (rtl))
15972           {
15973             HOST_WIDE_INT val = INTVAL (rtl);
15974             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15975               val &= GET_MODE_MASK (DECL_MODE (loc));
15976             ret = int_loc_descriptor (val);
15977           }
15978         else if (GET_CODE (rtl) == CONST_STRING)
15979           {
15980             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15981             return 0;
15982           }
15983         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15984           {
15985             ret = new_loc_descr (DW_OP_addr, 0, 0);
15986             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15987             ret->dw_loc_oprnd1.v.val_addr = rtl;
15988           }
15989         else
15990           {
15991             enum machine_mode mode, mem_mode;
15992
15993             /* Certain constructs can only be represented at top-level.  */
15994             if (want_address == 2)
15995               {
15996                 ret = loc_descriptor (rtl, VOIDmode,
15997                                       VAR_INIT_STATUS_INITIALIZED);
15998                 have_address = 1;
15999               }
16000             else
16001               {
16002                 mode = GET_MODE (rtl);
16003                 mem_mode = VOIDmode;
16004                 if (MEM_P (rtl))
16005                   {
16006                     mem_mode = mode;
16007                     mode = get_address_mode (rtl);
16008                     rtl = XEXP (rtl, 0);
16009                     have_address = 1;
16010                   }
16011                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16012                                           VAR_INIT_STATUS_INITIALIZED);
16013               }
16014             if (!ret)
16015               expansion_failed (loc, rtl,
16016                                 "failed to produce loc descriptor for rtl");
16017           }
16018       }
16019       break;
16020
16021     case MEM_REF:
16022       /* ??? FIXME.  */
16023       if (!integer_zerop (TREE_OPERAND (loc, 1)))
16024         return 0;
16025       /* Fallthru.  */
16026     case INDIRECT_REF:
16027       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16028       have_address = 1;
16029       break;
16030
16031     case COMPOUND_EXPR:
16032       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
16033
16034     CASE_CONVERT:
16035     case VIEW_CONVERT_EXPR:
16036     case SAVE_EXPR:
16037     case MODIFY_EXPR:
16038       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
16039
16040     case COMPONENT_REF:
16041     case BIT_FIELD_REF:
16042     case ARRAY_REF:
16043     case ARRAY_RANGE_REF:
16044     case REALPART_EXPR:
16045     case IMAGPART_EXPR:
16046       {
16047         tree obj, offset;
16048         HOST_WIDE_INT bitsize, bitpos, bytepos;
16049         enum machine_mode mode;
16050         int volatilep;
16051         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
16052
16053         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16054                                    &unsignedp, &volatilep, false);
16055
16056         gcc_assert (obj != loc);
16057
16058         list_ret = loc_list_from_tree (obj,
16059                                        want_address == 2
16060                                        && !bitpos && !offset ? 2 : 1);
16061         /* TODO: We can extract value of the small expression via shifting even
16062            for nonzero bitpos.  */
16063         if (list_ret == 0)
16064           return 0;
16065         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16066           {
16067             expansion_failed (loc, NULL_RTX,
16068                               "bitfield access");
16069             return 0;
16070           }
16071
16072         if (offset != NULL_TREE)
16073           {
16074             /* Variable offset.  */
16075             list_ret1 = loc_list_from_tree (offset, 0);
16076             if (list_ret1 == 0)
16077               return 0;
16078             add_loc_list (&list_ret, list_ret1);
16079             if (!list_ret)
16080               return 0;
16081             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16082           }
16083
16084         bytepos = bitpos / BITS_PER_UNIT;
16085         if (bytepos > 0)
16086           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16087         else if (bytepos < 0)
16088           loc_list_plus_const (list_ret, bytepos);
16089
16090         have_address = 1;
16091         break;
16092       }
16093
16094     case INTEGER_CST:
16095       if ((want_address || !host_integerp (loc, 0))
16096           && (ret = cst_pool_loc_descr (loc)))
16097         have_address = 1;
16098       else if (want_address == 2
16099                && host_integerp (loc, 0)
16100                && (ret = address_of_int_loc_descriptor
16101                            (int_size_in_bytes (TREE_TYPE (loc)),
16102                             tree_low_cst (loc, 0))))
16103         have_address = 1;
16104       else if (host_integerp (loc, 0))
16105         ret = int_loc_descriptor (tree_low_cst (loc, 0));
16106       else
16107         {
16108           expansion_failed (loc, NULL_RTX,
16109                             "Integer operand is not host integer");
16110           return 0;
16111         }
16112       break;
16113
16114     case CONSTRUCTOR:
16115     case REAL_CST:
16116     case STRING_CST:
16117     case COMPLEX_CST:
16118       if ((ret = cst_pool_loc_descr (loc)))
16119         have_address = 1;
16120       else
16121       /* We can construct small constants here using int_loc_descriptor.  */
16122         expansion_failed (loc, NULL_RTX,
16123                           "constructor or constant not in constant pool");
16124       break;
16125
16126     case TRUTH_AND_EXPR:
16127     case TRUTH_ANDIF_EXPR:
16128     case BIT_AND_EXPR:
16129       op = DW_OP_and;
16130       goto do_binop;
16131
16132     case TRUTH_XOR_EXPR:
16133     case BIT_XOR_EXPR:
16134       op = DW_OP_xor;
16135       goto do_binop;
16136
16137     case TRUTH_OR_EXPR:
16138     case TRUTH_ORIF_EXPR:
16139     case BIT_IOR_EXPR:
16140       op = DW_OP_or;
16141       goto do_binop;
16142
16143     case FLOOR_DIV_EXPR:
16144     case CEIL_DIV_EXPR:
16145     case ROUND_DIV_EXPR:
16146     case TRUNC_DIV_EXPR:
16147       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16148         return 0;
16149       op = DW_OP_div;
16150       goto do_binop;
16151
16152     case MINUS_EXPR:
16153       op = DW_OP_minus;
16154       goto do_binop;
16155
16156     case FLOOR_MOD_EXPR:
16157     case CEIL_MOD_EXPR:
16158     case ROUND_MOD_EXPR:
16159     case TRUNC_MOD_EXPR:
16160       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16161         {
16162           op = DW_OP_mod;
16163           goto do_binop;
16164         }
16165       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16166       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16167       if (list_ret == 0 || list_ret1 == 0)
16168         return 0;
16169
16170       add_loc_list (&list_ret, list_ret1);
16171       if (list_ret == 0)
16172         return 0;
16173       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16174       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16175       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16176       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16177       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16178       break;
16179
16180     case MULT_EXPR:
16181       op = DW_OP_mul;
16182       goto do_binop;
16183
16184     case LSHIFT_EXPR:
16185       op = DW_OP_shl;
16186       goto do_binop;
16187
16188     case RSHIFT_EXPR:
16189       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16190       goto do_binop;
16191
16192     case POINTER_PLUS_EXPR:
16193     case PLUS_EXPR:
16194       if (host_integerp (TREE_OPERAND (loc, 1), 0))
16195         {
16196           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16197           if (list_ret == 0)
16198             return 0;
16199
16200           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
16201           break;
16202         }
16203
16204       op = DW_OP_plus;
16205       goto do_binop;
16206
16207     case LE_EXPR:
16208       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16209         return 0;
16210
16211       op = DW_OP_le;
16212       goto do_binop;
16213
16214     case GE_EXPR:
16215       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16216         return 0;
16217
16218       op = DW_OP_ge;
16219       goto do_binop;
16220
16221     case LT_EXPR:
16222       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16223         return 0;
16224
16225       op = DW_OP_lt;
16226       goto do_binop;
16227
16228     case GT_EXPR:
16229       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16230         return 0;
16231
16232       op = DW_OP_gt;
16233       goto do_binop;
16234
16235     case EQ_EXPR:
16236       op = DW_OP_eq;
16237       goto do_binop;
16238
16239     case NE_EXPR:
16240       op = DW_OP_ne;
16241       goto do_binop;
16242
16243     do_binop:
16244       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16245       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16246       if (list_ret == 0 || list_ret1 == 0)
16247         return 0;
16248
16249       add_loc_list (&list_ret, list_ret1);
16250       if (list_ret == 0)
16251         return 0;
16252       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16253       break;
16254
16255     case TRUTH_NOT_EXPR:
16256     case BIT_NOT_EXPR:
16257       op = DW_OP_not;
16258       goto do_unop;
16259
16260     case ABS_EXPR:
16261       op = DW_OP_abs;
16262       goto do_unop;
16263
16264     case NEGATE_EXPR:
16265       op = DW_OP_neg;
16266       goto do_unop;
16267
16268     do_unop:
16269       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16270       if (list_ret == 0)
16271         return 0;
16272
16273       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16274       break;
16275
16276     case MIN_EXPR:
16277     case MAX_EXPR:
16278       {
16279         const enum tree_code code =
16280           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16281
16282         loc = build3 (COND_EXPR, TREE_TYPE (loc),
16283                       build2 (code, integer_type_node,
16284                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16285                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16286       }
16287
16288       /* ... fall through ...  */
16289
16290     case COND_EXPR:
16291       {
16292         dw_loc_descr_ref lhs
16293           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
16294         dw_loc_list_ref rhs
16295           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
16296         dw_loc_descr_ref bra_node, jump_node, tmp;
16297
16298         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16299         if (list_ret == 0 || lhs == 0 || rhs == 0)
16300           return 0;
16301
16302         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16303         add_loc_descr_to_each (list_ret, bra_node);
16304
16305         add_loc_list (&list_ret, rhs);
16306         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16307         add_loc_descr_to_each (list_ret, jump_node);
16308
16309         add_loc_descr_to_each (list_ret, lhs);
16310         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16311         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16312
16313         /* ??? Need a node to point the skip at.  Use a nop.  */
16314         tmp = new_loc_descr (DW_OP_nop, 0, 0);
16315         add_loc_descr_to_each (list_ret, tmp);
16316         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16317         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16318       }
16319       break;
16320
16321     case FIX_TRUNC_EXPR:
16322       return 0;
16323
16324     default:
16325       /* Leave front-end specific codes as simply unknown.  This comes
16326          up, for instance, with the C STMT_EXPR.  */
16327       if ((unsigned int) TREE_CODE (loc)
16328           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16329         {
16330           expansion_failed (loc, NULL_RTX,
16331                             "language specific tree node");
16332           return 0;
16333         }
16334
16335 #ifdef ENABLE_CHECKING
16336       /* Otherwise this is a generic code; we should just lists all of
16337          these explicitly.  We forgot one.  */
16338       gcc_unreachable ();
16339 #else
16340       /* In a release build, we want to degrade gracefully: better to
16341          generate incomplete debugging information than to crash.  */
16342       return NULL;
16343 #endif
16344     }
16345
16346   if (!ret && !list_ret)
16347     return 0;
16348
16349   if (want_address == 2 && !have_address
16350       && (dwarf_version >= 4 || !dwarf_strict))
16351     {
16352       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16353         {
16354           expansion_failed (loc, NULL_RTX,
16355                             "DWARF address size mismatch");
16356           return 0;
16357         }
16358       if (ret)
16359         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16360       else
16361         add_loc_descr_to_each (list_ret,
16362                                new_loc_descr (DW_OP_stack_value, 0, 0));
16363       have_address = 1;
16364     }
16365   /* Show if we can't fill the request for an address.  */
16366   if (want_address && !have_address)
16367     {
16368       expansion_failed (loc, NULL_RTX,
16369                         "Want address and only have value");
16370       return 0;
16371     }
16372
16373   gcc_assert (!ret || !list_ret);
16374
16375   /* If we've got an address and don't want one, dereference.  */
16376   if (!want_address && have_address)
16377     {
16378       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16379
16380       if (size > DWARF2_ADDR_SIZE || size == -1)
16381         {
16382           expansion_failed (loc, NULL_RTX,
16383                             "DWARF address size mismatch");
16384           return 0;
16385         }
16386       else if (size == DWARF2_ADDR_SIZE)
16387         op = DW_OP_deref;
16388       else
16389         op = DW_OP_deref_size;
16390
16391       if (ret)
16392         add_loc_descr (&ret, new_loc_descr (op, size, 0));
16393       else
16394         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16395     }
16396   if (ret)
16397     list_ret = new_loc_list (ret, NULL, NULL, NULL);
16398
16399   return list_ret;
16400 }
16401
16402 /* Same as above but return only single location expression.  */
16403 static dw_loc_descr_ref
16404 loc_descriptor_from_tree (tree loc, int want_address)
16405 {
16406   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
16407   if (!ret)
16408     return NULL;
16409   if (ret->dw_loc_next)
16410     {
16411       expansion_failed (loc, NULL_RTX,
16412                         "Location list where only loc descriptor needed");
16413       return NULL;
16414     }
16415   return ret->expr;
16416 }
16417
16418 /* Given a value, round it up to the lowest multiple of `boundary'
16419    which is not less than the value itself.  */
16420
16421 static inline HOST_WIDE_INT
16422 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16423 {
16424   return (((value + boundary - 1) / boundary) * boundary);
16425 }
16426
16427 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16428    pointer to the declared type for the relevant field variable, or return
16429    `integer_type_node' if the given node turns out to be an
16430    ERROR_MARK node.  */
16431
16432 static inline tree
16433 field_type (const_tree decl)
16434 {
16435   tree type;
16436
16437   if (TREE_CODE (decl) == ERROR_MARK)
16438     return integer_type_node;
16439
16440   type = DECL_BIT_FIELD_TYPE (decl);
16441   if (type == NULL_TREE)
16442     type = TREE_TYPE (decl);
16443
16444   return type;
16445 }
16446
16447 /* Given a pointer to a tree node, return the alignment in bits for
16448    it, or else return BITS_PER_WORD if the node actually turns out to
16449    be an ERROR_MARK node.  */
16450
16451 static inline unsigned
16452 simple_type_align_in_bits (const_tree type)
16453 {
16454   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16455 }
16456
16457 static inline unsigned
16458 simple_decl_align_in_bits (const_tree decl)
16459 {
16460   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16461 }
16462
16463 /* Return the result of rounding T up to ALIGN.  */
16464
16465 static inline double_int
16466 round_up_to_align (double_int t, unsigned int align)
16467 {
16468   double_int alignd = uhwi_to_double_int (align);
16469   t = double_int_add (t, alignd);
16470   t = double_int_add (t, double_int_minus_one);
16471   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
16472   t = double_int_mul (t, alignd);
16473   return t;
16474 }
16475
16476 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
16477    lowest addressed byte of the "containing object" for the given FIELD_DECL,
16478    or return 0 if we are unable to determine what that offset is, either
16479    because the argument turns out to be a pointer to an ERROR_MARK node, or
16480    because the offset is actually variable.  (We can't handle the latter case
16481    just yet).  */
16482
16483 static HOST_WIDE_INT
16484 field_byte_offset (const_tree decl)
16485 {
16486   double_int object_offset_in_bits;
16487   double_int object_offset_in_bytes;
16488   double_int bitpos_int;
16489
16490   if (TREE_CODE (decl) == ERROR_MARK)
16491     return 0;
16492
16493   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16494
16495   /* We cannot yet cope with fields whose positions are variable, so
16496      for now, when we see such things, we simply return 0.  Someday, we may
16497      be able to handle such cases, but it will be damn difficult.  */
16498   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16499     return 0;
16500
16501   bitpos_int = tree_to_double_int (bit_position (decl));
16502
16503 #ifdef PCC_BITFIELD_TYPE_MATTERS
16504   if (PCC_BITFIELD_TYPE_MATTERS)
16505     {
16506       tree type;
16507       tree field_size_tree;
16508       double_int deepest_bitpos;
16509       double_int field_size_in_bits;
16510       unsigned int type_align_in_bits;
16511       unsigned int decl_align_in_bits;
16512       double_int type_size_in_bits;
16513
16514       type = field_type (decl);
16515       type_size_in_bits = double_int_type_size_in_bits (type);
16516       type_align_in_bits = simple_type_align_in_bits (type);
16517
16518       field_size_tree = DECL_SIZE (decl);
16519
16520       /* The size could be unspecified if there was an error, or for
16521          a flexible array member.  */
16522       if (!field_size_tree)
16523         field_size_tree = bitsize_zero_node;
16524
16525       /* If the size of the field is not constant, use the type size.  */
16526       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16527         field_size_in_bits = tree_to_double_int (field_size_tree);
16528       else
16529         field_size_in_bits = type_size_in_bits;
16530
16531       decl_align_in_bits = simple_decl_align_in_bits (decl);
16532
16533       /* The GCC front-end doesn't make any attempt to keep track of the
16534          starting bit offset (relative to the start of the containing
16535          structure type) of the hypothetical "containing object" for a
16536          bit-field.  Thus, when computing the byte offset value for the
16537          start of the "containing object" of a bit-field, we must deduce
16538          this information on our own. This can be rather tricky to do in
16539          some cases.  For example, handling the following structure type
16540          definition when compiling for an i386/i486 target (which only
16541          aligns long long's to 32-bit boundaries) can be very tricky:
16542
16543          struct S { int field1; long long field2:31; };
16544
16545          Fortunately, there is a simple rule-of-thumb which can be used
16546          in such cases.  When compiling for an i386/i486, GCC will
16547          allocate 8 bytes for the structure shown above.  It decides to
16548          do this based upon one simple rule for bit-field allocation.
16549          GCC allocates each "containing object" for each bit-field at
16550          the first (i.e. lowest addressed) legitimate alignment boundary
16551          (based upon the required minimum alignment for the declared
16552          type of the field) which it can possibly use, subject to the
16553          condition that there is still enough available space remaining
16554          in the containing object (when allocated at the selected point)
16555          to fully accommodate all of the bits of the bit-field itself.
16556
16557          This simple rule makes it obvious why GCC allocates 8 bytes for
16558          each object of the structure type shown above.  When looking
16559          for a place to allocate the "containing object" for `field2',
16560          the compiler simply tries to allocate a 64-bit "containing
16561          object" at each successive 32-bit boundary (starting at zero)
16562          until it finds a place to allocate that 64- bit field such that
16563          at least 31 contiguous (and previously unallocated) bits remain
16564          within that selected 64 bit field.  (As it turns out, for the
16565          example above, the compiler finds it is OK to allocate the
16566          "containing object" 64-bit field at bit-offset zero within the
16567          structure type.)
16568
16569          Here we attempt to work backwards from the limited set of facts
16570          we're given, and we try to deduce from those facts, where GCC
16571          must have believed that the containing object started (within
16572          the structure type). The value we deduce is then used (by the
16573          callers of this routine) to generate DW_AT_location and
16574          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16575          the case of DW_AT_location, regular fields as well).  */
16576
16577       /* Figure out the bit-distance from the start of the structure to
16578          the "deepest" bit of the bit-field.  */
16579       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16580
16581       /* This is the tricky part.  Use some fancy footwork to deduce
16582          where the lowest addressed bit of the containing object must
16583          be.  */
16584       object_offset_in_bits
16585         = double_int_sub (deepest_bitpos, type_size_in_bits);
16586
16587       /* Round up to type_align by default.  This works best for
16588          bitfields.  */
16589       object_offset_in_bits
16590         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16591
16592       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16593         {
16594           object_offset_in_bits
16595             = double_int_sub (deepest_bitpos, type_size_in_bits);
16596
16597           /* Round up to decl_align instead.  */
16598           object_offset_in_bits
16599             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16600         }
16601     }
16602   else
16603 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16604     object_offset_in_bits = bitpos_int;
16605
16606   object_offset_in_bytes
16607     = double_int_div (object_offset_in_bits,
16608                       uhwi_to_double_int (BITS_PER_UNIT), true,
16609                       TRUNC_DIV_EXPR);
16610   return double_int_to_shwi (object_offset_in_bytes);
16611 }
16612 \f
16613 /* The following routines define various Dwarf attributes and any data
16614    associated with them.  */
16615
16616 /* Add a location description attribute value to a DIE.
16617
16618    This emits location attributes suitable for whole variables and
16619    whole parameters.  Note that the location attributes for struct fields are
16620    generated by the routine `data_member_location_attribute' below.  */
16621
16622 static inline void
16623 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16624                              dw_loc_list_ref descr)
16625 {
16626   if (descr == 0)
16627     return;
16628   if (single_element_loc_list_p (descr))
16629     add_AT_loc (die, attr_kind, descr->expr);
16630   else
16631     add_AT_loc_list (die, attr_kind, descr);
16632 }
16633
16634 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16635
16636 static void
16637 add_accessibility_attribute (dw_die_ref die, tree decl)
16638 {
16639   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16640      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16641      the default has always been DW_ACCESS_public.  */
16642   if (TREE_PROTECTED (decl))
16643     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16644   else if (TREE_PRIVATE (decl))
16645     {
16646       if (dwarf_version == 2
16647           || die->die_parent == NULL
16648           || die->die_parent->die_tag != DW_TAG_class_type)
16649         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16650     }
16651   else if (dwarf_version > 2
16652            && die->die_parent
16653            && die->die_parent->die_tag == DW_TAG_class_type)
16654     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16655 }
16656
16657 /* Attach the specialized form of location attribute used for data members of
16658    struct and union types.  In the special case of a FIELD_DECL node which
16659    represents a bit-field, the "offset" part of this special location
16660    descriptor must indicate the distance in bytes from the lowest-addressed
16661    byte of the containing struct or union type to the lowest-addressed byte of
16662    the "containing object" for the bit-field.  (See the `field_byte_offset'
16663    function above).
16664
16665    For any given bit-field, the "containing object" is a hypothetical object
16666    (of some integral or enum type) within which the given bit-field lives.  The
16667    type of this hypothetical "containing object" is always the same as the
16668    declared type of the individual bit-field itself (for GCC anyway... the
16669    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16670    bytes) of the hypothetical "containing object" which will be given in the
16671    DW_AT_byte_size attribute for this bit-field.  (See the
16672    `byte_size_attribute' function below.)  It is also used when calculating the
16673    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16674    function below.)  */
16675
16676 static void
16677 add_data_member_location_attribute (dw_die_ref die, tree decl)
16678 {
16679   HOST_WIDE_INT offset;
16680   dw_loc_descr_ref loc_descr = 0;
16681
16682   if (TREE_CODE (decl) == TREE_BINFO)
16683     {
16684       /* We're working on the TAG_inheritance for a base class.  */
16685       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16686         {
16687           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16688              aren't at a fixed offset from all (sub)objects of the same
16689              type.  We need to extract the appropriate offset from our
16690              vtable.  The following dwarf expression means
16691
16692                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16693
16694              This is specific to the V3 ABI, of course.  */
16695
16696           dw_loc_descr_ref tmp;
16697
16698           /* Make a copy of the object address.  */
16699           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16700           add_loc_descr (&loc_descr, tmp);
16701
16702           /* Extract the vtable address.  */
16703           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16704           add_loc_descr (&loc_descr, tmp);
16705
16706           /* Calculate the address of the offset.  */
16707           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16708           gcc_assert (offset < 0);
16709
16710           tmp = int_loc_descriptor (-offset);
16711           add_loc_descr (&loc_descr, tmp);
16712           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16713           add_loc_descr (&loc_descr, tmp);
16714
16715           /* Extract the offset.  */
16716           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16717           add_loc_descr (&loc_descr, tmp);
16718
16719           /* Add it to the object address.  */
16720           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16721           add_loc_descr (&loc_descr, tmp);
16722         }
16723       else
16724         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16725     }
16726   else
16727     offset = field_byte_offset (decl);
16728
16729   if (! loc_descr)
16730     {
16731       if (dwarf_version > 2)
16732         {
16733           /* Don't need to output a location expression, just the constant. */
16734           if (offset < 0)
16735             add_AT_int (die, DW_AT_data_member_location, offset);
16736           else
16737             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16738           return;
16739         }
16740       else
16741         {
16742           enum dwarf_location_atom op;
16743
16744           /* The DWARF2 standard says that we should assume that the structure
16745              address is already on the stack, so we can specify a structure
16746              field address by using DW_OP_plus_uconst.  */
16747
16748 #ifdef MIPS_DEBUGGING_INFO
16749           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16750              operator correctly.  It works only if we leave the offset on the
16751              stack.  */
16752           op = DW_OP_constu;
16753 #else
16754           op = DW_OP_plus_uconst;
16755 #endif
16756
16757           loc_descr = new_loc_descr (op, offset, 0);
16758         }
16759     }
16760
16761   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16762 }
16763
16764 /* Writes integer values to dw_vec_const array.  */
16765
16766 static void
16767 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16768 {
16769   while (size != 0)
16770     {
16771       *dest++ = val & 0xff;
16772       val >>= 8;
16773       --size;
16774     }
16775 }
16776
16777 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16778
16779 static HOST_WIDE_INT
16780 extract_int (const unsigned char *src, unsigned int size)
16781 {
16782   HOST_WIDE_INT val = 0;
16783
16784   src += size;
16785   while (size != 0)
16786     {
16787       val <<= 8;
16788       val |= *--src & 0xff;
16789       --size;
16790     }
16791   return val;
16792 }
16793
16794 /* Writes double_int values to dw_vec_const array.  */
16795
16796 static void
16797 insert_double (double_int val, unsigned char *dest)
16798 {
16799   unsigned char *p0 = dest;
16800   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16801
16802   if (WORDS_BIG_ENDIAN)
16803     {
16804       p0 = p1;
16805       p1 = dest;
16806     }
16807
16808   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16809   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16810 }
16811
16812 /* Writes floating point values to dw_vec_const array.  */
16813
16814 static void
16815 insert_float (const_rtx rtl, unsigned char *array)
16816 {
16817   REAL_VALUE_TYPE rv;
16818   long val[4];
16819   int i;
16820
16821   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16822   real_to_target (val, &rv, GET_MODE (rtl));
16823
16824   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16825   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16826     {
16827       insert_int (val[i], 4, array);
16828       array += 4;
16829     }
16830 }
16831
16832 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16833    does not have a "location" either in memory or in a register.  These
16834    things can arise in GNU C when a constant is passed as an actual parameter
16835    to an inlined function.  They can also arise in C++ where declared
16836    constants do not necessarily get memory "homes".  */
16837
16838 static bool
16839 add_const_value_attribute (dw_die_ref die, rtx rtl)
16840 {
16841   switch (GET_CODE (rtl))
16842     {
16843     case CONST_INT:
16844       {
16845         HOST_WIDE_INT val = INTVAL (rtl);
16846
16847         if (val < 0)
16848           add_AT_int (die, DW_AT_const_value, val);
16849         else
16850           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16851       }
16852       return true;
16853
16854     case CONST_DOUBLE:
16855       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16856          floating-point constant.  A CONST_DOUBLE is used whenever the
16857          constant requires more than one word in order to be adequately
16858          represented.  */
16859       {
16860         enum machine_mode mode = GET_MODE (rtl);
16861
16862         if (SCALAR_FLOAT_MODE_P (mode))
16863           {
16864             unsigned int length = GET_MODE_SIZE (mode);
16865             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16866
16867             insert_float (rtl, array);
16868             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16869           }
16870         else
16871           add_AT_double (die, DW_AT_const_value,
16872                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16873       }
16874       return true;
16875
16876     case CONST_VECTOR:
16877       {
16878         enum machine_mode mode = GET_MODE (rtl);
16879         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16880         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16881         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16882           (length * elt_size);
16883         unsigned int i;
16884         unsigned char *p;
16885
16886         switch (GET_MODE_CLASS (mode))
16887           {
16888           case MODE_VECTOR_INT:
16889             for (i = 0, p = array; i < length; i++, p += elt_size)
16890               {
16891                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16892                 double_int val = rtx_to_double_int (elt);
16893
16894                 if (elt_size <= sizeof (HOST_WIDE_INT))
16895                   insert_int (double_int_to_shwi (val), elt_size, p);
16896                 else
16897                   {
16898                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16899                     insert_double (val, p);
16900                   }
16901               }
16902             break;
16903
16904           case MODE_VECTOR_FLOAT:
16905             for (i = 0, p = array; i < length; i++, p += elt_size)
16906               {
16907                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16908                 insert_float (elt, p);
16909               }
16910             break;
16911
16912           default:
16913             gcc_unreachable ();
16914           }
16915
16916         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16917       }
16918       return true;
16919
16920     case CONST_STRING:
16921       if (dwarf_version >= 4 || !dwarf_strict)
16922         {
16923           dw_loc_descr_ref loc_result;
16924           resolve_one_addr (&rtl, NULL);
16925         rtl_addr:
16926           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16927           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16928           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16929           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16930           add_AT_loc (die, DW_AT_location, loc_result);
16931           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16932           return true;
16933         }
16934       return false;
16935
16936     case CONST:
16937       if (CONSTANT_P (XEXP (rtl, 0)))
16938         return add_const_value_attribute (die, XEXP (rtl, 0));
16939       /* FALLTHROUGH */
16940     case SYMBOL_REF:
16941       if (!const_ok_for_output (rtl))
16942         return false;
16943     case LABEL_REF:
16944       if (dwarf_version >= 4 || !dwarf_strict)
16945         goto rtl_addr;
16946       return false;
16947
16948     case PLUS:
16949       /* In cases where an inlined instance of an inline function is passed
16950          the address of an `auto' variable (which is local to the caller) we
16951          can get a situation where the DECL_RTL of the artificial local
16952          variable (for the inlining) which acts as a stand-in for the
16953          corresponding formal parameter (of the inline function) will look
16954          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16955          exactly a compile-time constant expression, but it isn't the address
16956          of the (artificial) local variable either.  Rather, it represents the
16957          *value* which the artificial local variable always has during its
16958          lifetime.  We currently have no way to represent such quasi-constant
16959          values in Dwarf, so for now we just punt and generate nothing.  */
16960       return false;
16961
16962     case HIGH:
16963     case CONST_FIXED:
16964       return false;
16965
16966     case MEM:
16967       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16968           && MEM_READONLY_P (rtl)
16969           && GET_MODE (rtl) == BLKmode)
16970         {
16971           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16972           return true;
16973         }
16974       return false;
16975
16976     default:
16977       /* No other kinds of rtx should be possible here.  */
16978       gcc_unreachable ();
16979     }
16980   return false;
16981 }
16982
16983 /* Determine whether the evaluation of EXPR references any variables
16984    or functions which aren't otherwise used (and therefore may not be
16985    output).  */
16986 static tree
16987 reference_to_unused (tree * tp, int * walk_subtrees,
16988                      void * data ATTRIBUTE_UNUSED)
16989 {
16990   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16991     *walk_subtrees = 0;
16992
16993   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16994       && ! TREE_ASM_WRITTEN (*tp))
16995     return *tp;
16996   /* ???  The C++ FE emits debug information for using decls, so
16997      putting gcc_unreachable here falls over.  See PR31899.  For now
16998      be conservative.  */
16999   else if (!cgraph_global_info_ready
17000            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17001     return *tp;
17002   else if (TREE_CODE (*tp) == VAR_DECL)
17003     {
17004       struct varpool_node *node = varpool_get_node (*tp);
17005       if (!node || !node->needed)
17006         return *tp;
17007     }
17008   else if (TREE_CODE (*tp) == FUNCTION_DECL
17009            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17010     {
17011       /* The call graph machinery must have finished analyzing,
17012          optimizing and gimplifying the CU by now.
17013          So if *TP has no call graph node associated
17014          to it, it means *TP will not be emitted.  */
17015       if (!cgraph_get_node (*tp))
17016         return *tp;
17017     }
17018   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17019     return *tp;
17020
17021   return NULL_TREE;
17022 }
17023
17024 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17025    for use in a later add_const_value_attribute call.  */
17026
17027 static rtx
17028 rtl_for_decl_init (tree init, tree type)
17029 {
17030   rtx rtl = NULL_RTX;
17031
17032   STRIP_NOPS (init);
17033
17034   /* If a variable is initialized with a string constant without embedded
17035      zeros, build CONST_STRING.  */
17036   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17037     {
17038       tree enttype = TREE_TYPE (type);
17039       tree domain = TYPE_DOMAIN (type);
17040       enum machine_mode mode = TYPE_MODE (enttype);
17041
17042       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17043           && domain
17044           && integer_zerop (TYPE_MIN_VALUE (domain))
17045           && compare_tree_int (TYPE_MAX_VALUE (domain),
17046                                TREE_STRING_LENGTH (init) - 1) == 0
17047           && ((size_t) TREE_STRING_LENGTH (init)
17048               == strlen (TREE_STRING_POINTER (init)) + 1))
17049         {
17050           rtl = gen_rtx_CONST_STRING (VOIDmode,
17051                                       ggc_strdup (TREE_STRING_POINTER (init)));
17052           rtl = gen_rtx_MEM (BLKmode, rtl);
17053           MEM_READONLY_P (rtl) = 1;
17054         }
17055     }
17056   /* Other aggregates, and complex values, could be represented using
17057      CONCAT: FIXME!  */
17058   else if (AGGREGATE_TYPE_P (type)
17059            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17060                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17061            || TREE_CODE (type) == COMPLEX_TYPE)
17062     ;
17063   /* Vectors only work if their mode is supported by the target.
17064      FIXME: generic vectors ought to work too.  */
17065   else if (TREE_CODE (type) == VECTOR_TYPE
17066            && !VECTOR_MODE_P (TYPE_MODE (type)))
17067     ;
17068   /* If the initializer is something that we know will expand into an
17069      immediate RTL constant, expand it now.  We must be careful not to
17070      reference variables which won't be output.  */
17071   else if (initializer_constant_valid_p (init, type)
17072            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17073     {
17074       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17075          possible.  */
17076       if (TREE_CODE (type) == VECTOR_TYPE)
17077         switch (TREE_CODE (init))
17078           {
17079           case VECTOR_CST:
17080             break;
17081           case CONSTRUCTOR:
17082             if (TREE_CONSTANT (init))
17083               {
17084                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
17085                 bool constant_p = true;
17086                 tree value;
17087                 unsigned HOST_WIDE_INT ix;
17088
17089                 /* Even when ctor is constant, it might contain non-*_CST
17090                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17091                    belong into VECTOR_CST nodes.  */
17092                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17093                   if (!CONSTANT_CLASS_P (value))
17094                     {
17095                       constant_p = false;
17096                       break;
17097                     }
17098
17099                 if (constant_p)
17100                   {
17101                     init = build_vector_from_ctor (type, elts);
17102                     break;
17103                   }
17104               }
17105             /* FALLTHRU */
17106
17107           default:
17108             return NULL;
17109           }
17110
17111       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17112
17113       /* If expand_expr returns a MEM, it wasn't immediate.  */
17114       gcc_assert (!rtl || !MEM_P (rtl));
17115     }
17116
17117   return rtl;
17118 }
17119
17120 /* Generate RTL for the variable DECL to represent its location.  */
17121
17122 static rtx
17123 rtl_for_decl_location (tree decl)
17124 {
17125   rtx rtl;
17126
17127   /* Here we have to decide where we are going to say the parameter "lives"
17128      (as far as the debugger is concerned).  We only have a couple of
17129      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17130
17131      DECL_RTL normally indicates where the parameter lives during most of the
17132      activation of the function.  If optimization is enabled however, this
17133      could be either NULL or else a pseudo-reg.  Both of those cases indicate
17134      that the parameter doesn't really live anywhere (as far as the code
17135      generation parts of GCC are concerned) during most of the function's
17136      activation.  That will happen (for example) if the parameter is never
17137      referenced within the function.
17138
17139      We could just generate a location descriptor here for all non-NULL
17140      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17141      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17142      where DECL_RTL is NULL or is a pseudo-reg.
17143
17144      Note however that we can only get away with using DECL_INCOMING_RTL as
17145      a backup substitute for DECL_RTL in certain limited cases.  In cases
17146      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17147      we can be sure that the parameter was passed using the same type as it is
17148      declared to have within the function, and that its DECL_INCOMING_RTL
17149      points us to a place where a value of that type is passed.
17150
17151      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17152      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17153      because in these cases DECL_INCOMING_RTL points us to a value of some
17154      type which is *different* from the type of the parameter itself.  Thus,
17155      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17156      such cases, the debugger would end up (for example) trying to fetch a
17157      `float' from a place which actually contains the first part of a
17158      `double'.  That would lead to really incorrect and confusing
17159      output at debug-time.
17160
17161      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17162      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
17163      are a couple of exceptions however.  On little-endian machines we can
17164      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17165      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17166      an integral type that is smaller than TREE_TYPE (decl). These cases arise
17167      when (on a little-endian machine) a non-prototyped function has a
17168      parameter declared to be of type `short' or `char'.  In such cases,
17169      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17170      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17171      passed `int' value.  If the debugger then uses that address to fetch
17172      a `short' or a `char' (on a little-endian machine) the result will be
17173      the correct data, so we allow for such exceptional cases below.
17174
17175      Note that our goal here is to describe the place where the given formal
17176      parameter lives during most of the function's activation (i.e. between the
17177      end of the prologue and the start of the epilogue).  We'll do that as best
17178      as we can. Note however that if the given formal parameter is modified
17179      sometime during the execution of the function, then a stack backtrace (at
17180      debug-time) will show the function as having been called with the *new*
17181      value rather than the value which was originally passed in.  This happens
17182      rarely enough that it is not a major problem, but it *is* a problem, and
17183      I'd like to fix it.
17184
17185      A future version of dwarf2out.c may generate two additional attributes for
17186      any given DW_TAG_formal_parameter DIE which will describe the "passed
17187      type" and the "passed location" for the given formal parameter in addition
17188      to the attributes we now generate to indicate the "declared type" and the
17189      "active location" for each parameter.  This additional set of attributes
17190      could be used by debuggers for stack backtraces. Separately, note that
17191      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17192      This happens (for example) for inlined-instances of inline function formal
17193      parameters which are never referenced.  This really shouldn't be
17194      happening.  All PARM_DECL nodes should get valid non-NULL
17195      DECL_INCOMING_RTL values.  FIXME.  */
17196
17197   /* Use DECL_RTL as the "location" unless we find something better.  */
17198   rtl = DECL_RTL_IF_SET (decl);
17199
17200   /* When generating abstract instances, ignore everything except
17201      constants, symbols living in memory, and symbols living in
17202      fixed registers.  */
17203   if (! reload_completed)
17204     {
17205       if (rtl
17206           && (CONSTANT_P (rtl)
17207               || (MEM_P (rtl)
17208                   && CONSTANT_P (XEXP (rtl, 0)))
17209               || (REG_P (rtl)
17210                   && TREE_CODE (decl) == VAR_DECL
17211                   && TREE_STATIC (decl))))
17212         {
17213           rtl = targetm.delegitimize_address (rtl);
17214           return rtl;
17215         }
17216       rtl = NULL_RTX;
17217     }
17218   else if (TREE_CODE (decl) == PARM_DECL)
17219     {
17220       if (rtl == NULL_RTX
17221           || is_pseudo_reg (rtl)
17222           || (MEM_P (rtl)
17223               && is_pseudo_reg (XEXP (rtl, 0))
17224               && DECL_INCOMING_RTL (decl)
17225               && MEM_P (DECL_INCOMING_RTL (decl))
17226               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17227         {
17228           tree declared_type = TREE_TYPE (decl);
17229           tree passed_type = DECL_ARG_TYPE (decl);
17230           enum machine_mode dmode = TYPE_MODE (declared_type);
17231           enum machine_mode pmode = TYPE_MODE (passed_type);
17232
17233           /* This decl represents a formal parameter which was optimized out.
17234              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17235              all cases where (rtl == NULL_RTX) just below.  */
17236           if (dmode == pmode)
17237             rtl = DECL_INCOMING_RTL (decl);
17238           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17239                    && SCALAR_INT_MODE_P (dmode)
17240                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17241                    && DECL_INCOMING_RTL (decl))
17242             {
17243               rtx inc = DECL_INCOMING_RTL (decl);
17244               if (REG_P (inc))
17245                 rtl = inc;
17246               else if (MEM_P (inc))
17247                 {
17248                   if (BYTES_BIG_ENDIAN)
17249                     rtl = adjust_address_nv (inc, dmode,
17250                                              GET_MODE_SIZE (pmode)
17251                                              - GET_MODE_SIZE (dmode));
17252                   else
17253                     rtl = inc;
17254                 }
17255             }
17256         }
17257
17258       /* If the parm was passed in registers, but lives on the stack, then
17259          make a big endian correction if the mode of the type of the
17260          parameter is not the same as the mode of the rtl.  */
17261       /* ??? This is the same series of checks that are made in dbxout.c before
17262          we reach the big endian correction code there.  It isn't clear if all
17263          of these checks are necessary here, but keeping them all is the safe
17264          thing to do.  */
17265       else if (MEM_P (rtl)
17266                && XEXP (rtl, 0) != const0_rtx
17267                && ! CONSTANT_P (XEXP (rtl, 0))
17268                /* Not passed in memory.  */
17269                && !MEM_P (DECL_INCOMING_RTL (decl))
17270                /* Not passed by invisible reference.  */
17271                && (!REG_P (XEXP (rtl, 0))
17272                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17273                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17274 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17275                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17276 #endif
17277                      )
17278                /* Big endian correction check.  */
17279                && BYTES_BIG_ENDIAN
17280                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17281                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17282                    < UNITS_PER_WORD))
17283         {
17284           int offset = (UNITS_PER_WORD
17285                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17286
17287           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17288                              plus_constant (XEXP (rtl, 0), offset));
17289         }
17290     }
17291   else if (TREE_CODE (decl) == VAR_DECL
17292            && rtl
17293            && MEM_P (rtl)
17294            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17295            && BYTES_BIG_ENDIAN)
17296     {
17297       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17298       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17299
17300       /* If a variable is declared "register" yet is smaller than
17301          a register, then if we store the variable to memory, it
17302          looks like we're storing a register-sized value, when in
17303          fact we are not.  We need to adjust the offset of the
17304          storage location to reflect the actual value's bytes,
17305          else gdb will not be able to display it.  */
17306       if (rsize > dsize)
17307         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17308                            plus_constant (XEXP (rtl, 0), rsize-dsize));
17309     }
17310
17311   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17312      and will have been substituted directly into all expressions that use it.
17313      C does not have such a concept, but C++ and other languages do.  */
17314   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17315     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17316
17317   if (rtl)
17318     rtl = targetm.delegitimize_address (rtl);
17319
17320   /* If we don't look past the constant pool, we risk emitting a
17321      reference to a constant pool entry that isn't referenced from
17322      code, and thus is not emitted.  */
17323   if (rtl)
17324     rtl = avoid_constant_pool_reference (rtl);
17325
17326   /* Try harder to get a rtl.  If this symbol ends up not being emitted
17327      in the current CU, resolve_addr will remove the expression referencing
17328      it.  */
17329   if (rtl == NULL_RTX
17330       && TREE_CODE (decl) == VAR_DECL
17331       && !DECL_EXTERNAL (decl)
17332       && TREE_STATIC (decl)
17333       && DECL_NAME (decl)
17334       && !DECL_HARD_REGISTER (decl)
17335       && DECL_MODE (decl) != VOIDmode)
17336     {
17337       rtl = make_decl_rtl_for_debug (decl);
17338       if (!MEM_P (rtl)
17339           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17340           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17341         rtl = NULL_RTX;
17342     }
17343
17344   return rtl;
17345 }
17346
17347 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17348    returned.  If so, the decl for the COMMON block is returned, and the
17349    value is the offset into the common block for the symbol.  */
17350
17351 static tree
17352 fortran_common (tree decl, HOST_WIDE_INT *value)
17353 {
17354   tree val_expr, cvar;
17355   enum machine_mode mode;
17356   HOST_WIDE_INT bitsize, bitpos;
17357   tree offset;
17358   int volatilep = 0, unsignedp = 0;
17359
17360   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17361      it does not have a value (the offset into the common area), or if it
17362      is thread local (as opposed to global) then it isn't common, and shouldn't
17363      be handled as such.  */
17364   if (TREE_CODE (decl) != VAR_DECL
17365       || !TREE_STATIC (decl)
17366       || !DECL_HAS_VALUE_EXPR_P (decl)
17367       || !is_fortran ())
17368     return NULL_TREE;
17369
17370   val_expr = DECL_VALUE_EXPR (decl);
17371   if (TREE_CODE (val_expr) != COMPONENT_REF)
17372     return NULL_TREE;
17373
17374   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
17375                               &mode, &unsignedp, &volatilep, true);
17376
17377   if (cvar == NULL_TREE
17378       || TREE_CODE (cvar) != VAR_DECL
17379       || DECL_ARTIFICIAL (cvar)
17380       || !TREE_PUBLIC (cvar))
17381     return NULL_TREE;
17382
17383   *value = 0;
17384   if (offset != NULL)
17385     {
17386       if (!host_integerp (offset, 0))
17387         return NULL_TREE;
17388       *value = tree_low_cst (offset, 0);
17389     }
17390   if (bitpos != 0)
17391     *value += bitpos / BITS_PER_UNIT;
17392
17393   return cvar;
17394 }
17395
17396 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17397    data attribute for a variable or a parameter.  We generate the
17398    DW_AT_const_value attribute only in those cases where the given variable
17399    or parameter does not have a true "location" either in memory or in a
17400    register.  This can happen (for example) when a constant is passed as an
17401    actual argument in a call to an inline function.  (It's possible that
17402    these things can crop up in other ways also.)  Note that one type of
17403    constant value which can be passed into an inlined function is a constant
17404    pointer.  This can happen for example if an actual argument in an inlined
17405    function call evaluates to a compile-time constant address.
17406
17407    CACHE_P is true if it is worth caching the location list for DECL,
17408    so that future calls can reuse it rather than regenerate it from scratch.
17409    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17410    since we will need to refer to them each time the function is inlined.  */
17411
17412 static bool
17413 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
17414                                        enum dwarf_attribute attr)
17415 {
17416   rtx rtl;
17417   dw_loc_list_ref list;
17418   var_loc_list *loc_list;
17419   cached_dw_loc_list *cache;
17420   void **slot;
17421
17422   if (TREE_CODE (decl) == ERROR_MARK)
17423     return false;
17424
17425   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17426               || TREE_CODE (decl) == RESULT_DECL);
17427
17428   /* Try to get some constant RTL for this decl, and use that as the value of
17429      the location.  */
17430
17431   rtl = rtl_for_decl_location (decl);
17432   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17433       && add_const_value_attribute (die, rtl))
17434     return true;
17435
17436   /* See if we have single element location list that is equivalent to
17437      a constant value.  That way we are better to use add_const_value_attribute
17438      rather than expanding constant value equivalent.  */
17439   loc_list = lookup_decl_loc (decl);
17440   if (loc_list
17441       && loc_list->first
17442       && loc_list->first->next == NULL
17443       && NOTE_P (loc_list->first->loc)
17444       && NOTE_VAR_LOCATION (loc_list->first->loc)
17445       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17446     {
17447       struct var_loc_node *node;
17448
17449       node = loc_list->first;
17450       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17451       if (GET_CODE (rtl) == EXPR_LIST)
17452         rtl = XEXP (rtl, 0);
17453       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17454           && add_const_value_attribute (die, rtl))
17455          return true;
17456     }
17457   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17458      list several times.  See if we've already cached the contents.  */
17459   list = NULL;
17460   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17461     cache_p = false;
17462   if (cache_p)
17463     {
17464       cache = (cached_dw_loc_list *)
17465         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
17466       if (cache)
17467         list = cache->loc_list;
17468     }
17469   if (list == NULL)
17470     {
17471       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
17472       /* It is usually worth caching this result if the decl is from
17473          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
17474       if (cache_p && list && list->dw_loc_next)
17475         {
17476           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
17477                                            DECL_UID (decl), INSERT);
17478           cache = ggc_alloc_cleared_cached_dw_loc_list ();
17479           cache->decl_id = DECL_UID (decl);
17480           cache->loc_list = list;
17481           *slot = cache;
17482         }
17483     }
17484   if (list)
17485     {
17486       add_AT_location_description (die, attr, list);
17487       return true;
17488     }
17489   /* None of that worked, so it must not really have a location;
17490      try adding a constant value attribute from the DECL_INITIAL.  */
17491   return tree_add_const_value_attribute_for_decl (die, decl);
17492 }
17493
17494 /* Add VARIABLE and DIE into deferred locations list.  */
17495
17496 static void
17497 defer_location (tree variable, dw_die_ref die)
17498 {
17499   deferred_locations entry;
17500   entry.variable = variable;
17501   entry.die = die;
17502   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
17503 }
17504
17505 /* Helper function for tree_add_const_value_attribute.  Natively encode
17506    initializer INIT into an array.  Return true if successful.  */
17507
17508 static bool
17509 native_encode_initializer (tree init, unsigned char *array, int size)
17510 {
17511   tree type;
17512
17513   if (init == NULL_TREE)
17514     return false;
17515
17516   STRIP_NOPS (init);
17517   switch (TREE_CODE (init))
17518     {
17519     case STRING_CST:
17520       type = TREE_TYPE (init);
17521       if (TREE_CODE (type) == ARRAY_TYPE)
17522         {
17523           tree enttype = TREE_TYPE (type);
17524           enum machine_mode mode = TYPE_MODE (enttype);
17525
17526           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17527             return false;
17528           if (int_size_in_bytes (type) != size)
17529             return false;
17530           if (size > TREE_STRING_LENGTH (init))
17531             {
17532               memcpy (array, TREE_STRING_POINTER (init),
17533                       TREE_STRING_LENGTH (init));
17534               memset (array + TREE_STRING_LENGTH (init),
17535                       '\0', size - TREE_STRING_LENGTH (init));
17536             }
17537           else
17538             memcpy (array, TREE_STRING_POINTER (init), size);
17539           return true;
17540         }
17541       return false;
17542     case CONSTRUCTOR:
17543       type = TREE_TYPE (init);
17544       if (int_size_in_bytes (type) != size)
17545         return false;
17546       if (TREE_CODE (type) == ARRAY_TYPE)
17547         {
17548           HOST_WIDE_INT min_index;
17549           unsigned HOST_WIDE_INT cnt;
17550           int curpos = 0, fieldsize;
17551           constructor_elt *ce;
17552
17553           if (TYPE_DOMAIN (type) == NULL_TREE
17554               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17555             return false;
17556
17557           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17558           if (fieldsize <= 0)
17559             return false;
17560
17561           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17562           memset (array, '\0', size);
17563           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17564             {
17565               tree val = ce->value;
17566               tree index = ce->index;
17567               int pos = curpos;
17568               if (index && TREE_CODE (index) == RANGE_EXPR)
17569                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17570                       * fieldsize;
17571               else if (index)
17572                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17573
17574               if (val)
17575                 {
17576                   STRIP_NOPS (val);
17577                   if (!native_encode_initializer (val, array + pos, fieldsize))
17578                     return false;
17579                 }
17580               curpos = pos + fieldsize;
17581               if (index && TREE_CODE (index) == RANGE_EXPR)
17582                 {
17583                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17584                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17585                   while (count > 0)
17586                     {
17587                       if (val)
17588                         memcpy (array + curpos, array + pos, fieldsize);
17589                       curpos += fieldsize;
17590                     }
17591                 }
17592               gcc_assert (curpos <= size);
17593             }
17594           return true;
17595         }
17596       else if (TREE_CODE (type) == RECORD_TYPE
17597                || TREE_CODE (type) == UNION_TYPE)
17598         {
17599           tree field = NULL_TREE;
17600           unsigned HOST_WIDE_INT cnt;
17601           constructor_elt *ce;
17602
17603           if (int_size_in_bytes (type) != size)
17604             return false;
17605
17606           if (TREE_CODE (type) == RECORD_TYPE)
17607             field = TYPE_FIELDS (type);
17608
17609           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17610             {
17611               tree val = ce->value;
17612               int pos, fieldsize;
17613
17614               if (ce->index != 0)
17615                 field = ce->index;
17616
17617               if (val)
17618                 STRIP_NOPS (val);
17619
17620               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17621                 return false;
17622
17623               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17624                   && TYPE_DOMAIN (TREE_TYPE (field))
17625                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17626                 return false;
17627               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17628                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17629                 return false;
17630               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17631               pos = int_byte_position (field);
17632               gcc_assert (pos + fieldsize <= size);
17633               if (val
17634                   && !native_encode_initializer (val, array + pos, fieldsize))
17635                 return false;
17636             }
17637           return true;
17638         }
17639       return false;
17640     case VIEW_CONVERT_EXPR:
17641     case NON_LVALUE_EXPR:
17642       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17643     default:
17644       return native_encode_expr (init, array, size) == size;
17645     }
17646 }
17647
17648 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17649    attribute is the const value T.  */
17650
17651 static bool
17652 tree_add_const_value_attribute (dw_die_ref die, tree t)
17653 {
17654   tree init;
17655   tree type = TREE_TYPE (t);
17656   rtx rtl;
17657
17658   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17659     return false;
17660
17661   init = t;
17662   gcc_assert (!DECL_P (init));
17663
17664   rtl = rtl_for_decl_init (init, type);
17665   if (rtl)
17666     return add_const_value_attribute (die, rtl);
17667   /* If the host and target are sane, try harder.  */
17668   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17669            && initializer_constant_valid_p (init, type))
17670     {
17671       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17672       if (size > 0 && (int) size == size)
17673         {
17674           unsigned char *array = (unsigned char *)
17675             ggc_alloc_cleared_atomic (size);
17676
17677           if (native_encode_initializer (init, array, size))
17678             {
17679               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17680               return true;
17681             }
17682         }
17683     }
17684   return false;
17685 }
17686
17687 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17688    attribute is the const value of T, where T is an integral constant
17689    variable with static storage duration
17690    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17691
17692 static bool
17693 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17694 {
17695
17696   if (!decl
17697       || (TREE_CODE (decl) != VAR_DECL
17698           && TREE_CODE (decl) != CONST_DECL))
17699     return false;
17700
17701     if (TREE_READONLY (decl)
17702         && ! TREE_THIS_VOLATILE (decl)
17703         && DECL_INITIAL (decl))
17704       /* OK */;
17705     else
17706       return false;
17707
17708   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17709   if (get_AT (var_die, DW_AT_const_value))
17710     return false;
17711
17712   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17713 }
17714
17715 /* Convert the CFI instructions for the current function into a
17716    location list.  This is used for DW_AT_frame_base when we targeting
17717    a dwarf2 consumer that does not support the dwarf3
17718    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17719    expressions.  */
17720
17721 static dw_loc_list_ref
17722 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17723 {
17724   int ix;
17725   dw_fde_ref fde;
17726   dw_loc_list_ref list, *list_tail;
17727   dw_cfi_ref cfi;
17728   dw_cfa_location last_cfa, next_cfa;
17729   const char *start_label, *last_label, *section;
17730   dw_cfa_location remember;
17731
17732   fde = current_fde ();
17733   gcc_assert (fde != NULL);
17734
17735   section = secname_for_decl (current_function_decl);
17736   list_tail = &list;
17737   list = NULL;
17738
17739   memset (&next_cfa, 0, sizeof (next_cfa));
17740   next_cfa.reg = INVALID_REGNUM;
17741   remember = next_cfa;
17742
17743   start_label = fde->dw_fde_begin;
17744
17745   /* ??? Bald assumption that the CIE opcode list does not contain
17746      advance opcodes.  */
17747   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
17748     lookup_cfa_1 (cfi, &next_cfa, &remember);
17749
17750   last_cfa = next_cfa;
17751   last_label = start_label;
17752
17753   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
17754     {
17755       /* If the first partition contained no CFI adjustments, the
17756          CIE opcodes apply to the whole first partition.  */
17757       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17758                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17759       list_tail =&(*list_tail)->dw_loc_next;
17760       start_label = last_label = fde->dw_fde_second_begin;
17761     }
17762
17763   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
17764     {
17765       switch (cfi->dw_cfi_opc)
17766         {
17767         case DW_CFA_set_loc:
17768         case DW_CFA_advance_loc1:
17769         case DW_CFA_advance_loc2:
17770         case DW_CFA_advance_loc4:
17771           if (!cfa_equal_p (&last_cfa, &next_cfa))
17772             {
17773               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17774                                          start_label, last_label, section);
17775
17776               list_tail = &(*list_tail)->dw_loc_next;
17777               last_cfa = next_cfa;
17778               start_label = last_label;
17779             }
17780           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17781           break;
17782
17783         case DW_CFA_advance_loc:
17784           /* The encoding is complex enough that we should never emit this.  */
17785           gcc_unreachable ();
17786
17787         default:
17788           lookup_cfa_1 (cfi, &next_cfa, &remember);
17789           break;
17790         }
17791       if (ix + 1 == fde->dw_fde_switch_cfi_index)
17792         {
17793           if (!cfa_equal_p (&last_cfa, &next_cfa))
17794             {
17795               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17796                                          start_label, last_label, section);
17797
17798               list_tail = &(*list_tail)->dw_loc_next;
17799               last_cfa = next_cfa;
17800               start_label = last_label;
17801             }
17802           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17803                                      start_label, fde->dw_fde_end, section);
17804           list_tail = &(*list_tail)->dw_loc_next;
17805           start_label = last_label = fde->dw_fde_second_begin;
17806         }
17807     }
17808
17809   if (!cfa_equal_p (&last_cfa, &next_cfa))
17810     {
17811       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17812                                  start_label, last_label, section);
17813       list_tail = &(*list_tail)->dw_loc_next;
17814       start_label = last_label;
17815     }
17816
17817   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17818                              start_label,
17819                              fde->dw_fde_second_begin
17820                              ? fde->dw_fde_second_end : fde->dw_fde_end,
17821                              section);
17822
17823   if (list && list->dw_loc_next)
17824     gen_llsym (list);
17825
17826   return list;
17827 }
17828
17829 /* Compute a displacement from the "steady-state frame pointer" to the
17830    frame base (often the same as the CFA), and store it in
17831    frame_pointer_fb_offset.  OFFSET is added to the displacement
17832    before the latter is negated.  */
17833
17834 static void
17835 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17836 {
17837   rtx reg, elim;
17838
17839 #ifdef FRAME_POINTER_CFA_OFFSET
17840   reg = frame_pointer_rtx;
17841   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17842 #else
17843   reg = arg_pointer_rtx;
17844   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17845 #endif
17846
17847   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17848   if (GET_CODE (elim) == PLUS)
17849     {
17850       offset += INTVAL (XEXP (elim, 1));
17851       elim = XEXP (elim, 0);
17852     }
17853
17854   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17855                && (elim == hard_frame_pointer_rtx
17856                    || elim == stack_pointer_rtx))
17857               || elim == (frame_pointer_needed
17858                           ? hard_frame_pointer_rtx
17859                           : stack_pointer_rtx));
17860
17861   frame_pointer_fb_offset = -offset;
17862 }
17863
17864 /* Generate a DW_AT_name attribute given some string value to be included as
17865    the value of the attribute.  */
17866
17867 static void
17868 add_name_attribute (dw_die_ref die, const char *name_string)
17869 {
17870   if (name_string != NULL && *name_string != 0)
17871     {
17872       if (demangle_name_func)
17873         name_string = (*demangle_name_func) (name_string);
17874
17875       add_AT_string (die, DW_AT_name, name_string);
17876     }
17877 }
17878
17879 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
17880    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
17881    of TYPE accordingly.
17882
17883    ??? This is a temporary measure until after we're able to generate
17884    regular DWARF for the complex Ada type system.  */
17885
17886 static void 
17887 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
17888                                      dw_die_ref context_die)
17889 {
17890   tree dtype;
17891   dw_die_ref dtype_die;
17892
17893   if (!lang_hooks.types.descriptive_type)
17894     return;
17895
17896   dtype = lang_hooks.types.descriptive_type (type);
17897   if (!dtype)
17898     return;
17899
17900   dtype_die = lookup_type_die (dtype);
17901   if (!dtype_die)
17902     {
17903       gen_type_die (dtype, context_die);
17904       dtype_die = lookup_type_die (dtype);
17905       gcc_assert (dtype_die);
17906     }
17907
17908   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
17909 }
17910
17911 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17912
17913 static void
17914 add_comp_dir_attribute (dw_die_ref die)
17915 {
17916   const char *wd = get_src_pwd ();
17917   char *wd1;
17918
17919   if (wd == NULL)
17920     return;
17921
17922   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17923     {
17924       int wdlen;
17925
17926       wdlen = strlen (wd);
17927       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17928       strcpy (wd1, wd);
17929       wd1 [wdlen] = DIR_SEPARATOR;
17930       wd1 [wdlen + 1] = 0;
17931       wd = wd1;
17932     }
17933
17934     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17935 }
17936
17937 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17938    default.  */
17939
17940 static int
17941 lower_bound_default (void)
17942 {
17943   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17944     {
17945     case DW_LANG_C:
17946     case DW_LANG_C89:
17947     case DW_LANG_C99:
17948     case DW_LANG_C_plus_plus:
17949     case DW_LANG_ObjC:
17950     case DW_LANG_ObjC_plus_plus:
17951     case DW_LANG_Java:
17952       return 0;
17953     case DW_LANG_Fortran77:
17954     case DW_LANG_Fortran90:
17955     case DW_LANG_Fortran95:
17956       return 1;
17957     case DW_LANG_UPC:
17958     case DW_LANG_D:
17959     case DW_LANG_Python:
17960       return dwarf_version >= 4 ? 0 : -1;
17961     case DW_LANG_Ada95:
17962     case DW_LANG_Ada83:
17963     case DW_LANG_Cobol74:
17964     case DW_LANG_Cobol85:
17965     case DW_LANG_Pascal83:
17966     case DW_LANG_Modula2:
17967     case DW_LANG_PLI:
17968       return dwarf_version >= 4 ? 1 : -1;
17969     default:
17970       return -1;
17971     }
17972 }
17973
17974 /* Given a tree node describing an array bound (either lower or upper) output
17975    a representation for that bound.  */
17976
17977 static void
17978 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17979 {
17980   switch (TREE_CODE (bound))
17981     {
17982     case ERROR_MARK:
17983       return;
17984
17985     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17986     case INTEGER_CST:
17987       {
17988         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17989         int dflt;
17990
17991         /* Use the default if possible.  */
17992         if (bound_attr == DW_AT_lower_bound
17993             && host_integerp (bound, 0)
17994             && (dflt = lower_bound_default ()) != -1
17995             && tree_low_cst (bound, 0) == dflt)
17996           ;
17997
17998         /* Otherwise represent the bound as an unsigned value with the
17999            precision of its type.  The precision and signedness of the
18000            type will be necessary to re-interpret it unambiguously.  */
18001         else if (prec < HOST_BITS_PER_WIDE_INT)
18002           {
18003             unsigned HOST_WIDE_INT mask
18004               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
18005             add_AT_unsigned (subrange_die, bound_attr,
18006                              TREE_INT_CST_LOW (bound) & mask);
18007           }
18008         else if (prec == HOST_BITS_PER_WIDE_INT
18009                  || TREE_INT_CST_HIGH (bound) == 0)
18010           add_AT_unsigned (subrange_die, bound_attr,
18011                            TREE_INT_CST_LOW (bound));
18012         else
18013           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
18014                          TREE_INT_CST_LOW (bound));
18015       }
18016       break;
18017
18018     CASE_CONVERT:
18019     case VIEW_CONVERT_EXPR:
18020       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
18021       break;
18022
18023     case SAVE_EXPR:
18024       break;
18025
18026     case VAR_DECL:
18027     case PARM_DECL:
18028     case RESULT_DECL:
18029       {
18030         dw_die_ref decl_die = lookup_decl_die (bound);
18031
18032         /* ??? Can this happen, or should the variable have been bound
18033            first?  Probably it can, since I imagine that we try to create
18034            the types of parameters in the order in which they exist in
18035            the list, and won't have created a forward reference to a
18036            later parameter.  */
18037         if (decl_die != NULL)
18038           {
18039             add_AT_die_ref (subrange_die, bound_attr, decl_die);
18040             break;
18041           }
18042       }
18043       /* FALLTHRU */
18044
18045     default:
18046       {
18047         /* Otherwise try to create a stack operation procedure to
18048            evaluate the value of the array bound.  */
18049
18050         dw_die_ref ctx, decl_die;
18051         dw_loc_list_ref list;
18052
18053         list = loc_list_from_tree (bound, 2);
18054         if (list == NULL || single_element_loc_list_p (list))
18055           {
18056             /* If DW_AT_*bound is not a reference nor constant, it is
18057                a DWARF expression rather than location description.
18058                For that loc_list_from_tree (bound, 0) is needed.
18059                If that fails to give a single element list,
18060                fall back to outputting this as a reference anyway.  */
18061             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
18062             if (list2 && single_element_loc_list_p (list2))
18063               {
18064                 add_AT_loc (subrange_die, bound_attr, list2->expr);
18065                 break;
18066               }
18067           }
18068         if (list == NULL)
18069           break;
18070
18071         if (current_function_decl == 0)
18072           ctx = comp_unit_die ();
18073         else
18074           ctx = lookup_decl_die (current_function_decl);
18075
18076         decl_die = new_die (DW_TAG_variable, ctx, bound);
18077         add_AT_flag (decl_die, DW_AT_artificial, 1);
18078         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
18079         add_AT_location_description (decl_die, DW_AT_location, list);
18080         add_AT_die_ref (subrange_die, bound_attr, decl_die);
18081         break;
18082       }
18083     }
18084 }
18085
18086 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18087    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18088    Note that the block of subscript information for an array type also
18089    includes information about the element type of the given array type.  */
18090
18091 static void
18092 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18093 {
18094   unsigned dimension_number;
18095   tree lower, upper;
18096   dw_die_ref subrange_die;
18097
18098   for (dimension_number = 0;
18099        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18100        type = TREE_TYPE (type), dimension_number++)
18101     {
18102       tree domain = TYPE_DOMAIN (type);
18103
18104       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18105         break;
18106
18107       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18108          and (in GNU C only) variable bounds.  Handle all three forms
18109          here.  */
18110       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18111       if (domain)
18112         {
18113           /* We have an array type with specified bounds.  */
18114           lower = TYPE_MIN_VALUE (domain);
18115           upper = TYPE_MAX_VALUE (domain);
18116
18117           /* Define the index type.  */
18118           if (TREE_TYPE (domain))
18119             {
18120               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
18121                  TREE_TYPE field.  We can't emit debug info for this
18122                  because it is an unnamed integral type.  */
18123               if (TREE_CODE (domain) == INTEGER_TYPE
18124                   && TYPE_NAME (domain) == NULL_TREE
18125                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18126                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18127                 ;
18128               else
18129                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
18130                                     type_die);
18131             }
18132
18133           /* ??? If upper is NULL, the array has unspecified length,
18134              but it does have a lower bound.  This happens with Fortran
18135                dimension arr(N:*)
18136              Since the debugger is definitely going to need to know N
18137              to produce useful results, go ahead and output the lower
18138              bound solo, and hope the debugger can cope.  */
18139
18140           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
18141           if (upper)
18142             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
18143         }
18144
18145       /* Otherwise we have an array type with an unspecified length.  The
18146          DWARF-2 spec does not say how to handle this; let's just leave out the
18147          bounds.  */
18148     }
18149 }
18150
18151 static void
18152 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18153 {
18154   unsigned size;
18155
18156   switch (TREE_CODE (tree_node))
18157     {
18158     case ERROR_MARK:
18159       size = 0;
18160       break;
18161     case ENUMERAL_TYPE:
18162     case RECORD_TYPE:
18163     case UNION_TYPE:
18164     case QUAL_UNION_TYPE:
18165       size = int_size_in_bytes (tree_node);
18166       break;
18167     case FIELD_DECL:
18168       /* For a data member of a struct or union, the DW_AT_byte_size is
18169          generally given as the number of bytes normally allocated for an
18170          object of the *declared* type of the member itself.  This is true
18171          even for bit-fields.  */
18172       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
18173       break;
18174     default:
18175       gcc_unreachable ();
18176     }
18177
18178   /* Note that `size' might be -1 when we get to this point.  If it is, that
18179      indicates that the byte size of the entity in question is variable.  We
18180      have no good way of expressing this fact in Dwarf at the present time,
18181      so just let the -1 pass on through.  */
18182   add_AT_unsigned (die, DW_AT_byte_size, size);
18183 }
18184
18185 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18186    which specifies the distance in bits from the highest order bit of the
18187    "containing object" for the bit-field to the highest order bit of the
18188    bit-field itself.
18189
18190    For any given bit-field, the "containing object" is a hypothetical object
18191    (of some integral or enum type) within which the given bit-field lives.  The
18192    type of this hypothetical "containing object" is always the same as the
18193    declared type of the individual bit-field itself.  The determination of the
18194    exact location of the "containing object" for a bit-field is rather
18195    complicated.  It's handled by the `field_byte_offset' function (above).
18196
18197    Note that it is the size (in bytes) of the hypothetical "containing object"
18198    which will be given in the DW_AT_byte_size attribute for this bit-field.
18199    (See `byte_size_attribute' above).  */
18200
18201 static inline void
18202 add_bit_offset_attribute (dw_die_ref die, tree decl)
18203 {
18204   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
18205   tree type = DECL_BIT_FIELD_TYPE (decl);
18206   HOST_WIDE_INT bitpos_int;
18207   HOST_WIDE_INT highest_order_object_bit_offset;
18208   HOST_WIDE_INT highest_order_field_bit_offset;
18209   HOST_WIDE_INT bit_offset;
18210
18211   /* Must be a field and a bit field.  */
18212   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
18213
18214   /* We can't yet handle bit-fields whose offsets are variable, so if we
18215      encounter such things, just return without generating any attribute
18216      whatsoever.  Likewise for variable or too large size.  */
18217   if (! host_integerp (bit_position (decl), 0)
18218       || ! host_integerp (DECL_SIZE (decl), 1))
18219     return;
18220
18221   bitpos_int = int_bit_position (decl);
18222
18223   /* Note that the bit offset is always the distance (in bits) from the
18224      highest-order bit of the "containing object" to the highest-order bit of
18225      the bit-field itself.  Since the "high-order end" of any object or field
18226      is different on big-endian and little-endian machines, the computation
18227      below must take account of these differences.  */
18228   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18229   highest_order_field_bit_offset = bitpos_int;
18230
18231   if (! BYTES_BIG_ENDIAN)
18232     {
18233       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
18234       highest_order_object_bit_offset += simple_type_size_in_bits (type);
18235     }
18236
18237   bit_offset
18238     = (! BYTES_BIG_ENDIAN
18239        ? highest_order_object_bit_offset - highest_order_field_bit_offset
18240        : highest_order_field_bit_offset - highest_order_object_bit_offset);
18241
18242   if (bit_offset < 0)
18243     add_AT_int (die, DW_AT_bit_offset, bit_offset);
18244   else
18245     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18246 }
18247
18248 /* For a FIELD_DECL node which represents a bit field, output an attribute
18249    which specifies the length in bits of the given field.  */
18250
18251 static inline void
18252 add_bit_size_attribute (dw_die_ref die, tree decl)
18253 {
18254   /* Must be a field and a bit field.  */
18255   gcc_assert (TREE_CODE (decl) == FIELD_DECL
18256               && DECL_BIT_FIELD_TYPE (decl));
18257
18258   if (host_integerp (DECL_SIZE (decl), 1))
18259     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
18260 }
18261
18262 /* If the compiled language is ANSI C, then add a 'prototyped'
18263    attribute, if arg types are given for the parameters of a function.  */
18264
18265 static inline void
18266 add_prototyped_attribute (dw_die_ref die, tree func_type)
18267 {
18268   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
18269       && prototype_p (func_type))
18270     add_AT_flag (die, DW_AT_prototyped, 1);
18271 }
18272
18273 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
18274    by looking in either the type declaration or object declaration
18275    equate table.  */
18276
18277 static inline dw_die_ref
18278 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18279 {
18280   dw_die_ref origin_die = NULL;
18281
18282   if (TREE_CODE (origin) != FUNCTION_DECL)
18283     {
18284       /* We may have gotten separated from the block for the inlined
18285          function, if we're in an exception handler or some such; make
18286          sure that the abstract function has been written out.
18287
18288          Doing this for nested functions is wrong, however; functions are
18289          distinct units, and our context might not even be inline.  */
18290       tree fn = origin;
18291
18292       if (TYPE_P (fn))
18293         fn = TYPE_STUB_DECL (fn);
18294
18295       fn = decl_function_context (fn);
18296       if (fn)
18297         dwarf2out_abstract_function (fn);
18298     }
18299
18300   if (DECL_P (origin))
18301     origin_die = lookup_decl_die (origin);
18302   else if (TYPE_P (origin))
18303     origin_die = lookup_type_die (origin);
18304
18305   /* XXX: Functions that are never lowered don't always have correct block
18306      trees (in the case of java, they simply have no block tree, in some other
18307      languages).  For these functions, there is nothing we can really do to
18308      output correct debug info for inlined functions in all cases.  Rather
18309      than die, we'll just produce deficient debug info now, in that we will
18310      have variables without a proper abstract origin.  In the future, when all
18311      functions are lowered, we should re-add a gcc_assert (origin_die)
18312      here.  */
18313
18314   if (origin_die)
18315     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18316   return origin_die;
18317 }
18318
18319 /* We do not currently support the pure_virtual attribute.  */
18320
18321 static inline void
18322 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18323 {
18324   if (DECL_VINDEX (func_decl))
18325     {
18326       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18327
18328       if (host_integerp (DECL_VINDEX (func_decl), 0))
18329         add_AT_loc (die, DW_AT_vtable_elem_location,
18330                     new_loc_descr (DW_OP_constu,
18331                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
18332                                    0));
18333
18334       /* GNU extension: Record what type this method came from originally.  */
18335       if (debug_info_level > DINFO_LEVEL_TERSE
18336           && DECL_CONTEXT (func_decl))
18337         add_AT_die_ref (die, DW_AT_containing_type,
18338                         lookup_type_die (DECL_CONTEXT (func_decl)));
18339     }
18340 }
18341 \f
18342 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18343    given decl.  This used to be a vendor extension until after DWARF 4
18344    standardized it.  */
18345
18346 static void
18347 add_linkage_attr (dw_die_ref die, tree decl)
18348 {
18349   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18350
18351   /* Mimic what assemble_name_raw does with a leading '*'.  */
18352   if (name[0] == '*')
18353     name = &name[1];
18354
18355   if (dwarf_version >= 4)
18356     add_AT_string (die, DW_AT_linkage_name, name);
18357   else
18358     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18359 }
18360
18361 /* Add source coordinate attributes for the given decl.  */
18362
18363 static void
18364 add_src_coords_attributes (dw_die_ref die, tree decl)
18365 {
18366   expanded_location s;
18367
18368   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
18369     return;
18370   s = expand_location (DECL_SOURCE_LOCATION (decl));
18371   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18372   add_AT_unsigned (die, DW_AT_decl_line, s.line);
18373 }
18374
18375 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
18376
18377 static void
18378 add_linkage_name (dw_die_ref die, tree decl)
18379 {
18380   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18381        && TREE_PUBLIC (decl)
18382        && !DECL_ABSTRACT (decl)
18383        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18384        && die->die_tag != DW_TAG_member)
18385     {
18386       /* Defer until we have an assembler name set.  */
18387       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18388         {
18389           limbo_die_node *asm_name;
18390
18391           asm_name = ggc_alloc_cleared_limbo_die_node ();
18392           asm_name->die = die;
18393           asm_name->created_for = decl;
18394           asm_name->next = deferred_asm_name;
18395           deferred_asm_name = asm_name;
18396         }
18397       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18398         add_linkage_attr (die, decl);
18399     }
18400 }
18401
18402 /* Add a DW_AT_name attribute and source coordinate attribute for the
18403    given decl, but only if it actually has a name.  */
18404
18405 static void
18406 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18407 {
18408   tree decl_name;
18409
18410   decl_name = DECL_NAME (decl);
18411   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18412     {
18413       const char *name = dwarf2_name (decl, 0);
18414       if (name)
18415         add_name_attribute (die, name);
18416       if (! DECL_ARTIFICIAL (decl))
18417         add_src_coords_attributes (die, decl);
18418
18419       add_linkage_name (die, decl);
18420     }
18421
18422 #ifdef VMS_DEBUGGING_INFO
18423   /* Get the function's name, as described by its RTL.  This may be different
18424      from the DECL_NAME name used in the source file.  */
18425   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18426     {
18427       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18428                    XEXP (DECL_RTL (decl), 0));
18429       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
18430     }
18431 #endif /* VMS_DEBUGGING_INFO */
18432 }
18433
18434 #ifdef VMS_DEBUGGING_INFO
18435 /* Output the debug main pointer die for VMS */
18436
18437 void
18438 dwarf2out_vms_debug_main_pointer (void)
18439 {
18440   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18441   dw_die_ref die;
18442
18443   /* Allocate the VMS debug main subprogram die.  */
18444   die = ggc_alloc_cleared_die_node ();
18445   die->die_tag = DW_TAG_subprogram;
18446   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18447   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18448                                current_function_funcdef_no);
18449   add_AT_lbl_id (die, DW_AT_entry_pc, label);
18450
18451   /* Make it the first child of comp_unit_die ().  */
18452   die->die_parent = comp_unit_die ();
18453   if (comp_unit_die ()->die_child)
18454     {
18455       die->die_sib = comp_unit_die ()->die_child->die_sib;
18456       comp_unit_die ()->die_child->die_sib = die;
18457     }
18458   else
18459     {
18460       die->die_sib = die;
18461       comp_unit_die ()->die_child = die;
18462     }
18463 }
18464 #endif /* VMS_DEBUGGING_INFO */
18465
18466 /* Push a new declaration scope.  */
18467
18468 static void
18469 push_decl_scope (tree scope)
18470 {
18471   VEC_safe_push (tree, gc, decl_scope_table, scope);
18472 }
18473
18474 /* Pop a declaration scope.  */
18475
18476 static inline void
18477 pop_decl_scope (void)
18478 {
18479   VEC_pop (tree, decl_scope_table);
18480 }
18481
18482 /* Return the DIE for the scope that immediately contains this type.
18483    Non-named types get global scope.  Named types nested in other
18484    types get their containing scope if it's open, or global scope
18485    otherwise.  All other types (i.e. function-local named types) get
18486    the current active scope.  */
18487
18488 static dw_die_ref
18489 scope_die_for (tree t, dw_die_ref context_die)
18490 {
18491   dw_die_ref scope_die = NULL;
18492   tree containing_scope;
18493   int i;
18494
18495   /* Non-types always go in the current scope.  */
18496   gcc_assert (TYPE_P (t));
18497
18498   containing_scope = TYPE_CONTEXT (t);
18499
18500   /* Use the containing namespace if it was passed in (for a declaration).  */
18501   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18502     {
18503       if (context_die == lookup_decl_die (containing_scope))
18504         /* OK */;
18505       else
18506         containing_scope = NULL_TREE;
18507     }
18508
18509   /* Ignore function type "scopes" from the C frontend.  They mean that
18510      a tagged type is local to a parmlist of a function declarator, but
18511      that isn't useful to DWARF.  */
18512   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18513     containing_scope = NULL_TREE;
18514
18515   if (SCOPE_FILE_SCOPE_P (containing_scope))
18516     scope_die = comp_unit_die ();
18517   else if (TYPE_P (containing_scope))
18518     {
18519       /* For types, we can just look up the appropriate DIE.  But
18520          first we check to see if we're in the middle of emitting it
18521          so we know where the new DIE should go.  */
18522       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
18523         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
18524           break;
18525
18526       if (i < 0)
18527         {
18528           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
18529                       || TREE_ASM_WRITTEN (containing_scope));
18530           /*We are not in the middle of emitting the type
18531             CONTAINING_SCOPE. Let's see if it's emitted already.  */
18532           scope_die = lookup_type_die (containing_scope);
18533
18534           /* If none of the current dies are suitable, we get file scope.  */
18535           if (scope_die == NULL)
18536             scope_die = comp_unit_die ();
18537         }
18538       else
18539         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18540     }
18541   else
18542     scope_die = context_die;
18543
18544   return scope_die;
18545 }
18546
18547 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18548
18549 static inline int
18550 local_scope_p (dw_die_ref context_die)
18551 {
18552   for (; context_die; context_die = context_die->die_parent)
18553     if (context_die->die_tag == DW_TAG_inlined_subroutine
18554         || context_die->die_tag == DW_TAG_subprogram)
18555       return 1;
18556
18557   return 0;
18558 }
18559
18560 /* Returns nonzero if CONTEXT_DIE is a class.  */
18561
18562 static inline int
18563 class_scope_p (dw_die_ref context_die)
18564 {
18565   return (context_die
18566           && (context_die->die_tag == DW_TAG_structure_type
18567               || context_die->die_tag == DW_TAG_class_type
18568               || context_die->die_tag == DW_TAG_interface_type
18569               || context_die->die_tag == DW_TAG_union_type));
18570 }
18571
18572 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18573    whether or not to treat a DIE in this context as a declaration.  */
18574
18575 static inline int
18576 class_or_namespace_scope_p (dw_die_ref context_die)
18577 {
18578   return (class_scope_p (context_die)
18579           || (context_die && context_die->die_tag == DW_TAG_namespace));
18580 }
18581
18582 /* Many forms of DIEs require a "type description" attribute.  This
18583    routine locates the proper "type descriptor" die for the type given
18584    by 'type', and adds a DW_AT_type attribute below the given die.  */
18585
18586 static void
18587 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18588                     int decl_volatile, dw_die_ref context_die)
18589 {
18590   enum tree_code code  = TREE_CODE (type);
18591   dw_die_ref type_die  = NULL;
18592
18593   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18594      or fixed-point type, use the inner type.  This is because we have no
18595      support for unnamed types in base_type_die.  This can happen if this is
18596      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18597   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18598       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18599     type = TREE_TYPE (type), code = TREE_CODE (type);
18600
18601   if (code == ERROR_MARK
18602       /* Handle a special case.  For functions whose return type is void, we
18603          generate *no* type attribute.  (Note that no object may have type
18604          `void', so this only applies to function return types).  */
18605       || code == VOID_TYPE)
18606     return;
18607
18608   type_die = modified_type_die (type,
18609                                 decl_const || TYPE_READONLY (type),
18610                                 decl_volatile || TYPE_VOLATILE (type),
18611                                 context_die);
18612
18613   if (type_die != NULL)
18614     add_AT_die_ref (object_die, DW_AT_type, type_die);
18615 }
18616
18617 /* Given an object die, add the calling convention attribute for the
18618    function call type.  */
18619 static void
18620 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18621 {
18622   enum dwarf_calling_convention value = DW_CC_normal;
18623
18624   value = ((enum dwarf_calling_convention)
18625            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18626
18627   if (is_fortran ()
18628       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18629     {
18630       /* DWARF 2 doesn't provide a way to identify a program's source-level
18631         entry point.  DW_AT_calling_convention attributes are only meant
18632         to describe functions' calling conventions.  However, lacking a
18633         better way to signal the Fortran main program, we used this for 
18634         a long time, following existing custom.  Now, DWARF 4 has 
18635         DW_AT_main_subprogram, which we add below, but some tools still
18636         rely on the old way, which we thus keep.  */
18637       value = DW_CC_program;
18638
18639       if (dwarf_version >= 4 || !dwarf_strict)
18640         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18641     }
18642
18643   /* Only add the attribute if the backend requests it, and
18644      is not DW_CC_normal.  */
18645   if (value && (value != DW_CC_normal))
18646     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18647 }
18648
18649 /* Given a tree pointer to a struct, class, union, or enum type node, return
18650    a pointer to the (string) tag name for the given type, or zero if the type
18651    was declared without a tag.  */
18652
18653 static const char *
18654 type_tag (const_tree type)
18655 {
18656   const char *name = 0;
18657
18658   if (TYPE_NAME (type) != 0)
18659     {
18660       tree t = 0;
18661
18662       /* Find the IDENTIFIER_NODE for the type name.  */
18663       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18664           && !TYPE_NAMELESS (type))
18665         t = TYPE_NAME (type);
18666
18667       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18668          a TYPE_DECL node, regardless of whether or not a `typedef' was
18669          involved.  */
18670       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18671                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18672         {
18673           /* We want to be extra verbose.  Don't call dwarf_name if
18674              DECL_NAME isn't set.  The default hook for decl_printable_name
18675              doesn't like that, and in this context it's correct to return
18676              0, instead of "<anonymous>" or the like.  */
18677           if (DECL_NAME (TYPE_NAME (type))
18678               && !DECL_NAMELESS (TYPE_NAME (type)))
18679             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18680         }
18681
18682       /* Now get the name as a string, or invent one.  */
18683       if (!name && t != 0)
18684         name = IDENTIFIER_POINTER (t);
18685     }
18686
18687   return (name == 0 || *name == '\0') ? 0 : name;
18688 }
18689
18690 /* Return the type associated with a data member, make a special check
18691    for bit field types.  */
18692
18693 static inline tree
18694 member_declared_type (const_tree member)
18695 {
18696   return (DECL_BIT_FIELD_TYPE (member)
18697           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18698 }
18699
18700 /* Get the decl's label, as described by its RTL. This may be different
18701    from the DECL_NAME name used in the source file.  */
18702
18703 #if 0
18704 static const char *
18705 decl_start_label (tree decl)
18706 {
18707   rtx x;
18708   const char *fnname;
18709
18710   x = DECL_RTL (decl);
18711   gcc_assert (MEM_P (x));
18712
18713   x = XEXP (x, 0);
18714   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18715
18716   fnname = XSTR (x, 0);
18717   return fnname;
18718 }
18719 #endif
18720 \f
18721 /* These routines generate the internal representation of the DIE's for
18722    the compilation unit.  Debugging information is collected by walking
18723    the declaration trees passed in from dwarf2out_decl().  */
18724
18725 static void
18726 gen_array_type_die (tree type, dw_die_ref context_die)
18727 {
18728   dw_die_ref scope_die = scope_die_for (type, context_die);
18729   dw_die_ref array_die;
18730
18731   /* GNU compilers represent multidimensional array types as sequences of one
18732      dimensional array types whose element types are themselves array types.
18733      We sometimes squish that down to a single array_type DIE with multiple
18734      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18735      say that we are allowed to do this kind of compression in C, because
18736      there is no difference between an array of arrays and a multidimensional
18737      array.  We don't do this for Ada to remain as close as possible to the
18738      actual representation, which is especially important against the language
18739      flexibilty wrt arrays of variable size.  */
18740
18741   bool collapse_nested_arrays = !is_ada ();
18742   tree element_type;
18743
18744   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18745      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18746   if (TYPE_STRING_FLAG (type)
18747       && TREE_CODE (type) == ARRAY_TYPE
18748       && is_fortran ()
18749       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18750     {
18751       HOST_WIDE_INT size;
18752
18753       array_die = new_die (DW_TAG_string_type, scope_die, type);
18754       add_name_attribute (array_die, type_tag (type));
18755       equate_type_number_to_die (type, array_die);
18756       size = int_size_in_bytes (type);
18757       if (size >= 0)
18758         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18759       else if (TYPE_DOMAIN (type) != NULL_TREE
18760                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18761                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18762         {
18763           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18764           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18765
18766           size = int_size_in_bytes (TREE_TYPE (szdecl));
18767           if (loc && size > 0)
18768             {
18769               add_AT_location_description (array_die, DW_AT_string_length, loc);
18770               if (size != DWARF2_ADDR_SIZE)
18771                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18772             }
18773         }
18774       return;
18775     }
18776
18777   /* ??? The SGI dwarf reader fails for array of array of enum types
18778      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18779      array type comes before the outer array type.  We thus call gen_type_die
18780      before we new_die and must prevent nested array types collapsing for this
18781      target.  */
18782
18783 #ifdef MIPS_DEBUGGING_INFO
18784   gen_type_die (TREE_TYPE (type), context_die);
18785   collapse_nested_arrays = false;
18786 #endif
18787
18788   array_die = new_die (DW_TAG_array_type, scope_die, type);
18789   add_name_attribute (array_die, type_tag (type));
18790   add_gnat_descriptive_type_attribute (array_die, type, context_die);
18791   equate_type_number_to_die (type, array_die);
18792
18793   if (TREE_CODE (type) == VECTOR_TYPE)
18794     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18795
18796   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18797   if (is_fortran ()
18798       && TREE_CODE (type) == ARRAY_TYPE
18799       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18800       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18801     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18802
18803 #if 0
18804   /* We default the array ordering.  SDB will probably do
18805      the right things even if DW_AT_ordering is not present.  It's not even
18806      an issue until we start to get into multidimensional arrays anyway.  If
18807      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18808      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18809      and when we find out that we need to put these in, we will only do so
18810      for multidimensional arrays.  */
18811   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18812 #endif
18813
18814 #ifdef MIPS_DEBUGGING_INFO
18815   /* The SGI compilers handle arrays of unknown bound by setting
18816      AT_declaration and not emitting any subrange DIEs.  */
18817   if (TREE_CODE (type) == ARRAY_TYPE
18818       && ! TYPE_DOMAIN (type))
18819     add_AT_flag (array_die, DW_AT_declaration, 1);
18820   else
18821 #endif
18822   if (TREE_CODE (type) == VECTOR_TYPE)
18823     {
18824       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18825       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18826       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18827       add_bound_info (subrange_die, DW_AT_upper_bound,
18828                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18829     }
18830   else
18831     add_subscript_info (array_die, type, collapse_nested_arrays);
18832
18833   /* Add representation of the type of the elements of this array type and
18834      emit the corresponding DIE if we haven't done it already.  */
18835   element_type = TREE_TYPE (type);
18836   if (collapse_nested_arrays)
18837     while (TREE_CODE (element_type) == ARRAY_TYPE)
18838       {
18839         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18840           break;
18841         element_type = TREE_TYPE (element_type);
18842       }
18843
18844 #ifndef MIPS_DEBUGGING_INFO
18845   gen_type_die (element_type, context_die);
18846 #endif
18847
18848   add_type_attribute (array_die, element_type, 0, 0, context_die);
18849
18850   if (get_AT (array_die, DW_AT_name))
18851     add_pubtype (type, array_die);
18852 }
18853
18854 static dw_loc_descr_ref
18855 descr_info_loc (tree val, tree base_decl)
18856 {
18857   HOST_WIDE_INT size;
18858   dw_loc_descr_ref loc, loc2;
18859   enum dwarf_location_atom op;
18860
18861   if (val == base_decl)
18862     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18863
18864   switch (TREE_CODE (val))
18865     {
18866     CASE_CONVERT:
18867       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18868     case VAR_DECL:
18869       return loc_descriptor_from_tree (val, 0);
18870     case INTEGER_CST:
18871       if (host_integerp (val, 0))
18872         return int_loc_descriptor (tree_low_cst (val, 0));
18873       break;
18874     case INDIRECT_REF:
18875       size = int_size_in_bytes (TREE_TYPE (val));
18876       if (size < 0)
18877         break;
18878       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18879       if (!loc)
18880         break;
18881       if (size == DWARF2_ADDR_SIZE)
18882         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18883       else
18884         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18885       return loc;
18886     case POINTER_PLUS_EXPR:
18887     case PLUS_EXPR:
18888       if (host_integerp (TREE_OPERAND (val, 1), 1)
18889           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18890              < 16384)
18891         {
18892           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18893           if (!loc)
18894             break;
18895           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18896         }
18897       else
18898         {
18899           op = DW_OP_plus;
18900         do_binop:
18901           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18902           if (!loc)
18903             break;
18904           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18905           if (!loc2)
18906             break;
18907           add_loc_descr (&loc, loc2);
18908           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18909         }
18910       return loc;
18911     case MINUS_EXPR:
18912       op = DW_OP_minus;
18913       goto do_binop;
18914     case MULT_EXPR:
18915       op = DW_OP_mul;
18916       goto do_binop;
18917     case EQ_EXPR:
18918       op = DW_OP_eq;
18919       goto do_binop;
18920     case NE_EXPR:
18921       op = DW_OP_ne;
18922       goto do_binop;
18923     default:
18924       break;
18925     }
18926   return NULL;
18927 }
18928
18929 static void
18930 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18931                       tree val, tree base_decl)
18932 {
18933   dw_loc_descr_ref loc;
18934
18935   if (host_integerp (val, 0))
18936     {
18937       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18938       return;
18939     }
18940
18941   loc = descr_info_loc (val, base_decl);
18942   if (!loc)
18943     return;
18944
18945   add_AT_loc (die, attr, loc);
18946 }
18947
18948 /* This routine generates DIE for array with hidden descriptor, details
18949    are filled into *info by a langhook.  */
18950
18951 static void
18952 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18953                           dw_die_ref context_die)
18954 {
18955   dw_die_ref scope_die = scope_die_for (type, context_die);
18956   dw_die_ref array_die;
18957   int dim;
18958
18959   array_die = new_die (DW_TAG_array_type, scope_die, type);
18960   add_name_attribute (array_die, type_tag (type));
18961   equate_type_number_to_die (type, array_die);
18962
18963   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18964   if (is_fortran ()
18965       && info->ndimensions >= 2)
18966     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18967
18968   if (info->data_location)
18969     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18970                           info->base_decl);
18971   if (info->associated)
18972     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18973                           info->base_decl);
18974   if (info->allocated)
18975     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18976                           info->base_decl);
18977
18978   for (dim = 0; dim < info->ndimensions; dim++)
18979     {
18980       dw_die_ref subrange_die
18981         = new_die (DW_TAG_subrange_type, array_die, NULL);
18982
18983       if (info->dimen[dim].lower_bound)
18984         {
18985           /* If it is the default value, omit it.  */
18986           int dflt;
18987
18988           if (host_integerp (info->dimen[dim].lower_bound, 0)
18989               && (dflt = lower_bound_default ()) != -1
18990               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18991             ;
18992           else
18993             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18994                                   info->dimen[dim].lower_bound,
18995                                   info->base_decl);
18996         }
18997       if (info->dimen[dim].upper_bound)
18998         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18999                               info->dimen[dim].upper_bound,
19000                               info->base_decl);
19001       if (info->dimen[dim].stride)
19002         add_descr_info_field (subrange_die, DW_AT_byte_stride,
19003                               info->dimen[dim].stride,
19004                               info->base_decl);
19005     }
19006
19007   gen_type_die (info->element_type, context_die);
19008   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
19009
19010   if (get_AT (array_die, DW_AT_name))
19011     add_pubtype (type, array_die);
19012 }
19013
19014 #if 0
19015 static void
19016 gen_entry_point_die (tree decl, dw_die_ref context_die)
19017 {
19018   tree origin = decl_ultimate_origin (decl);
19019   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19020
19021   if (origin != NULL)
19022     add_abstract_origin_attribute (decl_die, origin);
19023   else
19024     {
19025       add_name_and_src_coords_attributes (decl_die, decl);
19026       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19027                           0, 0, context_die);
19028     }
19029
19030   if (DECL_ABSTRACT (decl))
19031     equate_decl_number_to_die (decl, decl_die);
19032   else
19033     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19034 }
19035 #endif
19036
19037 /* Walk through the list of incomplete types again, trying once more to
19038    emit full debugging info for them.  */
19039
19040 static void
19041 retry_incomplete_types (void)
19042 {
19043   int i;
19044
19045   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
19046     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
19047                                   DINFO_USAGE_DIR_USE))
19048       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
19049 }
19050
19051 /* Determine what tag to use for a record type.  */
19052
19053 static enum dwarf_tag
19054 record_type_tag (tree type)
19055 {
19056   if (! lang_hooks.types.classify_record)
19057     return DW_TAG_structure_type;
19058
19059   switch (lang_hooks.types.classify_record (type))
19060     {
19061     case RECORD_IS_STRUCT:
19062       return DW_TAG_structure_type;
19063
19064     case RECORD_IS_CLASS:
19065       return DW_TAG_class_type;
19066
19067     case RECORD_IS_INTERFACE:
19068       if (dwarf_version >= 3 || !dwarf_strict)
19069         return DW_TAG_interface_type;
19070       return DW_TAG_structure_type;
19071
19072     default:
19073       gcc_unreachable ();
19074     }
19075 }
19076
19077 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
19078    include all of the information about the enumeration values also. Each
19079    enumerated type name/value is listed as a child of the enumerated type
19080    DIE.  */
19081
19082 static dw_die_ref
19083 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19084 {
19085   dw_die_ref type_die = lookup_type_die (type);
19086
19087   if (type_die == NULL)
19088     {
19089       type_die = new_die (DW_TAG_enumeration_type,
19090                           scope_die_for (type, context_die), type);
19091       equate_type_number_to_die (type, type_die);
19092       add_name_attribute (type_die, type_tag (type));
19093       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19094       if (dwarf_version >= 4 || !dwarf_strict)
19095         {
19096           if (ENUM_IS_SCOPED (type))
19097             add_AT_flag (type_die, DW_AT_enum_class, 1);
19098           if (ENUM_IS_OPAQUE (type))
19099             add_AT_flag (type_die, DW_AT_declaration, 1);
19100         }
19101     }
19102   else if (! TYPE_SIZE (type))
19103     return type_die;
19104   else
19105     remove_AT (type_die, DW_AT_declaration);
19106
19107   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
19108      given enum type is incomplete, do not generate the DW_AT_byte_size
19109      attribute or the DW_AT_element_list attribute.  */
19110   if (TYPE_SIZE (type))
19111     {
19112       tree link;
19113
19114       TREE_ASM_WRITTEN (type) = 1;
19115       add_byte_size_attribute (type_die, type);
19116       if (TYPE_STUB_DECL (type) != NULL_TREE)
19117         {
19118           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19119           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19120         }
19121
19122       /* If the first reference to this type was as the return type of an
19123          inline function, then it may not have a parent.  Fix this now.  */
19124       if (type_die->die_parent == NULL)
19125         add_child_die (scope_die_for (type, context_die), type_die);
19126
19127       for (link = TYPE_VALUES (type);
19128            link != NULL; link = TREE_CHAIN (link))
19129         {
19130           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19131           tree value = TREE_VALUE (link);
19132
19133           add_name_attribute (enum_die,
19134                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19135
19136           if (TREE_CODE (value) == CONST_DECL)
19137             value = DECL_INITIAL (value);
19138
19139           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
19140             /* DWARF2 does not provide a way of indicating whether or
19141                not enumeration constants are signed or unsigned.  GDB
19142                always assumes the values are signed, so we output all
19143                values as if they were signed.  That means that
19144                enumeration constants with very large unsigned values
19145                will appear to have negative values in the debugger.  */
19146             add_AT_int (enum_die, DW_AT_const_value,
19147                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
19148         }
19149     }
19150   else
19151     add_AT_flag (type_die, DW_AT_declaration, 1);
19152
19153   if (get_AT (type_die, DW_AT_name))
19154     add_pubtype (type, type_die);
19155
19156   return type_die;
19157 }
19158
19159 /* Generate a DIE to represent either a real live formal parameter decl or to
19160    represent just the type of some formal parameter position in some function
19161    type.
19162
19163    Note that this routine is a bit unusual because its argument may be a
19164    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19165    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19166    node.  If it's the former then this function is being called to output a
19167    DIE to represent a formal parameter object (or some inlining thereof).  If
19168    it's the latter, then this function is only being called to output a
19169    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19170    argument type of some subprogram type.
19171    If EMIT_NAME_P is true, name and source coordinate attributes
19172    are emitted.  */
19173
19174 static dw_die_ref
19175 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19176                           dw_die_ref context_die)
19177 {
19178   tree node_or_origin = node ? node : origin;
19179   tree ultimate_origin;
19180   dw_die_ref parm_die
19181     = new_die (DW_TAG_formal_parameter, context_die, node);
19182
19183   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19184     {
19185     case tcc_declaration:
19186       ultimate_origin = decl_ultimate_origin (node_or_origin);
19187       if (node || ultimate_origin)
19188         origin = ultimate_origin;
19189       if (origin != NULL)
19190         add_abstract_origin_attribute (parm_die, origin);
19191       else if (emit_name_p)
19192         add_name_and_src_coords_attributes (parm_die, node);
19193       if (origin == NULL
19194           || (! DECL_ABSTRACT (node_or_origin)
19195               && variably_modified_type_p (TREE_TYPE (node_or_origin),
19196                                            decl_function_context
19197                                                             (node_or_origin))))
19198         {
19199           tree type = TREE_TYPE (node_or_origin);
19200           if (decl_by_reference_p (node_or_origin))
19201             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
19202                                 context_die);
19203           else
19204             add_type_attribute (parm_die, type,
19205                                 TREE_READONLY (node_or_origin),
19206                                 TREE_THIS_VOLATILE (node_or_origin),
19207                                 context_die);
19208         }
19209       if (origin == NULL && DECL_ARTIFICIAL (node))
19210         add_AT_flag (parm_die, DW_AT_artificial, 1);
19211
19212       if (node && node != origin)
19213         equate_decl_number_to_die (node, parm_die);
19214       if (! DECL_ABSTRACT (node_or_origin))
19215         add_location_or_const_value_attribute (parm_die, node_or_origin,
19216                                                node == NULL, DW_AT_location);
19217
19218       break;
19219
19220     case tcc_type:
19221       /* We were called with some kind of a ..._TYPE node.  */
19222       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
19223       break;
19224
19225     default:
19226       gcc_unreachable ();
19227     }
19228
19229   return parm_die;
19230 }
19231
19232 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19233    children DW_TAG_formal_parameter DIEs representing the arguments of the
19234    parameter pack.
19235
19236    PARM_PACK must be a function parameter pack.
19237    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19238    must point to the subsequent arguments of the function PACK_ARG belongs to.
19239    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19240    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19241    following the last one for which a DIE was generated.  */
19242
19243 static dw_die_ref
19244 gen_formal_parameter_pack_die  (tree parm_pack,
19245                                 tree pack_arg,
19246                                 dw_die_ref subr_die,
19247                                 tree *next_arg)
19248 {
19249   tree arg;
19250   dw_die_ref parm_pack_die;
19251
19252   gcc_assert (parm_pack
19253               && lang_hooks.function_parameter_pack_p (parm_pack)
19254               && subr_die);
19255
19256   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19257   add_src_coords_attributes (parm_pack_die, parm_pack);
19258
19259   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19260     {
19261       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19262                                                                  parm_pack))
19263         break;
19264       gen_formal_parameter_die (arg, NULL,
19265                                 false /* Don't emit name attribute.  */,
19266                                 parm_pack_die);
19267     }
19268   if (next_arg)
19269     *next_arg = arg;
19270   return parm_pack_die;
19271 }
19272
19273 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19274    at the end of an (ANSI prototyped) formal parameters list.  */
19275
19276 static void
19277 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19278 {
19279   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19280 }
19281
19282 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19283    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19284    parameters as specified in some function type specification (except for
19285    those which appear as part of a function *definition*).  */
19286
19287 static void
19288 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19289 {
19290   tree link;
19291   tree formal_type = NULL;
19292   tree first_parm_type;
19293   tree arg;
19294
19295   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19296     {
19297       arg = DECL_ARGUMENTS (function_or_method_type);
19298       function_or_method_type = TREE_TYPE (function_or_method_type);
19299     }
19300   else
19301     arg = NULL_TREE;
19302
19303   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19304
19305   /* Make our first pass over the list of formal parameter types and output a
19306      DW_TAG_formal_parameter DIE for each one.  */
19307   for (link = first_parm_type; link; )
19308     {
19309       dw_die_ref parm_die;
19310
19311       formal_type = TREE_VALUE (link);
19312       if (formal_type == void_type_node)
19313         break;
19314
19315       /* Output a (nameless) DIE to represent the formal parameter itself.  */
19316       parm_die = gen_formal_parameter_die (formal_type, NULL,
19317                                            true /* Emit name attribute.  */,
19318                                            context_die);
19319       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19320           && link == first_parm_type)
19321         {
19322           add_AT_flag (parm_die, DW_AT_artificial, 1);
19323           if (dwarf_version >= 3 || !dwarf_strict)
19324             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19325         }
19326       else if (arg && DECL_ARTIFICIAL (arg))
19327         add_AT_flag (parm_die, DW_AT_artificial, 1);
19328
19329       link = TREE_CHAIN (link);
19330       if (arg)
19331         arg = DECL_CHAIN (arg);
19332     }
19333
19334   /* If this function type has an ellipsis, add a
19335      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
19336   if (formal_type != void_type_node)
19337     gen_unspecified_parameters_die (function_or_method_type, context_die);
19338
19339   /* Make our second (and final) pass over the list of formal parameter types
19340      and output DIEs to represent those types (as necessary).  */
19341   for (link = TYPE_ARG_TYPES (function_or_method_type);
19342        link && TREE_VALUE (link);
19343        link = TREE_CHAIN (link))
19344     gen_type_die (TREE_VALUE (link), context_die);
19345 }
19346
19347 /* We want to generate the DIE for TYPE so that we can generate the
19348    die for MEMBER, which has been defined; we will need to refer back
19349    to the member declaration nested within TYPE.  If we're trying to
19350    generate minimal debug info for TYPE, processing TYPE won't do the
19351    trick; we need to attach the member declaration by hand.  */
19352
19353 static void
19354 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19355 {
19356   gen_type_die (type, context_die);
19357
19358   /* If we're trying to avoid duplicate debug info, we may not have
19359      emitted the member decl for this function.  Emit it now.  */
19360   if (TYPE_STUB_DECL (type)
19361       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19362       && ! lookup_decl_die (member))
19363     {
19364       dw_die_ref type_die;
19365       gcc_assert (!decl_ultimate_origin (member));
19366
19367       push_decl_scope (type);
19368       type_die = lookup_type_die_strip_naming_typedef (type);
19369       if (TREE_CODE (member) == FUNCTION_DECL)
19370         gen_subprogram_die (member, type_die);
19371       else if (TREE_CODE (member) == FIELD_DECL)
19372         {
19373           /* Ignore the nameless fields that are used to skip bits but handle
19374              C++ anonymous unions and structs.  */
19375           if (DECL_NAME (member) != NULL_TREE
19376               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19377               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19378             {
19379               gen_type_die (member_declared_type (member), type_die);
19380               gen_field_die (member, type_die);
19381             }
19382         }
19383       else
19384         gen_variable_die (member, NULL_TREE, type_die);
19385
19386       pop_decl_scope ();
19387     }
19388 }
19389
19390 /* Generate the DWARF2 info for the "abstract" instance of a function which we
19391    may later generate inlined and/or out-of-line instances of.  */
19392
19393 static void
19394 dwarf2out_abstract_function (tree decl)
19395 {
19396   dw_die_ref old_die;
19397   tree save_fn;
19398   tree context;
19399   int was_abstract;
19400   htab_t old_decl_loc_table;
19401   htab_t old_cached_dw_loc_list_table;
19402   int old_call_site_count, old_tail_call_site_count;
19403   struct call_arg_loc_node *old_call_arg_locations;
19404
19405   /* Make sure we have the actual abstract inline, not a clone.  */
19406   decl = DECL_ORIGIN (decl);
19407
19408   old_die = lookup_decl_die (decl);
19409   if (old_die && get_AT (old_die, DW_AT_inline))
19410     /* We've already generated the abstract instance.  */
19411     return;
19412
19413   /* We can be called while recursively when seeing block defining inlined subroutine
19414      DIE.  Be sure to not clobber the outer location table nor use it or we would
19415      get locations in abstract instantces.  */
19416   old_decl_loc_table = decl_loc_table;
19417   decl_loc_table = NULL;
19418   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
19419   cached_dw_loc_list_table = NULL;
19420   old_call_arg_locations = call_arg_locations;
19421   call_arg_locations = NULL;
19422   old_call_site_count = call_site_count;
19423   call_site_count = -1;
19424   old_tail_call_site_count = tail_call_site_count;
19425   tail_call_site_count = -1;
19426
19427   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
19428      we don't get confused by DECL_ABSTRACT.  */
19429   if (debug_info_level > DINFO_LEVEL_TERSE)
19430     {
19431       context = decl_class_context (decl);
19432       if (context)
19433         gen_type_die_for_member
19434           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
19435     }
19436
19437   /* Pretend we've just finished compiling this function.  */
19438   save_fn = current_function_decl;
19439   current_function_decl = decl;
19440   push_cfun (DECL_STRUCT_FUNCTION (decl));
19441
19442   was_abstract = DECL_ABSTRACT (decl);
19443   set_decl_abstract_flags (decl, 1);
19444   dwarf2out_decl (decl);
19445   if (! was_abstract)
19446     set_decl_abstract_flags (decl, 0);
19447
19448   current_function_decl = save_fn;
19449   decl_loc_table = old_decl_loc_table;
19450   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
19451   call_arg_locations = old_call_arg_locations;
19452   call_site_count = old_call_site_count;
19453   tail_call_site_count = old_tail_call_site_count;
19454   pop_cfun ();
19455 }
19456
19457 /* Helper function of premark_used_types() which gets called through
19458    htab_traverse.
19459
19460    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19461    marked as unused by prune_unused_types.  */
19462
19463 static int
19464 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
19465 {
19466   tree type;
19467   dw_die_ref die;
19468
19469   type = (tree) *slot;
19470   die = lookup_type_die (type);
19471   if (die != NULL)
19472     die->die_perennial_p = 1;
19473   return 1;
19474 }
19475
19476 /* Helper function of premark_types_used_by_global_vars which gets called
19477    through htab_traverse.
19478
19479    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19480    marked as unused by prune_unused_types. The DIE of the type is marked
19481    only if the global variable using the type will actually be emitted.  */
19482
19483 static int
19484 premark_types_used_by_global_vars_helper (void **slot,
19485                                           void *data ATTRIBUTE_UNUSED)
19486 {
19487   struct types_used_by_vars_entry *entry;
19488   dw_die_ref die;
19489
19490   entry = (struct types_used_by_vars_entry *) *slot;
19491   gcc_assert (entry->type != NULL
19492               && entry->var_decl != NULL);
19493   die = lookup_type_die (entry->type);
19494   if (die)
19495     {
19496       /* Ask cgraph if the global variable really is to be emitted.
19497          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
19498       struct varpool_node *node = varpool_get_node (entry->var_decl);
19499       if (node && node->needed)
19500         {
19501           die->die_perennial_p = 1;
19502           /* Keep the parent DIEs as well.  */
19503           while ((die = die->die_parent) && die->die_perennial_p == 0)
19504             die->die_perennial_p = 1;
19505         }
19506     }
19507   return 1;
19508 }
19509
19510 /* Mark all members of used_types_hash as perennial.  */
19511
19512 static void
19513 premark_used_types (void)
19514 {
19515   if (cfun && cfun->used_types_hash)
19516     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
19517 }
19518
19519 /* Mark all members of types_used_by_vars_entry as perennial.  */
19520
19521 static void
19522 premark_types_used_by_global_vars (void)
19523 {
19524   if (types_used_by_vars_hash)
19525     htab_traverse (types_used_by_vars_hash,
19526                    premark_types_used_by_global_vars_helper, NULL);
19527 }
19528
19529 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
19530    for CA_LOC call arg loc node.  */
19531
19532 static dw_die_ref
19533 gen_call_site_die (tree decl, dw_die_ref subr_die,
19534                    struct call_arg_loc_node *ca_loc)
19535 {
19536   dw_die_ref stmt_die = NULL, die;
19537   tree block = ca_loc->block;
19538
19539   while (block
19540          && block != DECL_INITIAL (decl)
19541          && TREE_CODE (block) == BLOCK)
19542     {
19543       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
19544         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
19545       if (stmt_die)
19546         break;
19547       block = BLOCK_SUPERCONTEXT (block);
19548     }
19549   if (stmt_die == NULL)
19550     stmt_die = subr_die;
19551   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
19552   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
19553   if (ca_loc->tail_call_p)
19554     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
19555   if (ca_loc->symbol_ref)
19556     {
19557       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
19558       if (tdie)
19559         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
19560       else
19561         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
19562     }
19563   return die;
19564 }
19565
19566 /* Generate a DIE to represent a declared function (either file-scope or
19567    block-local).  */
19568
19569 static void
19570 gen_subprogram_die (tree decl, dw_die_ref context_die)
19571 {
19572   tree origin = decl_ultimate_origin (decl);
19573   dw_die_ref subr_die;
19574   tree outer_scope;
19575   dw_die_ref old_die = lookup_decl_die (decl);
19576   int declaration = (current_function_decl != decl
19577                      || class_or_namespace_scope_p (context_die));
19578
19579   premark_used_types ();
19580
19581   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19582      started to generate the abstract instance of an inline, decided to output
19583      its containing class, and proceeded to emit the declaration of the inline
19584      from the member list for the class.  If so, DECLARATION takes priority;
19585      we'll get back to the abstract instance when done with the class.  */
19586
19587   /* The class-scope declaration DIE must be the primary DIE.  */
19588   if (origin && declaration && class_or_namespace_scope_p (context_die))
19589     {
19590       origin = NULL;
19591       gcc_assert (!old_die);
19592     }
19593
19594   /* Now that the C++ front end lazily declares artificial member fns, we
19595      might need to retrofit the declaration into its class.  */
19596   if (!declaration && !origin && !old_die
19597       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19598       && !class_or_namespace_scope_p (context_die)
19599       && debug_info_level > DINFO_LEVEL_TERSE)
19600     old_die = force_decl_die (decl);
19601
19602   if (origin != NULL)
19603     {
19604       gcc_assert (!declaration || local_scope_p (context_die));
19605
19606       /* Fixup die_parent for the abstract instance of a nested
19607          inline function.  */
19608       if (old_die && old_die->die_parent == NULL)
19609         add_child_die (context_die, old_die);
19610
19611       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19612       add_abstract_origin_attribute (subr_die, origin);
19613     }
19614   else if (old_die)
19615     {
19616       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19617       struct dwarf_file_data * file_index = lookup_filename (s.file);
19618
19619       if (!get_AT_flag (old_die, DW_AT_declaration)
19620           /* We can have a normal definition following an inline one in the
19621              case of redefinition of GNU C extern inlines.
19622              It seems reasonable to use AT_specification in this case.  */
19623           && !get_AT (old_die, DW_AT_inline))
19624         {
19625           /* Detect and ignore this case, where we are trying to output
19626              something we have already output.  */
19627           return;
19628         }
19629
19630       /* If the definition comes from the same place as the declaration,
19631          maybe use the old DIE.  We always want the DIE for this function
19632          that has the *_pc attributes to be under comp_unit_die so the
19633          debugger can find it.  We also need to do this for abstract
19634          instances of inlines, since the spec requires the out-of-line copy
19635          to have the same parent.  For local class methods, this doesn't
19636          apply; we just use the old DIE.  */
19637       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19638           && (DECL_ARTIFICIAL (decl)
19639               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19640                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19641                       == (unsigned) s.line))))
19642         {
19643           subr_die = old_die;
19644
19645           /* Clear out the declaration attribute and the formal parameters.
19646              Do not remove all children, because it is possible that this
19647              declaration die was forced using force_decl_die(). In such
19648              cases die that forced declaration die (e.g. TAG_imported_module)
19649              is one of the children that we do not want to remove.  */
19650           remove_AT (subr_die, DW_AT_declaration);
19651           remove_AT (subr_die, DW_AT_object_pointer);
19652           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19653         }
19654       else
19655         {
19656           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19657           add_AT_specification (subr_die, old_die);
19658           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19659             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19660           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19661             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19662         }
19663     }
19664   else
19665     {
19666       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19667
19668       if (TREE_PUBLIC (decl))
19669         add_AT_flag (subr_die, DW_AT_external, 1);
19670
19671       add_name_and_src_coords_attributes (subr_die, decl);
19672       if (debug_info_level > DINFO_LEVEL_TERSE)
19673         {
19674           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19675           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19676                               0, 0, context_die);
19677         }
19678
19679       add_pure_or_virtual_attribute (subr_die, decl);
19680       if (DECL_ARTIFICIAL (decl))
19681         add_AT_flag (subr_die, DW_AT_artificial, 1);
19682
19683       add_accessibility_attribute (subr_die, decl);
19684     }
19685
19686   if (declaration)
19687     {
19688       if (!old_die || !get_AT (old_die, DW_AT_inline))
19689         {
19690           add_AT_flag (subr_die, DW_AT_declaration, 1);
19691
19692           /* If this is an explicit function declaration then generate
19693              a DW_AT_explicit attribute.  */
19694           if (lang_hooks.decls.function_decl_explicit_p (decl)
19695               && (dwarf_version >= 3 || !dwarf_strict))
19696             add_AT_flag (subr_die, DW_AT_explicit, 1);
19697
19698           /* The first time we see a member function, it is in the context of
19699              the class to which it belongs.  We make sure of this by emitting
19700              the class first.  The next time is the definition, which is
19701              handled above.  The two may come from the same source text.
19702
19703              Note that force_decl_die() forces function declaration die. It is
19704              later reused to represent definition.  */
19705           equate_decl_number_to_die (decl, subr_die);
19706         }
19707     }
19708   else if (DECL_ABSTRACT (decl))
19709     {
19710       if (DECL_DECLARED_INLINE_P (decl))
19711         {
19712           if (cgraph_function_possibly_inlined_p (decl))
19713             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19714           else
19715             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19716         }
19717       else
19718         {
19719           if (cgraph_function_possibly_inlined_p (decl))
19720             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19721           else
19722             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19723         }
19724
19725       if (DECL_DECLARED_INLINE_P (decl)
19726           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19727         add_AT_flag (subr_die, DW_AT_artificial, 1);
19728
19729       equate_decl_number_to_die (decl, subr_die);
19730     }
19731   else if (!DECL_EXTERNAL (decl))
19732     {
19733       HOST_WIDE_INT cfa_fb_offset;
19734
19735       if (!old_die || !get_AT (old_die, DW_AT_inline))
19736         equate_decl_number_to_die (decl, subr_die);
19737
19738       if (!flag_reorder_blocks_and_partition)
19739         {
19740           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19741           if (fde->dw_fde_begin)
19742             {
19743               /* We have already generated the labels.  */
19744               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19745               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19746             }
19747           else
19748             {
19749               /* Create start/end labels and add the range.  */
19750               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19751               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19752                                            current_function_funcdef_no);
19753               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19754               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19755                                            current_function_funcdef_no);
19756               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19757             }
19758
19759 #if VMS_DEBUGGING_INFO
19760       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19761          Section 2.3 Prologue and Epilogue Attributes:
19762          When a breakpoint is set on entry to a function, it is generally
19763          desirable for execution to be suspended, not on the very first
19764          instruction of the function, but rather at a point after the
19765          function's frame has been set up, after any language defined local
19766          declaration processing has been completed, and before execution of
19767          the first statement of the function begins. Debuggers generally
19768          cannot properly determine where this point is.  Similarly for a
19769          breakpoint set on exit from a function. The prologue and epilogue
19770          attributes allow a compiler to communicate the location(s) to use.  */
19771
19772       {
19773         if (fde->dw_fde_vms_end_prologue)
19774           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19775             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19776
19777         if (fde->dw_fde_vms_begin_epilogue)
19778           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19779             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19780       }
19781 #endif
19782
19783           add_pubname (decl, subr_die);
19784         }
19785       else
19786         {  /* Generate pubnames entries for the split function code
19787               ranges.  */
19788           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19789
19790           if (fde->dw_fde_second_begin)
19791             {
19792               if (dwarf_version >= 3 || !dwarf_strict)
19793                 {
19794                   /* We should use ranges for non-contiguous code section 
19795                      addresses.  Use the actual code range for the initial
19796                      section, since the HOT/COLD labels might precede an 
19797                      alignment offset.  */
19798                   bool range_list_added = false;
19799                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19800                                         fde->dw_fde_end, &range_list_added);
19801                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19802                                         fde->dw_fde_second_end,
19803                                         &range_list_added);
19804                   add_pubname (decl, subr_die);
19805                   if (range_list_added)
19806                     add_ranges (NULL);
19807                 }
19808               else
19809                 {
19810                   /* There is no real support in DW2 for this .. so we make
19811                      a work-around.  First, emit the pub name for the segment
19812                      containing the function label.  Then make and emit a
19813                      simplified subprogram DIE for the second segment with the
19814                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19815                      linkage name for the second die so that gdb will find both
19816                      sections when given "b foo".  */
19817                   const char *name = NULL;
19818                   tree decl_name = DECL_NAME (decl);
19819                   dw_die_ref seg_die;
19820
19821                   /* Do the 'primary' section.   */
19822                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19823                                  fde->dw_fde_begin);
19824                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19825                                  fde->dw_fde_end);
19826                   /* Add it.   */
19827                   add_pubname (decl, subr_die);
19828
19829                   /* Build a minimal DIE for the secondary section.  */
19830                   seg_die = new_die (DW_TAG_subprogram,
19831                                      subr_die->die_parent, decl);
19832
19833                   if (TREE_PUBLIC (decl))
19834                     add_AT_flag (seg_die, DW_AT_external, 1);
19835
19836                   if (decl_name != NULL 
19837                       && IDENTIFIER_POINTER (decl_name) != NULL)
19838                     {
19839                       name = dwarf2_name (decl, 1);
19840                       if (! DECL_ARTIFICIAL (decl))
19841                         add_src_coords_attributes (seg_die, decl);
19842
19843                       add_linkage_name (seg_die, decl);
19844                     }
19845                   gcc_assert (name != NULL);
19846                   add_pure_or_virtual_attribute (seg_die, decl);
19847                   if (DECL_ARTIFICIAL (decl))
19848                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19849
19850                   name = concat ("__second_sect_of_", name, NULL); 
19851                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
19852                                  fde->dw_fde_second_begin);
19853                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
19854                                  fde->dw_fde_second_end);
19855                   add_name_attribute (seg_die, name);
19856                   add_pubname_string (name, seg_die);
19857                 }
19858             }
19859           else
19860             {
19861               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19862               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19863               add_pubname (decl, subr_die);
19864             }
19865         }
19866
19867 #ifdef MIPS_DEBUGGING_INFO
19868       /* Add a reference to the FDE for this routine.  */
19869       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19870 #endif
19871
19872       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19873
19874       /* We define the "frame base" as the function's CFA.  This is more
19875          convenient for several reasons: (1) It's stable across the prologue
19876          and epilogue, which makes it better than just a frame pointer,
19877          (2) With dwarf3, there exists a one-byte encoding that allows us
19878          to reference the .debug_frame data by proxy, but failing that,
19879          (3) We can at least reuse the code inspection and interpretation
19880          code that determines the CFA position at various points in the
19881          function.  */
19882       if (dwarf_version >= 3)
19883         {
19884           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19885           add_AT_loc (subr_die, DW_AT_frame_base, op);
19886         }
19887       else
19888         {
19889           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19890           if (list->dw_loc_next)
19891             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19892           else
19893             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19894         }
19895
19896       /* Compute a displacement from the "steady-state frame pointer" to
19897          the CFA.  The former is what all stack slots and argument slots
19898          will reference in the rtl; the later is what we've told the
19899          debugger about.  We'll need to adjust all frame_base references
19900          by this displacement.  */
19901       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19902
19903       if (cfun->static_chain_decl)
19904         add_AT_location_description (subr_die, DW_AT_static_link,
19905                  loc_list_from_tree (cfun->static_chain_decl, 2));
19906     }
19907
19908   /* Generate child dies for template paramaters.  */
19909   if (debug_info_level > DINFO_LEVEL_TERSE)
19910     gen_generic_params_dies (decl);
19911
19912   /* Now output descriptions of the arguments for this function. This gets
19913      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19914      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19915      `...' at the end of the formal parameter list.  In order to find out if
19916      there was a trailing ellipsis or not, we must instead look at the type
19917      associated with the FUNCTION_DECL.  This will be a node of type
19918      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19919      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19920      an ellipsis at the end.  */
19921
19922   /* In the case where we are describing a mere function declaration, all we
19923      need to do here (and all we *can* do here) is to describe the *types* of
19924      its formal parameters.  */
19925   if (debug_info_level <= DINFO_LEVEL_TERSE)
19926     ;
19927   else if (declaration)
19928     gen_formal_types_die (decl, subr_die);
19929   else
19930     {
19931       /* Generate DIEs to represent all known formal parameters.  */
19932       tree parm = DECL_ARGUMENTS (decl);
19933       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19934       tree generic_decl_parm = generic_decl
19935                                 ? DECL_ARGUMENTS (generic_decl)
19936                                 : NULL;
19937
19938       /* Now we want to walk the list of parameters of the function and
19939          emit their relevant DIEs.
19940
19941          We consider the case of DECL being an instance of a generic function
19942          as well as it being a normal function.
19943
19944          If DECL is an instance of a generic function we walk the
19945          parameters of the generic function declaration _and_ the parameters of
19946          DECL itself. This is useful because we want to emit specific DIEs for
19947          function parameter packs and those are declared as part of the
19948          generic function declaration. In that particular case,
19949          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19950          That DIE has children DIEs representing the set of arguments
19951          of the pack. Note that the set of pack arguments can be empty.
19952          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19953          children DIE.
19954
19955          Otherwise, we just consider the parameters of DECL.  */
19956       while (generic_decl_parm || parm)
19957         {
19958           if (generic_decl_parm
19959               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19960             gen_formal_parameter_pack_die (generic_decl_parm,
19961                                            parm, subr_die,
19962                                            &parm);
19963           else if (parm)
19964             {
19965               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19966
19967               if (parm == DECL_ARGUMENTS (decl)
19968                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19969                   && parm_die
19970                   && (dwarf_version >= 3 || !dwarf_strict))
19971                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19972
19973               parm = DECL_CHAIN (parm);
19974             }
19975
19976           if (generic_decl_parm)
19977             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19978         }
19979
19980       /* Decide whether we need an unspecified_parameters DIE at the end.
19981          There are 2 more cases to do this for: 1) the ansi ... declaration -
19982          this is detectable when the end of the arg list is not a
19983          void_type_node 2) an unprototyped function declaration (not a
19984          definition).  This just means that we have no info about the
19985          parameters at all.  */
19986       if (prototype_p (TREE_TYPE (decl)))
19987         {
19988           /* This is the prototyped case, check for....  */
19989           if (stdarg_p (TREE_TYPE (decl)))
19990             gen_unspecified_parameters_die (decl, subr_die);
19991         }
19992       else if (DECL_INITIAL (decl) == NULL_TREE)
19993         gen_unspecified_parameters_die (decl, subr_die);
19994     }
19995
19996   /* Output Dwarf info for all of the stuff within the body of the function
19997      (if it has one - it may be just a declaration).  */
19998   outer_scope = DECL_INITIAL (decl);
19999
20000   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20001      a function.  This BLOCK actually represents the outermost binding contour
20002      for the function, i.e. the contour in which the function's formal
20003      parameters and labels get declared. Curiously, it appears that the front
20004      end doesn't actually put the PARM_DECL nodes for the current function onto
20005      the BLOCK_VARS list for this outer scope, but are strung off of the
20006      DECL_ARGUMENTS list for the function instead.
20007
20008      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20009      the LABEL_DECL nodes for the function however, and we output DWARF info
20010      for those in decls_for_scope.  Just within the `outer_scope' there will be
20011      a BLOCK node representing the function's outermost pair of curly braces,
20012      and any blocks used for the base and member initializers of a C++
20013      constructor function.  */
20014   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
20015     {
20016       int call_site_note_count = 0;
20017       int tail_call_site_note_count = 0;
20018
20019       /* Emit a DW_TAG_variable DIE for a named return value.  */
20020       if (DECL_NAME (DECL_RESULT (decl)))
20021         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
20022
20023       current_function_has_inlines = 0;
20024       decls_for_scope (outer_scope, subr_die, 0);
20025
20026       if (call_arg_locations && !dwarf_strict)
20027         {
20028           struct call_arg_loc_node *ca_loc;
20029           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20030             {
20031               dw_die_ref die = NULL;
20032               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20033               rtx arg, next_arg;
20034
20035               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
20036                    arg; arg = next_arg)
20037                 {
20038                   dw_loc_descr_ref reg, val;
20039                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20040                   dw_die_ref cdie;
20041
20042                   next_arg = XEXP (arg, 1);
20043                   if (REG_P (XEXP (XEXP (arg, 0), 0))
20044                       && next_arg
20045                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20046                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20047                       && REGNO (XEXP (XEXP (arg, 0), 0))
20048                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20049                     next_arg = XEXP (next_arg, 1);
20050                   if (mode == VOIDmode)
20051                     {
20052                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20053                       if (mode == VOIDmode)
20054                         mode = GET_MODE (XEXP (arg, 0));
20055                     }
20056                   if (mode == VOIDmode || mode == BLKmode)
20057                     continue;
20058                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20059                     {
20060                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20061                       tloc = XEXP (XEXP (arg, 0), 1);
20062                       continue;
20063                     }
20064                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20065                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20066                     {
20067                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20068                       tlocc = XEXP (XEXP (arg, 0), 1);
20069                       continue;
20070                     }
20071                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
20072                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20073                                               VAR_INIT_STATUS_INITIALIZED);
20074                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20075                     {
20076                       rtx mem = XEXP (XEXP (arg, 0), 0);
20077                       reg = mem_loc_descriptor (XEXP (mem, 0),
20078                                                 get_address_mode (mem),
20079                                                 GET_MODE (mem),
20080                                                 VAR_INIT_STATUS_INITIALIZED);
20081                     }
20082                   else
20083                     continue;
20084                   if (reg == NULL)
20085                     continue;
20086                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20087                                             VOIDmode,
20088                                             VAR_INIT_STATUS_INITIALIZED);
20089                   if (val == NULL)
20090                     continue;
20091                   if (die == NULL)
20092                     die = gen_call_site_die (decl, subr_die, ca_loc);
20093                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20094                                   NULL_TREE);           
20095                   add_AT_loc (cdie, DW_AT_location, reg);
20096                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20097                   if (next_arg != XEXP (arg, 1))
20098                     {
20099                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20100                       if (mode == VOIDmode)
20101                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20102                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20103                                                             0), 1),
20104                                                 mode, VOIDmode,
20105                                                 VAR_INIT_STATUS_INITIALIZED);
20106                       if (val != NULL)
20107                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20108                     }
20109                 }
20110               if (die == NULL
20111                   && (ca_loc->symbol_ref || tloc))
20112                 die = gen_call_site_die (decl, subr_die, ca_loc);
20113               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20114                 {
20115                   dw_loc_descr_ref tval = NULL;
20116
20117                   if (tloc != NULL_RTX)
20118                     tval = mem_loc_descriptor (tloc,
20119                                                GET_MODE (tloc) == VOIDmode
20120                                                ? Pmode : GET_MODE (tloc),
20121                                                VOIDmode,
20122                                                VAR_INIT_STATUS_INITIALIZED);
20123                   if (tval)
20124                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
20125                   else if (tlocc != NULL_RTX)
20126                     {
20127                       tval = mem_loc_descriptor (tlocc,
20128                                                  GET_MODE (tlocc) == VOIDmode
20129                                                  ? Pmode : GET_MODE (tlocc),
20130                                                  VOIDmode,
20131                                                  VAR_INIT_STATUS_INITIALIZED);
20132                       if (tval)
20133                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
20134                                     tval);
20135                     }
20136                 }
20137               if (die != NULL)
20138                 {
20139                   call_site_note_count++;
20140                   if (ca_loc->tail_call_p)
20141                     tail_call_site_note_count++;
20142                 }
20143             }
20144         }
20145       call_arg_locations = NULL;
20146       call_arg_loc_last = NULL;
20147       if (tail_call_site_count >= 0
20148           && tail_call_site_count == tail_call_site_note_count
20149           && !dwarf_strict)
20150         {
20151           if (call_site_count >= 0
20152               && call_site_count == call_site_note_count)
20153             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
20154           else
20155             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
20156         }
20157       call_site_count = -1;
20158       tail_call_site_count = -1;
20159     }
20160   /* Add the calling convention attribute if requested.  */
20161   add_calling_convention_attribute (subr_die, decl);
20162
20163 }
20164
20165 /* Returns a hash value for X (which really is a die_struct).  */
20166
20167 static hashval_t
20168 common_block_die_table_hash (const void *x)
20169 {
20170   const_dw_die_ref d = (const_dw_die_ref) x;
20171   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
20172 }
20173
20174 /* Return nonzero if decl_id and die_parent of die_struct X is the same
20175    as decl_id and die_parent of die_struct Y.  */
20176
20177 static int
20178 common_block_die_table_eq (const void *x, const void *y)
20179 {
20180   const_dw_die_ref d = (const_dw_die_ref) x;
20181   const_dw_die_ref e = (const_dw_die_ref) y;
20182   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
20183 }
20184
20185 /* Generate a DIE to represent a declared data object.
20186    Either DECL or ORIGIN must be non-null.  */
20187
20188 static void
20189 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
20190 {
20191   HOST_WIDE_INT off;
20192   tree com_decl;
20193   tree decl_or_origin = decl ? decl : origin;
20194   tree ultimate_origin;
20195   dw_die_ref var_die;
20196   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
20197   dw_die_ref origin_die;
20198   bool declaration = (DECL_EXTERNAL (decl_or_origin)
20199                       || class_or_namespace_scope_p (context_die));
20200   bool specialization_p = false;
20201
20202   ultimate_origin = decl_ultimate_origin (decl_or_origin);
20203   if (decl || ultimate_origin)
20204     origin = ultimate_origin;
20205   com_decl = fortran_common (decl_or_origin, &off);
20206
20207   /* Symbol in common gets emitted as a child of the common block, in the form
20208      of a data member.  */
20209   if (com_decl)
20210     {
20211       dw_die_ref com_die;
20212       dw_loc_list_ref loc;
20213       die_node com_die_arg;
20214
20215       var_die = lookup_decl_die (decl_or_origin);
20216       if (var_die)
20217         {
20218           if (get_AT (var_die, DW_AT_location) == NULL)
20219             {
20220               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
20221               if (loc)
20222                 {
20223                   if (off)
20224                     {
20225                       /* Optimize the common case.  */
20226                       if (single_element_loc_list_p (loc)
20227                           && loc->expr->dw_loc_opc == DW_OP_addr
20228                           && loc->expr->dw_loc_next == NULL
20229                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
20230                              == SYMBOL_REF)
20231                         loc->expr->dw_loc_oprnd1.v.val_addr
20232                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20233                         else
20234                           loc_list_plus_const (loc, off);
20235                     }
20236                   add_AT_location_description (var_die, DW_AT_location, loc);
20237                   remove_AT (var_die, DW_AT_declaration);
20238                 }
20239             }
20240           return;
20241         }
20242
20243       if (common_block_die_table == NULL)
20244         common_block_die_table
20245           = htab_create_ggc (10, common_block_die_table_hash,
20246                              common_block_die_table_eq, NULL);
20247
20248       com_die_arg.decl_id = DECL_UID (com_decl);
20249       com_die_arg.die_parent = context_die;
20250       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
20251       loc = loc_list_from_tree (com_decl, 2);
20252       if (com_die == NULL)
20253         {
20254           const char *cnam
20255             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
20256           void **slot;
20257
20258           com_die = new_die (DW_TAG_common_block, context_die, decl);
20259           add_name_and_src_coords_attributes (com_die, com_decl);
20260           if (loc)
20261             {
20262               add_AT_location_description (com_die, DW_AT_location, loc);
20263               /* Avoid sharing the same loc descriptor between
20264                  DW_TAG_common_block and DW_TAG_variable.  */
20265               loc = loc_list_from_tree (com_decl, 2);
20266             }
20267           else if (DECL_EXTERNAL (decl))
20268             add_AT_flag (com_die, DW_AT_declaration, 1);
20269           add_pubname_string (cnam, com_die); /* ??? needed? */
20270           com_die->decl_id = DECL_UID (com_decl);
20271           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
20272           *slot = (void *) com_die;
20273         }
20274       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
20275         {
20276           add_AT_location_description (com_die, DW_AT_location, loc);
20277           loc = loc_list_from_tree (com_decl, 2);
20278           remove_AT (com_die, DW_AT_declaration);
20279         }
20280       var_die = new_die (DW_TAG_variable, com_die, decl);
20281       add_name_and_src_coords_attributes (var_die, decl);
20282       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
20283                           TREE_THIS_VOLATILE (decl), context_die);
20284       add_AT_flag (var_die, DW_AT_external, 1);
20285       if (loc)
20286         {
20287           if (off)
20288             {
20289               /* Optimize the common case.  */
20290               if (single_element_loc_list_p (loc)
20291                   && loc->expr->dw_loc_opc == DW_OP_addr
20292                   && loc->expr->dw_loc_next == NULL
20293                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
20294                 loc->expr->dw_loc_oprnd1.v.val_addr
20295                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20296               else
20297                 loc_list_plus_const (loc, off);
20298             }
20299           add_AT_location_description (var_die, DW_AT_location, loc);
20300         }
20301       else if (DECL_EXTERNAL (decl))
20302         add_AT_flag (var_die, DW_AT_declaration, 1);
20303       equate_decl_number_to_die (decl, var_die);
20304       return;
20305     }
20306
20307   /* If the compiler emitted a definition for the DECL declaration
20308      and if we already emitted a DIE for it, don't emit a second
20309      DIE for it again. Allow re-declarations of DECLs that are
20310      inside functions, though.  */
20311   if (old_die && declaration && !local_scope_p (context_die))
20312     return;
20313
20314   /* For static data members, the declaration in the class is supposed
20315      to have DW_TAG_member tag; the specification should still be
20316      DW_TAG_variable referencing the DW_TAG_member DIE.  */
20317   if (declaration && class_scope_p (context_die))
20318     var_die = new_die (DW_TAG_member, context_die, decl);
20319   else
20320     var_die = new_die (DW_TAG_variable, context_die, decl);
20321
20322   origin_die = NULL;
20323   if (origin != NULL)
20324     origin_die = add_abstract_origin_attribute (var_die, origin);
20325
20326   /* Loop unrolling can create multiple blocks that refer to the same
20327      static variable, so we must test for the DW_AT_declaration flag.
20328
20329      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
20330      copy decls and set the DECL_ABSTRACT flag on them instead of
20331      sharing them.
20332
20333      ??? Duplicated blocks have been rewritten to use .debug_ranges.
20334
20335      ??? The declare_in_namespace support causes us to get two DIEs for one
20336      variable, both of which are declarations.  We want to avoid considering
20337      one to be a specification, so we must test that this DIE is not a
20338      declaration.  */
20339   else if (old_die && TREE_STATIC (decl) && ! declaration
20340            && get_AT_flag (old_die, DW_AT_declaration) == 1)
20341     {
20342       /* This is a definition of a C++ class level static.  */
20343       add_AT_specification (var_die, old_die);
20344       specialization_p = true;
20345       if (DECL_NAME (decl))
20346         {
20347           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20348           struct dwarf_file_data * file_index = lookup_filename (s.file);
20349
20350           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20351             add_AT_file (var_die, DW_AT_decl_file, file_index);
20352
20353           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20354             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
20355
20356           if (old_die->die_tag == DW_TAG_member)
20357             add_linkage_name (var_die, decl);
20358         }
20359     }
20360   else
20361     add_name_and_src_coords_attributes (var_die, decl);
20362
20363   if ((origin == NULL && !specialization_p)
20364       || (origin != NULL
20365           && !DECL_ABSTRACT (decl_or_origin)
20366           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
20367                                        decl_function_context
20368                                                         (decl_or_origin))))
20369     {
20370       tree type = TREE_TYPE (decl_or_origin);
20371
20372       if (decl_by_reference_p (decl_or_origin))
20373         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
20374       else
20375         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
20376                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
20377     }
20378
20379   if (origin == NULL && !specialization_p)
20380     {
20381       if (TREE_PUBLIC (decl))
20382         add_AT_flag (var_die, DW_AT_external, 1);
20383
20384       if (DECL_ARTIFICIAL (decl))
20385         add_AT_flag (var_die, DW_AT_artificial, 1);
20386
20387       add_accessibility_attribute (var_die, decl);
20388     }
20389
20390   if (declaration)
20391     add_AT_flag (var_die, DW_AT_declaration, 1);
20392
20393   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
20394     equate_decl_number_to_die (decl, var_die);
20395
20396   if (! declaration
20397       && (! DECL_ABSTRACT (decl_or_origin)
20398           /* Local static vars are shared between all clones/inlines,
20399              so emit DW_AT_location on the abstract DIE if DECL_RTL is
20400              already set.  */
20401           || (TREE_CODE (decl_or_origin) == VAR_DECL
20402               && TREE_STATIC (decl_or_origin)
20403               && DECL_RTL_SET_P (decl_or_origin)))
20404       /* When abstract origin already has DW_AT_location attribute, no need
20405          to add it again.  */
20406       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
20407     {
20408       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
20409           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
20410         defer_location (decl_or_origin, var_die);
20411       else
20412         add_location_or_const_value_attribute (var_die, decl_or_origin,
20413                                                decl == NULL, DW_AT_location);
20414       add_pubname (decl_or_origin, var_die);
20415     }
20416   else
20417     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
20418 }
20419
20420 /* Generate a DIE to represent a named constant.  */
20421
20422 static void
20423 gen_const_die (tree decl, dw_die_ref context_die)
20424 {
20425   dw_die_ref const_die;
20426   tree type = TREE_TYPE (decl);
20427
20428   const_die = new_die (DW_TAG_constant, context_die, decl);
20429   add_name_and_src_coords_attributes (const_die, decl);
20430   add_type_attribute (const_die, type, 1, 0, context_die);
20431   if (TREE_PUBLIC (decl))
20432     add_AT_flag (const_die, DW_AT_external, 1);
20433   if (DECL_ARTIFICIAL (decl))
20434     add_AT_flag (const_die, DW_AT_artificial, 1);
20435   tree_add_const_value_attribute_for_decl (const_die, decl);
20436 }
20437
20438 /* Generate a DIE to represent a label identifier.  */
20439
20440 static void
20441 gen_label_die (tree decl, dw_die_ref context_die)
20442 {
20443   tree origin = decl_ultimate_origin (decl);
20444   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
20445   rtx insn;
20446   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20447
20448   if (origin != NULL)
20449     add_abstract_origin_attribute (lbl_die, origin);
20450   else
20451     add_name_and_src_coords_attributes (lbl_die, decl);
20452
20453   if (DECL_ABSTRACT (decl))
20454     equate_decl_number_to_die (decl, lbl_die);
20455   else
20456     {
20457       insn = DECL_RTL_IF_SET (decl);
20458
20459       /* Deleted labels are programmer specified labels which have been
20460          eliminated because of various optimizations.  We still emit them
20461          here so that it is possible to put breakpoints on them.  */
20462       if (insn
20463           && (LABEL_P (insn)
20464               || ((NOTE_P (insn)
20465                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
20466         {
20467           /* When optimization is enabled (via -O) some parts of the compiler
20468              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
20469              represent source-level labels which were explicitly declared by
20470              the user.  This really shouldn't be happening though, so catch
20471              it if it ever does happen.  */
20472           gcc_assert (!INSN_DELETED_P (insn));
20473
20474           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
20475           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
20476         }
20477     }
20478 }
20479
20480 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
20481    attributes to the DIE for a block STMT, to describe where the inlined
20482    function was called from.  This is similar to add_src_coords_attributes.  */
20483
20484 static inline void
20485 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
20486 {
20487   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
20488
20489   if (dwarf_version >= 3 || !dwarf_strict)
20490     {
20491       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
20492       add_AT_unsigned (die, DW_AT_call_line, s.line);
20493     }
20494 }
20495
20496
20497 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
20498    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
20499
20500 static inline void
20501 add_high_low_attributes (tree stmt, dw_die_ref die)
20502 {
20503   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20504
20505   if (BLOCK_FRAGMENT_CHAIN (stmt)
20506       && (dwarf_version >= 3 || !dwarf_strict))
20507     {
20508       tree chain;
20509
20510       if (inlined_function_outer_scope_p (stmt))
20511         {
20512           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20513                                        BLOCK_NUMBER (stmt));
20514           add_AT_lbl_id (die, DW_AT_entry_pc, label);
20515         }
20516
20517       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
20518
20519       chain = BLOCK_FRAGMENT_CHAIN (stmt);
20520       do
20521         {
20522           add_ranges (chain);
20523           chain = BLOCK_FRAGMENT_CHAIN (chain);
20524         }
20525       while (chain);
20526       add_ranges (NULL);
20527     }
20528   else
20529     {
20530       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20531                                    BLOCK_NUMBER (stmt));
20532       add_AT_lbl_id (die, DW_AT_low_pc, label);
20533       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
20534                                    BLOCK_NUMBER (stmt));
20535       add_AT_lbl_id (die, DW_AT_high_pc, label);
20536     }
20537 }
20538
20539 /* Generate a DIE for a lexical block.  */
20540
20541 static void
20542 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
20543 {
20544   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
20545
20546   if (call_arg_locations)
20547     {
20548       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20549         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20550                                BLOCK_NUMBER (stmt) + 1);
20551       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
20552     }
20553
20554   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
20555     add_high_low_attributes (stmt, stmt_die);
20556
20557   decls_for_scope (stmt, stmt_die, depth);
20558 }
20559
20560 /* Generate a DIE for an inlined subprogram.  */
20561
20562 static void
20563 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
20564 {
20565   tree decl;
20566
20567   /* The instance of function that is effectively being inlined shall not
20568      be abstract.  */
20569   gcc_assert (! BLOCK_ABSTRACT (stmt));
20570
20571   decl = block_ultimate_origin (stmt);
20572
20573   /* Emit info for the abstract instance first, if we haven't yet.  We
20574      must emit this even if the block is abstract, otherwise when we
20575      emit the block below (or elsewhere), we may end up trying to emit
20576      a die whose origin die hasn't been emitted, and crashing.  */
20577   dwarf2out_abstract_function (decl);
20578
20579   if (! BLOCK_ABSTRACT (stmt))
20580     {
20581       dw_die_ref subr_die
20582         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20583
20584       if (call_arg_locations)
20585         {
20586           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20587             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20588                                    BLOCK_NUMBER (stmt) + 1);
20589           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20590         }
20591       add_abstract_origin_attribute (subr_die, decl);
20592       if (TREE_ASM_WRITTEN (stmt))
20593         add_high_low_attributes (stmt, subr_die);
20594       add_call_src_coords_attributes (stmt, subr_die);
20595
20596       decls_for_scope (stmt, subr_die, depth);
20597       current_function_has_inlines = 1;
20598     }
20599 }
20600
20601 /* Generate a DIE for a field in a record, or structure.  */
20602
20603 static void
20604 gen_field_die (tree decl, dw_die_ref context_die)
20605 {
20606   dw_die_ref decl_die;
20607
20608   if (TREE_TYPE (decl) == error_mark_node)
20609     return;
20610
20611   decl_die = new_die (DW_TAG_member, context_die, decl);
20612   add_name_and_src_coords_attributes (decl_die, decl);
20613   add_type_attribute (decl_die, member_declared_type (decl),
20614                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20615                       context_die);
20616
20617   if (DECL_BIT_FIELD_TYPE (decl))
20618     {
20619       add_byte_size_attribute (decl_die, decl);
20620       add_bit_size_attribute (decl_die, decl);
20621       add_bit_offset_attribute (decl_die, decl);
20622     }
20623
20624   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20625     add_data_member_location_attribute (decl_die, decl);
20626
20627   if (DECL_ARTIFICIAL (decl))
20628     add_AT_flag (decl_die, DW_AT_artificial, 1);
20629
20630   add_accessibility_attribute (decl_die, decl);
20631
20632   /* Equate decl number to die, so that we can look up this decl later on.  */
20633   equate_decl_number_to_die (decl, decl_die);
20634 }
20635
20636 #if 0
20637 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20638    Use modified_type_die instead.
20639    We keep this code here just in case these types of DIEs may be needed to
20640    represent certain things in other languages (e.g. Pascal) someday.  */
20641
20642 static void
20643 gen_pointer_type_die (tree type, dw_die_ref context_die)
20644 {
20645   dw_die_ref ptr_die
20646     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20647
20648   equate_type_number_to_die (type, ptr_die);
20649   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20650   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20651 }
20652
20653 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20654    Use modified_type_die instead.
20655    We keep this code here just in case these types of DIEs may be needed to
20656    represent certain things in other languages (e.g. Pascal) someday.  */
20657
20658 static void
20659 gen_reference_type_die (tree type, dw_die_ref context_die)
20660 {
20661   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20662
20663   if (TYPE_REF_IS_RVALUE (type) && use_debug_types)
20664     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20665   else
20666     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20667
20668   equate_type_number_to_die (type, ref_die);
20669   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20670   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20671 }
20672 #endif
20673
20674 /* Generate a DIE for a pointer to a member type.  */
20675
20676 static void
20677 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20678 {
20679   dw_die_ref ptr_die
20680     = new_die (DW_TAG_ptr_to_member_type,
20681                scope_die_for (type, context_die), type);
20682
20683   equate_type_number_to_die (type, ptr_die);
20684   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20685                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20686   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20687 }
20688
20689 /* Generate the DIE for the compilation unit.  */
20690
20691 static dw_die_ref
20692 gen_compile_unit_die (const char *filename)
20693 {
20694   dw_die_ref die;
20695   char producer[250];
20696   const char *language_string = lang_hooks.name;
20697   int language;
20698
20699   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20700
20701   if (filename)
20702     {
20703       add_name_attribute (die, filename);
20704       /* Don't add cwd for <built-in>.  */
20705       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20706         add_comp_dir_attribute (die);
20707     }
20708
20709   sprintf (producer, "%s %s", language_string, version_string);
20710
20711 #ifdef MIPS_DEBUGGING_INFO
20712   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20713      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20714      not appear in the producer string, the debugger reaches the conclusion
20715      that the object file is stripped and has no debugging information.
20716      To get the MIPS/SGI debugger to believe that there is debugging
20717      information in the object file, we add a -g to the producer string.  */
20718   if (debug_info_level > DINFO_LEVEL_TERSE)
20719     strcat (producer, " -g");
20720 #endif
20721
20722   add_AT_string (die, DW_AT_producer, producer);
20723
20724   /* If our producer is LTO try to figure out a common language to use
20725      from the global list of translation units.  */
20726   if (strcmp (language_string, "GNU GIMPLE") == 0)
20727     {
20728       unsigned i;
20729       tree t;
20730       const char *common_lang = NULL;
20731
20732       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20733         {
20734           if (!TRANSLATION_UNIT_LANGUAGE (t))
20735             continue;
20736           if (!common_lang)
20737             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20738           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20739             ;
20740           else if (strncmp (common_lang, "GNU C", 5) == 0
20741                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20742             /* Mixing C and C++ is ok, use C++ in that case.  */
20743             common_lang = "GNU C++";
20744           else
20745             {
20746               /* Fall back to C.  */
20747               common_lang = NULL;
20748               break;
20749             }
20750         }
20751
20752       if (common_lang)
20753         language_string = common_lang;
20754     }
20755
20756   language = DW_LANG_C89;
20757   if (strcmp (language_string, "GNU C++") == 0)
20758     language = DW_LANG_C_plus_plus;
20759   else if (strcmp (language_string, "GNU F77") == 0)
20760     language = DW_LANG_Fortran77;
20761   else if (strcmp (language_string, "GNU Pascal") == 0)
20762     language = DW_LANG_Pascal83;
20763   else if (dwarf_version >= 3 || !dwarf_strict)
20764     {
20765       if (strcmp (language_string, "GNU Ada") == 0)
20766         language = DW_LANG_Ada95;
20767       else if (strcmp (language_string, "GNU Fortran") == 0)
20768         language = DW_LANG_Fortran95;
20769       else if (strcmp (language_string, "GNU Java") == 0)
20770         language = DW_LANG_Java;
20771       else if (strcmp (language_string, "GNU Objective-C") == 0)
20772         language = DW_LANG_ObjC;
20773       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20774         language = DW_LANG_ObjC_plus_plus;
20775     }
20776
20777   add_AT_unsigned (die, DW_AT_language, language);
20778
20779   switch (language)
20780     {
20781     case DW_LANG_Fortran77:
20782     case DW_LANG_Fortran90:
20783     case DW_LANG_Fortran95:
20784       /* Fortran has case insensitive identifiers and the front-end
20785          lowercases everything.  */
20786       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20787       break;
20788     default:
20789       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20790       break;
20791     }
20792   return die;
20793 }
20794
20795 /* Generate the DIE for a base class.  */
20796
20797 static void
20798 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20799 {
20800   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20801
20802   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20803   add_data_member_location_attribute (die, binfo);
20804
20805   if (BINFO_VIRTUAL_P (binfo))
20806     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20807
20808   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20809      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20810      the default has always been DW_ACCESS_private.  */
20811   if (access == access_public_node)
20812     {
20813       if (dwarf_version == 2
20814           || context_die->die_tag == DW_TAG_class_type)
20815       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20816     }
20817   else if (access == access_protected_node)
20818     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20819   else if (dwarf_version > 2
20820            && context_die->die_tag != DW_TAG_class_type)
20821     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20822 }
20823
20824 /* Generate a DIE for a class member.  */
20825
20826 static void
20827 gen_member_die (tree type, dw_die_ref context_die)
20828 {
20829   tree member;
20830   tree binfo = TYPE_BINFO (type);
20831   dw_die_ref child;
20832
20833   /* If this is not an incomplete type, output descriptions of each of its
20834      members. Note that as we output the DIEs necessary to represent the
20835      members of this record or union type, we will also be trying to output
20836      DIEs to represent the *types* of those members. However the `type'
20837      function (above) will specifically avoid generating type DIEs for member
20838      types *within* the list of member DIEs for this (containing) type except
20839      for those types (of members) which are explicitly marked as also being
20840      members of this (containing) type themselves.  The g++ front- end can
20841      force any given type to be treated as a member of some other (containing)
20842      type by setting the TYPE_CONTEXT of the given (member) type to point to
20843      the TREE node representing the appropriate (containing) type.  */
20844
20845   /* First output info about the base classes.  */
20846   if (binfo)
20847     {
20848       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20849       int i;
20850       tree base;
20851
20852       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20853         gen_inheritance_die (base,
20854                              (accesses ? VEC_index (tree, accesses, i)
20855                               : access_public_node), context_die);
20856     }
20857
20858   /* Now output info about the data members and type members.  */
20859   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20860     {
20861       /* If we thought we were generating minimal debug info for TYPE
20862          and then changed our minds, some of the member declarations
20863          may have already been defined.  Don't define them again, but
20864          do put them in the right order.  */
20865
20866       child = lookup_decl_die (member);
20867       if (child)
20868         splice_child_die (context_die, child);
20869       else
20870         gen_decl_die (member, NULL, context_die);
20871     }
20872
20873   /* Now output info about the function members (if any).  */
20874   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20875     {
20876       /* Don't include clones in the member list.  */
20877       if (DECL_ABSTRACT_ORIGIN (member))
20878         continue;
20879
20880       child = lookup_decl_die (member);
20881       if (child)
20882         splice_child_die (context_die, child);
20883       else
20884         gen_decl_die (member, NULL, context_die);
20885     }
20886 }
20887
20888 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20889    is set, we pretend that the type was never defined, so we only get the
20890    member DIEs needed by later specification DIEs.  */
20891
20892 static void
20893 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20894                                 enum debug_info_usage usage)
20895 {
20896   dw_die_ref type_die = lookup_type_die (type);
20897   dw_die_ref scope_die = 0;
20898   int nested = 0;
20899   int complete = (TYPE_SIZE (type)
20900                   && (! TYPE_STUB_DECL (type)
20901                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20902   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20903   complete = complete && should_emit_struct_debug (type, usage);
20904
20905   if (type_die && ! complete)
20906     return;
20907
20908   if (TYPE_CONTEXT (type) != NULL_TREE
20909       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20910           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20911     nested = 1;
20912
20913   scope_die = scope_die_for (type, context_die);
20914
20915   if (! type_die || (nested && is_cu_die (scope_die)))
20916     /* First occurrence of type or toplevel definition of nested class.  */
20917     {
20918       dw_die_ref old_die = type_die;
20919
20920       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20921                           ? record_type_tag (type) : DW_TAG_union_type,
20922                           scope_die, type);
20923       equate_type_number_to_die (type, type_die);
20924       if (old_die)
20925         add_AT_specification (type_die, old_die);
20926       else
20927         {
20928           add_name_attribute (type_die, type_tag (type));
20929           add_gnat_descriptive_type_attribute (type_die, type, context_die);
20930         }
20931     }
20932   else
20933     remove_AT (type_die, DW_AT_declaration);
20934
20935   /* Generate child dies for template paramaters.  */
20936   if (debug_info_level > DINFO_LEVEL_TERSE
20937       && COMPLETE_TYPE_P (type))
20938     schedule_generic_params_dies_gen (type);
20939
20940   /* If this type has been completed, then give it a byte_size attribute and
20941      then give a list of members.  */
20942   if (complete && !ns_decl)
20943     {
20944       /* Prevent infinite recursion in cases where the type of some member of
20945          this type is expressed in terms of this type itself.  */
20946       TREE_ASM_WRITTEN (type) = 1;
20947       add_byte_size_attribute (type_die, type);
20948       if (TYPE_STUB_DECL (type) != NULL_TREE)
20949         {
20950           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20951           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20952         }
20953
20954       /* If the first reference to this type was as the return type of an
20955          inline function, then it may not have a parent.  Fix this now.  */
20956       if (type_die->die_parent == NULL)
20957         add_child_die (scope_die, type_die);
20958
20959       push_decl_scope (type);
20960       gen_member_die (type, type_die);
20961       pop_decl_scope ();
20962
20963       /* GNU extension: Record what type our vtable lives in.  */
20964       if (TYPE_VFIELD (type))
20965         {
20966           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20967
20968           gen_type_die (vtype, context_die);
20969           add_AT_die_ref (type_die, DW_AT_containing_type,
20970                           lookup_type_die (vtype));
20971         }
20972     }
20973   else
20974     {
20975       add_AT_flag (type_die, DW_AT_declaration, 1);
20976
20977       /* We don't need to do this for function-local types.  */
20978       if (TYPE_STUB_DECL (type)
20979           && ! decl_function_context (TYPE_STUB_DECL (type)))
20980         VEC_safe_push (tree, gc, incomplete_types, type);
20981     }
20982
20983   if (get_AT (type_die, DW_AT_name))
20984     add_pubtype (type, type_die);
20985 }
20986
20987 /* Generate a DIE for a subroutine _type_.  */
20988
20989 static void
20990 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20991 {
20992   tree return_type = TREE_TYPE (type);
20993   dw_die_ref subr_die
20994     = new_die (DW_TAG_subroutine_type,
20995                scope_die_for (type, context_die), type);
20996
20997   equate_type_number_to_die (type, subr_die);
20998   add_prototyped_attribute (subr_die, type);
20999   add_type_attribute (subr_die, return_type, 0, 0, context_die);
21000   gen_formal_types_die (type, subr_die);
21001
21002   if (get_AT (subr_die, DW_AT_name))
21003     add_pubtype (type, subr_die);
21004 }
21005
21006 /* Generate a DIE for a type definition.  */
21007
21008 static void
21009 gen_typedef_die (tree decl, dw_die_ref context_die)
21010 {
21011   dw_die_ref type_die;
21012   tree origin;
21013
21014   if (TREE_ASM_WRITTEN (decl))
21015     return;
21016
21017   TREE_ASM_WRITTEN (decl) = 1;
21018   type_die = new_die (DW_TAG_typedef, context_die, decl);
21019   origin = decl_ultimate_origin (decl);
21020   if (origin != NULL)
21021     add_abstract_origin_attribute (type_die, origin);
21022   else
21023     {
21024       tree type;
21025
21026       add_name_and_src_coords_attributes (type_die, decl);
21027       if (DECL_ORIGINAL_TYPE (decl))
21028         {
21029           type = DECL_ORIGINAL_TYPE (decl);
21030
21031           gcc_assert (type != TREE_TYPE (decl));
21032           equate_type_number_to_die (TREE_TYPE (decl), type_die);
21033         }
21034       else
21035         {
21036           type = TREE_TYPE (decl);
21037
21038           if (is_naming_typedef_decl (TYPE_NAME (type)))
21039             {
21040               /* Here, we are in the case of decl being a typedef naming
21041                  an anonymous type, e.g:
21042                      typedef struct {...} foo;
21043                  In that case TREE_TYPE (decl) is not a typedef variant
21044                  type and TYPE_NAME of the anonymous type is set to the
21045                  TYPE_DECL of the typedef. This construct is emitted by
21046                  the C++ FE.
21047
21048                  TYPE is the anonymous struct named by the typedef
21049                  DECL. As we need the DW_AT_type attribute of the
21050                  DW_TAG_typedef to point to the DIE of TYPE, let's
21051                  generate that DIE right away. add_type_attribute
21052                  called below will then pick (via lookup_type_die) that
21053                  anonymous struct DIE.  */
21054               if (!TREE_ASM_WRITTEN (type))
21055                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
21056
21057               /* This is a GNU Extension.  We are adding a
21058                  DW_AT_linkage_name attribute to the DIE of the
21059                  anonymous struct TYPE.  The value of that attribute
21060                  is the name of the typedef decl naming the anonymous
21061                  struct.  This greatly eases the work of consumers of
21062                  this debug info.  */
21063               add_linkage_attr (lookup_type_die (type), decl);
21064             }
21065         }
21066
21067       add_type_attribute (type_die, type, TREE_READONLY (decl),
21068                           TREE_THIS_VOLATILE (decl), context_die);
21069
21070       if (is_naming_typedef_decl (decl))
21071         /* We want that all subsequent calls to lookup_type_die with
21072            TYPE in argument yield the DW_TAG_typedef we have just
21073            created.  */
21074         equate_type_number_to_die (type, type_die);
21075
21076       add_accessibility_attribute (type_die, decl);
21077     }
21078
21079   if (DECL_ABSTRACT (decl))
21080     equate_decl_number_to_die (decl, type_die);
21081
21082   if (get_AT (type_die, DW_AT_name))
21083     add_pubtype (decl, type_die);
21084 }
21085
21086 /* Generate a DIE for a struct, class, enum or union type.  */
21087
21088 static void
21089 gen_tagged_type_die (tree type,
21090                      dw_die_ref context_die,
21091                      enum debug_info_usage usage)
21092 {
21093   int need_pop;
21094
21095   if (type == NULL_TREE
21096       || !is_tagged_type (type))
21097     return;
21098
21099   /* If this is a nested type whose containing class hasn't been written
21100      out yet, writing it out will cover this one, too.  This does not apply
21101      to instantiations of member class templates; they need to be added to
21102      the containing class as they are generated.  FIXME: This hurts the
21103      idea of combining type decls from multiple TUs, since we can't predict
21104      what set of template instantiations we'll get.  */
21105   if (TYPE_CONTEXT (type)
21106       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
21107       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
21108     {
21109       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
21110
21111       if (TREE_ASM_WRITTEN (type))
21112         return;
21113
21114       /* If that failed, attach ourselves to the stub.  */
21115       push_decl_scope (TYPE_CONTEXT (type));
21116       context_die = lookup_type_die (TYPE_CONTEXT (type));
21117       need_pop = 1;
21118     }
21119   else if (TYPE_CONTEXT (type) != NULL_TREE
21120            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
21121     {
21122       /* If this type is local to a function that hasn't been written
21123          out yet, use a NULL context for now; it will be fixed up in
21124          decls_for_scope.  */
21125       context_die = lookup_decl_die (TYPE_CONTEXT (type));
21126       /* A declaration DIE doesn't count; nested types need to go in the
21127          specification.  */
21128       if (context_die && is_declaration_die (context_die))
21129         context_die = NULL;
21130       need_pop = 0;
21131     }
21132   else
21133     {
21134       context_die = declare_in_namespace (type, context_die);
21135       need_pop = 0;
21136     }
21137
21138   if (TREE_CODE (type) == ENUMERAL_TYPE)
21139     {
21140       /* This might have been written out by the call to
21141          declare_in_namespace.  */
21142       if (!TREE_ASM_WRITTEN (type))
21143         gen_enumeration_type_die (type, context_die);
21144     }
21145   else
21146     gen_struct_or_union_type_die (type, context_die, usage);
21147
21148   if (need_pop)
21149     pop_decl_scope ();
21150
21151   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
21152      it up if it is ever completed.  gen_*_type_die will set it for us
21153      when appropriate.  */
21154 }
21155
21156 /* Generate a type description DIE.  */
21157
21158 static void
21159 gen_type_die_with_usage (tree type, dw_die_ref context_die,
21160                          enum debug_info_usage usage)
21161 {
21162   struct array_descr_info info;
21163
21164   if (type == NULL_TREE || type == error_mark_node)
21165     return;
21166
21167   if (TYPE_NAME (type) != NULL_TREE
21168       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21169       && is_redundant_typedef (TYPE_NAME (type))
21170       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
21171     /* The DECL of this type is a typedef we don't want to emit debug
21172        info for but we want debug info for its underlying typedef.
21173        This can happen for e.g, the injected-class-name of a C++
21174        type.  */
21175     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
21176
21177   /* If TYPE is a typedef type variant, let's generate debug info
21178      for the parent typedef which TYPE is a type of.  */
21179   if (typedef_variant_p (type))
21180     {
21181       if (TREE_ASM_WRITTEN (type))
21182         return;
21183
21184       /* Prevent broken recursion; we can't hand off to the same type.  */
21185       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
21186
21187       /* Use the DIE of the containing namespace as the parent DIE of
21188          the type description DIE we want to generate.  */
21189       if (DECL_CONTEXT (TYPE_NAME (type))
21190           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21191         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21192
21193       TREE_ASM_WRITTEN (type) = 1;
21194
21195       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21196       return;
21197     }
21198
21199   /* If type is an anonymous tagged type named by a typedef, let's
21200      generate debug info for the typedef.  */
21201   if (is_naming_typedef_decl (TYPE_NAME (type)))
21202     {
21203       /* Use the DIE of the containing namespace as the parent DIE of
21204          the type description DIE we want to generate.  */
21205       if (DECL_CONTEXT (TYPE_NAME (type))
21206           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21207         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21208       
21209       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21210       return;
21211     }
21212
21213   /* If this is an array type with hidden descriptor, handle it first.  */
21214   if (!TREE_ASM_WRITTEN (type)
21215       && lang_hooks.types.get_array_descr_info
21216       && lang_hooks.types.get_array_descr_info (type, &info)
21217       && (dwarf_version >= 3 || !dwarf_strict))
21218     {
21219       gen_descr_array_type_die (type, &info, context_die);
21220       TREE_ASM_WRITTEN (type) = 1;
21221       return;
21222     }
21223
21224   /* We are going to output a DIE to represent the unqualified version
21225      of this type (i.e. without any const or volatile qualifiers) so
21226      get the main variant (i.e. the unqualified version) of this type
21227      now.  (Vectors are special because the debugging info is in the
21228      cloned type itself).  */
21229   if (TREE_CODE (type) != VECTOR_TYPE)
21230     type = type_main_variant (type);
21231
21232   if (TREE_ASM_WRITTEN (type))
21233     return;
21234
21235   switch (TREE_CODE (type))
21236     {
21237     case ERROR_MARK:
21238       break;
21239
21240     case POINTER_TYPE:
21241     case REFERENCE_TYPE:
21242       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
21243          ensures that the gen_type_die recursion will terminate even if the
21244          type is recursive.  Recursive types are possible in Ada.  */
21245       /* ??? We could perhaps do this for all types before the switch
21246          statement.  */
21247       TREE_ASM_WRITTEN (type) = 1;
21248
21249       /* For these types, all that is required is that we output a DIE (or a
21250          set of DIEs) to represent the "basis" type.  */
21251       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21252                                 DINFO_USAGE_IND_USE);
21253       break;
21254
21255     case OFFSET_TYPE:
21256       /* This code is used for C++ pointer-to-data-member types.
21257          Output a description of the relevant class type.  */
21258       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
21259                                         DINFO_USAGE_IND_USE);
21260
21261       /* Output a description of the type of the object pointed to.  */
21262       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21263                                         DINFO_USAGE_IND_USE);
21264
21265       /* Now output a DIE to represent this pointer-to-data-member type
21266          itself.  */
21267       gen_ptr_to_mbr_type_die (type, context_die);
21268       break;
21269
21270     case FUNCTION_TYPE:
21271       /* Force out return type (in case it wasn't forced out already).  */
21272       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21273                                         DINFO_USAGE_DIR_USE);
21274       gen_subroutine_type_die (type, context_die);
21275       break;
21276
21277     case METHOD_TYPE:
21278       /* Force out return type (in case it wasn't forced out already).  */
21279       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21280                                         DINFO_USAGE_DIR_USE);
21281       gen_subroutine_type_die (type, context_die);
21282       break;
21283
21284     case ARRAY_TYPE:
21285       gen_array_type_die (type, context_die);
21286       break;
21287
21288     case VECTOR_TYPE:
21289       gen_array_type_die (type, context_die);
21290       break;
21291
21292     case ENUMERAL_TYPE:
21293     case RECORD_TYPE:
21294     case UNION_TYPE:
21295     case QUAL_UNION_TYPE:
21296       gen_tagged_type_die (type, context_die, usage);
21297       return;
21298
21299     case VOID_TYPE:
21300     case INTEGER_TYPE:
21301     case REAL_TYPE:
21302     case FIXED_POINT_TYPE:
21303     case COMPLEX_TYPE:
21304     case BOOLEAN_TYPE:
21305       /* No DIEs needed for fundamental types.  */
21306       break;
21307
21308     case NULLPTR_TYPE:
21309     case LANG_TYPE:
21310       /* Just use DW_TAG_unspecified_type.  */
21311       {
21312         dw_die_ref type_die = lookup_type_die (type);
21313         if (type_die == NULL)
21314           {
21315             tree name = TYPE_NAME (type);
21316             if (TREE_CODE (name) == TYPE_DECL)
21317               name = DECL_NAME (name);
21318             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
21319             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
21320             equate_type_number_to_die (type, type_die);
21321           }
21322       }
21323       break;
21324
21325     default:
21326       gcc_unreachable ();
21327     }
21328
21329   TREE_ASM_WRITTEN (type) = 1;
21330 }
21331
21332 static void
21333 gen_type_die (tree type, dw_die_ref context_die)
21334 {
21335   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
21336 }
21337
21338 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
21339    things which are local to the given block.  */
21340
21341 static void
21342 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
21343 {
21344   int must_output_die = 0;
21345   bool inlined_func;
21346
21347   /* Ignore blocks that are NULL.  */
21348   if (stmt == NULL_TREE)
21349     return;
21350
21351   inlined_func = inlined_function_outer_scope_p (stmt);
21352
21353   /* If the block is one fragment of a non-contiguous block, do not
21354      process the variables, since they will have been done by the
21355      origin block.  Do process subblocks.  */
21356   if (BLOCK_FRAGMENT_ORIGIN (stmt))
21357     {
21358       tree sub;
21359
21360       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
21361         gen_block_die (sub, context_die, depth + 1);
21362
21363       return;
21364     }
21365
21366   /* Determine if we need to output any Dwarf DIEs at all to represent this
21367      block.  */
21368   if (inlined_func)
21369     /* The outer scopes for inlinings *must* always be represented.  We
21370        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
21371     must_output_die = 1;
21372   else
21373     {
21374       /* Determine if this block directly contains any "significant"
21375          local declarations which we will need to output DIEs for.  */
21376       if (debug_info_level > DINFO_LEVEL_TERSE)
21377         /* We are not in terse mode so *any* local declaration counts
21378            as being a "significant" one.  */
21379         must_output_die = ((BLOCK_VARS (stmt) != NULL
21380                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
21381                            && (TREE_USED (stmt)
21382                                || TREE_ASM_WRITTEN (stmt)
21383                                || BLOCK_ABSTRACT (stmt)));
21384       else if ((TREE_USED (stmt)
21385                 || TREE_ASM_WRITTEN (stmt)
21386                 || BLOCK_ABSTRACT (stmt))
21387                && !dwarf2out_ignore_block (stmt))
21388         must_output_die = 1;
21389     }
21390
21391   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
21392      DIE for any block which contains no significant local declarations at
21393      all.  Rather, in such cases we just call `decls_for_scope' so that any
21394      needed Dwarf info for any sub-blocks will get properly generated. Note
21395      that in terse mode, our definition of what constitutes a "significant"
21396      local declaration gets restricted to include only inlined function
21397      instances and local (nested) function definitions.  */
21398   if (must_output_die)
21399     {
21400       if (inlined_func)
21401         {
21402           /* If STMT block is abstract, that means we have been called
21403              indirectly from dwarf2out_abstract_function.
21404              That function rightfully marks the descendent blocks (of
21405              the abstract function it is dealing with) as being abstract,
21406              precisely to prevent us from emitting any
21407              DW_TAG_inlined_subroutine DIE as a descendent
21408              of an abstract function instance. So in that case, we should
21409              not call gen_inlined_subroutine_die.
21410
21411              Later though, when cgraph asks dwarf2out to emit info
21412              for the concrete instance of the function decl into which
21413              the concrete instance of STMT got inlined, the later will lead
21414              to the generation of a DW_TAG_inlined_subroutine DIE.  */
21415           if (! BLOCK_ABSTRACT (stmt))
21416             gen_inlined_subroutine_die (stmt, context_die, depth);
21417         }
21418       else
21419         gen_lexical_block_die (stmt, context_die, depth);
21420     }
21421   else
21422     decls_for_scope (stmt, context_die, depth);
21423 }
21424
21425 /* Process variable DECL (or variable with origin ORIGIN) within
21426    block STMT and add it to CONTEXT_DIE.  */
21427 static void
21428 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21429 {
21430   dw_die_ref die;
21431   tree decl_or_origin = decl ? decl : origin;
21432
21433   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21434     die = lookup_decl_die (decl_or_origin);
21435   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21436            && TYPE_DECL_IS_STUB (decl_or_origin))
21437     die = lookup_type_die (TREE_TYPE (decl_or_origin));
21438   else
21439     die = NULL;
21440
21441   if (die != NULL && die->die_parent == NULL)
21442     add_child_die (context_die, die);
21443   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21444     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21445                                          stmt, context_die);
21446   else
21447     gen_decl_die (decl, origin, context_die);
21448 }
21449
21450 /* Generate all of the decls declared within a given scope and (recursively)
21451    all of its sub-blocks.  */
21452
21453 static void
21454 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
21455 {
21456   tree decl;
21457   unsigned int i;
21458   tree subblocks;
21459
21460   /* Ignore NULL blocks.  */
21461   if (stmt == NULL_TREE)
21462     return;
21463
21464   /* Output the DIEs to represent all of the data objects and typedefs
21465      declared directly within this block but not within any nested
21466      sub-blocks.  Also, nested function and tag DIEs have been
21467      generated with a parent of NULL; fix that up now.  */
21468   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21469     process_scope_var (stmt, decl, NULL_TREE, context_die);
21470   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21471     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21472                        context_die);
21473
21474   /* If we're at -g1, we're not interested in subblocks.  */
21475   if (debug_info_level <= DINFO_LEVEL_TERSE)
21476     return;
21477
21478   /* Output the DIEs to represent all sub-blocks (and the items declared
21479      therein) of this block.  */
21480   for (subblocks = BLOCK_SUBBLOCKS (stmt);
21481        subblocks != NULL;
21482        subblocks = BLOCK_CHAIN (subblocks))
21483     gen_block_die (subblocks, context_die, depth + 1);
21484 }
21485
21486 /* Is this a typedef we can avoid emitting?  */
21487
21488 static inline int
21489 is_redundant_typedef (const_tree decl)
21490 {
21491   if (TYPE_DECL_IS_STUB (decl))
21492     return 1;
21493
21494   if (DECL_ARTIFICIAL (decl)
21495       && DECL_CONTEXT (decl)
21496       && is_tagged_type (DECL_CONTEXT (decl))
21497       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21498       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21499     /* Also ignore the artificial member typedef for the class name.  */
21500     return 1;
21501
21502   return 0;
21503 }
21504
21505 /* Return TRUE if TYPE is a typedef that names a type for linkage
21506    purposes. This kind of typedefs is produced by the C++ FE for
21507    constructs like:
21508
21509    typedef struct {...} foo;
21510
21511    In that case, there is no typedef variant type produced for foo.
21512    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21513    struct type.  */
21514
21515 static bool
21516 is_naming_typedef_decl (const_tree decl)
21517 {
21518   if (decl == NULL_TREE
21519       || TREE_CODE (decl) != TYPE_DECL
21520       || !is_tagged_type (TREE_TYPE (decl))
21521       || DECL_IS_BUILTIN (decl)
21522       || is_redundant_typedef (decl)
21523       /* It looks like Ada produces TYPE_DECLs that are very similar
21524          to C++ naming typedefs but that have different
21525          semantics. Let's be specific to c++ for now.  */
21526       || !is_cxx ())
21527     return FALSE;
21528
21529   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21530           && TYPE_NAME (TREE_TYPE (decl)) == decl
21531           && (TYPE_STUB_DECL (TREE_TYPE (decl))
21532               != TYPE_NAME (TREE_TYPE (decl))));
21533 }
21534
21535 /* Returns the DIE for a context.  */
21536
21537 static inline dw_die_ref
21538 get_context_die (tree context)
21539 {
21540   if (context)
21541     {
21542       /* Find die that represents this context.  */
21543       if (TYPE_P (context))
21544         {
21545           context = TYPE_MAIN_VARIANT (context);
21546           return strip_naming_typedef (context, force_type_die (context));
21547         }
21548       else
21549         return force_decl_die (context);
21550     }
21551   return comp_unit_die ();
21552 }
21553
21554 /* Returns the DIE for decl.  A DIE will always be returned.  */
21555
21556 static dw_die_ref
21557 force_decl_die (tree decl)
21558 {
21559   dw_die_ref decl_die;
21560   unsigned saved_external_flag;
21561   tree save_fn = NULL_TREE;
21562   decl_die = lookup_decl_die (decl);
21563   if (!decl_die)
21564     {
21565       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21566
21567       decl_die = lookup_decl_die (decl);
21568       if (decl_die)
21569         return decl_die;
21570
21571       switch (TREE_CODE (decl))
21572         {
21573         case FUNCTION_DECL:
21574           /* Clear current_function_decl, so that gen_subprogram_die thinks
21575              that this is a declaration. At this point, we just want to force
21576              declaration die.  */
21577           save_fn = current_function_decl;
21578           current_function_decl = NULL_TREE;
21579           gen_subprogram_die (decl, context_die);
21580           current_function_decl = save_fn;
21581           break;
21582
21583         case VAR_DECL:
21584           /* Set external flag to force declaration die. Restore it after
21585            gen_decl_die() call.  */
21586           saved_external_flag = DECL_EXTERNAL (decl);
21587           DECL_EXTERNAL (decl) = 1;
21588           gen_decl_die (decl, NULL, context_die);
21589           DECL_EXTERNAL (decl) = saved_external_flag;
21590           break;
21591
21592         case NAMESPACE_DECL:
21593           if (dwarf_version >= 3 || !dwarf_strict)
21594             dwarf2out_decl (decl);
21595           else
21596             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21597             decl_die = comp_unit_die ();
21598           break;
21599
21600         case TRANSLATION_UNIT_DECL:
21601           decl_die = comp_unit_die ();
21602           break;
21603
21604         default:
21605           gcc_unreachable ();
21606         }
21607
21608       /* We should be able to find the DIE now.  */
21609       if (!decl_die)
21610         decl_die = lookup_decl_die (decl);
21611       gcc_assert (decl_die);
21612     }
21613
21614   return decl_die;
21615 }
21616
21617 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21618    always returned.  */
21619
21620 static dw_die_ref
21621 force_type_die (tree type)
21622 {
21623   dw_die_ref type_die;
21624
21625   type_die = lookup_type_die (type);
21626   if (!type_die)
21627     {
21628       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21629
21630       type_die = modified_type_die (type, TYPE_READONLY (type),
21631                                     TYPE_VOLATILE (type), context_die);
21632       gcc_assert (type_die);
21633     }
21634   return type_die;
21635 }
21636
21637 /* Force out any required namespaces to be able to output DECL,
21638    and return the new context_die for it, if it's changed.  */
21639
21640 static dw_die_ref
21641 setup_namespace_context (tree thing, dw_die_ref context_die)
21642 {
21643   tree context = (DECL_P (thing)
21644                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21645   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21646     /* Force out the namespace.  */
21647     context_die = force_decl_die (context);
21648
21649   return context_die;
21650 }
21651
21652 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21653    type) within its namespace, if appropriate.
21654
21655    For compatibility with older debuggers, namespace DIEs only contain
21656    declarations; all definitions are emitted at CU scope.  */
21657
21658 static dw_die_ref
21659 declare_in_namespace (tree thing, dw_die_ref context_die)
21660 {
21661   dw_die_ref ns_context;
21662
21663   if (debug_info_level <= DINFO_LEVEL_TERSE)
21664     return context_die;
21665
21666   /* If this decl is from an inlined function, then don't try to emit it in its
21667      namespace, as we will get confused.  It would have already been emitted
21668      when the abstract instance of the inline function was emitted anyways.  */
21669   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21670     return context_die;
21671
21672   ns_context = setup_namespace_context (thing, context_die);
21673
21674   if (ns_context != context_die)
21675     {
21676       if (is_fortran ())
21677         return ns_context;
21678       if (DECL_P (thing))
21679         gen_decl_die (thing, NULL, ns_context);
21680       else
21681         gen_type_die (thing, ns_context);
21682     }
21683   return context_die;
21684 }
21685
21686 /* Generate a DIE for a namespace or namespace alias.  */
21687
21688 static void
21689 gen_namespace_die (tree decl, dw_die_ref context_die)
21690 {
21691   dw_die_ref namespace_die;
21692
21693   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21694      they are an alias of.  */
21695   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21696     {
21697       /* Output a real namespace or module.  */
21698       context_die = setup_namespace_context (decl, comp_unit_die ());
21699       namespace_die = new_die (is_fortran ()
21700                                ? DW_TAG_module : DW_TAG_namespace,
21701                                context_die, decl);
21702       /* For Fortran modules defined in different CU don't add src coords.  */
21703       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21704         {
21705           const char *name = dwarf2_name (decl, 0);
21706           if (name)
21707             add_name_attribute (namespace_die, name);
21708         }
21709       else
21710         add_name_and_src_coords_attributes (namespace_die, decl);
21711       if (DECL_EXTERNAL (decl))
21712         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21713       equate_decl_number_to_die (decl, namespace_die);
21714     }
21715   else
21716     {
21717       /* Output a namespace alias.  */
21718
21719       /* Force out the namespace we are an alias of, if necessary.  */
21720       dw_die_ref origin_die
21721         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21722
21723       if (DECL_FILE_SCOPE_P (decl)
21724           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21725         context_die = setup_namespace_context (decl, comp_unit_die ());
21726       /* Now create the namespace alias DIE.  */
21727       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21728       add_name_and_src_coords_attributes (namespace_die, decl);
21729       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21730       equate_decl_number_to_die (decl, namespace_die);
21731     }
21732 }
21733
21734 /* Generate Dwarf debug information for a decl described by DECL.
21735    The return value is currently only meaningful for PARM_DECLs,
21736    for all other decls it returns NULL.  */
21737
21738 static dw_die_ref
21739 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21740 {
21741   tree decl_or_origin = decl ? decl : origin;
21742   tree class_origin = NULL, ultimate_origin;
21743
21744   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21745     return NULL;
21746
21747   switch (TREE_CODE (decl_or_origin))
21748     {
21749     case ERROR_MARK:
21750       break;
21751
21752     case CONST_DECL:
21753       if (!is_fortran () && !is_ada ())
21754         {
21755           /* The individual enumerators of an enum type get output when we output
21756              the Dwarf representation of the relevant enum type itself.  */
21757           break;
21758         }
21759
21760       /* Emit its type.  */
21761       gen_type_die (TREE_TYPE (decl), context_die);
21762
21763       /* And its containing namespace.  */
21764       context_die = declare_in_namespace (decl, context_die);
21765
21766       gen_const_die (decl, context_die);
21767       break;
21768
21769     case FUNCTION_DECL:
21770       /* Don't output any DIEs to represent mere function declarations,
21771          unless they are class members or explicit block externs.  */
21772       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21773           && DECL_FILE_SCOPE_P (decl_or_origin)
21774           && (current_function_decl == NULL_TREE
21775               || DECL_ARTIFICIAL (decl_or_origin)))
21776         break;
21777
21778 #if 0
21779       /* FIXME */
21780       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21781          on local redeclarations of global functions.  That seems broken.  */
21782       if (current_function_decl != decl)
21783         /* This is only a declaration.  */;
21784 #endif
21785
21786       /* If we're emitting a clone, emit info for the abstract instance.  */
21787       if (origin || DECL_ORIGIN (decl) != decl)
21788         dwarf2out_abstract_function (origin
21789                                      ? DECL_ORIGIN (origin)
21790                                      : DECL_ABSTRACT_ORIGIN (decl));
21791
21792       /* If we're emitting an out-of-line copy of an inline function,
21793          emit info for the abstract instance and set up to refer to it.  */
21794       else if (cgraph_function_possibly_inlined_p (decl)
21795                && ! DECL_ABSTRACT (decl)
21796                && ! class_or_namespace_scope_p (context_die)
21797                /* dwarf2out_abstract_function won't emit a die if this is just
21798                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21799                   that case, because that works only if we have a die.  */
21800                && DECL_INITIAL (decl) != NULL_TREE)
21801         {
21802           dwarf2out_abstract_function (decl);
21803           set_decl_origin_self (decl);
21804         }
21805
21806       /* Otherwise we're emitting the primary DIE for this decl.  */
21807       else if (debug_info_level > DINFO_LEVEL_TERSE)
21808         {
21809           /* Before we describe the FUNCTION_DECL itself, make sure that we
21810              have its containing type.  */
21811           if (!origin)
21812             origin = decl_class_context (decl);
21813           if (origin != NULL_TREE)
21814             gen_type_die (origin, context_die);
21815
21816           /* And its return type.  */
21817           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21818
21819           /* And its virtual context.  */
21820           if (DECL_VINDEX (decl) != NULL_TREE)
21821             gen_type_die (DECL_CONTEXT (decl), context_die);
21822
21823           /* Make sure we have a member DIE for decl.  */
21824           if (origin != NULL_TREE)
21825             gen_type_die_for_member (origin, decl, context_die);
21826
21827           /* And its containing namespace.  */
21828           context_die = declare_in_namespace (decl, context_die);
21829         }
21830
21831       /* Now output a DIE to represent the function itself.  */
21832       if (decl)
21833         gen_subprogram_die (decl, context_die);
21834       break;
21835
21836     case TYPE_DECL:
21837       /* If we are in terse mode, don't generate any DIEs to represent any
21838          actual typedefs.  */
21839       if (debug_info_level <= DINFO_LEVEL_TERSE)
21840         break;
21841
21842       /* In the special case of a TYPE_DECL node representing the declaration
21843          of some type tag, if the given TYPE_DECL is marked as having been
21844          instantiated from some other (original) TYPE_DECL node (e.g. one which
21845          was generated within the original definition of an inline function) we
21846          used to generate a special (abbreviated) DW_TAG_structure_type,
21847          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21848          should be actually referencing those DIEs, as variable DIEs with that
21849          type would be emitted already in the abstract origin, so it was always
21850          removed during unused type prunning.  Don't add anything in this
21851          case.  */
21852       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21853         break;
21854
21855       if (is_redundant_typedef (decl))
21856         gen_type_die (TREE_TYPE (decl), context_die);
21857       else
21858         /* Output a DIE to represent the typedef itself.  */
21859         gen_typedef_die (decl, context_die);
21860       break;
21861
21862     case LABEL_DECL:
21863       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21864         gen_label_die (decl, context_die);
21865       break;
21866
21867     case VAR_DECL:
21868     case RESULT_DECL:
21869       /* If we are in terse mode, don't generate any DIEs to represent any
21870          variable declarations or definitions.  */
21871       if (debug_info_level <= DINFO_LEVEL_TERSE)
21872         break;
21873
21874       /* Output any DIEs that are needed to specify the type of this data
21875          object.  */
21876       if (decl_by_reference_p (decl_or_origin))
21877         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21878       else
21879         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21880
21881       /* And its containing type.  */
21882       class_origin = decl_class_context (decl_or_origin);
21883       if (class_origin != NULL_TREE)
21884         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21885
21886       /* And its containing namespace.  */
21887       context_die = declare_in_namespace (decl_or_origin, context_die);
21888
21889       /* Now output the DIE to represent the data object itself.  This gets
21890          complicated because of the possibility that the VAR_DECL really
21891          represents an inlined instance of a formal parameter for an inline
21892          function.  */
21893       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21894       if (ultimate_origin != NULL_TREE
21895           && TREE_CODE (ultimate_origin) == PARM_DECL)
21896         gen_formal_parameter_die (decl, origin,
21897                                   true /* Emit name attribute.  */,
21898                                   context_die);
21899       else
21900         gen_variable_die (decl, origin, context_die);
21901       break;
21902
21903     case FIELD_DECL:
21904       /* Ignore the nameless fields that are used to skip bits but handle C++
21905          anonymous unions and structs.  */
21906       if (DECL_NAME (decl) != NULL_TREE
21907           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21908           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21909         {
21910           gen_type_die (member_declared_type (decl), context_die);
21911           gen_field_die (decl, context_die);
21912         }
21913       break;
21914
21915     case PARM_DECL:
21916       if (DECL_BY_REFERENCE (decl_or_origin))
21917         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21918       else
21919         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21920       return gen_formal_parameter_die (decl, origin,
21921                                        true /* Emit name attribute.  */,
21922                                        context_die);
21923
21924     case NAMESPACE_DECL:
21925     case IMPORTED_DECL:
21926       if (dwarf_version >= 3 || !dwarf_strict)
21927         gen_namespace_die (decl, context_die);
21928       break;
21929
21930     default:
21931       /* Probably some frontend-internal decl.  Assume we don't care.  */
21932       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21933       break;
21934     }
21935
21936   return NULL;
21937 }
21938 \f
21939 /* Output debug information for global decl DECL.  Called from toplev.c after
21940    compilation proper has finished.  */
21941
21942 static void
21943 dwarf2out_global_decl (tree decl)
21944 {
21945   /* Output DWARF2 information for file-scope tentative data object
21946      declarations, file-scope (extern) function declarations (which
21947      had no corresponding body) and file-scope tagged type declarations
21948      and definitions which have not yet been forced out.  */
21949   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21950     dwarf2out_decl (decl);
21951 }
21952
21953 /* Output debug information for type decl DECL.  Called from toplev.c
21954    and from language front ends (to record built-in types).  */
21955 static void
21956 dwarf2out_type_decl (tree decl, int local)
21957 {
21958   if (!local)
21959     dwarf2out_decl (decl);
21960 }
21961
21962 /* Output debug information for imported module or decl DECL.
21963    NAME is non-NULL name in the lexical block if the decl has been renamed.
21964    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21965    that DECL belongs to.
21966    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21967 static void
21968 dwarf2out_imported_module_or_decl_1 (tree decl,
21969                                      tree name,
21970                                      tree lexical_block,
21971                                      dw_die_ref lexical_block_die)
21972 {
21973   expanded_location xloc;
21974   dw_die_ref imported_die = NULL;
21975   dw_die_ref at_import_die;
21976
21977   if (TREE_CODE (decl) == IMPORTED_DECL)
21978     {
21979       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21980       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21981       gcc_assert (decl);
21982     }
21983   else
21984     xloc = expand_location (input_location);
21985
21986   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21987     {
21988       at_import_die = force_type_die (TREE_TYPE (decl));
21989       /* For namespace N { typedef void T; } using N::T; base_type_die
21990          returns NULL, but DW_TAG_imported_declaration requires
21991          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21992       if (!at_import_die)
21993         {
21994           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21995           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21996           at_import_die = lookup_type_die (TREE_TYPE (decl));
21997           gcc_assert (at_import_die);
21998         }
21999     }
22000   else
22001     {
22002       at_import_die = lookup_decl_die (decl);
22003       if (!at_import_die)
22004         {
22005           /* If we're trying to avoid duplicate debug info, we may not have
22006              emitted the member decl for this field.  Emit it now.  */
22007           if (TREE_CODE (decl) == FIELD_DECL)
22008             {
22009               tree type = DECL_CONTEXT (decl);
22010
22011               if (TYPE_CONTEXT (type)
22012                   && TYPE_P (TYPE_CONTEXT (type))
22013                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
22014                                                 DINFO_USAGE_DIR_USE))
22015                 return;
22016               gen_type_die_for_member (type, decl,
22017                                        get_context_die (TYPE_CONTEXT (type)));
22018             }
22019           at_import_die = force_decl_die (decl);
22020         }
22021     }
22022
22023   if (TREE_CODE (decl) == NAMESPACE_DECL)
22024     {
22025       if (dwarf_version >= 3 || !dwarf_strict)
22026         imported_die = new_die (DW_TAG_imported_module,
22027                                 lexical_block_die,
22028                                 lexical_block);
22029       else
22030         return;
22031     }
22032   else
22033     imported_die = new_die (DW_TAG_imported_declaration,
22034                             lexical_block_die,
22035                             lexical_block);
22036
22037   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
22038   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
22039   if (name)
22040     add_AT_string (imported_die, DW_AT_name,
22041                    IDENTIFIER_POINTER (name));
22042   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
22043 }
22044
22045 /* Output debug information for imported module or decl DECL.
22046    NAME is non-NULL name in context if the decl has been renamed.
22047    CHILD is true if decl is one of the renamed decls as part of
22048    importing whole module.  */
22049
22050 static void
22051 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
22052                                    bool child)
22053 {
22054   /* dw_die_ref at_import_die;  */
22055   dw_die_ref scope_die;
22056
22057   if (debug_info_level <= DINFO_LEVEL_TERSE)
22058     return;
22059
22060   gcc_assert (decl);
22061
22062   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
22063      We need decl DIE for reference and scope die. First, get DIE for the decl
22064      itself.  */
22065
22066   /* Get the scope die for decl context. Use comp_unit_die for global module
22067      or decl. If die is not found for non globals, force new die.  */
22068   if (context
22069       && TYPE_P (context)
22070       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
22071     return;
22072
22073   if (!(dwarf_version >= 3 || !dwarf_strict))
22074     return;
22075
22076   scope_die = get_context_die (context);
22077
22078   if (child)
22079     {
22080       gcc_assert (scope_die->die_child);
22081       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
22082       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
22083       scope_die = scope_die->die_child;
22084     }
22085
22086   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
22087   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
22088
22089 }
22090
22091 /* Write the debugging output for DECL.  */
22092
22093 void
22094 dwarf2out_decl (tree decl)
22095 {
22096   dw_die_ref context_die = comp_unit_die ();
22097
22098   switch (TREE_CODE (decl))
22099     {
22100     case ERROR_MARK:
22101       return;
22102
22103     case FUNCTION_DECL:
22104       /* What we would really like to do here is to filter out all mere
22105          file-scope declarations of file-scope functions which are never
22106          referenced later within this translation unit (and keep all of ones
22107          that *are* referenced later on) but we aren't clairvoyant, so we have
22108          no idea which functions will be referenced in the future (i.e. later
22109          on within the current translation unit). So here we just ignore all
22110          file-scope function declarations which are not also definitions.  If
22111          and when the debugger needs to know something about these functions,
22112          it will have to hunt around and find the DWARF information associated
22113          with the definition of the function.
22114
22115          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
22116          nodes represent definitions and which ones represent mere
22117          declarations.  We have to check DECL_INITIAL instead. That's because
22118          the C front-end supports some weird semantics for "extern inline"
22119          function definitions.  These can get inlined within the current
22120          translation unit (and thus, we need to generate Dwarf info for their
22121          abstract instances so that the Dwarf info for the concrete inlined
22122          instances can have something to refer to) but the compiler never
22123          generates any out-of-lines instances of such things (despite the fact
22124          that they *are* definitions).
22125
22126          The important point is that the C front-end marks these "extern
22127          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
22128          them anyway. Note that the C++ front-end also plays some similar games
22129          for inline function definitions appearing within include files which
22130          also contain `#pragma interface' pragmas.  */
22131       if (DECL_INITIAL (decl) == NULL_TREE)
22132         return;
22133
22134       /* If we're a nested function, initially use a parent of NULL; if we're
22135          a plain function, this will be fixed up in decls_for_scope.  If
22136          we're a method, it will be ignored, since we already have a DIE.  */
22137       if (decl_function_context (decl)
22138           /* But if we're in terse mode, we don't care about scope.  */
22139           && debug_info_level > DINFO_LEVEL_TERSE)
22140         context_die = NULL;
22141       break;
22142
22143     case VAR_DECL:
22144       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
22145          declaration and if the declaration was never even referenced from
22146          within this entire compilation unit.  We suppress these DIEs in
22147          order to save space in the .debug section (by eliminating entries
22148          which are probably useless).  Note that we must not suppress
22149          block-local extern declarations (whether used or not) because that
22150          would screw-up the debugger's name lookup mechanism and cause it to
22151          miss things which really ought to be in scope at a given point.  */
22152       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
22153         return;
22154
22155       /* For local statics lookup proper context die.  */
22156       if (TREE_STATIC (decl) && decl_function_context (decl))
22157         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22158
22159       /* If we are in terse mode, don't generate any DIEs to represent any
22160          variable declarations or definitions.  */
22161       if (debug_info_level <= DINFO_LEVEL_TERSE)
22162         return;
22163       break;
22164
22165     case CONST_DECL:
22166       if (debug_info_level <= DINFO_LEVEL_TERSE)
22167         return;
22168       if (!is_fortran () && !is_ada ())
22169         return;
22170       if (TREE_STATIC (decl) && decl_function_context (decl))
22171         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22172       break;
22173
22174     case NAMESPACE_DECL:
22175     case IMPORTED_DECL:
22176       if (debug_info_level <= DINFO_LEVEL_TERSE)
22177         return;
22178       if (lookup_decl_die (decl) != NULL)
22179         return;
22180       break;
22181
22182     case TYPE_DECL:
22183       /* Don't emit stubs for types unless they are needed by other DIEs.  */
22184       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
22185         return;
22186
22187       /* Don't bother trying to generate any DIEs to represent any of the
22188          normal built-in types for the language we are compiling.  */
22189       if (DECL_IS_BUILTIN (decl))
22190         return;
22191
22192       /* If we are in terse mode, don't generate any DIEs for types.  */
22193       if (debug_info_level <= DINFO_LEVEL_TERSE)
22194         return;
22195
22196       /* If we're a function-scope tag, initially use a parent of NULL;
22197          this will be fixed up in decls_for_scope.  */
22198       if (decl_function_context (decl))
22199         context_die = NULL;
22200
22201       break;
22202
22203     default:
22204       return;
22205     }
22206
22207   gen_decl_die (decl, NULL, context_die);
22208 }
22209
22210 /* Write the debugging output for DECL.  */
22211
22212 static void
22213 dwarf2out_function_decl (tree decl)
22214 {
22215   dwarf2out_decl (decl);
22216   call_arg_locations = NULL;
22217   call_arg_loc_last = NULL;
22218   call_site_count = -1;
22219   tail_call_site_count = -1;
22220   VEC_free (dw_die_ref, heap, block_map);
22221   htab_empty (decl_loc_table);
22222   htab_empty (cached_dw_loc_list_table);
22223 }
22224
22225 /* Output a marker (i.e. a label) for the beginning of the generated code for
22226    a lexical block.  */
22227
22228 static void
22229 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
22230                        unsigned int blocknum)
22231 {
22232   switch_to_section (current_function_section ());
22233   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
22234 }
22235
22236 /* Output a marker (i.e. a label) for the end of the generated code for a
22237    lexical block.  */
22238
22239 static void
22240 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22241 {
22242   switch_to_section (current_function_section ());
22243   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22244 }
22245
22246 /* Returns nonzero if it is appropriate not to emit any debugging
22247    information for BLOCK, because it doesn't contain any instructions.
22248
22249    Don't allow this for blocks with nested functions or local classes
22250    as we would end up with orphans, and in the presence of scheduling
22251    we may end up calling them anyway.  */
22252
22253 static bool
22254 dwarf2out_ignore_block (const_tree block)
22255 {
22256   tree decl;
22257   unsigned int i;
22258
22259   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22260     if (TREE_CODE (decl) == FUNCTION_DECL
22261         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22262       return 0;
22263   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22264     {
22265       decl = BLOCK_NONLOCALIZED_VAR (block, i);
22266       if (TREE_CODE (decl) == FUNCTION_DECL
22267           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22268       return 0;
22269     }
22270
22271   return 1;
22272 }
22273
22274 /* Hash table routines for file_hash.  */
22275
22276 static int
22277 file_table_eq (const void *p1_p, const void *p2_p)
22278 {
22279   const struct dwarf_file_data *const p1 =
22280     (const struct dwarf_file_data *) p1_p;
22281   const char *const p2 = (const char *) p2_p;
22282   return filename_cmp (p1->filename, p2) == 0;
22283 }
22284
22285 static hashval_t
22286 file_table_hash (const void *p_p)
22287 {
22288   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
22289   return htab_hash_string (p->filename);
22290 }
22291
22292 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22293    dwarf2out.c) and return its "index".  The index of each (known) filename is
22294    just a unique number which is associated with only that one filename.  We
22295    need such numbers for the sake of generating labels (in the .debug_sfnames
22296    section) and references to those files numbers (in the .debug_srcinfo
22297    and.debug_macinfo sections).  If the filename given as an argument is not
22298    found in our current list, add it to the list and assign it the next
22299    available unique index number.  In order to speed up searches, we remember
22300    the index of the filename was looked up last.  This handles the majority of
22301    all searches.  */
22302
22303 static struct dwarf_file_data *
22304 lookup_filename (const char *file_name)
22305 {
22306   void ** slot;
22307   struct dwarf_file_data * created;
22308
22309   /* Check to see if the file name that was searched on the previous
22310      call matches this file name.  If so, return the index.  */
22311   if (file_table_last_lookup
22312       && (file_name == file_table_last_lookup->filename
22313           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
22314     return file_table_last_lookup;
22315
22316   /* Didn't match the previous lookup, search the table.  */
22317   slot = htab_find_slot_with_hash (file_table, file_name,
22318                                    htab_hash_string (file_name), INSERT);
22319   if (*slot)
22320     return (struct dwarf_file_data *) *slot;
22321
22322   created = ggc_alloc_dwarf_file_data ();
22323   created->filename = file_name;
22324   created->emitted_number = 0;
22325   *slot = created;
22326   return created;
22327 }
22328
22329 /* If the assembler will construct the file table, then translate the compiler
22330    internal file table number into the assembler file table number, and emit
22331    a .file directive if we haven't already emitted one yet.  The file table
22332    numbers are different because we prune debug info for unused variables and
22333    types, which may include filenames.  */
22334
22335 static int
22336 maybe_emit_file (struct dwarf_file_data * fd)
22337 {
22338   if (! fd->emitted_number)
22339     {
22340       if (last_emitted_file)
22341         fd->emitted_number = last_emitted_file->emitted_number + 1;
22342       else
22343         fd->emitted_number = 1;
22344       last_emitted_file = fd;
22345
22346       if (DWARF2_ASM_LINE_DEBUG_INFO)
22347         {
22348           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22349           output_quoted_string (asm_out_file,
22350                                 remap_debug_filename (fd->filename));
22351           fputc ('\n', asm_out_file);
22352         }
22353     }
22354
22355   return fd->emitted_number;
22356 }
22357
22358 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22359    That generation should happen after function debug info has been
22360    generated. The value of the attribute is the constant value of ARG.  */
22361
22362 static void
22363 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22364 {
22365   die_arg_entry entry;
22366
22367   if (!die || !arg)
22368     return;
22369
22370   if (!tmpl_value_parm_die_table)
22371     tmpl_value_parm_die_table
22372       = VEC_alloc (die_arg_entry, gc, 32);
22373
22374   entry.die = die;
22375   entry.arg = arg;
22376   VEC_safe_push (die_arg_entry, gc,
22377                  tmpl_value_parm_die_table,
22378                  &entry);
22379 }
22380
22381 /* Return TRUE if T is an instance of generic type, FALSE
22382    otherwise.  */
22383
22384 static bool
22385 generic_type_p (tree t)
22386 {
22387   if (t == NULL_TREE || !TYPE_P (t))
22388     return false;
22389   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22390 }
22391
22392 /* Schedule the generation of the generic parameter dies for the
22393   instance of generic type T. The proper generation itself is later
22394   done by gen_scheduled_generic_parms_dies. */
22395
22396 static void
22397 schedule_generic_params_dies_gen (tree t)
22398 {
22399   if (!generic_type_p (t))
22400     return;
22401
22402   if (generic_type_instances == NULL)
22403     generic_type_instances = VEC_alloc (tree, gc, 256);
22404
22405   VEC_safe_push (tree, gc, generic_type_instances, t);
22406 }
22407
22408 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22409    by append_entry_to_tmpl_value_parm_die_table. This function must
22410    be called after function DIEs have been generated.  */
22411
22412 static void
22413 gen_remaining_tmpl_value_param_die_attribute (void)
22414 {
22415   if (tmpl_value_parm_die_table)
22416     {
22417       unsigned i;
22418       die_arg_entry *e;
22419
22420       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
22421         tree_add_const_value_attribute (e->die, e->arg);
22422     }
22423 }
22424
22425 /* Generate generic parameters DIEs for instances of generic types
22426    that have been previously scheduled by
22427    schedule_generic_params_dies_gen. This function must be called
22428    after all the types of the CU have been laid out.  */
22429
22430 static void
22431 gen_scheduled_generic_parms_dies (void)
22432 {
22433   unsigned i;
22434   tree t;
22435
22436   if (generic_type_instances == NULL)
22437     return;
22438   
22439   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
22440     gen_generic_params_dies (t);
22441 }
22442
22443
22444 /* Replace DW_AT_name for the decl with name.  */
22445
22446 static void
22447 dwarf2out_set_name (tree decl, tree name)
22448 {
22449   dw_die_ref die;
22450   dw_attr_ref attr;
22451   const char *dname;
22452
22453   die = TYPE_SYMTAB_DIE (decl);
22454   if (!die)
22455     return;
22456
22457   dname = dwarf2_name (name, 0);
22458   if (!dname)
22459     return;
22460
22461   attr = get_AT (die, DW_AT_name);
22462   if (attr)
22463     {
22464       struct indirect_string_node *node;
22465
22466       node = find_AT_string (dname);
22467       /* replace the string.  */
22468       attr->dw_attr_val.v.val_str = node;
22469     }
22470
22471   else
22472     add_name_attribute (die, dname);
22473 }
22474
22475 /* Called by the final INSN scan whenever we see a var location.  We
22476    use it to drop labels in the right places, and throw the location in
22477    our lookup table.  */
22478
22479 static void
22480 dwarf2out_var_location (rtx loc_note)
22481 {
22482   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22483   struct var_loc_node *newloc;
22484   rtx next_real;
22485   static const char *last_label;
22486   static const char *last_postcall_label;
22487   static bool last_in_cold_section_p;
22488   tree decl;
22489   bool var_loc_p;
22490
22491   if (!NOTE_P (loc_note))
22492     {
22493       if (CALL_P (loc_note))
22494         {
22495           call_site_count++;
22496           if (SIBLING_CALL_P (loc_note))
22497             tail_call_site_count++;
22498         }
22499       return;
22500     }
22501
22502   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22503   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22504     return;
22505
22506   next_real = next_real_insn (loc_note);
22507
22508   /* If there are no instructions which would be affected by this note,
22509      don't do anything.  */
22510   if (var_loc_p
22511       && next_real == NULL_RTX
22512       && !NOTE_DURING_CALL_P (loc_note))
22513     return;
22514
22515   if (next_real == NULL_RTX)
22516     next_real = get_last_insn ();
22517
22518   /* If there were any real insns between note we processed last time
22519      and this note (or if it is the first note), clear
22520      last_{,postcall_}label so that they are not reused this time.  */
22521   if (last_var_location_insn == NULL_RTX
22522       || last_var_location_insn != next_real
22523       || last_in_cold_section_p != in_cold_section_p)
22524     {
22525       last_label = NULL;
22526       last_postcall_label = NULL;
22527     }
22528
22529   if (var_loc_p)
22530     {
22531       decl = NOTE_VAR_LOCATION_DECL (loc_note);
22532       newloc = add_var_loc_to_decl (decl, loc_note,
22533                                     NOTE_DURING_CALL_P (loc_note)
22534                                     ? last_postcall_label : last_label);
22535       if (newloc == NULL)
22536         return;
22537     }
22538   else
22539     {
22540       decl = NULL_TREE;
22541       newloc = NULL;
22542     }
22543
22544   /* If there were no real insns between note we processed last time
22545      and this note, use the label we emitted last time.  Otherwise
22546      create a new label and emit it.  */
22547   if (last_label == NULL)
22548     {
22549       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22550       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22551       loclabel_num++;
22552       last_label = ggc_strdup (loclabel);
22553     }
22554
22555   if (!var_loc_p)
22556     {
22557       struct call_arg_loc_node *ca_loc
22558         = ggc_alloc_cleared_call_arg_loc_node ();
22559       rtx prev = prev_real_insn (loc_note), x;
22560       ca_loc->call_arg_loc_note = loc_note;
22561       ca_loc->next = NULL;
22562       ca_loc->label = last_label;
22563       gcc_assert (prev
22564                   && (CALL_P (prev)
22565                       || (NONJUMP_INSN_P (prev)
22566                           && GET_CODE (PATTERN (prev)) == SEQUENCE
22567                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22568       if (!CALL_P (prev))
22569         prev = XVECEXP (PATTERN (prev), 0, 0);
22570       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22571       x = PATTERN (prev);
22572       if (GET_CODE (x) == PARALLEL)
22573         x = XVECEXP (x, 0, 0);
22574       if (GET_CODE (x) == SET)
22575         x = SET_SRC (x);
22576       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22577         {
22578           x = XEXP (XEXP (x, 0), 0);
22579           if (GET_CODE (x) == SYMBOL_REF
22580               && SYMBOL_REF_DECL (x)
22581               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22582             ca_loc->symbol_ref = x;
22583         }
22584       ca_loc->block = insn_scope (prev);
22585       if (call_arg_locations)
22586         call_arg_loc_last->next = ca_loc;
22587       else
22588         call_arg_locations = ca_loc;
22589       call_arg_loc_last = ca_loc;
22590     }
22591   else if (!NOTE_DURING_CALL_P (loc_note))
22592     newloc->label = last_label;
22593   else
22594     {
22595       if (!last_postcall_label)
22596         {
22597           sprintf (loclabel, "%s-1", last_label);
22598           last_postcall_label = ggc_strdup (loclabel);
22599         }
22600       newloc->label = last_postcall_label;
22601     }
22602
22603   last_var_location_insn = next_real;
22604   last_in_cold_section_p = in_cold_section_p;
22605 }
22606
22607 /* Note in one location list that text section has changed.  */
22608
22609 static int
22610 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
22611 {
22612   var_loc_list *list = (var_loc_list *) *slot;
22613   if (list->first)
22614     list->last_before_switch
22615       = list->last->next ? list->last->next : list->last;
22616   return 1;
22617 }
22618
22619 /* Note in all location lists that text section has changed.  */
22620
22621 static void
22622 var_location_switch_text_section (void)
22623 {
22624   if (decl_loc_table == NULL)
22625     return;
22626
22627   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
22628 }
22629
22630 /* Create a new line number table.  */
22631
22632 static dw_line_info_table *
22633 new_line_info_table (void)
22634 {
22635   dw_line_info_table *table;
22636
22637   table = ggc_alloc_cleared_dw_line_info_table_struct ();
22638   table->file_num = 1;
22639   table->line_num = 1;
22640   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22641
22642   return table;
22643 }
22644
22645 /* Lookup the "current" table into which we emit line info, so
22646    that we don't have to do it for every source line.  */
22647
22648 static void
22649 set_cur_line_info_table (section *sec)
22650 {
22651   dw_line_info_table *table;
22652
22653   if (sec == text_section)
22654     table = text_section_line_info;
22655   else if (sec == cold_text_section)
22656     {
22657       table = cold_text_section_line_info;
22658       if (!table)
22659         {
22660           cold_text_section_line_info = table = new_line_info_table ();
22661           table->end_label = cold_end_label;
22662         }
22663     }
22664   else
22665     {
22666       const char *end_label;
22667
22668       if (flag_reorder_blocks_and_partition)
22669         {
22670           if (in_cold_section_p)
22671             end_label = crtl->subsections.cold_section_end_label;
22672           else
22673             end_label = crtl->subsections.hot_section_end_label;
22674         }
22675       else
22676         {
22677           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22678           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22679                                        current_function_funcdef_no);
22680           end_label = ggc_strdup (label);
22681         }
22682
22683       table = new_line_info_table ();
22684       table->end_label = end_label;
22685
22686       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
22687     }
22688
22689   cur_line_info_table = table;
22690 }
22691
22692
22693 /* We need to reset the locations at the beginning of each
22694    function. We can't do this in the end_function hook, because the
22695    declarations that use the locations won't have been output when
22696    that hook is called.  Also compute have_multiple_function_sections here.  */
22697
22698 static void
22699 dwarf2out_begin_function (tree fun)
22700 {
22701   section *sec = function_section (fun);
22702
22703   if (sec != text_section)
22704     have_multiple_function_sections = true;
22705
22706   if (flag_reorder_blocks_and_partition && !cold_text_section)
22707     {
22708       gcc_assert (current_function_decl == fun);
22709       cold_text_section = unlikely_text_section ();
22710       switch_to_section (cold_text_section);
22711       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22712       switch_to_section (sec);
22713     }
22714
22715   dwarf2out_note_section_used ();
22716   call_site_count = 0;
22717   tail_call_site_count = 0;
22718
22719   set_cur_line_info_table (sec);
22720 }
22721
22722 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22723
22724 static void
22725 push_dw_line_info_entry (dw_line_info_table *table,
22726                          enum dw_line_info_opcode opcode, unsigned int val)
22727 {
22728   dw_line_info_entry e;
22729   e.opcode = opcode;
22730   e.val = val;
22731   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
22732 }
22733
22734 /* Output a label to mark the beginning of a source code line entry
22735    and record information relating to this source line, in
22736    'line_info_table' for later output of the .debug_line section.  */
22737 /* ??? The discriminator parameter ought to be unsigned.  */
22738
22739 static void
22740 dwarf2out_source_line (unsigned int line, const char *filename,
22741                        int discriminator, bool is_stmt)
22742 {
22743   unsigned int file_num;
22744   dw_line_info_table *table;
22745
22746   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
22747     return;
22748
22749   /* The discriminator column was added in dwarf4.  Simplify the below
22750      by simply removing it if we're not supposed to output it.  */
22751   if (dwarf_version < 4 && dwarf_strict)
22752     discriminator = 0;
22753
22754   table = cur_line_info_table;
22755   file_num = maybe_emit_file (lookup_filename (filename));
22756
22757   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22758      the debugger has used the second (possibly duplicate) line number
22759      at the beginning of the function to mark the end of the prologue.
22760      We could eliminate any other duplicates within the function.  For
22761      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22762      that second line number entry.  */
22763   /* Recall that this end-of-prologue indication is *not* the same thing
22764      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22765      to which the hook corresponds, follows the last insn that was 
22766      emitted by gen_prologue.  What we need is to preceed the first insn
22767      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22768      insn that corresponds to something the user wrote.  These may be
22769      very different locations once scheduling is enabled.  */
22770
22771   if (0 && file_num == table->file_num
22772       && line == table->line_num
22773       && discriminator == table->discrim_num
22774       && is_stmt == table->is_stmt)
22775     return;
22776
22777   switch_to_section (current_function_section ());
22778
22779   /* If requested, emit something human-readable.  */
22780   if (flag_debug_asm)
22781     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22782
22783   if (DWARF2_ASM_LINE_DEBUG_INFO)
22784     {
22785       /* Emit the .loc directive understood by GNU as.  */
22786       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22787       if (is_stmt != table->is_stmt)
22788         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22789       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22790         fprintf (asm_out_file, " discriminator %d", discriminator);
22791       fputc ('\n', asm_out_file);
22792     }
22793   else
22794     {
22795       unsigned int label_num = ++line_info_label_num;
22796
22797       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22798
22799       push_dw_line_info_entry (table, LI_set_address, label_num);
22800       if (file_num != table->file_num)
22801         push_dw_line_info_entry (table, LI_set_file, file_num);
22802       if (discriminator != table->discrim_num)
22803         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22804       if (is_stmt != table->is_stmt)
22805         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22806       push_dw_line_info_entry (table, LI_set_line, line);
22807     }
22808
22809   table->file_num = file_num;
22810   table->line_num = line;
22811   table->discrim_num = discriminator;
22812   table->is_stmt = is_stmt;
22813   table->in_use = true;
22814 }
22815
22816 /* Record the beginning of a new source file.  */
22817
22818 static void
22819 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22820 {
22821   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22822     {
22823       /* Record the beginning of the file for break_out_includes.  */
22824       dw_die_ref bincl_die;
22825
22826       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22827       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22828     }
22829
22830   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22831     {
22832       macinfo_entry e;
22833       e.code = DW_MACINFO_start_file;
22834       e.lineno = lineno;
22835       e.info = xstrdup (filename);
22836       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22837     }
22838 }
22839
22840 /* Record the end of a source file.  */
22841
22842 static void
22843 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22844 {
22845   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22846     /* Record the end of the file for break_out_includes.  */
22847     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22848
22849   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22850     {
22851       macinfo_entry e;
22852       e.code = DW_MACINFO_end_file;
22853       e.lineno = lineno;
22854       e.info = NULL;
22855       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22856     }
22857 }
22858
22859 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22860    the tail part of the directive line, i.e. the part which is past the
22861    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22862
22863 static void
22864 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22865                   const char *buffer ATTRIBUTE_UNUSED)
22866 {
22867   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22868     {
22869       macinfo_entry e;
22870       e.code = DW_MACINFO_define;
22871       e.lineno = lineno;
22872       e.info = xstrdup (buffer);;
22873       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22874     }
22875 }
22876
22877 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22878    the tail part of the directive line, i.e. the part which is past the
22879    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22880
22881 static void
22882 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22883                  const char *buffer ATTRIBUTE_UNUSED)
22884 {
22885   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22886     {
22887       macinfo_entry e;
22888       e.code = DW_MACINFO_undef;
22889       e.lineno = lineno;
22890       e.info = xstrdup (buffer);;
22891       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22892     }
22893 }
22894
22895 static void
22896 output_macinfo (void)
22897 {
22898   unsigned i;
22899   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22900   macinfo_entry *ref;
22901
22902   if (! length)
22903     return;
22904
22905   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22906     {
22907       switch (ref->code)
22908         {
22909           case DW_MACINFO_start_file:
22910             {
22911               int file_num = maybe_emit_file (lookup_filename (ref->info));
22912               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22913               dw2_asm_output_data_uleb128 
22914                         (ref->lineno, "Included from line number %lu", 
22915                                                 (unsigned long)ref->lineno);
22916               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22917             }
22918             break;
22919           case DW_MACINFO_end_file:
22920             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22921             break;
22922           case DW_MACINFO_define:
22923             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22924             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22925                                                 (unsigned long)ref->lineno);
22926             dw2_asm_output_nstring (ref->info, -1, "The macro");
22927             break;
22928           case DW_MACINFO_undef:
22929             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22930             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22931                                                 (unsigned long)ref->lineno);
22932             dw2_asm_output_nstring (ref->info, -1, "The macro");
22933             break;
22934           default:
22935            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22936              ASM_COMMENT_START, (unsigned long)ref->code);
22937           break;
22938         }
22939     }
22940 }
22941
22942 /* Set up for Dwarf output at the start of compilation.  */
22943
22944 static void
22945 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22946 {
22947   /* Allocate the file_table.  */
22948   file_table = htab_create_ggc (50, file_table_hash,
22949                                 file_table_eq, NULL);
22950
22951   /* Allocate the decl_die_table.  */
22952   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22953                                     decl_die_table_eq, NULL);
22954
22955   /* Allocate the decl_loc_table.  */
22956   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22957                                     decl_loc_table_eq, NULL);
22958
22959   /* Allocate the cached_dw_loc_list_table.  */
22960   cached_dw_loc_list_table
22961     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22962                        cached_dw_loc_list_table_eq, NULL);
22963
22964   /* Allocate the initial hunk of the decl_scope_table.  */
22965   decl_scope_table = VEC_alloc (tree, gc, 256);
22966
22967   /* Allocate the initial hunk of the abbrev_die_table.  */
22968   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22969     (ABBREV_DIE_TABLE_INCREMENT);
22970   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22971   /* Zero-th entry is allocated, but unused.  */
22972   abbrev_die_table_in_use = 1;
22973
22974   /* Allocate the pubtypes and pubnames vectors.  */
22975   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22976   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22977
22978   incomplete_types = VEC_alloc (tree, gc, 64);
22979
22980   used_rtx_array = VEC_alloc (rtx, gc, 32);
22981
22982   debug_info_section = get_section (DEBUG_INFO_SECTION,
22983                                     SECTION_DEBUG, NULL);
22984   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22985                                       SECTION_DEBUG, NULL);
22986   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22987                                        SECTION_DEBUG, NULL);
22988   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22989                                        SECTION_DEBUG, NULL);
22990   debug_line_section = get_section (DEBUG_LINE_SECTION,
22991                                     SECTION_DEBUG, NULL);
22992   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22993                                    SECTION_DEBUG, NULL);
22994   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22995                                         SECTION_DEBUG, NULL);
22996   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22997                                         SECTION_DEBUG, NULL);
22998   debug_str_section = get_section (DEBUG_STR_SECTION,
22999                                    DEBUG_STR_SECTION_FLAGS, NULL);
23000   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23001                                       SECTION_DEBUG, NULL);
23002   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23003                                      SECTION_DEBUG, NULL);
23004
23005   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23006   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23007                                DEBUG_ABBREV_SECTION_LABEL, 0);
23008   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23009   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23010                                COLD_TEXT_SECTION_LABEL, 0);
23011   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23012
23013   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23014                                DEBUG_INFO_SECTION_LABEL, 0);
23015   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23016                                DEBUG_LINE_SECTION_LABEL, 0);
23017   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23018                                DEBUG_RANGES_SECTION_LABEL, 0);
23019   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23020                                DEBUG_MACINFO_SECTION_LABEL, 0);
23021
23022   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23023     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
23024
23025   switch_to_section (text_section);
23026   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23027
23028   /* Make sure the line number table for .text always exists.  */
23029   text_section_line_info = new_line_info_table ();
23030   text_section_line_info->end_label = text_end_label;
23031 }
23032
23033 /* Called before cgraph_optimize starts outputtting functions, variables
23034    and toplevel asms into assembly.  */
23035
23036 static void
23037 dwarf2out_assembly_start (void)
23038 {
23039   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23040       && dwarf2out_do_cfi_asm ()
23041       && (!(flag_unwind_tables || flag_exceptions)
23042           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
23043     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23044 }
23045
23046 /* A helper function for dwarf2out_finish called through
23047    htab_traverse.  Emit one queued .debug_str string.  */
23048
23049 static int
23050 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
23051 {
23052   struct indirect_string_node *node = (struct indirect_string_node *) *h;
23053
23054   if (node->form == DW_FORM_strp)
23055     {
23056       switch_to_section (debug_str_section);
23057       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23058       assemble_string (node->str, strlen (node->str) + 1);
23059     }
23060
23061   return 1;
23062 }
23063
23064 #if ENABLE_ASSERT_CHECKING
23065 /* Verify that all marks are clear.  */
23066
23067 static void
23068 verify_marks_clear (dw_die_ref die)
23069 {
23070   dw_die_ref c;
23071
23072   gcc_assert (! die->die_mark);
23073   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23074 }
23075 #endif /* ENABLE_ASSERT_CHECKING */
23076
23077 /* Clear the marks for a die and its children.
23078    Be cool if the mark isn't set.  */
23079
23080 static void
23081 prune_unmark_dies (dw_die_ref die)
23082 {
23083   dw_die_ref c;
23084
23085   if (die->die_mark)
23086     die->die_mark = 0;
23087   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23088 }
23089
23090 /* Given DIE that we're marking as used, find any other dies
23091    it references as attributes and mark them as used.  */
23092
23093 static void
23094 prune_unused_types_walk_attribs (dw_die_ref die)
23095 {
23096   dw_attr_ref a;
23097   unsigned ix;
23098
23099   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23100     {
23101       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23102         {
23103           /* A reference to another DIE.
23104              Make sure that it will get emitted.
23105              If it was broken out into a comdat group, don't follow it.  */
23106           if (! use_debug_types
23107               || a->dw_attr == DW_AT_specification
23108               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
23109             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23110         }
23111       /* Set the string's refcount to 0 so that prune_unused_types_mark
23112          accounts properly for it.  */
23113       if (AT_class (a) == dw_val_class_str)
23114         a->dw_attr_val.v.val_str->refcount = 0;
23115     }
23116 }
23117
23118 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23119
23120 static void
23121 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23122 {
23123   dw_die_ref c;
23124
23125   if (die == NULL || die->die_child == NULL)
23126     return;
23127   c = die->die_child;
23128   do
23129     {
23130       switch (c->die_tag)
23131         {
23132         case DW_TAG_template_type_param:
23133         case DW_TAG_template_value_param:
23134         case DW_TAG_GNU_template_template_param:
23135         case DW_TAG_GNU_template_parameter_pack:
23136           prune_unused_types_mark (c, 1);
23137           break;
23138         default:
23139           break;
23140         }
23141       c = c->die_sib;
23142     } while (c && c != die->die_child);
23143 }
23144
23145 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23146    to DIE's children.  */
23147
23148 static void
23149 prune_unused_types_mark (dw_die_ref die, int dokids)
23150 {
23151   dw_die_ref c;
23152
23153   if (die->die_mark == 0)
23154     {
23155       /* We haven't done this node yet.  Mark it as used.  */
23156       die->die_mark = 1;
23157       /* If this is the DIE of a generic type instantiation,
23158          mark the children DIEs that describe its generic parms and
23159          args.  */
23160       prune_unused_types_mark_generic_parms_dies (die);
23161
23162       /* We also have to mark its parents as used.
23163          (But we don't want to mark our parents' kids due to this.)  */
23164       if (die->die_parent)
23165         prune_unused_types_mark (die->die_parent, 0);
23166
23167       /* Mark any referenced nodes.  */
23168       prune_unused_types_walk_attribs (die);
23169
23170       /* If this node is a specification,
23171          also mark the definition, if it exists.  */
23172       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23173         prune_unused_types_mark (die->die_definition, 1);
23174     }
23175
23176   if (dokids && die->die_mark != 2)
23177     {
23178       /* We need to walk the children, but haven't done so yet.
23179          Remember that we've walked the kids.  */
23180       die->die_mark = 2;
23181
23182       /* If this is an array type, we need to make sure our
23183          kids get marked, even if they're types.  If we're
23184          breaking out types into comdat sections, do this
23185          for all type definitions.  */
23186       if (die->die_tag == DW_TAG_array_type
23187           || (use_debug_types
23188               && is_type_die (die) && ! is_declaration_die (die)))
23189         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23190       else
23191         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23192     }
23193 }
23194
23195 /* For local classes, look if any static member functions were emitted
23196    and if so, mark them.  */
23197
23198 static void
23199 prune_unused_types_walk_local_classes (dw_die_ref die)
23200 {
23201   dw_die_ref c;
23202
23203   if (die->die_mark == 2)
23204     return;
23205
23206   switch (die->die_tag)
23207     {
23208     case DW_TAG_structure_type:
23209     case DW_TAG_union_type:
23210     case DW_TAG_class_type:
23211       break;
23212
23213     case DW_TAG_subprogram:
23214       if (!get_AT_flag (die, DW_AT_declaration)
23215           || die->die_definition != NULL)
23216         prune_unused_types_mark (die, 1);
23217       return;
23218
23219     default:
23220       return;
23221     }
23222
23223   /* Mark children.  */
23224   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23225 }
23226
23227 /* Walk the tree DIE and mark types that we actually use.  */
23228
23229 static void
23230 prune_unused_types_walk (dw_die_ref die)
23231 {
23232   dw_die_ref c;
23233
23234   /* Don't do anything if this node is already marked and
23235      children have been marked as well.  */
23236   if (die->die_mark == 2)
23237     return;
23238
23239   switch (die->die_tag)
23240     {
23241     case DW_TAG_structure_type:
23242     case DW_TAG_union_type:
23243     case DW_TAG_class_type:
23244       if (die->die_perennial_p)
23245         break;
23246
23247       for (c = die->die_parent; c; c = c->die_parent)
23248         if (c->die_tag == DW_TAG_subprogram)
23249           break;
23250
23251       /* Finding used static member functions inside of classes
23252          is needed just for local classes, because for other classes
23253          static member function DIEs with DW_AT_specification
23254          are emitted outside of the DW_TAG_*_type.  If we ever change
23255          it, we'd need to call this even for non-local classes.  */
23256       if (c)
23257         prune_unused_types_walk_local_classes (die);
23258
23259       /* It's a type node --- don't mark it.  */
23260       return;
23261
23262     case DW_TAG_const_type:
23263     case DW_TAG_packed_type:
23264     case DW_TAG_pointer_type:
23265     case DW_TAG_reference_type:
23266     case DW_TAG_rvalue_reference_type:
23267     case DW_TAG_volatile_type:
23268     case DW_TAG_typedef:
23269     case DW_TAG_array_type:
23270     case DW_TAG_interface_type:
23271     case DW_TAG_friend:
23272     case DW_TAG_variant_part:
23273     case DW_TAG_enumeration_type:
23274     case DW_TAG_subroutine_type:
23275     case DW_TAG_string_type:
23276     case DW_TAG_set_type:
23277     case DW_TAG_subrange_type:
23278     case DW_TAG_ptr_to_member_type:
23279     case DW_TAG_file_type:
23280       if (die->die_perennial_p)
23281         break;
23282
23283       /* It's a type node --- don't mark it.  */
23284       return;
23285
23286     default:
23287       /* Mark everything else.  */
23288       break;
23289   }
23290
23291   if (die->die_mark == 0)
23292     {
23293       die->die_mark = 1;
23294
23295       /* Now, mark any dies referenced from here.  */
23296       prune_unused_types_walk_attribs (die);
23297     }
23298
23299   die->die_mark = 2;
23300
23301   /* Mark children.  */
23302   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23303 }
23304
23305 /* Increment the string counts on strings referred to from DIE's
23306    attributes.  */
23307
23308 static void
23309 prune_unused_types_update_strings (dw_die_ref die)
23310 {
23311   dw_attr_ref a;
23312   unsigned ix;
23313
23314   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23315     if (AT_class (a) == dw_val_class_str)
23316       {
23317         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23318         s->refcount++;
23319         /* Avoid unnecessarily putting strings that are used less than
23320            twice in the hash table.  */
23321         if (s->refcount
23322             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23323           {
23324             void ** slot;
23325             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
23326                                              htab_hash_string (s->str),
23327                                              INSERT);
23328             gcc_assert (*slot == NULL);
23329             *slot = s;
23330           }
23331       }
23332 }
23333
23334 /* Remove from the tree DIE any dies that aren't marked.  */
23335
23336 static void
23337 prune_unused_types_prune (dw_die_ref die)
23338 {
23339   dw_die_ref c;
23340
23341   gcc_assert (die->die_mark);
23342   prune_unused_types_update_strings (die);
23343
23344   if (! die->die_child)
23345     return;
23346
23347   c = die->die_child;
23348   do {
23349     dw_die_ref prev = c;
23350     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23351       if (c == die->die_child)
23352         {
23353           /* No marked children between 'prev' and the end of the list.  */
23354           if (prev == c)
23355             /* No marked children at all.  */
23356             die->die_child = NULL;
23357           else
23358             {
23359               prev->die_sib = c->die_sib;
23360               die->die_child = prev;
23361             }
23362           return;
23363         }
23364
23365     if (c != prev->die_sib)
23366       prev->die_sib = c;
23367     prune_unused_types_prune (c);
23368   } while (c != die->die_child);
23369 }
23370
23371 /* Remove dies representing declarations that we never use.  */
23372
23373 static void
23374 prune_unused_types (void)
23375 {
23376   unsigned int i;
23377   limbo_die_node *node;
23378   comdat_type_node *ctnode;
23379   pubname_ref pub;
23380   dw_die_ref base_type;
23381
23382 #if ENABLE_ASSERT_CHECKING
23383   /* All the marks should already be clear.  */
23384   verify_marks_clear (comp_unit_die ());
23385   for (node = limbo_die_list; node; node = node->next)
23386     verify_marks_clear (node->die);
23387   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23388     verify_marks_clear (ctnode->root_die);
23389 #endif /* ENABLE_ASSERT_CHECKING */
23390
23391   /* Mark types that are used in global variables.  */
23392   premark_types_used_by_global_vars ();
23393
23394   /* Set the mark on nodes that are actually used.  */
23395   prune_unused_types_walk (comp_unit_die ());
23396   for (node = limbo_die_list; node; node = node->next)
23397     prune_unused_types_walk (node->die);
23398   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23399     {
23400       prune_unused_types_walk (ctnode->root_die);
23401       prune_unused_types_mark (ctnode->type_die, 1);
23402     }
23403
23404   /* Also set the mark on nodes referenced from the
23405      pubname_table.  */
23406   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
23407     prune_unused_types_mark (pub->die, 1);
23408   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
23409     prune_unused_types_mark (base_type, 1);
23410
23411   if (debug_str_hash)
23412     htab_empty (debug_str_hash);
23413   prune_unused_types_prune (comp_unit_die ());
23414   for (node = limbo_die_list; node; node = node->next)
23415     prune_unused_types_prune (node->die);
23416   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23417     prune_unused_types_prune (ctnode->root_die);
23418
23419   /* Leave the marks clear.  */
23420   prune_unmark_dies (comp_unit_die ());
23421   for (node = limbo_die_list; node; node = node->next)
23422     prune_unmark_dies (node->die);
23423   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23424     prune_unmark_dies (ctnode->root_die);
23425 }
23426
23427 /* Set the parameter to true if there are any relative pathnames in
23428    the file table.  */
23429 static int
23430 file_table_relative_p (void ** slot, void *param)
23431 {
23432   bool *p = (bool *) param;
23433   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
23434   if (!IS_ABSOLUTE_PATH (d->filename))
23435     {
23436       *p = true;
23437       return 0;
23438     }
23439   return 1;
23440 }
23441
23442 /* Routines to manipulate hash table of comdat type units.  */
23443
23444 static hashval_t
23445 htab_ct_hash (const void *of)
23446 {
23447   hashval_t h;
23448   const comdat_type_node *const type_node = (const comdat_type_node *) of;
23449
23450   memcpy (&h, type_node->signature, sizeof (h));
23451   return h;
23452 }
23453
23454 static int
23455 htab_ct_eq (const void *of1, const void *of2)
23456 {
23457   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
23458   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
23459
23460   return (! memcmp (type_node_1->signature, type_node_2->signature,
23461                     DWARF_TYPE_SIGNATURE_SIZE));
23462 }
23463
23464 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23465    to the location it would have been added, should we know its
23466    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23467    probably improve compactness of debug info, removing equivalent
23468    abbrevs, and hide any differences caused by deferring the
23469    computation of the assembler name, triggered by e.g. PCH.  */
23470
23471 static inline void
23472 move_linkage_attr (dw_die_ref die)
23473 {
23474   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
23475   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
23476
23477   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23478               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23479
23480   while (--ix > 0)
23481     {
23482       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
23483
23484       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23485         break;
23486     }
23487
23488   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
23489     {
23490       VEC_pop (dw_attr_node, die->die_attr);
23491       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
23492     }
23493 }
23494
23495 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23496    referenced from typed stack ops and count how often they are used.  */
23497
23498 static void
23499 mark_base_types (dw_loc_descr_ref loc)
23500 {
23501   dw_die_ref base_type = NULL;
23502
23503   for (; loc; loc = loc->dw_loc_next)
23504     {
23505       switch (loc->dw_loc_opc)
23506         {
23507         case DW_OP_GNU_regval_type:
23508         case DW_OP_GNU_deref_type:
23509           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23510           break;
23511         case DW_OP_GNU_const_type:
23512         case DW_OP_GNU_convert:
23513         case DW_OP_GNU_reinterpret:
23514           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23515           break;
23516         case DW_OP_GNU_entry_value:
23517           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23518           continue;
23519         default:
23520           continue;
23521         }
23522       gcc_assert (base_type->die_parent == comp_unit_die ());
23523       if (base_type->die_mark)
23524         base_type->die_mark++;
23525       else
23526         {
23527           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
23528           base_type->die_mark = 1;
23529         }
23530     }
23531 }
23532
23533 /* Comparison function for sorting marked base types.  */
23534
23535 static int
23536 base_type_cmp (const void *x, const void *y)
23537 {
23538   dw_die_ref dx = *(const dw_die_ref *) x;
23539   dw_die_ref dy = *(const dw_die_ref *) y;
23540   unsigned int byte_size1, byte_size2;
23541   unsigned int encoding1, encoding2;
23542   if (dx->die_mark > dy->die_mark)
23543     return -1;
23544   if (dx->die_mark < dy->die_mark)
23545     return 1;
23546   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23547   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23548   if (byte_size1 < byte_size2)
23549     return 1;
23550   if (byte_size1 > byte_size2)
23551     return -1;
23552   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23553   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23554   if (encoding1 < encoding2)
23555     return 1;
23556   if (encoding1 > encoding2)
23557     return -1;
23558   return 0;
23559 }
23560
23561 /* Move base types marked by mark_base_types as early as possible
23562    in the CU, sorted by decreasing usage count both to make the
23563    uleb128 references as small as possible and to make sure they
23564    will have die_offset already computed by calc_die_sizes when
23565    sizes of typed stack loc ops is computed.  */
23566
23567 static void
23568 move_marked_base_types (void)
23569 {
23570   unsigned int i;
23571   dw_die_ref base_type, die, c;
23572
23573   if (VEC_empty (dw_die_ref, base_types))
23574     return;
23575
23576   /* Sort by decreasing usage count, they will be added again in that
23577      order later on.  */
23578   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
23579   die = comp_unit_die ();
23580   c = die->die_child;
23581   do
23582     {
23583       dw_die_ref prev = c;
23584       c = c->die_sib;
23585       while (c->die_mark)
23586         {
23587           remove_child_with_prev (c, prev);
23588           /* As base types got marked, there must be at least
23589              one node other than DW_TAG_base_type.  */
23590           gcc_assert (c != c->die_sib);
23591           c = c->die_sib;
23592         }
23593     }
23594   while (c != die->die_child);
23595   gcc_assert (die->die_child);
23596   c = die->die_child;
23597   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
23598     {
23599       base_type->die_mark = 0;
23600       base_type->die_sib = c->die_sib;
23601       c->die_sib = base_type;
23602       c = base_type;
23603     }
23604 }
23605
23606 /* Helper function for resolve_addr, attempt to resolve
23607    one CONST_STRING, return non-zero if not successful.  Similarly verify that
23608    SYMBOL_REFs refer to variables emitted in the current CU.  */
23609
23610 static int
23611 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
23612 {
23613   rtx rtl = *addr;
23614
23615   if (GET_CODE (rtl) == CONST_STRING)
23616     {
23617       size_t len = strlen (XSTR (rtl, 0)) + 1;
23618       tree t = build_string (len, XSTR (rtl, 0));
23619       tree tlen = size_int (len - 1);
23620       TREE_TYPE (t)
23621         = build_array_type (char_type_node, build_index_type (tlen));
23622       rtl = lookup_constant_def (t);
23623       if (!rtl || !MEM_P (rtl))
23624         return 1;
23625       rtl = XEXP (rtl, 0);
23626       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
23627       *addr = rtl;
23628       return 0;
23629     }
23630
23631   if (GET_CODE (rtl) == SYMBOL_REF
23632       && SYMBOL_REF_DECL (rtl))
23633     {
23634       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23635         {
23636           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23637             return 1;
23638         }
23639       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23640         return 1;
23641     }
23642
23643   if (GET_CODE (rtl) == CONST
23644       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23645     return 1;
23646
23647   return 0;
23648 }
23649
23650 /* Helper function for resolve_addr, handle one location
23651    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23652    the location list couldn't be resolved.  */
23653
23654 static bool
23655 resolve_addr_in_expr (dw_loc_descr_ref loc)
23656 {
23657   for (; loc; loc = loc->dw_loc_next)
23658     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
23659          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23660         || (loc->dw_loc_opc == DW_OP_implicit_value
23661             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23662             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
23663       return false;
23664     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
23665              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23666       {
23667         dw_die_ref ref
23668           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23669         if (ref == NULL)
23670           return false;
23671         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23672         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23673         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23674       }
23675   return true;
23676 }
23677
23678 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23679    an address in .rodata section if the string literal is emitted there,
23680    or remove the containing location list or replace DW_AT_const_value
23681    with DW_AT_location and empty location expression, if it isn't found
23682    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23683    to something that has been emitted in the current CU.  */
23684
23685 static void
23686 resolve_addr (dw_die_ref die)
23687 {
23688   dw_die_ref c;
23689   dw_attr_ref a;
23690   dw_loc_list_ref *curr, *start, loc;
23691   unsigned ix;
23692
23693   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23694     switch (AT_class (a))
23695       {
23696       case dw_val_class_loc_list:
23697         start = curr = AT_loc_list_ptr (a);
23698         loc = *curr;
23699         gcc_assert (loc);
23700         /* The same list can be referenced more than once.  See if we have
23701            already recorded the result from a previous pass.  */
23702         if (loc->replaced)
23703           *curr = loc->dw_loc_next;
23704         else if (!loc->resolved_addr)
23705           {
23706             /* As things stand, we do not expect or allow one die to
23707                reference a suffix of another die's location list chain.
23708                References must be identical or completely separate.
23709                There is therefore no need to cache the result of this
23710                pass on any list other than the first; doing so
23711                would lead to unnecessary writes.  */
23712             while (*curr)
23713               {
23714                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23715                 if (!resolve_addr_in_expr ((*curr)->expr))
23716                   {
23717                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23718                     if (next && (*curr)->ll_symbol)
23719                       {
23720                         gcc_assert (!next->ll_symbol);
23721                         next->ll_symbol = (*curr)->ll_symbol;
23722                       }
23723                     *curr = next;
23724                   }
23725                 else
23726                   {
23727                     mark_base_types ((*curr)->expr);
23728                     curr = &(*curr)->dw_loc_next;
23729                   }
23730               }
23731             if (loc == *start)
23732               loc->resolved_addr = 1;
23733             else
23734               {
23735                 loc->replaced = 1;
23736                 loc->dw_loc_next = *start;
23737               }
23738           }
23739         if (!*start)
23740           {
23741             remove_AT (die, a->dw_attr);
23742             ix--;
23743           }
23744         break;
23745       case dw_val_class_loc:
23746         if (!resolve_addr_in_expr (AT_loc (a)))
23747           {
23748             remove_AT (die, a->dw_attr);
23749             ix--;
23750           }
23751         else
23752           mark_base_types (AT_loc (a));
23753         break;
23754       case dw_val_class_addr:
23755         if (a->dw_attr == DW_AT_const_value
23756             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23757           {
23758             remove_AT (die, a->dw_attr);
23759             ix--;
23760           }
23761         if (die->die_tag == DW_TAG_GNU_call_site
23762             && a->dw_attr == DW_AT_abstract_origin)
23763           {
23764             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23765             dw_die_ref tdie = lookup_decl_die (tdecl);
23766             if (tdie == NULL
23767                 && DECL_EXTERNAL (tdecl)
23768                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23769               {
23770                 force_decl_die (tdecl);
23771                 tdie = lookup_decl_die (tdecl);
23772               }
23773             if (tdie)
23774               {
23775                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23776                 a->dw_attr_val.v.val_die_ref.die = tdie;
23777                 a->dw_attr_val.v.val_die_ref.external = 0;
23778               }
23779             else
23780               {
23781                 remove_AT (die, a->dw_attr);
23782                 ix--;
23783               }
23784           }
23785         break;
23786       default:
23787         break;
23788       }
23789
23790   FOR_EACH_CHILD (die, c, resolve_addr (c));
23791 }
23792 \f
23793 /* Helper routines for optimize_location_lists.
23794    This pass tries to share identical local lists in .debug_loc
23795    section.  */
23796
23797 /* Iteratively hash operands of LOC opcode.  */
23798
23799 static inline hashval_t
23800 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23801 {
23802   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23803   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23804
23805   switch (loc->dw_loc_opc)
23806     {
23807     case DW_OP_const4u:
23808     case DW_OP_const8u:
23809       if (loc->dtprel)
23810         goto hash_addr;
23811       /* FALLTHRU */
23812     case DW_OP_const1u:
23813     case DW_OP_const1s:
23814     case DW_OP_const2u:
23815     case DW_OP_const2s:
23816     case DW_OP_const4s:
23817     case DW_OP_const8s:
23818     case DW_OP_constu:
23819     case DW_OP_consts:
23820     case DW_OP_pick:
23821     case DW_OP_plus_uconst:
23822     case DW_OP_breg0:
23823     case DW_OP_breg1:
23824     case DW_OP_breg2:
23825     case DW_OP_breg3:
23826     case DW_OP_breg4:
23827     case DW_OP_breg5:
23828     case DW_OP_breg6:
23829     case DW_OP_breg7:
23830     case DW_OP_breg8:
23831     case DW_OP_breg9:
23832     case DW_OP_breg10:
23833     case DW_OP_breg11:
23834     case DW_OP_breg12:
23835     case DW_OP_breg13:
23836     case DW_OP_breg14:
23837     case DW_OP_breg15:
23838     case DW_OP_breg16:
23839     case DW_OP_breg17:
23840     case DW_OP_breg18:
23841     case DW_OP_breg19:
23842     case DW_OP_breg20:
23843     case DW_OP_breg21:
23844     case DW_OP_breg22:
23845     case DW_OP_breg23:
23846     case DW_OP_breg24:
23847     case DW_OP_breg25:
23848     case DW_OP_breg26:
23849     case DW_OP_breg27:
23850     case DW_OP_breg28:
23851     case DW_OP_breg29:
23852     case DW_OP_breg30:
23853     case DW_OP_breg31:
23854     case DW_OP_regx:
23855     case DW_OP_fbreg:
23856     case DW_OP_piece:
23857     case DW_OP_deref_size:
23858     case DW_OP_xderef_size:
23859       hash = iterative_hash_object (val1->v.val_int, hash);
23860       break;
23861     case DW_OP_skip:
23862     case DW_OP_bra:
23863       {
23864         int offset;
23865
23866         gcc_assert (val1->val_class == dw_val_class_loc);
23867         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23868         hash = iterative_hash_object (offset, hash);
23869       }
23870       break;
23871     case DW_OP_implicit_value:
23872       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23873       switch (val2->val_class)
23874         {
23875         case dw_val_class_const:
23876           hash = iterative_hash_object (val2->v.val_int, hash);
23877           break;
23878         case dw_val_class_vec:
23879           {
23880             unsigned int elt_size = val2->v.val_vec.elt_size;
23881             unsigned int len = val2->v.val_vec.length;
23882
23883             hash = iterative_hash_object (elt_size, hash);
23884             hash = iterative_hash_object (len, hash);
23885             hash = iterative_hash (val2->v.val_vec.array,
23886                                    len * elt_size, hash);
23887           }
23888           break;
23889         case dw_val_class_const_double:
23890           hash = iterative_hash_object (val2->v.val_double.low, hash);
23891           hash = iterative_hash_object (val2->v.val_double.high, hash);
23892           break;
23893         case dw_val_class_addr:
23894           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23895           break;
23896         default:
23897           gcc_unreachable ();
23898         }
23899       break;
23900     case DW_OP_bregx:
23901     case DW_OP_bit_piece:
23902       hash = iterative_hash_object (val1->v.val_int, hash);
23903       hash = iterative_hash_object (val2->v.val_int, hash);
23904       break;
23905     case DW_OP_addr:
23906     hash_addr:
23907       if (loc->dtprel)
23908         {
23909           unsigned char dtprel = 0xd1;
23910           hash = iterative_hash_object (dtprel, hash);
23911         }
23912       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23913       break;
23914     case DW_OP_GNU_implicit_pointer:
23915       hash = iterative_hash_object (val2->v.val_int, hash);
23916       break;
23917     case DW_OP_GNU_entry_value:
23918       hash = hash_loc_operands (val1->v.val_loc, hash);
23919       break;
23920     case DW_OP_GNU_regval_type:
23921     case DW_OP_GNU_deref_type:
23922       {
23923         unsigned int byte_size
23924           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23925         unsigned int encoding
23926           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23927         hash = iterative_hash_object (val1->v.val_int, hash);
23928         hash = iterative_hash_object (byte_size, hash);
23929         hash = iterative_hash_object (encoding, hash);
23930       }
23931       break;
23932     case DW_OP_GNU_convert:
23933     case DW_OP_GNU_reinterpret:
23934     case DW_OP_GNU_const_type:
23935       {
23936         unsigned int byte_size
23937           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23938         unsigned int encoding
23939           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23940         hash = iterative_hash_object (byte_size, hash);
23941         hash = iterative_hash_object (encoding, hash);
23942         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23943           break;
23944         hash = iterative_hash_object (val2->val_class, hash);
23945         switch (val2->val_class)
23946           {
23947           case dw_val_class_const:
23948             hash = iterative_hash_object (val2->v.val_int, hash);
23949             break;
23950           case dw_val_class_vec:
23951             {
23952               unsigned int elt_size = val2->v.val_vec.elt_size;
23953               unsigned int len = val2->v.val_vec.length;
23954
23955               hash = iterative_hash_object (elt_size, hash);
23956               hash = iterative_hash_object (len, hash);
23957               hash = iterative_hash (val2->v.val_vec.array,
23958                                      len * elt_size, hash);
23959             }
23960             break;
23961           case dw_val_class_const_double:
23962             hash = iterative_hash_object (val2->v.val_double.low, hash);
23963             hash = iterative_hash_object (val2->v.val_double.high, hash);
23964             break;
23965           default:
23966             gcc_unreachable ();
23967           }
23968       }
23969       break;
23970
23971     default:
23972       /* Other codes have no operands.  */
23973       break;
23974     }
23975   return hash;
23976 }
23977
23978 /* Iteratively hash the whole DWARF location expression LOC.  */
23979
23980 static inline hashval_t
23981 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23982 {
23983   dw_loc_descr_ref l;
23984   bool sizes_computed = false;
23985   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23986   size_of_locs (loc);
23987
23988   for (l = loc; l != NULL; l = l->dw_loc_next)
23989     {
23990       enum dwarf_location_atom opc = l->dw_loc_opc;
23991       hash = iterative_hash_object (opc, hash);
23992       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23993         {
23994           size_of_locs (loc);
23995           sizes_computed = true;
23996         }
23997       hash = hash_loc_operands (l, hash);
23998     }
23999   return hash;
24000 }
24001
24002 /* Compute hash of the whole location list LIST_HEAD.  */
24003
24004 static inline void
24005 hash_loc_list (dw_loc_list_ref list_head)
24006 {
24007   dw_loc_list_ref curr = list_head;
24008   hashval_t hash = 0;
24009
24010   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24011     {
24012       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
24013       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
24014       if (curr->section)
24015         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
24016                                hash);
24017       hash = hash_locs (curr->expr, hash);
24018     }
24019   list_head->hash = hash;
24020 }
24021
24022 /* Return true if X and Y opcodes have the same operands.  */
24023
24024 static inline bool
24025 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24026 {
24027   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24028   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24029   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24030   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24031
24032   switch (x->dw_loc_opc)
24033     {
24034     case DW_OP_const4u:
24035     case DW_OP_const8u:
24036       if (x->dtprel)
24037         goto hash_addr;
24038       /* FALLTHRU */
24039     case DW_OP_const1u:
24040     case DW_OP_const1s:
24041     case DW_OP_const2u:
24042     case DW_OP_const2s:
24043     case DW_OP_const4s:
24044     case DW_OP_const8s:
24045     case DW_OP_constu:
24046     case DW_OP_consts:
24047     case DW_OP_pick:
24048     case DW_OP_plus_uconst:
24049     case DW_OP_breg0:
24050     case DW_OP_breg1:
24051     case DW_OP_breg2:
24052     case DW_OP_breg3:
24053     case DW_OP_breg4:
24054     case DW_OP_breg5:
24055     case DW_OP_breg6:
24056     case DW_OP_breg7:
24057     case DW_OP_breg8:
24058     case DW_OP_breg9:
24059     case DW_OP_breg10:
24060     case DW_OP_breg11:
24061     case DW_OP_breg12:
24062     case DW_OP_breg13:
24063     case DW_OP_breg14:
24064     case DW_OP_breg15:
24065     case DW_OP_breg16:
24066     case DW_OP_breg17:
24067     case DW_OP_breg18:
24068     case DW_OP_breg19:
24069     case DW_OP_breg20:
24070     case DW_OP_breg21:
24071     case DW_OP_breg22:
24072     case DW_OP_breg23:
24073     case DW_OP_breg24:
24074     case DW_OP_breg25:
24075     case DW_OP_breg26:
24076     case DW_OP_breg27:
24077     case DW_OP_breg28:
24078     case DW_OP_breg29:
24079     case DW_OP_breg30:
24080     case DW_OP_breg31:
24081     case DW_OP_regx:
24082     case DW_OP_fbreg:
24083     case DW_OP_piece:
24084     case DW_OP_deref_size:
24085     case DW_OP_xderef_size:
24086       return valx1->v.val_int == valy1->v.val_int;
24087     case DW_OP_skip:
24088     case DW_OP_bra:
24089       gcc_assert (valx1->val_class == dw_val_class_loc
24090                   && valy1->val_class == dw_val_class_loc
24091                   && x->dw_loc_addr == y->dw_loc_addr);
24092       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24093     case DW_OP_implicit_value:
24094       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24095           || valx2->val_class != valy2->val_class)
24096         return false;
24097       switch (valx2->val_class)
24098         {
24099         case dw_val_class_const:
24100           return valx2->v.val_int == valy2->v.val_int;
24101         case dw_val_class_vec:
24102           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24103                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24104                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24105                             valx2->v.val_vec.elt_size
24106                             * valx2->v.val_vec.length) == 0;
24107         case dw_val_class_const_double:
24108           return valx2->v.val_double.low == valy2->v.val_double.low
24109                  && valx2->v.val_double.high == valy2->v.val_double.high;
24110         case dw_val_class_addr:
24111           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24112         default:
24113           gcc_unreachable ();
24114         }
24115     case DW_OP_bregx:
24116     case DW_OP_bit_piece:
24117       return valx1->v.val_int == valy1->v.val_int
24118              && valx2->v.val_int == valy2->v.val_int;
24119     case DW_OP_addr:
24120     hash_addr:
24121       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24122     case DW_OP_GNU_implicit_pointer:
24123       return valx1->val_class == dw_val_class_die_ref
24124              && valx1->val_class == valy1->val_class
24125              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24126              && valx2->v.val_int == valy2->v.val_int;
24127     case DW_OP_GNU_entry_value:
24128       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24129     case DW_OP_GNU_const_type:
24130       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24131           || valx2->val_class != valy2->val_class)
24132         return false;
24133       switch (valx2->val_class)
24134         {
24135         case dw_val_class_const:
24136           return valx2->v.val_int == valy2->v.val_int;
24137         case dw_val_class_vec:
24138           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24139                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24140                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24141                             valx2->v.val_vec.elt_size
24142                             * valx2->v.val_vec.length) == 0;
24143         case dw_val_class_const_double:
24144           return valx2->v.val_double.low == valy2->v.val_double.low
24145                  && valx2->v.val_double.high == valy2->v.val_double.high;
24146         default:
24147           gcc_unreachable ();
24148         }
24149     case DW_OP_GNU_regval_type:
24150     case DW_OP_GNU_deref_type:
24151       return valx1->v.val_int == valy1->v.val_int
24152              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24153     case DW_OP_GNU_convert:
24154     case DW_OP_GNU_reinterpret:
24155       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24156     default:
24157       /* Other codes have no operands.  */
24158       return true;
24159     }
24160 }
24161
24162 /* Return true if DWARF location expressions X and Y are the same.  */
24163
24164 static inline bool
24165 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24166 {
24167   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24168     if (x->dw_loc_opc != y->dw_loc_opc
24169         || x->dtprel != y->dtprel
24170         || !compare_loc_operands (x, y))
24171       break;
24172   return x == NULL && y == NULL;
24173 }
24174
24175 /* Return precomputed hash of location list X.  */
24176
24177 static hashval_t
24178 loc_list_hash (const void *x)
24179 {
24180   return ((const struct dw_loc_list_struct *) x)->hash;
24181 }
24182
24183 /* Return 1 if location lists X and Y are the same.  */
24184
24185 static int
24186 loc_list_eq (const void *x, const void *y)
24187 {
24188   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
24189   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
24190   if (a == b)
24191     return 1;
24192   if (a->hash != b->hash)
24193     return 0;
24194   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24195     if (strcmp (a->begin, b->begin) != 0
24196         || strcmp (a->end, b->end) != 0
24197         || (a->section == NULL) != (b->section == NULL)
24198         || (a->section && strcmp (a->section, b->section) != 0)
24199         || !compare_locs (a->expr, b->expr))
24200       break;
24201   return a == NULL && b == NULL;
24202 }
24203
24204 /* Recursively optimize location lists referenced from DIE
24205    children and share them whenever possible.  */
24206
24207 static void
24208 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
24209 {
24210   dw_die_ref c;
24211   dw_attr_ref a;
24212   unsigned ix;
24213   void **slot;
24214
24215   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
24216     if (AT_class (a) == dw_val_class_loc_list)
24217       {
24218         dw_loc_list_ref list = AT_loc_list (a);
24219         /* TODO: perform some optimizations here, before hashing
24220            it and storing into the hash table.  */
24221         hash_loc_list (list);
24222         slot = htab_find_slot_with_hash (htab, list, list->hash,
24223                                          INSERT);
24224         if (*slot == NULL)
24225           *slot = (void *) list;
24226         else
24227           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
24228       }
24229
24230   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24231 }
24232
24233 /* Optimize location lists referenced from DIE
24234    children and share them whenever possible.  */
24235
24236 static void
24237 optimize_location_lists (dw_die_ref die)
24238 {
24239   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
24240   optimize_location_lists_1 (die, htab);
24241   htab_delete (htab);
24242 }
24243 \f
24244 /* Output stuff that dwarf requires at the end of every file,
24245    and generate the DWARF-2 debugging info.  */
24246
24247 static void
24248 dwarf2out_finish (const char *filename)
24249 {
24250   limbo_die_node *node, *next_node;
24251   comdat_type_node *ctnode;
24252   htab_t comdat_type_table;
24253   unsigned int i;
24254
24255   gen_scheduled_generic_parms_dies ();
24256   gen_remaining_tmpl_value_param_die_attribute ();
24257
24258   /* Add the name for the main input file now.  We delayed this from
24259      dwarf2out_init to avoid complications with PCH.  */
24260   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24261   if (!IS_ABSOLUTE_PATH (filename))
24262     add_comp_dir_attribute (comp_unit_die ());
24263   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24264     {
24265       bool p = false;
24266       htab_traverse (file_table, file_table_relative_p, &p);
24267       if (p)
24268         add_comp_dir_attribute (comp_unit_die ());
24269     }
24270
24271   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
24272     {
24273       add_location_or_const_value_attribute (
24274         VEC_index (deferred_locations, deferred_locations_list, i)->die,
24275         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
24276         false,
24277         DW_AT_location);
24278     }
24279
24280   /* Traverse the limbo die list, and add parent/child links.  The only
24281      dies without parents that should be here are concrete instances of
24282      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24283      For concrete instances, we can get the parent die from the abstract
24284      instance.  */
24285   for (node = limbo_die_list; node; node = next_node)
24286     {
24287       dw_die_ref die = node->die;
24288       next_node = node->next;
24289
24290       if (die->die_parent == NULL)
24291         {
24292           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24293
24294           if (origin)
24295             add_child_die (origin->die_parent, die);
24296           else if (is_cu_die (die))
24297             ;
24298           else if (seen_error ())
24299             /* It's OK to be confused by errors in the input.  */
24300             add_child_die (comp_unit_die (), die);
24301           else
24302             {
24303               /* In certain situations, the lexical block containing a
24304                  nested function can be optimized away, which results
24305                  in the nested function die being orphaned.  Likewise
24306                  with the return type of that nested function.  Force
24307                  this to be a child of the containing function.
24308
24309                  It may happen that even the containing function got fully
24310                  inlined and optimized out.  In that case we are lost and
24311                  assign the empty child.  This should not be big issue as
24312                  the function is likely unreachable too.  */
24313               tree context = NULL_TREE;
24314
24315               gcc_assert (node->created_for);
24316
24317               if (DECL_P (node->created_for))
24318                 context = DECL_CONTEXT (node->created_for);
24319               else if (TYPE_P (node->created_for))
24320                 context = TYPE_CONTEXT (node->created_for);
24321
24322               gcc_assert (context
24323                           && (TREE_CODE (context) == FUNCTION_DECL
24324                               || TREE_CODE (context) == NAMESPACE_DECL));
24325
24326               origin = lookup_decl_die (context);
24327               if (origin)
24328                 add_child_die (origin, die);
24329               else
24330                 add_child_die (comp_unit_die (), die);
24331             }
24332         }
24333     }
24334
24335   limbo_die_list = NULL;
24336
24337 #if ENABLE_ASSERT_CHECKING
24338   {
24339     dw_die_ref die = comp_unit_die (), c;
24340     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24341   }
24342 #endif
24343   resolve_addr (comp_unit_die ());
24344   move_marked_base_types ();
24345
24346   for (node = deferred_asm_name; node; node = node->next)
24347     {
24348       tree decl = node->created_for;
24349       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24350         {
24351           add_linkage_attr (node->die, decl);
24352           move_linkage_attr (node->die);
24353         }
24354     }
24355
24356   deferred_asm_name = NULL;
24357
24358   /* Walk through the list of incomplete types again, trying once more to
24359      emit full debugging info for them.  */
24360   retry_incomplete_types ();
24361
24362   if (flag_eliminate_unused_debug_types)
24363     prune_unused_types ();
24364
24365   /* Generate separate CUs for each of the include files we've seen.
24366      They will go into limbo_die_list.  */
24367   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
24368     break_out_includes (comp_unit_die ());
24369
24370   /* Generate separate COMDAT sections for type DIEs. */
24371   if (use_debug_types)
24372     {
24373       break_out_comdat_types (comp_unit_die ());
24374
24375       /* Each new type_unit DIE was added to the limbo die list when created.
24376          Since these have all been added to comdat_type_list, clear the
24377          limbo die list.  */
24378       limbo_die_list = NULL;
24379
24380       /* For each new comdat type unit, copy declarations for incomplete
24381          types to make the new unit self-contained (i.e., no direct
24382          references to the main compile unit).  */
24383       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24384         copy_decls_for_unworthy_types (ctnode->root_die);
24385       copy_decls_for_unworthy_types (comp_unit_die ());
24386
24387       /* In the process of copying declarations from one unit to another,
24388          we may have left some declarations behind that are no longer
24389          referenced.  Prune them.  */
24390       prune_unused_types ();
24391     }
24392
24393   /* Traverse the DIE's and add add sibling attributes to those DIE's
24394      that have children.  */
24395   add_sibling_attributes (comp_unit_die ());
24396   for (node = limbo_die_list; node; node = node->next)
24397     add_sibling_attributes (node->die);
24398   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24399     add_sibling_attributes (ctnode->root_die);
24400
24401   /* Output a terminator label for the .text section.  */
24402   switch_to_section (text_section);
24403   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24404   if (cold_text_section)
24405     {
24406       switch_to_section (cold_text_section);
24407       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24408     }
24409
24410   /* We can only use the low/high_pc attributes if all of the code was
24411      in .text.  */
24412   if (!have_multiple_function_sections 
24413       || (dwarf_version < 3 && dwarf_strict))
24414     {
24415       /* Don't add if the CU has no associated code.  */
24416       if (text_section_used)
24417         {
24418           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
24419           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
24420         }
24421     }
24422   else
24423     {
24424       unsigned fde_idx = 0;
24425       bool range_list_added = false;
24426
24427       if (text_section_used)
24428         add_ranges_by_labels (comp_unit_die (), text_section_label,
24429                               text_end_label, &range_list_added);
24430       if (cold_text_section_used)
24431         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
24432                               cold_end_label, &range_list_added);
24433
24434       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
24435         {
24436           dw_fde_ref fde = &fde_table[fde_idx];
24437
24438           if (!fde->in_std_section)
24439             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
24440                                   fde->dw_fde_end, &range_list_added);
24441           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24442             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
24443                                   fde->dw_fde_second_end, &range_list_added);
24444         }
24445
24446       if (range_list_added)
24447         {
24448           /* We need to give .debug_loc and .debug_ranges an appropriate
24449              "base address".  Use zero so that these addresses become
24450              absolute.  Historically, we've emitted the unexpected
24451              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24452              Emit both to give time for other tools to adapt.  */
24453           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
24454           if (! dwarf_strict && dwarf_version < 4)
24455             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
24456
24457           add_ranges (NULL);
24458         }
24459     }
24460
24461   if (debug_info_level >= DINFO_LEVEL_NORMAL)
24462     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
24463                     debug_line_section_label);
24464
24465   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24466     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
24467
24468   if (have_location_lists)
24469     optimize_location_lists (comp_unit_die ());
24470
24471   /* Output all of the compilation units.  We put the main one last so that
24472      the offsets are available to output_pubnames.  */
24473   for (node = limbo_die_list; node; node = node->next)
24474     output_comp_unit (node->die, 0);
24475
24476   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
24477   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24478     {
24479       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
24480
24481       /* Don't output duplicate types.  */
24482       if (*slot != HTAB_EMPTY_ENTRY)
24483         continue;
24484
24485       /* Add a pointer to the line table for the main compilation unit
24486          so that the debugger can make sense of DW_AT_decl_file
24487          attributes.  */
24488       if (debug_info_level >= DINFO_LEVEL_NORMAL)
24489         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24490                         debug_line_section_label);
24491
24492       output_comdat_type_unit (ctnode);
24493       *slot = ctnode;
24494     }
24495   htab_delete (comdat_type_table);
24496
24497   /* Output the main compilation unit if non-empty or if .debug_macinfo
24498      will be emitted.  */
24499   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
24500
24501   /* Output the abbreviation table.  */
24502   switch_to_section (debug_abbrev_section);
24503   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24504   output_abbrev_section ();
24505
24506   /* Output location list section if necessary.  */
24507   if (have_location_lists)
24508     {
24509       /* Output the location lists info.  */
24510       switch_to_section (debug_loc_section);
24511       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
24512                                    DEBUG_LOC_SECTION_LABEL, 0);
24513       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24514       output_location_lists (comp_unit_die ());
24515     }
24516
24517   /* Output public names table if necessary.  */
24518   if (!VEC_empty (pubname_entry, pubname_table))
24519     {
24520       gcc_assert (info_section_emitted);
24521       switch_to_section (debug_pubnames_section);
24522       output_pubnames (pubname_table);
24523     }
24524
24525   /* Output public types table if necessary.  */
24526   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
24527      It shouldn't hurt to emit it always, since pure DWARF2 consumers
24528      simply won't look for the section.  */
24529   if (!VEC_empty (pubname_entry, pubtype_table))
24530     {
24531       bool empty = false;
24532       
24533       if (flag_eliminate_unused_debug_types)
24534         {
24535           /* The pubtypes table might be emptied by pruning unused items.  */
24536           unsigned i;
24537           pubname_ref p;
24538           empty = true;
24539           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
24540             if (p->die->die_offset != 0)
24541               {
24542                 empty = false;
24543                 break;
24544               }
24545         }
24546       if (!empty)
24547         {
24548           gcc_assert (info_section_emitted);
24549           switch_to_section (debug_pubtypes_section);
24550           output_pubnames (pubtype_table);
24551         }
24552     }
24553
24554   /* Output the address range information if a CU (.debug_info section)
24555      was emitted.  We output an empty table even if we had no functions
24556      to put in it.  This because the consumer has no way to tell the
24557      difference between an empty table that we omitted and failure to
24558      generate a table that would have contained data.  */
24559   if (info_section_emitted)
24560     {
24561       unsigned long aranges_length = size_of_aranges ();
24562
24563       switch_to_section (debug_aranges_section);
24564       output_aranges (aranges_length);
24565     }
24566
24567   /* Output ranges section if necessary.  */
24568   if (ranges_table_in_use)
24569     {
24570       switch_to_section (debug_ranges_section);
24571       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24572       output_ranges ();
24573     }
24574
24575   /* Output the source line correspondence table.  We must do this
24576      even if there is no line information.  Otherwise, on an empty
24577      translation unit, we will generate a present, but empty,
24578      .debug_info section.  IRIX 6.5 `nm' will then complain when
24579      examining the file.  This is done late so that any filenames
24580      used by the debug_info section are marked as 'used'.  */
24581   switch_to_section (debug_line_section);
24582   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24583   if (! DWARF2_ASM_LINE_DEBUG_INFO)
24584     output_line_info ();
24585
24586   /* Have to end the macro section.  */
24587   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24588     {
24589       switch_to_section (debug_macinfo_section);
24590       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24591       if (!VEC_empty (macinfo_entry, macinfo_table))
24592         output_macinfo ();
24593       dw2_asm_output_data (1, 0, "End compilation unit");
24594     }
24595
24596   /* If we emitted any DW_FORM_strp form attribute, output the string
24597      table too.  */
24598   if (debug_str_hash)
24599     htab_traverse (debug_str_hash, output_indirect_string, NULL);
24600 }
24601
24602 #include "gt-dwarf2out.h"