OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95 #include "cfglayout.h"
96
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
99
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102
103 /* Define this macro to be a nonzero value if the directory specifications
104     which are output in the debug info should end with a separator.  */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107    from generating indirect strings in DWARF2 debug information, for instance
108    if your target is stuck with an old version of GDB that is unable to
109    process them properly or uses VMS Debug.  */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
115
116 /* ??? Poison these here until it can be done generically.  They've been
117    totally replaced in this file; make sure it stays that way.  */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
123
124 #ifndef INCOMING_RETURN_ADDR_RTX
125 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
126 #endif
127
128 /* Map register numbers held in the call frame info that gcc has
129    collected using DWARF_FRAME_REGNUM to those that should be output in
130    .debug_frame and .eh_frame.  */
131 #ifndef DWARF2_FRAME_REG_OUT
132 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
133 #endif
134
135 /* Save the result of dwarf2out_do_frame across PCH.  */
136 static GTY(()) bool saved_do_cfi_asm = 0;
137
138 /* Decide whether we want to emit frame unwind information for the current
139    translation unit.  */
140
141 int
142 dwarf2out_do_frame (void)
143 {
144   /* We want to emit correct CFA location expressions or lists, so we
145      have to return true if we're going to output debug info, even if
146      we're not going to output frame or unwind info.  */
147   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
148     return true;
149
150   if (saved_do_cfi_asm)
151     return true;
152
153   if (targetm.debug_unwind_info () == UI_DWARF2)
154     return true;
155
156   if ((flag_unwind_tables || flag_exceptions)
157       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
158     return true;
159
160   return false;
161 }
162
163 /* Decide whether to emit frame unwind via assembler directives.  */
164
165 int
166 dwarf2out_do_cfi_asm (void)
167 {
168   int enc;
169
170 #ifdef MIPS_DEBUGGING_INFO
171   return false;
172 #endif
173   if (saved_do_cfi_asm)
174     return true;
175   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
176     return false;
177   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
178     return false;
179
180   /* Make sure the personality encoding is one the assembler can support.
181      In particular, aligned addresses can't be handled.  */
182   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
183   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184     return false;
185   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
186   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
187     return false;
188
189   /* If we can't get the assembler to emit only .debug_frame, and we don't need
190      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
191   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
192       && !flag_unwind_tables && !flag_exceptions
193       && targetm.except_unwind_info (&global_options) != UI_DWARF2)
194     return false;
195
196   saved_do_cfi_asm = true;
197   return true;
198 }
199
200 /* The size of the target's pointer type.  */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
204
205 /* Array of RTXes referenced by the debugging information, which therefore
206    must be kept around forever.  */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208
209 /* A pointer to the base of a list of incomplete types which might be
210    completed at some later time.  incomplete_types_list needs to be a
211    VEC(tree,gc) because we want to tell the garbage collector about
212    it.  */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
214
215 /* A pointer to the base of a table of references to declaration
216    scopes.  This table is a display which tracks the nesting
217    of declaration scopes at the current scope and containing
218    scopes.  This table is used to find the proper place to
219    define type declaration DIE's.  */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
221
222 /* Pointers to various DWARF2 sections.  */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_str_section;
232 static GTY(()) section *debug_ranges_section;
233 static GTY(()) section *debug_frame_section;
234
235 /* Personality decl of current unit.  Used only when assembler does not support
236    personality CFI.  */
237 static GTY(()) rtx current_unit_personality;
238
239 /* How to start an assembler comment.  */
240 #ifndef ASM_COMMENT_START
241 #define ASM_COMMENT_START ";#"
242 #endif
243
244 typedef struct dw_cfi_struct *dw_cfi_ref;
245 typedef struct dw_fde_struct *dw_fde_ref;
246 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
247
248 /* Call frames are described using a sequence of Call Frame
249    Information instructions.  The register number, offset
250    and address fields are provided as possible operands;
251    their use is selected by the opcode field.  */
252
253 enum dw_cfi_oprnd_type {
254   dw_cfi_oprnd_unused,
255   dw_cfi_oprnd_reg_num,
256   dw_cfi_oprnd_offset,
257   dw_cfi_oprnd_addr,
258   dw_cfi_oprnd_loc
259 };
260
261 typedef union GTY(()) dw_cfi_oprnd_struct {
262   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
263   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
264   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
265   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
266 }
267 dw_cfi_oprnd;
268
269 typedef struct GTY(()) dw_cfi_struct {
270   dw_cfi_ref dw_cfi_next;
271   enum dwarf_call_frame_info dw_cfi_opc;
272   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
273     dw_cfi_oprnd1;
274   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
275     dw_cfi_oprnd2;
276 }
277 dw_cfi_node;
278
279 /* This is how we define the location of the CFA. We use to handle it
280    as REG + OFFSET all the time,  but now it can be more complex.
281    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
282    Instead of passing around REG and OFFSET, we pass a copy
283    of this structure.  */
284 typedef struct cfa_loc {
285   HOST_WIDE_INT offset;
286   HOST_WIDE_INT base_offset;
287   unsigned int reg;
288   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
289   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
290 } dw_cfa_location;
291
292 /* All call frame descriptions (FDE's) in the GCC generated DWARF
293    refer to a single Common Information Entry (CIE), defined at
294    the beginning of the .debug_frame section.  This use of a single
295    CIE obviates the need to keep track of multiple CIE's
296    in the DWARF generation routines below.  */
297
298 typedef struct GTY(()) dw_fde_struct {
299   tree decl;
300   const char *dw_fde_begin;
301   const char *dw_fde_current_label;
302   const char *dw_fde_end;
303   const char *dw_fde_vms_end_prologue;
304   const char *dw_fde_vms_begin_epilogue;
305   const char *dw_fde_second_begin;
306   const char *dw_fde_second_end;
307   dw_cfi_ref dw_fde_cfi;
308   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
309   HOST_WIDE_INT stack_realignment;
310   unsigned funcdef_number;
311   /* Dynamic realign argument pointer register.  */
312   unsigned int drap_reg;
313   /* Virtual dynamic realign argument pointer register.  */
314   unsigned int vdrap_reg;
315   /* These 3 flags are copied from rtl_data in function.h.  */
316   unsigned all_throwers_are_sibcalls : 1;
317   unsigned uses_eh_lsda : 1;
318   unsigned nothrow : 1;
319   /* Whether we did stack realign in this call frame.  */
320   unsigned stack_realign : 1;
321   /* Whether dynamic realign argument pointer register has been saved.  */
322   unsigned drap_reg_saved: 1;
323   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
324   unsigned in_std_section : 1;
325   /* True iff dw_fde_second_begin label is in text_section or
326      cold_text_section.  */
327   unsigned second_in_std_section : 1;
328 }
329 dw_fde_node;
330
331 /* Maximum size (in bytes) of an artificially generated label.  */
332 #define MAX_ARTIFICIAL_LABEL_BYTES      30
333
334 /* The size of addresses as they appear in the Dwarf 2 data.
335    Some architectures use word addresses to refer to code locations,
336    but Dwarf 2 info always uses byte addresses.  On such machines,
337    Dwarf 2 addresses need to be larger than the architecture's
338    pointers.  */
339 #ifndef DWARF2_ADDR_SIZE
340 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
341 #endif
342
343 /* The size in bytes of a DWARF field indicating an offset or length
344    relative to a debug info section, specified to be 4 bytes in the
345    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
346    as PTR_SIZE.  */
347
348 #ifndef DWARF_OFFSET_SIZE
349 #define DWARF_OFFSET_SIZE 4
350 #endif
351
352 /* The size in bytes of a DWARF 4 type signature.  */
353
354 #ifndef DWARF_TYPE_SIGNATURE_SIZE
355 #define DWARF_TYPE_SIGNATURE_SIZE 8
356 #endif
357
358 /* According to the (draft) DWARF 3 specification, the initial length
359    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
360    bytes are 0xffffffff, followed by the length stored in the next 8
361    bytes.
362
363    However, the SGI/MIPS ABI uses an initial length which is equal to
364    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
365
366 #ifndef DWARF_INITIAL_LENGTH_SIZE
367 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
368 #endif
369
370 /* Round SIZE up to the nearest BOUNDARY.  */
371 #define DWARF_ROUND(SIZE,BOUNDARY) \
372   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
373
374 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
375 #ifndef DWARF_CIE_DATA_ALIGNMENT
376 #ifdef STACK_GROWS_DOWNWARD
377 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
378 #else
379 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
380 #endif
381 #endif
382
383 /* CIE identifier.  */
384 #if HOST_BITS_PER_WIDE_INT >= 64
385 #define DWARF_CIE_ID \
386   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
387 #else
388 #define DWARF_CIE_ID DW_CIE_ID
389 #endif
390
391 /* A pointer to the base of a table that contains frame description
392    information for each routine.  */
393 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
394
395 /* Number of elements currently allocated for fde_table.  */
396 static GTY(()) unsigned fde_table_allocated;
397
398 /* Number of elements in fde_table currently in use.  */
399 static GTY(()) unsigned fde_table_in_use;
400
401 /* Size (in elements) of increments by which we may expand the
402    fde_table.  */
403 #define FDE_TABLE_INCREMENT 256
404
405 /* Get the current fde_table entry we should use.  */
406
407 static inline dw_fde_ref
408 current_fde (void)
409 {
410   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
411 }
412
413 /* A list of call frame insns for the CIE.  */
414 static GTY(()) dw_cfi_ref cie_cfi_head;
415
416 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
417    attribute that accelerates the lookup of the FDE associated
418    with the subprogram.  This variable holds the table index of the FDE
419    associated with the current function (body) definition.  */
420 static unsigned current_funcdef_fde;
421
422 struct GTY(()) indirect_string_node {
423   const char *str;
424   unsigned int refcount;
425   enum dwarf_form form;
426   char *label;
427 };
428
429 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
430
431 /* True if the compilation unit has location entries that reference
432    debug strings.  */
433 static GTY(()) bool debug_str_hash_forced = false;
434
435 static GTY(()) int dw2_string_counter;
436 static GTY(()) unsigned long dwarf2out_cfi_label_num;
437
438 /* True if the compilation unit places functions in more than one section.  */
439 static GTY(()) bool have_multiple_function_sections = false;
440
441 /* Whether the default text and cold text sections have been used at all.  */
442
443 static GTY(()) bool text_section_used = false;
444 static GTY(()) bool cold_text_section_used = false;
445
446 /* The default cold text section.  */
447 static GTY(()) section *cold_text_section;
448
449 /* Forward declarations for functions defined in this file.  */
450
451 static char *stripattributes (const char *);
452 static const char *dwarf_cfi_name (unsigned);
453 static dw_cfi_ref new_cfi (void);
454 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
455 static void add_fde_cfi (const char *, dw_cfi_ref);
456 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
457 static void lookup_cfa (dw_cfa_location *);
458 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
459 static void initial_return_save (rtx);
460 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
461                                           HOST_WIDE_INT);
462 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
463 static void output_cfi_directive (dw_cfi_ref);
464 static void output_call_frame_info (int);
465 static void dwarf2out_note_section_used (void);
466 static bool clobbers_queued_reg_save (const_rtx);
467 static void dwarf2out_frame_debug_expr (rtx, const char *);
468
469 /* Support for complex CFA locations.  */
470 static void output_cfa_loc (dw_cfi_ref, int);
471 static void output_cfa_loc_raw (dw_cfi_ref);
472 static void get_cfa_from_loc_descr (dw_cfa_location *,
473                                     struct dw_loc_descr_struct *);
474 static struct dw_loc_descr_struct *build_cfa_loc
475   (dw_cfa_location *, HOST_WIDE_INT);
476 static struct dw_loc_descr_struct *build_cfa_aligned_loc
477   (HOST_WIDE_INT, HOST_WIDE_INT);
478 static void def_cfa_1 (const char *, dw_cfa_location *);
479 static struct dw_loc_descr_struct *mem_loc_descriptor
480   (rtx, enum machine_mode mode, enum var_init_status);
481
482 /* How to start an assembler comment.  */
483 #ifndef ASM_COMMENT_START
484 #define ASM_COMMENT_START ";#"
485 #endif
486
487 /* Data and reference forms for relocatable data.  */
488 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
489 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
490
491 #ifndef DEBUG_FRAME_SECTION
492 #define DEBUG_FRAME_SECTION     ".debug_frame"
493 #endif
494
495 #ifndef FUNC_BEGIN_LABEL
496 #define FUNC_BEGIN_LABEL        "LFB"
497 #endif
498
499 #ifndef FUNC_END_LABEL
500 #define FUNC_END_LABEL          "LFE"
501 #endif
502
503 #ifndef PROLOGUE_END_LABEL
504 #define PROLOGUE_END_LABEL      "LPE"
505 #endif
506
507 #ifndef EPILOGUE_BEGIN_LABEL
508 #define EPILOGUE_BEGIN_LABEL    "LEB"
509 #endif
510
511 #ifndef FRAME_BEGIN_LABEL
512 #define FRAME_BEGIN_LABEL       "Lframe"
513 #endif
514 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
515 #define CIE_END_LABEL           "LECIE"
516 #define FDE_LABEL               "LSFDE"
517 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
518 #define FDE_END_LABEL           "LEFDE"
519 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
520 #define LINE_NUMBER_END_LABEL   "LELT"
521 #define LN_PROLOG_AS_LABEL      "LASLTP"
522 #define LN_PROLOG_END_LABEL     "LELTP"
523 #define DIE_LABEL_PREFIX        "DW"
524
525 /* The DWARF 2 CFA column which tracks the return address.  Normally this
526    is the column for PC, or the first column after all of the hard
527    registers.  */
528 #ifndef DWARF_FRAME_RETURN_COLUMN
529 #ifdef PC_REGNUM
530 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
531 #else
532 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
533 #endif
534 #endif
535
536 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
537    default, we just provide columns for all registers.  */
538 #ifndef DWARF_FRAME_REGNUM
539 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
540 #endif
541 \f
542 /* Match the base name of a file to the base name of a compilation unit. */
543
544 static int
545 matches_main_base (const char *path)
546 {
547   /* Cache the last query. */
548   static const char *last_path = NULL;
549   static int last_match = 0;
550   if (path != last_path)
551     {
552       const char *base;
553       int length = base_of_path (path, &base);
554       last_path = path;
555       last_match = (length == main_input_baselength
556                     && memcmp (base, main_input_basename, length) == 0);
557     }
558   return last_match;
559 }
560
561 #ifdef DEBUG_DEBUG_STRUCT
562
563 static int
564 dump_struct_debug (tree type, enum debug_info_usage usage,
565                    enum debug_struct_file criterion, int generic,
566                    int matches, int result)
567 {
568   /* Find the type name. */
569   tree type_decl = TYPE_STUB_DECL (type);
570   tree t = type_decl;
571   const char *name = 0;
572   if (TREE_CODE (t) == TYPE_DECL)
573     t = DECL_NAME (t);
574   if (t)
575     name = IDENTIFIER_POINTER (t);
576
577   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
578            criterion,
579            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
580            matches ? "bas" : "hdr",
581            generic ? "gen" : "ord",
582            usage == DINFO_USAGE_DFN ? ";" :
583              usage == DINFO_USAGE_DIR_USE ? "." : "*",
584            result,
585            (void*) type_decl, name);
586   return result;
587 }
588 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
589   dump_struct_debug (type, usage, criterion, generic, matches, result)
590
591 #else
592
593 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
594   (result)
595
596 #endif
597
598 static bool
599 should_emit_struct_debug (tree type, enum debug_info_usage usage)
600 {
601   enum debug_struct_file criterion;
602   tree type_decl;
603   bool generic = lang_hooks.types.generic_p (type);
604
605   if (generic)
606     criterion = debug_struct_generic[usage];
607   else
608     criterion = debug_struct_ordinary[usage];
609
610   if (criterion == DINFO_STRUCT_FILE_NONE)
611     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
612   if (criterion == DINFO_STRUCT_FILE_ANY)
613     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
614
615   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
616
617   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
618     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
619
620   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
621     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
622   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
623 }
624 \f
625 /* Hook used by __throw.  */
626
627 rtx
628 expand_builtin_dwarf_sp_column (void)
629 {
630   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
631   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
632 }
633
634 /* Return a pointer to a copy of the section string name S with all
635    attributes stripped off, and an asterisk prepended (for assemble_name).  */
636
637 static inline char *
638 stripattributes (const char *s)
639 {
640   char *stripped = XNEWVEC (char, strlen (s) + 2);
641   char *p = stripped;
642
643   *p++ = '*';
644
645   while (*s && *s != ',')
646     *p++ = *s++;
647
648   *p = '\0';
649   return stripped;
650 }
651
652 /* MEM is a memory reference for the register size table, each element of
653    which has mode MODE.  Initialize column C as a return address column.  */
654
655 static void
656 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
657 {
658   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
659   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
660   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
661 }
662
663 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
664
665 static inline HOST_WIDE_INT
666 div_data_align (HOST_WIDE_INT off)
667 {
668   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
669   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
670   return r;
671 }
672
673 /* Return true if we need a signed version of a given opcode
674    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
675
676 static inline bool
677 need_data_align_sf_opcode (HOST_WIDE_INT off)
678 {
679   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
680 }
681
682 /* Generate code to initialize the register size table.  */
683
684 void
685 expand_builtin_init_dwarf_reg_sizes (tree address)
686 {
687   unsigned int i;
688   enum machine_mode mode = TYPE_MODE (char_type_node);
689   rtx addr = expand_normal (address);
690   rtx mem = gen_rtx_MEM (BLKmode, addr);
691   bool wrote_return_column = false;
692
693   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
694     {
695       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
696
697       if (rnum < DWARF_FRAME_REGISTERS)
698         {
699           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
700           enum machine_mode save_mode = reg_raw_mode[i];
701           HOST_WIDE_INT size;
702
703           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
704             save_mode = choose_hard_reg_mode (i, 1, true);
705           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
706             {
707               if (save_mode == VOIDmode)
708                 continue;
709               wrote_return_column = true;
710             }
711           size = GET_MODE_SIZE (save_mode);
712           if (offset < 0)
713             continue;
714
715           emit_move_insn (adjust_address (mem, mode, offset),
716                           gen_int_mode (size, mode));
717         }
718     }
719
720   if (!wrote_return_column)
721     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
722
723 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
724   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
725 #endif
726
727   targetm.init_dwarf_reg_sizes_extra (address);
728 }
729
730 /* Convert a DWARF call frame info. operation to its string name */
731
732 static const char *
733 dwarf_cfi_name (unsigned int cfi_opc)
734 {
735   switch (cfi_opc)
736     {
737     case DW_CFA_advance_loc:
738       return "DW_CFA_advance_loc";
739     case DW_CFA_offset:
740       return "DW_CFA_offset";
741     case DW_CFA_restore:
742       return "DW_CFA_restore";
743     case DW_CFA_nop:
744       return "DW_CFA_nop";
745     case DW_CFA_set_loc:
746       return "DW_CFA_set_loc";
747     case DW_CFA_advance_loc1:
748       return "DW_CFA_advance_loc1";
749     case DW_CFA_advance_loc2:
750       return "DW_CFA_advance_loc2";
751     case DW_CFA_advance_loc4:
752       return "DW_CFA_advance_loc4";
753     case DW_CFA_offset_extended:
754       return "DW_CFA_offset_extended";
755     case DW_CFA_restore_extended:
756       return "DW_CFA_restore_extended";
757     case DW_CFA_undefined:
758       return "DW_CFA_undefined";
759     case DW_CFA_same_value:
760       return "DW_CFA_same_value";
761     case DW_CFA_register:
762       return "DW_CFA_register";
763     case DW_CFA_remember_state:
764       return "DW_CFA_remember_state";
765     case DW_CFA_restore_state:
766       return "DW_CFA_restore_state";
767     case DW_CFA_def_cfa:
768       return "DW_CFA_def_cfa";
769     case DW_CFA_def_cfa_register:
770       return "DW_CFA_def_cfa_register";
771     case DW_CFA_def_cfa_offset:
772       return "DW_CFA_def_cfa_offset";
773
774     /* DWARF 3 */
775     case DW_CFA_def_cfa_expression:
776       return "DW_CFA_def_cfa_expression";
777     case DW_CFA_expression:
778       return "DW_CFA_expression";
779     case DW_CFA_offset_extended_sf:
780       return "DW_CFA_offset_extended_sf";
781     case DW_CFA_def_cfa_sf:
782       return "DW_CFA_def_cfa_sf";
783     case DW_CFA_def_cfa_offset_sf:
784       return "DW_CFA_def_cfa_offset_sf";
785
786     /* SGI/MIPS specific */
787     case DW_CFA_MIPS_advance_loc8:
788       return "DW_CFA_MIPS_advance_loc8";
789
790     /* GNU extensions */
791     case DW_CFA_GNU_window_save:
792       return "DW_CFA_GNU_window_save";
793     case DW_CFA_GNU_args_size:
794       return "DW_CFA_GNU_args_size";
795     case DW_CFA_GNU_negative_offset_extended:
796       return "DW_CFA_GNU_negative_offset_extended";
797
798     default:
799       return "DW_CFA_<unknown>";
800     }
801 }
802
803 /* Return a pointer to a newly allocated Call Frame Instruction.  */
804
805 static inline dw_cfi_ref
806 new_cfi (void)
807 {
808   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
809
810   cfi->dw_cfi_next = NULL;
811   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
812   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
813
814   return cfi;
815 }
816
817 /* Add a Call Frame Instruction to list of instructions.  */
818
819 static inline void
820 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
821 {
822   dw_cfi_ref *p;
823   dw_fde_ref fde = current_fde ();
824
825   /* When DRAP is used, CFA is defined with an expression.  Redefine
826      CFA may lead to a different CFA value.   */
827   /* ??? Of course, this heuristic fails when we're annotating epilogues,
828      because of course we'll always want to redefine the CFA back to the
829      stack pointer on the way out.  Where should we move this check?  */
830   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
831     switch (cfi->dw_cfi_opc)
832       {
833         case DW_CFA_def_cfa_register:
834         case DW_CFA_def_cfa_offset:
835         case DW_CFA_def_cfa_offset_sf:
836         case DW_CFA_def_cfa:
837         case DW_CFA_def_cfa_sf:
838           gcc_unreachable ();
839
840         default:
841           break;
842       }
843
844   /* Find the end of the chain.  */
845   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
846     ;
847
848   *p = cfi;
849 }
850
851 /* Generate a new label for the CFI info to refer to.  FORCE is true
852    if a label needs to be output even when using .cfi_* directives.  */
853
854 char *
855 dwarf2out_cfi_label (bool force)
856 {
857   static char label[20];
858
859   if (!force && dwarf2out_do_cfi_asm ())
860     {
861       /* In this case, we will be emitting the asm directive instead of
862          the label, so just return a placeholder to keep the rest of the
863          interfaces happy.  */
864       strcpy (label, "<do not output>");
865     }
866   else
867     {
868       int num = dwarf2out_cfi_label_num++;
869       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
870       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
871     }
872
873   return label;
874 }
875
876 /* True if remember_state should be emitted before following CFI directive.  */
877 static bool emit_cfa_remember;
878
879 /* True if any CFI directives were emitted at the current insn.  */
880 static bool any_cfis_emitted;
881
882 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
883    or to the CIE if LABEL is NULL.  */
884
885 static void
886 add_fde_cfi (const char *label, dw_cfi_ref cfi)
887 {
888   dw_cfi_ref *list_head;
889
890   if (emit_cfa_remember)
891     {
892       dw_cfi_ref cfi_remember;
893
894       /* Emit the state save.  */
895       emit_cfa_remember = false;
896       cfi_remember = new_cfi ();
897       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
898       add_fde_cfi (label, cfi_remember);
899     }
900
901   list_head = &cie_cfi_head;
902
903   if (dwarf2out_do_cfi_asm ())
904     {
905       if (label)
906         {
907           dw_fde_ref fde = current_fde ();
908
909           gcc_assert (fde != NULL);
910
911           /* We still have to add the cfi to the list so that lookup_cfa
912              works later on.  When -g2 and above we even need to force
913              emitting of CFI labels and add to list a DW_CFA_set_loc for
914              convert_cfa_to_fb_loc_list purposes.  If we're generating
915              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
916              convert_cfa_to_fb_loc_list.  */
917           if (dwarf_version == 2
918               && debug_info_level > DINFO_LEVEL_TERSE
919               && (write_symbols == DWARF2_DEBUG
920                   || write_symbols == VMS_AND_DWARF2_DEBUG))
921             {
922               switch (cfi->dw_cfi_opc)
923                 {
924                 case DW_CFA_def_cfa_offset:
925                 case DW_CFA_def_cfa_offset_sf:
926                 case DW_CFA_def_cfa_register:
927                 case DW_CFA_def_cfa:
928                 case DW_CFA_def_cfa_sf:
929                 case DW_CFA_def_cfa_expression:
930                 case DW_CFA_restore_state:
931                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
932                     label = dwarf2out_cfi_label (true);
933
934                   if (fde->dw_fde_current_label == NULL
935                       || strcmp (label, fde->dw_fde_current_label) != 0)
936                     {
937                       dw_cfi_ref xcfi;
938
939                       label = xstrdup (label);
940
941                       /* Set the location counter to the new label.  */
942                       xcfi = new_cfi ();
943                       /* It doesn't metter whether DW_CFA_set_loc
944                          or DW_CFA_advance_loc4 is added here, those aren't
945                          emitted into assembly, only looked up by
946                          convert_cfa_to_fb_loc_list.  */
947                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
948                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
949                       add_cfi (&fde->dw_fde_cfi, xcfi);
950                       fde->dw_fde_current_label = label;
951                     }
952                   break;
953                 default:
954                   break;
955                 }
956             }
957
958           output_cfi_directive (cfi);
959
960           list_head = &fde->dw_fde_cfi;
961           any_cfis_emitted = true;
962         }
963       /* ??? If this is a CFI for the CIE, we don't emit.  This
964          assumes that the standard CIE contents that the assembler
965          uses matches the standard CIE contents that the compiler
966          uses.  This is probably a bad assumption.  I'm not quite
967          sure how to address this for now.  */
968     }
969   else if (label)
970     {
971       dw_fde_ref fde = current_fde ();
972
973       gcc_assert (fde != NULL);
974
975       if (*label == 0)
976         label = dwarf2out_cfi_label (false);
977
978       if (fde->dw_fde_current_label == NULL
979           || strcmp (label, fde->dw_fde_current_label) != 0)
980         {
981           dw_cfi_ref xcfi;
982
983           label = xstrdup (label);
984
985           /* Set the location counter to the new label.  */
986           xcfi = new_cfi ();
987           /* If we have a current label, advance from there, otherwise
988              set the location directly using set_loc.  */
989           xcfi->dw_cfi_opc = fde->dw_fde_current_label
990                              ? DW_CFA_advance_loc4
991                              : DW_CFA_set_loc;
992           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
993           add_cfi (&fde->dw_fde_cfi, xcfi);
994
995           fde->dw_fde_current_label = label;
996         }
997
998       list_head = &fde->dw_fde_cfi;
999       any_cfis_emitted = true;
1000     }
1001
1002   add_cfi (list_head, cfi);
1003 }
1004
1005 /* Subroutine of lookup_cfa.  */
1006
1007 static void
1008 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1009 {
1010   switch (cfi->dw_cfi_opc)
1011     {
1012     case DW_CFA_def_cfa_offset:
1013     case DW_CFA_def_cfa_offset_sf:
1014       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1015       break;
1016     case DW_CFA_def_cfa_register:
1017       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1018       break;
1019     case DW_CFA_def_cfa:
1020     case DW_CFA_def_cfa_sf:
1021       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1022       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1023       break;
1024     case DW_CFA_def_cfa_expression:
1025       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1026       break;
1027
1028     case DW_CFA_remember_state:
1029       gcc_assert (!remember->in_use);
1030       *remember = *loc;
1031       remember->in_use = 1;
1032       break;
1033     case DW_CFA_restore_state:
1034       gcc_assert (remember->in_use);
1035       *loc = *remember;
1036       remember->in_use = 0;
1037       break;
1038
1039     default:
1040       break;
1041     }
1042 }
1043
1044 /* Find the previous value for the CFA.  */
1045
1046 static void
1047 lookup_cfa (dw_cfa_location *loc)
1048 {
1049   dw_cfi_ref cfi;
1050   dw_fde_ref fde;
1051   dw_cfa_location remember;
1052
1053   memset (loc, 0, sizeof (*loc));
1054   loc->reg = INVALID_REGNUM;
1055   remember = *loc;
1056
1057   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1058     lookup_cfa_1 (cfi, loc, &remember);
1059
1060   fde = current_fde ();
1061   if (fde)
1062     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1063       lookup_cfa_1 (cfi, loc, &remember);
1064 }
1065
1066 /* The current rule for calculating the DWARF2 canonical frame address.  */
1067 static dw_cfa_location cfa;
1068
1069 /* The register used for saving registers to the stack, and its offset
1070    from the CFA.  */
1071 static dw_cfa_location cfa_store;
1072
1073 /* The current save location around an epilogue.  */
1074 static dw_cfa_location cfa_remember;
1075
1076 /* The running total of the size of arguments pushed onto the stack.  */
1077 static HOST_WIDE_INT args_size;
1078
1079 /* The last args_size we actually output.  */
1080 static HOST_WIDE_INT old_args_size;
1081
1082 /* Entry point to update the canonical frame address (CFA).
1083    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1084    calculated from REG+OFFSET.  */
1085
1086 void
1087 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1088 {
1089   dw_cfa_location loc;
1090   loc.indirect = 0;
1091   loc.base_offset = 0;
1092   loc.reg = reg;
1093   loc.offset = offset;
1094   def_cfa_1 (label, &loc);
1095 }
1096
1097 /* Determine if two dw_cfa_location structures define the same data.  */
1098
1099 static bool
1100 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1101 {
1102   return (loc1->reg == loc2->reg
1103           && loc1->offset == loc2->offset
1104           && loc1->indirect == loc2->indirect
1105           && (loc1->indirect == 0
1106               || loc1->base_offset == loc2->base_offset));
1107 }
1108
1109 /* This routine does the actual work.  The CFA is now calculated from
1110    the dw_cfa_location structure.  */
1111
1112 static void
1113 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1114 {
1115   dw_cfi_ref cfi;
1116   dw_cfa_location old_cfa, loc;
1117
1118   cfa = *loc_p;
1119   loc = *loc_p;
1120
1121   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1122     cfa_store.offset = loc.offset;
1123
1124   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1125   lookup_cfa (&old_cfa);
1126
1127   /* If nothing changed, no need to issue any call frame instructions.  */
1128   if (cfa_equal_p (&loc, &old_cfa))
1129     return;
1130
1131   cfi = new_cfi ();
1132
1133   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1134     {
1135       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1136          the CFA register did not change but the offset did.  The data
1137          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1138          in the assembler via the .cfi_def_cfa_offset directive.  */
1139       if (loc.offset < 0)
1140         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1141       else
1142         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1143       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1144     }
1145
1146 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1147   else if (loc.offset == old_cfa.offset
1148            && old_cfa.reg != INVALID_REGNUM
1149            && !loc.indirect
1150            && !old_cfa.indirect)
1151     {
1152       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1153          indicating the CFA register has changed to <register> but the
1154          offset has not changed.  */
1155       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1156       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1157     }
1158 #endif
1159
1160   else if (loc.indirect == 0)
1161     {
1162       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1163          indicating the CFA register has changed to <register> with
1164          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1165          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1166          directive.  */
1167       if (loc.offset < 0)
1168         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1169       else
1170         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1171       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1172       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1173     }
1174   else
1175     {
1176       /* Construct a DW_CFA_def_cfa_expression instruction to
1177          calculate the CFA using a full location expression since no
1178          register-offset pair is available.  */
1179       struct dw_loc_descr_struct *loc_list;
1180
1181       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1182       loc_list = build_cfa_loc (&loc, 0);
1183       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1184     }
1185
1186   add_fde_cfi (label, cfi);
1187 }
1188
1189 /* Add the CFI for saving a register.  REG is the CFA column number.
1190    LABEL is passed to add_fde_cfi.
1191    If SREG is -1, the register is saved at OFFSET from the CFA;
1192    otherwise it is saved in SREG.  */
1193
1194 static void
1195 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1196 {
1197   dw_cfi_ref cfi = new_cfi ();
1198   dw_fde_ref fde = current_fde ();
1199
1200   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1201
1202   /* When stack is aligned, store REG using DW_CFA_expression with
1203      FP.  */
1204   if (fde
1205       && fde->stack_realign
1206       && sreg == INVALID_REGNUM)
1207     {
1208       cfi->dw_cfi_opc = DW_CFA_expression;
1209       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1210       cfi->dw_cfi_oprnd2.dw_cfi_loc
1211         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1212     }
1213   else if (sreg == INVALID_REGNUM)
1214     {
1215       if (need_data_align_sf_opcode (offset))
1216         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1217       else if (reg & ~0x3f)
1218         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1219       else
1220         cfi->dw_cfi_opc = DW_CFA_offset;
1221       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1222     }
1223   else if (sreg == reg)
1224     cfi->dw_cfi_opc = DW_CFA_same_value;
1225   else
1226     {
1227       cfi->dw_cfi_opc = DW_CFA_register;
1228       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1229     }
1230
1231   add_fde_cfi (label, cfi);
1232 }
1233
1234 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1235    This CFI tells the unwinder that it needs to restore the window registers
1236    from the previous frame's window save area.
1237
1238    ??? Perhaps we should note in the CIE where windows are saved (instead of
1239    assuming 0(cfa)) and what registers are in the window.  */
1240
1241 void
1242 dwarf2out_window_save (const char *label)
1243 {
1244   dw_cfi_ref cfi = new_cfi ();
1245
1246   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1247   add_fde_cfi (label, cfi);
1248 }
1249
1250 /* Entry point for saving a register to the stack.  REG is the GCC register
1251    number.  LABEL and OFFSET are passed to reg_save.  */
1252
1253 void
1254 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1255 {
1256   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1257 }
1258
1259 /* Entry point for saving the return address in the stack.
1260    LABEL and OFFSET are passed to reg_save.  */
1261
1262 void
1263 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1264 {
1265   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1266 }
1267
1268 /* Entry point for saving the return address in a register.
1269    LABEL and SREG are passed to reg_save.  */
1270
1271 void
1272 dwarf2out_return_reg (const char *label, unsigned int sreg)
1273 {
1274   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1275 }
1276
1277 /* Record the initial position of the return address.  RTL is
1278    INCOMING_RETURN_ADDR_RTX.  */
1279
1280 static void
1281 initial_return_save (rtx rtl)
1282 {
1283   unsigned int reg = INVALID_REGNUM;
1284   HOST_WIDE_INT offset = 0;
1285
1286   switch (GET_CODE (rtl))
1287     {
1288     case REG:
1289       /* RA is in a register.  */
1290       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1291       break;
1292
1293     case MEM:
1294       /* RA is on the stack.  */
1295       rtl = XEXP (rtl, 0);
1296       switch (GET_CODE (rtl))
1297         {
1298         case REG:
1299           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1300           offset = 0;
1301           break;
1302
1303         case PLUS:
1304           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1305           offset = INTVAL (XEXP (rtl, 1));
1306           break;
1307
1308         case MINUS:
1309           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1310           offset = -INTVAL (XEXP (rtl, 1));
1311           break;
1312
1313         default:
1314           gcc_unreachable ();
1315         }
1316
1317       break;
1318
1319     case PLUS:
1320       /* The return address is at some offset from any value we can
1321          actually load.  For instance, on the SPARC it is in %i7+8. Just
1322          ignore the offset for now; it doesn't matter for unwinding frames.  */
1323       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1324       initial_return_save (XEXP (rtl, 0));
1325       return;
1326
1327     default:
1328       gcc_unreachable ();
1329     }
1330
1331   if (reg != DWARF_FRAME_RETURN_COLUMN)
1332     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1333 }
1334
1335 /* Given a SET, calculate the amount of stack adjustment it
1336    contains.  */
1337
1338 static HOST_WIDE_INT
1339 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1340                      HOST_WIDE_INT cur_offset)
1341 {
1342   const_rtx src = SET_SRC (pattern);
1343   const_rtx dest = SET_DEST (pattern);
1344   HOST_WIDE_INT offset = 0;
1345   enum rtx_code code;
1346
1347   if (dest == stack_pointer_rtx)
1348     {
1349       code = GET_CODE (src);
1350
1351       /* Assume (set (reg sp) (reg whatever)) sets args_size
1352          level to 0.  */
1353       if (code == REG && src != stack_pointer_rtx)
1354         {
1355           offset = -cur_args_size;
1356 #ifndef STACK_GROWS_DOWNWARD
1357           offset = -offset;
1358 #endif
1359           return offset - cur_offset;
1360         }
1361
1362       if (! (code == PLUS || code == MINUS)
1363           || XEXP (src, 0) != stack_pointer_rtx
1364           || !CONST_INT_P (XEXP (src, 1)))
1365         return 0;
1366
1367       /* (set (reg sp) (plus (reg sp) (const_int))) */
1368       offset = INTVAL (XEXP (src, 1));
1369       if (code == PLUS)
1370         offset = -offset;
1371       return offset;
1372     }
1373
1374   if (MEM_P (src) && !MEM_P (dest))
1375     dest = src;
1376   if (MEM_P (dest))
1377     {
1378       /* (set (mem (pre_dec (reg sp))) (foo)) */
1379       src = XEXP (dest, 0);
1380       code = GET_CODE (src);
1381
1382       switch (code)
1383         {
1384         case PRE_MODIFY:
1385         case POST_MODIFY:
1386           if (XEXP (src, 0) == stack_pointer_rtx)
1387             {
1388               rtx val = XEXP (XEXP (src, 1), 1);
1389               /* We handle only adjustments by constant amount.  */
1390               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1391                           && CONST_INT_P (val));
1392               offset = -INTVAL (val);
1393               break;
1394             }
1395           return 0;
1396
1397         case PRE_DEC:
1398         case POST_DEC:
1399           if (XEXP (src, 0) == stack_pointer_rtx)
1400             {
1401               offset = GET_MODE_SIZE (GET_MODE (dest));
1402               break;
1403             }
1404           return 0;
1405
1406         case PRE_INC:
1407         case POST_INC:
1408           if (XEXP (src, 0) == stack_pointer_rtx)
1409             {
1410               offset = -GET_MODE_SIZE (GET_MODE (dest));
1411               break;
1412             }
1413           return 0;
1414
1415         default:
1416           return 0;
1417         }
1418     }
1419   else
1420     return 0;
1421
1422   return offset;
1423 }
1424
1425 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1426    indexed by INSN_UID.  */
1427
1428 static HOST_WIDE_INT *barrier_args_size;
1429
1430 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1431
1432 static HOST_WIDE_INT
1433 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1434                              VEC (rtx, heap) **next)
1435 {
1436   HOST_WIDE_INT offset = 0;
1437   int i;
1438
1439   if (! RTX_FRAME_RELATED_P (insn))
1440     {
1441       if (prologue_epilogue_contains (insn))
1442         /* Nothing */;
1443       else if (GET_CODE (PATTERN (insn)) == SET)
1444         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1445       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1446                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1447         {
1448           /* There may be stack adjustments inside compound insns.  Search
1449              for them.  */
1450           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1451             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1452               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1453                                              cur_args_size, offset);
1454         }
1455     }
1456   else
1457     {
1458       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1459
1460       if (expr)
1461         {
1462           expr = XEXP (expr, 0);
1463           if (GET_CODE (expr) == PARALLEL
1464               || GET_CODE (expr) == SEQUENCE)
1465             for (i = 1; i < XVECLEN (expr, 0); i++)
1466               {
1467                 rtx elem = XVECEXP (expr, 0, i);
1468
1469                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1470                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1471               }
1472         }
1473     }
1474
1475 #ifndef STACK_GROWS_DOWNWARD
1476   offset = -offset;
1477 #endif
1478
1479   cur_args_size += offset;
1480   if (cur_args_size < 0)
1481     cur_args_size = 0;
1482
1483   if (JUMP_P (insn))
1484     {
1485       rtx dest = JUMP_LABEL (insn);
1486
1487       if (dest)
1488         {
1489           if (barrier_args_size [INSN_UID (dest)] < 0)
1490             {
1491               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1492               VEC_safe_push (rtx, heap, *next, dest);
1493             }
1494         }
1495     }
1496
1497   return cur_args_size;
1498 }
1499
1500 /* Walk the whole function and compute args_size on BARRIERs.  */
1501
1502 static void
1503 compute_barrier_args_size (void)
1504 {
1505   int max_uid = get_max_uid (), i;
1506   rtx insn;
1507   VEC (rtx, heap) *worklist, *next, *tmp;
1508
1509   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1510   for (i = 0; i < max_uid; i++)
1511     barrier_args_size[i] = -1;
1512
1513   worklist = VEC_alloc (rtx, heap, 20);
1514   next = VEC_alloc (rtx, heap, 20);
1515   insn = get_insns ();
1516   barrier_args_size[INSN_UID (insn)] = 0;
1517   VEC_quick_push (rtx, worklist, insn);
1518   for (;;)
1519     {
1520       while (!VEC_empty (rtx, worklist))
1521         {
1522           rtx prev, body, first_insn;
1523           HOST_WIDE_INT cur_args_size;
1524
1525           first_insn = insn = VEC_pop (rtx, worklist);
1526           cur_args_size = barrier_args_size[INSN_UID (insn)];
1527           prev = prev_nonnote_insn (insn);
1528           if (prev && BARRIER_P (prev))
1529             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1530
1531           for (; insn; insn = NEXT_INSN (insn))
1532             {
1533               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1534                 continue;
1535               if (BARRIER_P (insn))
1536                 break;
1537
1538               if (LABEL_P (insn))
1539                 {
1540                   if (insn == first_insn)
1541                     continue;
1542                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1543                     {
1544                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1545                       continue;
1546                     }
1547                   else
1548                     {
1549                       /* The insns starting with this label have been
1550                          already scanned or are in the worklist.  */
1551                       break;
1552                     }
1553                 }
1554
1555               body = PATTERN (insn);
1556               if (GET_CODE (body) == SEQUENCE)
1557                 {
1558                   HOST_WIDE_INT dest_args_size = cur_args_size;
1559                   for (i = 1; i < XVECLEN (body, 0); i++)
1560                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1561                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1562                       dest_args_size
1563                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1564                                                        dest_args_size, &next);
1565                     else
1566                       cur_args_size
1567                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1568                                                        cur_args_size, &next);
1569
1570                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1571                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1572                                                  dest_args_size, &next);
1573                   else
1574                     cur_args_size
1575                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1576                                                      cur_args_size, &next);
1577                 }
1578               else
1579                 cur_args_size
1580                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1581             }
1582         }
1583
1584       if (VEC_empty (rtx, next))
1585         break;
1586
1587       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1588       tmp = next;
1589       next = worklist;
1590       worklist = tmp;
1591       VEC_truncate (rtx, next, 0);
1592     }
1593
1594   VEC_free (rtx, heap, worklist);
1595   VEC_free (rtx, heap, next);
1596 }
1597
1598 /* Add a CFI to update the running total of the size of arguments
1599    pushed onto the stack.  */
1600
1601 static void
1602 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1603 {
1604   dw_cfi_ref cfi;
1605
1606   if (size == old_args_size)
1607     return;
1608
1609   old_args_size = size;
1610
1611   cfi = new_cfi ();
1612   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1613   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1614   add_fde_cfi (label, cfi);
1615 }
1616
1617 /* Record a stack adjustment of OFFSET bytes.  */
1618
1619 static void
1620 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1621 {
1622   if (cfa.reg == STACK_POINTER_REGNUM)
1623     cfa.offset += offset;
1624
1625   if (cfa_store.reg == STACK_POINTER_REGNUM)
1626     cfa_store.offset += offset;
1627
1628   if (ACCUMULATE_OUTGOING_ARGS)
1629     return;
1630
1631 #ifndef STACK_GROWS_DOWNWARD
1632   offset = -offset;
1633 #endif
1634
1635   args_size += offset;
1636   if (args_size < 0)
1637     args_size = 0;
1638
1639   def_cfa_1 (label, &cfa);
1640   if (flag_asynchronous_unwind_tables)
1641     dwarf2out_args_size (label, args_size);
1642 }
1643
1644 /* Check INSN to see if it looks like a push or a stack adjustment, and
1645    make a note of it if it does.  EH uses this information to find out
1646    how much extra space it needs to pop off the stack.  */
1647
1648 static void
1649 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1650 {
1651   HOST_WIDE_INT offset;
1652   const char *label;
1653   int i;
1654
1655   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1656      with this function.  Proper support would require all frame-related
1657      insns to be marked, and to be able to handle saving state around
1658      epilogues textually in the middle of the function.  */
1659   if (prologue_epilogue_contains (insn))
1660     return;
1661
1662   /* If INSN is an instruction from target of an annulled branch, the
1663      effects are for the target only and so current argument size
1664      shouldn't change at all.  */
1665   if (final_sequence
1666       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1667       && INSN_FROM_TARGET_P (insn))
1668     return;
1669
1670   /* If only calls can throw, and we have a frame pointer,
1671      save up adjustments until we see the CALL_INSN.  */
1672   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1673     {
1674       if (CALL_P (insn) && !after_p)
1675         {
1676           /* Extract the size of the args from the CALL rtx itself.  */
1677           insn = PATTERN (insn);
1678           if (GET_CODE (insn) == PARALLEL)
1679             insn = XVECEXP (insn, 0, 0);
1680           if (GET_CODE (insn) == SET)
1681             insn = SET_SRC (insn);
1682           gcc_assert (GET_CODE (insn) == CALL);
1683           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1684         }
1685       return;
1686     }
1687
1688   if (CALL_P (insn) && !after_p)
1689     {
1690       if (!flag_asynchronous_unwind_tables)
1691         dwarf2out_args_size ("", args_size);
1692       return;
1693     }
1694   else if (BARRIER_P (insn))
1695     {
1696       /* Don't call compute_barrier_args_size () if the only
1697          BARRIER is at the end of function.  */
1698       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1699         compute_barrier_args_size ();
1700       if (barrier_args_size == NULL)
1701         offset = 0;
1702       else
1703         {
1704           offset = barrier_args_size[INSN_UID (insn)];
1705           if (offset < 0)
1706             offset = 0;
1707         }
1708
1709       offset -= args_size;
1710 #ifndef STACK_GROWS_DOWNWARD
1711       offset = -offset;
1712 #endif
1713     }
1714   else if (GET_CODE (PATTERN (insn)) == SET)
1715     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1716   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1717            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1718     {
1719       /* There may be stack adjustments inside compound insns.  Search
1720          for them.  */
1721       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1722         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1723           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1724                                          args_size, offset);
1725     }
1726   else
1727     return;
1728
1729   if (offset == 0)
1730     return;
1731
1732   label = dwarf2out_cfi_label (false);
1733   dwarf2out_stack_adjust (offset, label);
1734 }
1735
1736 /* We delay emitting a register save until either (a) we reach the end
1737    of the prologue or (b) the register is clobbered.  This clusters
1738    register saves so that there are fewer pc advances.  */
1739
1740 struct GTY(()) queued_reg_save {
1741   struct queued_reg_save *next;
1742   rtx reg;
1743   HOST_WIDE_INT cfa_offset;
1744   rtx saved_reg;
1745 };
1746
1747 static GTY(()) struct queued_reg_save *queued_reg_saves;
1748
1749 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1750 struct GTY(()) reg_saved_in_data {
1751   rtx orig_reg;
1752   rtx saved_in_reg;
1753 };
1754
1755 /* A list of registers saved in other registers.
1756    The list intentionally has a small maximum capacity of 4; if your
1757    port needs more than that, you might consider implementing a
1758    more efficient data structure.  */
1759 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1760 static GTY(()) size_t num_regs_saved_in_regs;
1761
1762 static const char *last_reg_save_label;
1763
1764 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1765    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1766
1767 static void
1768 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1769 {
1770   struct queued_reg_save *q;
1771
1772   /* Duplicates waste space, but it's also necessary to remove them
1773      for correctness, since the queue gets output in reverse
1774      order.  */
1775   for (q = queued_reg_saves; q != NULL; q = q->next)
1776     if (REGNO (q->reg) == REGNO (reg))
1777       break;
1778
1779   if (q == NULL)
1780     {
1781       q = ggc_alloc_queued_reg_save ();
1782       q->next = queued_reg_saves;
1783       queued_reg_saves = q;
1784     }
1785
1786   q->reg = reg;
1787   q->cfa_offset = offset;
1788   q->saved_reg = sreg;
1789
1790   last_reg_save_label = label;
1791 }
1792
1793 /* Output all the entries in QUEUED_REG_SAVES.  */
1794
1795 void
1796 dwarf2out_flush_queued_reg_saves (void)
1797 {
1798   struct queued_reg_save *q;
1799
1800   for (q = queued_reg_saves; q; q = q->next)
1801     {
1802       size_t i;
1803       unsigned int reg, sreg;
1804
1805       for (i = 0; i < num_regs_saved_in_regs; i++)
1806         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1807           break;
1808       if (q->saved_reg && i == num_regs_saved_in_regs)
1809         {
1810           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1811           num_regs_saved_in_regs++;
1812         }
1813       if (i != num_regs_saved_in_regs)
1814         {
1815           regs_saved_in_regs[i].orig_reg = q->reg;
1816           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1817         }
1818
1819       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1820       if (q->saved_reg)
1821         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1822       else
1823         sreg = INVALID_REGNUM;
1824       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1825     }
1826
1827   queued_reg_saves = NULL;
1828   last_reg_save_label = NULL;
1829 }
1830
1831 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1832    location for?  Or, does it clobber a register which we've previously
1833    said that some other register is saved in, and for which we now
1834    have a new location for?  */
1835
1836 static bool
1837 clobbers_queued_reg_save (const_rtx insn)
1838 {
1839   struct queued_reg_save *q;
1840
1841   for (q = queued_reg_saves; q; q = q->next)
1842     {
1843       size_t i;
1844       if (modified_in_p (q->reg, insn))
1845         return true;
1846       for (i = 0; i < num_regs_saved_in_regs; i++)
1847         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1848             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1849           return true;
1850     }
1851
1852   return false;
1853 }
1854
1855 /* Entry point for saving the first register into the second.  */
1856
1857 void
1858 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1859 {
1860   size_t i;
1861   unsigned int regno, sregno;
1862
1863   for (i = 0; i < num_regs_saved_in_regs; i++)
1864     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1865       break;
1866   if (i == num_regs_saved_in_regs)
1867     {
1868       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1869       num_regs_saved_in_regs++;
1870     }
1871   regs_saved_in_regs[i].orig_reg = reg;
1872   regs_saved_in_regs[i].saved_in_reg = sreg;
1873
1874   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1875   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1876   reg_save (label, regno, sregno, 0);
1877 }
1878
1879 /* What register, if any, is currently saved in REG?  */
1880
1881 static rtx
1882 reg_saved_in (rtx reg)
1883 {
1884   unsigned int regn = REGNO (reg);
1885   size_t i;
1886   struct queued_reg_save *q;
1887
1888   for (q = queued_reg_saves; q; q = q->next)
1889     if (q->saved_reg && regn == REGNO (q->saved_reg))
1890       return q->reg;
1891
1892   for (i = 0; i < num_regs_saved_in_regs; i++)
1893     if (regs_saved_in_regs[i].saved_in_reg
1894         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1895       return regs_saved_in_regs[i].orig_reg;
1896
1897   return NULL_RTX;
1898 }
1899
1900
1901 /* A temporary register holding an integral value used in adjusting SP
1902    or setting up the store_reg.  The "offset" field holds the integer
1903    value, not an offset.  */
1904 static dw_cfa_location cfa_temp;
1905
1906 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1907
1908 static void
1909 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1910 {
1911   memset (&cfa, 0, sizeof (cfa));
1912
1913   switch (GET_CODE (pat))
1914     {
1915     case PLUS:
1916       cfa.reg = REGNO (XEXP (pat, 0));
1917       cfa.offset = INTVAL (XEXP (pat, 1));
1918       break;
1919
1920     case REG:
1921       cfa.reg = REGNO (pat);
1922       break;
1923
1924     case MEM:
1925       cfa.indirect = 1;
1926       pat = XEXP (pat, 0);
1927       if (GET_CODE (pat) == PLUS)
1928         {
1929           cfa.base_offset = INTVAL (XEXP (pat, 1));
1930           pat = XEXP (pat, 0);
1931         }
1932       cfa.reg = REGNO (pat);
1933       break;
1934
1935     default:
1936       /* Recurse and define an expression.  */
1937       gcc_unreachable ();
1938     }
1939
1940   def_cfa_1 (label, &cfa);
1941 }
1942
1943 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1944
1945 static void
1946 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1947 {
1948   rtx src, dest;
1949
1950   gcc_assert (GET_CODE (pat) == SET);
1951   dest = XEXP (pat, 0);
1952   src = XEXP (pat, 1);
1953
1954   switch (GET_CODE (src))
1955     {
1956     case PLUS:
1957       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1958       cfa.offset -= INTVAL (XEXP (src, 1));
1959       break;
1960
1961     case REG:
1962         break;
1963
1964     default:
1965         gcc_unreachable ();
1966     }
1967
1968   cfa.reg = REGNO (dest);
1969   gcc_assert (cfa.indirect == 0);
1970
1971   def_cfa_1 (label, &cfa);
1972 }
1973
1974 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1975
1976 static void
1977 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1978 {
1979   HOST_WIDE_INT offset;
1980   rtx src, addr, span;
1981
1982   src = XEXP (set, 1);
1983   addr = XEXP (set, 0);
1984   gcc_assert (MEM_P (addr));
1985   addr = XEXP (addr, 0);
1986
1987   /* As documented, only consider extremely simple addresses.  */
1988   switch (GET_CODE (addr))
1989     {
1990     case REG:
1991       gcc_assert (REGNO (addr) == cfa.reg);
1992       offset = -cfa.offset;
1993       break;
1994     case PLUS:
1995       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1996       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1997       break;
1998     default:
1999       gcc_unreachable ();
2000     }
2001
2002   span = targetm.dwarf_register_span (src);
2003
2004   /* ??? We'd like to use queue_reg_save, but we need to come up with
2005      a different flushing heuristic for epilogues.  */
2006   if (!span)
2007     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2008   else
2009     {
2010       /* We have a PARALLEL describing where the contents of SRC live.
2011          Queue register saves for each piece of the PARALLEL.  */
2012       int par_index;
2013       int limit;
2014       HOST_WIDE_INT span_offset = offset;
2015
2016       gcc_assert (GET_CODE (span) == PARALLEL);
2017
2018       limit = XVECLEN (span, 0);
2019       for (par_index = 0; par_index < limit; par_index++)
2020         {
2021           rtx elem = XVECEXP (span, 0, par_index);
2022
2023           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2024                     INVALID_REGNUM, span_offset);
2025           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2026         }
2027     }
2028 }
2029
2030 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2031
2032 static void
2033 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2034 {
2035   rtx src, dest;
2036   unsigned sregno, dregno;
2037
2038   src = XEXP (set, 1);
2039   dest = XEXP (set, 0);
2040
2041   if (src == pc_rtx)
2042     sregno = DWARF_FRAME_RETURN_COLUMN;
2043   else
2044     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2045
2046   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2047
2048   /* ??? We'd like to use queue_reg_save, but we need to come up with
2049      a different flushing heuristic for epilogues.  */
2050   reg_save (label, sregno, dregno, 0);
2051 }
2052
2053 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2054
2055 static void
2056 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2057 {
2058   rtx src, dest, span;
2059   dw_cfi_ref cfi = new_cfi ();
2060
2061   dest = SET_DEST (set);
2062   src = SET_SRC (set);
2063
2064   gcc_assert (REG_P (src));
2065   gcc_assert (MEM_P (dest));
2066
2067   span = targetm.dwarf_register_span (src);
2068   gcc_assert (!span);
2069
2070   cfi->dw_cfi_opc = DW_CFA_expression;
2071   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2072   cfi->dw_cfi_oprnd2.dw_cfi_loc
2073     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2074                           VAR_INIT_STATUS_INITIALIZED);
2075
2076   /* ??? We'd like to use queue_reg_save, were the interface different,
2077      and, as above, we could manage flushing for epilogues.  */
2078   add_fde_cfi (label, cfi);
2079 }
2080
2081 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2082
2083 static void
2084 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2085 {
2086   dw_cfi_ref cfi = new_cfi ();
2087   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2088
2089   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2090   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2091
2092   add_fde_cfi (label, cfi);
2093 }
2094
2095 /* Record call frame debugging information for an expression EXPR,
2096    which either sets SP or FP (adjusting how we calculate the frame
2097    address) or saves a register to the stack or another register.
2098    LABEL indicates the address of EXPR.
2099
2100    This function encodes a state machine mapping rtxes to actions on
2101    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2102    users need not read the source code.
2103
2104   The High-Level Picture
2105
2106   Changes in the register we use to calculate the CFA: Currently we
2107   assume that if you copy the CFA register into another register, we
2108   should take the other one as the new CFA register; this seems to
2109   work pretty well.  If it's wrong for some target, it's simple
2110   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2111
2112   Changes in the register we use for saving registers to the stack:
2113   This is usually SP, but not always.  Again, we deduce that if you
2114   copy SP into another register (and SP is not the CFA register),
2115   then the new register is the one we will be using for register
2116   saves.  This also seems to work.
2117
2118   Register saves: There's not much guesswork about this one; if
2119   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2120   register save, and the register used to calculate the destination
2121   had better be the one we think we're using for this purpose.
2122   It's also assumed that a copy from a call-saved register to another
2123   register is saving that register if RTX_FRAME_RELATED_P is set on
2124   that instruction.  If the copy is from a call-saved register to
2125   the *same* register, that means that the register is now the same
2126   value as in the caller.
2127
2128   Except: If the register being saved is the CFA register, and the
2129   offset is nonzero, we are saving the CFA, so we assume we have to
2130   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2131   the intent is to save the value of SP from the previous frame.
2132
2133   In addition, if a register has previously been saved to a different
2134   register,
2135
2136   Invariants / Summaries of Rules
2137
2138   cfa          current rule for calculating the CFA.  It usually
2139                consists of a register and an offset.
2140   cfa_store    register used by prologue code to save things to the stack
2141                cfa_store.offset is the offset from the value of
2142                cfa_store.reg to the actual CFA
2143   cfa_temp     register holding an integral value.  cfa_temp.offset
2144                stores the value, which will be used to adjust the
2145                stack pointer.  cfa_temp is also used like cfa_store,
2146                to track stores to the stack via fp or a temp reg.
2147
2148   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2149                with cfa.reg as the first operand changes the cfa.reg and its
2150                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2151                cfa_temp.offset.
2152
2153   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2154                expression yielding a constant.  This sets cfa_temp.reg
2155                and cfa_temp.offset.
2156
2157   Rule 5:      Create a new register cfa_store used to save items to the
2158                stack.
2159
2160   Rules 10-14: Save a register to the stack.  Define offset as the
2161                difference of the original location and cfa_store's
2162                location (or cfa_temp's location if cfa_temp is used).
2163
2164   Rules 16-20: If AND operation happens on sp in prologue, we assume
2165                stack is realigned.  We will use a group of DW_OP_XXX
2166                expressions to represent the location of the stored
2167                register instead of CFA+offset.
2168
2169   The Rules
2170
2171   "{a,b}" indicates a choice of a xor b.
2172   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2173
2174   Rule 1:
2175   (set <reg1> <reg2>:cfa.reg)
2176   effects: cfa.reg = <reg1>
2177            cfa.offset unchanged
2178            cfa_temp.reg = <reg1>
2179            cfa_temp.offset = cfa.offset
2180
2181   Rule 2:
2182   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2183                               {<const_int>,<reg>:cfa_temp.reg}))
2184   effects: cfa.reg = sp if fp used
2185            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2186            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2187              if cfa_store.reg==sp
2188
2189   Rule 3:
2190   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2191   effects: cfa.reg = fp
2192            cfa_offset += +/- <const_int>
2193
2194   Rule 4:
2195   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2196   constraints: <reg1> != fp
2197                <reg1> != sp
2198   effects: cfa.reg = <reg1>
2199            cfa_temp.reg = <reg1>
2200            cfa_temp.offset = cfa.offset
2201
2202   Rule 5:
2203   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2204   constraints: <reg1> != fp
2205                <reg1> != sp
2206   effects: cfa_store.reg = <reg1>
2207            cfa_store.offset = cfa.offset - cfa_temp.offset
2208
2209   Rule 6:
2210   (set <reg> <const_int>)
2211   effects: cfa_temp.reg = <reg>
2212            cfa_temp.offset = <const_int>
2213
2214   Rule 7:
2215   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2216   effects: cfa_temp.reg = <reg1>
2217            cfa_temp.offset |= <const_int>
2218
2219   Rule 8:
2220   (set <reg> (high <exp>))
2221   effects: none
2222
2223   Rule 9:
2224   (set <reg> (lo_sum <exp> <const_int>))
2225   effects: cfa_temp.reg = <reg>
2226            cfa_temp.offset = <const_int>
2227
2228   Rule 10:
2229   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2230   effects: cfa_store.offset -= <const_int>
2231            cfa.offset = cfa_store.offset if cfa.reg == sp
2232            cfa.reg = sp
2233            cfa.base_offset = -cfa_store.offset
2234
2235   Rule 11:
2236   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2237   effects: cfa_store.offset += -/+ mode_size(mem)
2238            cfa.offset = cfa_store.offset if cfa.reg == sp
2239            cfa.reg = sp
2240            cfa.base_offset = -cfa_store.offset
2241
2242   Rule 12:
2243   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2244
2245        <reg2>)
2246   effects: cfa.reg = <reg1>
2247            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2248
2249   Rule 13:
2250   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2251   effects: cfa.reg = <reg1>
2252            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2253
2254   Rule 14:
2255   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2256   effects: cfa.reg = <reg1>
2257            cfa.base_offset = -cfa_temp.offset
2258            cfa_temp.offset -= mode_size(mem)
2259
2260   Rule 15:
2261   (set <reg> {unspec, unspec_volatile})
2262   effects: target-dependent
2263
2264   Rule 16:
2265   (set sp (and: sp <const_int>))
2266   constraints: cfa_store.reg == sp
2267   effects: current_fde.stack_realign = 1
2268            cfa_store.offset = 0
2269            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2270
2271   Rule 17:
2272   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2273   effects: cfa_store.offset += -/+ mode_size(mem)
2274
2275   Rule 18:
2276   (set (mem ({pre_inc, pre_dec} sp)) fp)
2277   constraints: fde->stack_realign == 1
2278   effects: cfa_store.offset = 0
2279            cfa.reg != HARD_FRAME_POINTER_REGNUM
2280
2281   Rule 19:
2282   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2283   constraints: fde->stack_realign == 1
2284                && cfa.offset == 0
2285                && cfa.indirect == 0
2286                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2287   effects: Use DW_CFA_def_cfa_expression to define cfa
2288            cfa.reg == fde->drap_reg  */
2289
2290 static void
2291 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2292 {
2293   rtx src, dest, span;
2294   HOST_WIDE_INT offset;
2295   dw_fde_ref fde;
2296
2297   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2298      the PARALLEL independently. The first element is always processed if
2299      it is a SET. This is for backward compatibility.   Other elements
2300      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2301      flag is set in them.  */
2302   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2303     {
2304       int par_index;
2305       int limit = XVECLEN (expr, 0);
2306       rtx elem;
2307
2308       /* PARALLELs have strict read-modify-write semantics, so we
2309          ought to evaluate every rvalue before changing any lvalue.
2310          It's cumbersome to do that in general, but there's an
2311          easy approximation that is enough for all current users:
2312          handle register saves before register assignments.  */
2313       if (GET_CODE (expr) == PARALLEL)
2314         for (par_index = 0; par_index < limit; par_index++)
2315           {
2316             elem = XVECEXP (expr, 0, par_index);
2317             if (GET_CODE (elem) == SET
2318                 && MEM_P (SET_DEST (elem))
2319                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2320               dwarf2out_frame_debug_expr (elem, label);
2321           }
2322
2323       for (par_index = 0; par_index < limit; par_index++)
2324         {
2325           elem = XVECEXP (expr, 0, par_index);
2326           if (GET_CODE (elem) == SET
2327               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2328               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2329             dwarf2out_frame_debug_expr (elem, label);
2330           else if (GET_CODE (elem) == SET
2331                    && par_index != 0
2332                    && !RTX_FRAME_RELATED_P (elem))
2333             {
2334               /* Stack adjustment combining might combine some post-prologue
2335                  stack adjustment into a prologue stack adjustment.  */
2336               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2337
2338               if (offset != 0)
2339                 dwarf2out_stack_adjust (offset, label);
2340             }
2341         }
2342       return;
2343     }
2344
2345   gcc_assert (GET_CODE (expr) == SET);
2346
2347   src = SET_SRC (expr);
2348   dest = SET_DEST (expr);
2349
2350   if (REG_P (src))
2351     {
2352       rtx rsi = reg_saved_in (src);
2353       if (rsi)
2354         src = rsi;
2355     }
2356
2357   fde = current_fde ();
2358
2359   switch (GET_CODE (dest))
2360     {
2361     case REG:
2362       switch (GET_CODE (src))
2363         {
2364           /* Setting FP from SP.  */
2365         case REG:
2366           if (cfa.reg == (unsigned) REGNO (src))
2367             {
2368               /* Rule 1 */
2369               /* Update the CFA rule wrt SP or FP.  Make sure src is
2370                  relative to the current CFA register.
2371
2372                  We used to require that dest be either SP or FP, but the
2373                  ARM copies SP to a temporary register, and from there to
2374                  FP.  So we just rely on the backends to only set
2375                  RTX_FRAME_RELATED_P on appropriate insns.  */
2376               cfa.reg = REGNO (dest);
2377               cfa_temp.reg = cfa.reg;
2378               cfa_temp.offset = cfa.offset;
2379             }
2380           else
2381             {
2382               /* Saving a register in a register.  */
2383               gcc_assert (!fixed_regs [REGNO (dest)]
2384                           /* For the SPARC and its register window.  */
2385                           || (DWARF_FRAME_REGNUM (REGNO (src))
2386                               == DWARF_FRAME_RETURN_COLUMN));
2387
2388               /* After stack is aligned, we can only save SP in FP
2389                  if drap register is used.  In this case, we have
2390                  to restore stack pointer with the CFA value and we
2391                  don't generate this DWARF information.  */
2392               if (fde
2393                   && fde->stack_realign
2394                   && REGNO (src) == STACK_POINTER_REGNUM)
2395                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2396                             && fde->drap_reg != INVALID_REGNUM
2397                             && cfa.reg != REGNO (src));
2398               else
2399                 queue_reg_save (label, src, dest, 0);
2400             }
2401           break;
2402
2403         case PLUS:
2404         case MINUS:
2405         case LO_SUM:
2406           if (dest == stack_pointer_rtx)
2407             {
2408               /* Rule 2 */
2409               /* Adjusting SP.  */
2410               switch (GET_CODE (XEXP (src, 1)))
2411                 {
2412                 case CONST_INT:
2413                   offset = INTVAL (XEXP (src, 1));
2414                   break;
2415                 case REG:
2416                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2417                               == cfa_temp.reg);
2418                   offset = cfa_temp.offset;
2419                   break;
2420                 default:
2421                   gcc_unreachable ();
2422                 }
2423
2424               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2425                 {
2426                   /* Restoring SP from FP in the epilogue.  */
2427                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2428                   cfa.reg = STACK_POINTER_REGNUM;
2429                 }
2430               else if (GET_CODE (src) == LO_SUM)
2431                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2432                 ;
2433               else
2434                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2435
2436               if (GET_CODE (src) != MINUS)
2437                 offset = -offset;
2438               if (cfa.reg == STACK_POINTER_REGNUM)
2439                 cfa.offset += offset;
2440               if (cfa_store.reg == STACK_POINTER_REGNUM)
2441                 cfa_store.offset += offset;
2442             }
2443           else if (dest == hard_frame_pointer_rtx)
2444             {
2445               /* Rule 3 */
2446               /* Either setting the FP from an offset of the SP,
2447                  or adjusting the FP */
2448               gcc_assert (frame_pointer_needed);
2449
2450               gcc_assert (REG_P (XEXP (src, 0))
2451                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2452                           && CONST_INT_P (XEXP (src, 1)));
2453               offset = INTVAL (XEXP (src, 1));
2454               if (GET_CODE (src) != MINUS)
2455                 offset = -offset;
2456               cfa.offset += offset;
2457               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2458             }
2459           else
2460             {
2461               gcc_assert (GET_CODE (src) != MINUS);
2462
2463               /* Rule 4 */
2464               if (REG_P (XEXP (src, 0))
2465                   && REGNO (XEXP (src, 0)) == cfa.reg
2466                   && CONST_INT_P (XEXP (src, 1)))
2467                 {
2468                   /* Setting a temporary CFA register that will be copied
2469                      into the FP later on.  */
2470                   offset = - INTVAL (XEXP (src, 1));
2471                   cfa.offset += offset;
2472                   cfa.reg = REGNO (dest);
2473                   /* Or used to save regs to the stack.  */
2474                   cfa_temp.reg = cfa.reg;
2475                   cfa_temp.offset = cfa.offset;
2476                 }
2477
2478               /* Rule 5 */
2479               else if (REG_P (XEXP (src, 0))
2480                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2481                        && XEXP (src, 1) == stack_pointer_rtx)
2482                 {
2483                   /* Setting a scratch register that we will use instead
2484                      of SP for saving registers to the stack.  */
2485                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2486                   cfa_store.reg = REGNO (dest);
2487                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2488                 }
2489
2490               /* Rule 9 */
2491               else if (GET_CODE (src) == LO_SUM
2492                        && CONST_INT_P (XEXP (src, 1)))
2493                 {
2494                   cfa_temp.reg = REGNO (dest);
2495                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2496                 }
2497               else
2498                 gcc_unreachable ();
2499             }
2500           break;
2501
2502           /* Rule 6 */
2503         case CONST_INT:
2504           cfa_temp.reg = REGNO (dest);
2505           cfa_temp.offset = INTVAL (src);
2506           break;
2507
2508           /* Rule 7 */
2509         case IOR:
2510           gcc_assert (REG_P (XEXP (src, 0))
2511                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2512                       && CONST_INT_P (XEXP (src, 1)));
2513
2514           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2515             cfa_temp.reg = REGNO (dest);
2516           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2517           break;
2518
2519           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2520              which will fill in all of the bits.  */
2521           /* Rule 8 */
2522         case HIGH:
2523           break;
2524
2525           /* Rule 15 */
2526         case UNSPEC:
2527         case UNSPEC_VOLATILE:
2528           gcc_assert (targetm.dwarf_handle_frame_unspec);
2529           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2530           return;
2531
2532           /* Rule 16 */
2533         case AND:
2534           /* If this AND operation happens on stack pointer in prologue,
2535              we assume the stack is realigned and we extract the
2536              alignment.  */
2537           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2538             {
2539               /* We interpret reg_save differently with stack_realign set.
2540                  Thus we must flush whatever we have queued first.  */
2541               dwarf2out_flush_queued_reg_saves ();
2542
2543               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2544               fde->stack_realign = 1;
2545               fde->stack_realignment = INTVAL (XEXP (src, 1));
2546               cfa_store.offset = 0;
2547
2548               if (cfa.reg != STACK_POINTER_REGNUM
2549                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2550                 fde->drap_reg = cfa.reg;
2551             }
2552           return;
2553
2554         default:
2555           gcc_unreachable ();
2556         }
2557
2558       def_cfa_1 (label, &cfa);
2559       break;
2560
2561     case MEM:
2562
2563       /* Saving a register to the stack.  Make sure dest is relative to the
2564          CFA register.  */
2565       switch (GET_CODE (XEXP (dest, 0)))
2566         {
2567           /* Rule 10 */
2568           /* With a push.  */
2569         case PRE_MODIFY:
2570           /* We can't handle variable size modifications.  */
2571           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2572                       == CONST_INT);
2573           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2574
2575           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2576                       && cfa_store.reg == STACK_POINTER_REGNUM);
2577
2578           cfa_store.offset += offset;
2579           if (cfa.reg == STACK_POINTER_REGNUM)
2580             cfa.offset = cfa_store.offset;
2581
2582           offset = -cfa_store.offset;
2583           break;
2584
2585           /* Rule 11 */
2586         case PRE_INC:
2587         case PRE_DEC:
2588         case POST_DEC:
2589           offset = GET_MODE_SIZE (GET_MODE (dest));
2590           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2591             offset = -offset;
2592
2593           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2594                        == STACK_POINTER_REGNUM)
2595                       && cfa_store.reg == STACK_POINTER_REGNUM);
2596
2597           cfa_store.offset += offset;
2598
2599           /* Rule 18: If stack is aligned, we will use FP as a
2600              reference to represent the address of the stored
2601              regiser.  */
2602           if (fde
2603               && fde->stack_realign
2604               && src == hard_frame_pointer_rtx)
2605             {
2606               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2607               cfa_store.offset = 0;
2608             }
2609
2610           if (cfa.reg == STACK_POINTER_REGNUM)
2611             cfa.offset = cfa_store.offset;
2612
2613           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2614             offset += -cfa_store.offset;
2615           else
2616             offset = -cfa_store.offset;
2617           break;
2618
2619           /* Rule 12 */
2620           /* With an offset.  */
2621         case PLUS:
2622         case MINUS:
2623         case LO_SUM:
2624           {
2625             int regno;
2626
2627             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2628                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2629             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2630             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2631               offset = -offset;
2632
2633             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2634
2635             if (cfa.reg == (unsigned) regno)
2636               offset -= cfa.offset;
2637             else if (cfa_store.reg == (unsigned) regno)
2638               offset -= cfa_store.offset;
2639             else
2640               {
2641                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2642                 offset -= cfa_temp.offset;
2643               }
2644           }
2645           break;
2646
2647           /* Rule 13 */
2648           /* Without an offset.  */
2649         case REG:
2650           {
2651             int regno = REGNO (XEXP (dest, 0));
2652
2653             if (cfa.reg == (unsigned) regno)
2654               offset = -cfa.offset;
2655             else if (cfa_store.reg == (unsigned) regno)
2656               offset = -cfa_store.offset;
2657             else
2658               {
2659                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2660                 offset = -cfa_temp.offset;
2661               }
2662           }
2663           break;
2664
2665           /* Rule 14 */
2666         case POST_INC:
2667           gcc_assert (cfa_temp.reg
2668                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2669           offset = -cfa_temp.offset;
2670           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2671           break;
2672
2673         default:
2674           gcc_unreachable ();
2675         }
2676
2677         /* Rule 17 */
2678         /* If the source operand of this MEM operation is not a
2679            register, basically the source is return address.  Here
2680            we only care how much stack grew and we don't save it.  */
2681       if (!REG_P (src))
2682         break;
2683
2684       if (REGNO (src) != STACK_POINTER_REGNUM
2685           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2686           && (unsigned) REGNO (src) == cfa.reg)
2687         {
2688           /* We're storing the current CFA reg into the stack.  */
2689
2690           if (cfa.offset == 0)
2691             {
2692               /* Rule 19 */
2693               /* If stack is aligned, putting CFA reg into stack means
2694                  we can no longer use reg + offset to represent CFA.
2695                  Here we use DW_CFA_def_cfa_expression instead.  The
2696                  result of this expression equals to the original CFA
2697                  value.  */
2698               if (fde
2699                   && fde->stack_realign
2700                   && cfa.indirect == 0
2701                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2702                 {
2703                   dw_cfa_location cfa_exp;
2704
2705                   gcc_assert (fde->drap_reg == cfa.reg);
2706
2707                   cfa_exp.indirect = 1;
2708                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2709                   cfa_exp.base_offset = offset;
2710                   cfa_exp.offset = 0;
2711
2712                   fde->drap_reg_saved = 1;
2713
2714                   def_cfa_1 (label, &cfa_exp);
2715                   break;
2716                 }
2717
2718               /* If the source register is exactly the CFA, assume
2719                  we're saving SP like any other register; this happens
2720                  on the ARM.  */
2721               def_cfa_1 (label, &cfa);
2722               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2723               break;
2724             }
2725           else
2726             {
2727               /* Otherwise, we'll need to look in the stack to
2728                  calculate the CFA.  */
2729               rtx x = XEXP (dest, 0);
2730
2731               if (!REG_P (x))
2732                 x = XEXP (x, 0);
2733               gcc_assert (REG_P (x));
2734
2735               cfa.reg = REGNO (x);
2736               cfa.base_offset = offset;
2737               cfa.indirect = 1;
2738               def_cfa_1 (label, &cfa);
2739               break;
2740             }
2741         }
2742
2743       def_cfa_1 (label, &cfa);
2744       {
2745         span = targetm.dwarf_register_span (src);
2746
2747         if (!span)
2748           queue_reg_save (label, src, NULL_RTX, offset);
2749         else
2750           {
2751             /* We have a PARALLEL describing where the contents of SRC
2752                live.  Queue register saves for each piece of the
2753                PARALLEL.  */
2754             int par_index;
2755             int limit;
2756             HOST_WIDE_INT span_offset = offset;
2757
2758             gcc_assert (GET_CODE (span) == PARALLEL);
2759
2760             limit = XVECLEN (span, 0);
2761             for (par_index = 0; par_index < limit; par_index++)
2762               {
2763                 rtx elem = XVECEXP (span, 0, par_index);
2764
2765                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2766                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2767               }
2768           }
2769       }
2770       break;
2771
2772     default:
2773       gcc_unreachable ();
2774     }
2775 }
2776
2777 /* Record call frame debugging information for INSN, which either
2778    sets SP or FP (adjusting how we calculate the frame address) or saves a
2779    register to the stack.  If INSN is NULL_RTX, initialize our state.
2780
2781    If AFTER_P is false, we're being called before the insn is emitted,
2782    otherwise after.  Call instructions get invoked twice.  */
2783
2784 void
2785 dwarf2out_frame_debug (rtx insn, bool after_p)
2786 {
2787   const char *label;
2788   rtx note, n;
2789   bool handled_one = false;
2790
2791   if (insn == NULL_RTX)
2792     {
2793       size_t i;
2794
2795       /* Flush any queued register saves.  */
2796       dwarf2out_flush_queued_reg_saves ();
2797
2798       /* Set up state for generating call frame debug info.  */
2799       lookup_cfa (&cfa);
2800       gcc_assert (cfa.reg
2801                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2802
2803       cfa.reg = STACK_POINTER_REGNUM;
2804       cfa_store = cfa;
2805       cfa_temp.reg = -1;
2806       cfa_temp.offset = 0;
2807
2808       for (i = 0; i < num_regs_saved_in_regs; i++)
2809         {
2810           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2811           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2812         }
2813       num_regs_saved_in_regs = 0;
2814
2815       if (barrier_args_size)
2816         {
2817           XDELETEVEC (barrier_args_size);
2818           barrier_args_size = NULL;
2819         }
2820       return;
2821     }
2822
2823   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2824     dwarf2out_flush_queued_reg_saves ();
2825
2826   if (!RTX_FRAME_RELATED_P (insn))
2827     {
2828       /* ??? This should be done unconditionally since stack adjustments
2829          matter if the stack pointer is not the CFA register anymore but
2830          is still used to save registers.  */
2831       if (!ACCUMULATE_OUTGOING_ARGS)
2832         dwarf2out_notice_stack_adjust (insn, after_p);
2833       return;
2834     }
2835
2836   label = dwarf2out_cfi_label (false);
2837   any_cfis_emitted = false;
2838
2839   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2840     switch (REG_NOTE_KIND (note))
2841       {
2842       case REG_FRAME_RELATED_EXPR:
2843         insn = XEXP (note, 0);
2844         goto found;
2845
2846       case REG_CFA_DEF_CFA:
2847         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2848         handled_one = true;
2849         break;
2850
2851       case REG_CFA_ADJUST_CFA:
2852         n = XEXP (note, 0);
2853         if (n == NULL)
2854           {
2855             n = PATTERN (insn);
2856             if (GET_CODE (n) == PARALLEL)
2857               n = XVECEXP (n, 0, 0);
2858           }
2859         dwarf2out_frame_debug_adjust_cfa (n, label);
2860         handled_one = true;
2861         break;
2862
2863       case REG_CFA_OFFSET:
2864         n = XEXP (note, 0);
2865         if (n == NULL)
2866           n = single_set (insn);
2867         dwarf2out_frame_debug_cfa_offset (n, label);
2868         handled_one = true;
2869         break;
2870
2871       case REG_CFA_REGISTER:
2872         n = XEXP (note, 0);
2873         if (n == NULL)
2874           {
2875             n = PATTERN (insn);
2876             if (GET_CODE (n) == PARALLEL)
2877               n = XVECEXP (n, 0, 0);
2878           }
2879         dwarf2out_frame_debug_cfa_register (n, label);
2880         handled_one = true;
2881         break;
2882
2883       case REG_CFA_EXPRESSION:
2884         n = XEXP (note, 0);
2885         if (n == NULL)
2886           n = single_set (insn);
2887         dwarf2out_frame_debug_cfa_expression (n, label);
2888         handled_one = true;
2889         break;
2890
2891       case REG_CFA_RESTORE:
2892         n = XEXP (note, 0);
2893         if (n == NULL)
2894           {
2895             n = PATTERN (insn);
2896             if (GET_CODE (n) == PARALLEL)
2897               n = XVECEXP (n, 0, 0);
2898             n = XEXP (n, 0);
2899           }
2900         dwarf2out_frame_debug_cfa_restore (n, label);
2901         handled_one = true;
2902         break;
2903
2904       case REG_CFA_SET_VDRAP:
2905         n = XEXP (note, 0);
2906         if (REG_P (n))
2907           {
2908             dw_fde_ref fde = current_fde ();
2909             if (fde)
2910               {
2911                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2912                 if (REG_P (n))
2913                   fde->vdrap_reg = REGNO (n);
2914               }
2915           }
2916         handled_one = true;
2917         break;
2918
2919       default:
2920         break;
2921       }
2922   if (handled_one)
2923     {
2924       if (any_cfis_emitted)
2925         dwarf2out_flush_queued_reg_saves ();
2926       return;
2927     }
2928
2929   insn = PATTERN (insn);
2930  found:
2931   dwarf2out_frame_debug_expr (insn, label);
2932
2933   /* Check again.  A parallel can save and update the same register.
2934      We could probably check just once, here, but this is safer than
2935      removing the check above.  */
2936   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2937     dwarf2out_flush_queued_reg_saves ();
2938 }
2939
2940 /* Determine if we need to save and restore CFI information around this
2941    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2942    we do need to save/restore, then emit the save now, and insert a
2943    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2944
2945 void
2946 dwarf2out_cfi_begin_epilogue (rtx insn)
2947 {
2948   bool saw_frp = false;
2949   rtx i;
2950
2951   /* Scan forward to the return insn, noticing if there are possible
2952      frame related insns.  */
2953   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2954     {
2955       if (!INSN_P (i))
2956         continue;
2957
2958       /* Look for both regular and sibcalls to end the block.  */
2959       if (returnjump_p (i))
2960         break;
2961       if (CALL_P (i) && SIBLING_CALL_P (i))
2962         break;
2963
2964       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2965         {
2966           int idx;
2967           rtx seq = PATTERN (i);
2968
2969           if (returnjump_p (XVECEXP (seq, 0, 0)))
2970             break;
2971           if (CALL_P (XVECEXP (seq, 0, 0))
2972               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2973             break;
2974
2975           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2976             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2977               saw_frp = true;
2978         }
2979
2980       if (RTX_FRAME_RELATED_P (i))
2981         saw_frp = true;
2982     }
2983
2984   /* If the port doesn't emit epilogue unwind info, we don't need a
2985      save/restore pair.  */
2986   if (!saw_frp)
2987     return;
2988
2989   /* Otherwise, search forward to see if the return insn was the last
2990      basic block of the function.  If so, we don't need save/restore.  */
2991   gcc_assert (i != NULL);
2992   i = next_real_insn (i);
2993   if (i == NULL)
2994     return;
2995
2996   /* Insert the restore before that next real insn in the stream, and before
2997      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2998      properly nested.  This should be after any label or alignment.  This
2999      will be pushed into the CFI stream by the function below.  */
3000   while (1)
3001     {
3002       rtx p = PREV_INSN (i);
3003       if (!NOTE_P (p))
3004         break;
3005       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3006         break;
3007       i = p;
3008     }
3009   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3010
3011   emit_cfa_remember = true;
3012
3013   /* And emulate the state save.  */
3014   gcc_assert (!cfa_remember.in_use);
3015   cfa_remember = cfa;
3016   cfa_remember.in_use = 1;
3017 }
3018
3019 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3020    required.  */
3021
3022 void
3023 dwarf2out_frame_debug_restore_state (void)
3024 {
3025   dw_cfi_ref cfi = new_cfi ();
3026   const char *label = dwarf2out_cfi_label (false);
3027
3028   cfi->dw_cfi_opc = DW_CFA_restore_state;
3029   add_fde_cfi (label, cfi);
3030
3031   gcc_assert (cfa_remember.in_use);
3032   cfa = cfa_remember;
3033   cfa_remember.in_use = 0;
3034 }
3035
3036 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3037 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3038  (enum dwarf_call_frame_info cfi);
3039
3040 static enum dw_cfi_oprnd_type
3041 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3042 {
3043   switch (cfi)
3044     {
3045     case DW_CFA_nop:
3046     case DW_CFA_GNU_window_save:
3047     case DW_CFA_remember_state:
3048     case DW_CFA_restore_state:
3049       return dw_cfi_oprnd_unused;
3050
3051     case DW_CFA_set_loc:
3052     case DW_CFA_advance_loc1:
3053     case DW_CFA_advance_loc2:
3054     case DW_CFA_advance_loc4:
3055     case DW_CFA_MIPS_advance_loc8:
3056       return dw_cfi_oprnd_addr;
3057
3058     case DW_CFA_offset:
3059     case DW_CFA_offset_extended:
3060     case DW_CFA_def_cfa:
3061     case DW_CFA_offset_extended_sf:
3062     case DW_CFA_def_cfa_sf:
3063     case DW_CFA_restore:
3064     case DW_CFA_restore_extended:
3065     case DW_CFA_undefined:
3066     case DW_CFA_same_value:
3067     case DW_CFA_def_cfa_register:
3068     case DW_CFA_register:
3069     case DW_CFA_expression:
3070       return dw_cfi_oprnd_reg_num;
3071
3072     case DW_CFA_def_cfa_offset:
3073     case DW_CFA_GNU_args_size:
3074     case DW_CFA_def_cfa_offset_sf:
3075       return dw_cfi_oprnd_offset;
3076
3077     case DW_CFA_def_cfa_expression:
3078       return dw_cfi_oprnd_loc;
3079
3080     default:
3081       gcc_unreachable ();
3082     }
3083 }
3084
3085 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3086 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3087  (enum dwarf_call_frame_info cfi);
3088
3089 static enum dw_cfi_oprnd_type
3090 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3091 {
3092   switch (cfi)
3093     {
3094     case DW_CFA_def_cfa:
3095     case DW_CFA_def_cfa_sf:
3096     case DW_CFA_offset:
3097     case DW_CFA_offset_extended_sf:
3098     case DW_CFA_offset_extended:
3099       return dw_cfi_oprnd_offset;
3100
3101     case DW_CFA_register:
3102       return dw_cfi_oprnd_reg_num;
3103
3104     case DW_CFA_expression:
3105       return dw_cfi_oprnd_loc;
3106
3107     default:
3108       return dw_cfi_oprnd_unused;
3109     }
3110 }
3111
3112 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3113    switch to the data section instead, and write out a synthetic start label
3114    for collect2 the first time around.  */
3115
3116 static void
3117 switch_to_eh_frame_section (bool back)
3118 {
3119   tree label;
3120
3121 #ifdef EH_FRAME_SECTION_NAME
3122   if (eh_frame_section == 0)
3123     {
3124       int flags;
3125
3126       if (EH_TABLES_CAN_BE_READ_ONLY)
3127         {
3128           int fde_encoding;
3129           int per_encoding;
3130           int lsda_encoding;
3131
3132           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3133                                                        /*global=*/0);
3134           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3135                                                        /*global=*/1);
3136           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3137                                                         /*global=*/0);
3138           flags = ((! flag_pic
3139                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3140                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3141                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3142                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3143                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3144                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3145                    ? 0 : SECTION_WRITE);
3146         }
3147       else
3148         flags = SECTION_WRITE;
3149       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3150     }
3151 #endif /* EH_FRAME_SECTION_NAME */
3152
3153   if (eh_frame_section)
3154     switch_to_section (eh_frame_section);
3155   else
3156     {
3157       /* We have no special eh_frame section.  Put the information in
3158          the data section and emit special labels to guide collect2.  */
3159       switch_to_section (data_section);
3160
3161       if (!back)
3162         {
3163           label = get_file_function_name ("F");
3164           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3165           targetm.asm_out.globalize_label (asm_out_file,
3166                                            IDENTIFIER_POINTER (label));
3167           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3168         }
3169     }
3170 }
3171
3172 /* Switch [BACK] to the eh or debug frame table section, depending on
3173    FOR_EH.  */
3174
3175 static void
3176 switch_to_frame_table_section (int for_eh, bool back)
3177 {
3178   if (for_eh)
3179     switch_to_eh_frame_section (back);
3180   else
3181     {
3182       if (!debug_frame_section)
3183         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3184                                            SECTION_DEBUG, NULL);
3185       switch_to_section (debug_frame_section);
3186     }
3187 }
3188
3189 /* Output a Call Frame Information opcode and its operand(s).  */
3190
3191 static void
3192 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3193 {
3194   unsigned long r;
3195   HOST_WIDE_INT off;
3196
3197   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3198     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3199                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3200                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3201                          ((unsigned HOST_WIDE_INT)
3202                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3203   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3204     {
3205       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3206       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3207                            "DW_CFA_offset, column %#lx", r);
3208       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3209       dw2_asm_output_data_uleb128 (off, NULL);
3210     }
3211   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3212     {
3213       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3214       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3215                            "DW_CFA_restore, column %#lx", r);
3216     }
3217   else
3218     {
3219       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3220                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3221
3222       switch (cfi->dw_cfi_opc)
3223         {
3224         case DW_CFA_set_loc:
3225           if (for_eh)
3226             dw2_asm_output_encoded_addr_rtx (
3227                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3228                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3229                 false, NULL);
3230           else
3231             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3232                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3233           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3234           break;
3235
3236         case DW_CFA_advance_loc1:
3237           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3238                                 fde->dw_fde_current_label, NULL);
3239           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3240           break;
3241
3242         case DW_CFA_advance_loc2:
3243           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3244                                 fde->dw_fde_current_label, NULL);
3245           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3246           break;
3247
3248         case DW_CFA_advance_loc4:
3249           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3250                                 fde->dw_fde_current_label, NULL);
3251           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3252           break;
3253
3254         case DW_CFA_MIPS_advance_loc8:
3255           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3256                                 fde->dw_fde_current_label, NULL);
3257           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3258           break;
3259
3260         case DW_CFA_offset_extended:
3261           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3262           dw2_asm_output_data_uleb128 (r, NULL);
3263           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3264           dw2_asm_output_data_uleb128 (off, NULL);
3265           break;
3266
3267         case DW_CFA_def_cfa:
3268           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3269           dw2_asm_output_data_uleb128 (r, NULL);
3270           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3271           break;
3272
3273         case DW_CFA_offset_extended_sf:
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_sleb128 (off, NULL);
3278           break;
3279
3280         case DW_CFA_def_cfa_sf:
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           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3284           dw2_asm_output_data_sleb128 (off, NULL);
3285           break;
3286
3287         case DW_CFA_restore_extended:
3288         case DW_CFA_undefined:
3289         case DW_CFA_same_value:
3290         case DW_CFA_def_cfa_register:
3291           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3292           dw2_asm_output_data_uleb128 (r, NULL);
3293           break;
3294
3295         case DW_CFA_register:
3296           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3297           dw2_asm_output_data_uleb128 (r, NULL);
3298           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3299           dw2_asm_output_data_uleb128 (r, NULL);
3300           break;
3301
3302         case DW_CFA_def_cfa_offset:
3303         case DW_CFA_GNU_args_size:
3304           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3305           break;
3306
3307         case DW_CFA_def_cfa_offset_sf:
3308           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3309           dw2_asm_output_data_sleb128 (off, NULL);
3310           break;
3311
3312         case DW_CFA_GNU_window_save:
3313           break;
3314
3315         case DW_CFA_def_cfa_expression:
3316         case DW_CFA_expression:
3317           output_cfa_loc (cfi, for_eh);
3318           break;
3319
3320         case DW_CFA_GNU_negative_offset_extended:
3321           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3322           gcc_unreachable ();
3323
3324         default:
3325           break;
3326         }
3327     }
3328 }
3329
3330 /* Similar, but do it via assembler directives instead.  */
3331
3332 static void
3333 output_cfi_directive (dw_cfi_ref cfi)
3334 {
3335   unsigned long r, r2;
3336
3337   switch (cfi->dw_cfi_opc)
3338     {
3339     case DW_CFA_advance_loc:
3340     case DW_CFA_advance_loc1:
3341     case DW_CFA_advance_loc2:
3342     case DW_CFA_advance_loc4:
3343     case DW_CFA_MIPS_advance_loc8:
3344     case DW_CFA_set_loc:
3345       /* Should only be created by add_fde_cfi in a code path not
3346          followed when emitting via directives.  The assembler is
3347          going to take care of this for us.  */
3348       gcc_unreachable ();
3349
3350     case DW_CFA_offset:
3351     case DW_CFA_offset_extended:
3352     case DW_CFA_offset_extended_sf:
3353       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3354       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3355                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3356       break;
3357
3358     case DW_CFA_restore:
3359     case DW_CFA_restore_extended:
3360       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3361       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3362       break;
3363
3364     case DW_CFA_undefined:
3365       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3366       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3367       break;
3368
3369     case DW_CFA_same_value:
3370       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3371       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3372       break;
3373
3374     case DW_CFA_def_cfa:
3375     case DW_CFA_def_cfa_sf:
3376       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3377       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3378                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3379       break;
3380
3381     case DW_CFA_def_cfa_register:
3382       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3383       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3384       break;
3385
3386     case DW_CFA_register:
3387       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3388       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3389       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3390       break;
3391
3392     case DW_CFA_def_cfa_offset:
3393     case DW_CFA_def_cfa_offset_sf:
3394       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3395                HOST_WIDE_INT_PRINT_DEC"\n",
3396                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3397       break;
3398
3399     case DW_CFA_remember_state:
3400       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3401       break;
3402     case DW_CFA_restore_state:
3403       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3404       break;
3405
3406     case DW_CFA_GNU_args_size:
3407       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3408       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3409       if (flag_debug_asm)
3410         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3411                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3412       fputc ('\n', asm_out_file);
3413       break;
3414
3415     case DW_CFA_GNU_window_save:
3416       fprintf (asm_out_file, "\t.cfi_window_save\n");
3417       break;
3418
3419     case DW_CFA_def_cfa_expression:
3420     case DW_CFA_expression:
3421       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3422       output_cfa_loc_raw (cfi);
3423       fputc ('\n', asm_out_file);
3424       break;
3425
3426     default:
3427       gcc_unreachable ();
3428     }
3429 }
3430
3431 DEF_VEC_P (dw_cfi_ref);
3432 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3433
3434 /* Output CFIs to bring current FDE to the same state as after executing
3435    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3436    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3437    other arguments to pass to output_cfi.  */
3438
3439 static void
3440 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3441 {
3442   struct dw_cfi_struct cfi_buf;
3443   dw_cfi_ref cfi2;
3444   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3445   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3446   unsigned int len, idx;
3447
3448   for (;; cfi = cfi->dw_cfi_next)
3449     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3450       {
3451       case DW_CFA_advance_loc:
3452       case DW_CFA_advance_loc1:
3453       case DW_CFA_advance_loc2:
3454       case DW_CFA_advance_loc4:
3455       case DW_CFA_MIPS_advance_loc8:
3456       case DW_CFA_set_loc:
3457         /* All advances should be ignored.  */
3458         break;
3459       case DW_CFA_remember_state:
3460         {
3461           dw_cfi_ref args_size = cfi_args_size;
3462
3463           /* Skip everything between .cfi_remember_state and
3464              .cfi_restore_state.  */
3465           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3466             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3467               break;
3468             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3469               args_size = cfi2;
3470             else
3471               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3472
3473           if (cfi2 == NULL)
3474             goto flush_all;
3475           else
3476             {
3477               cfi = cfi2;
3478               cfi_args_size = args_size;
3479             }
3480           break;
3481         }
3482       case DW_CFA_GNU_args_size:
3483         cfi_args_size = cfi;
3484         break;
3485       case DW_CFA_GNU_window_save:
3486         goto flush_all;
3487       case DW_CFA_offset:
3488       case DW_CFA_offset_extended:
3489       case DW_CFA_offset_extended_sf:
3490       case DW_CFA_restore:
3491       case DW_CFA_restore_extended:
3492       case DW_CFA_undefined:
3493       case DW_CFA_same_value:
3494       case DW_CFA_register:
3495       case DW_CFA_val_offset:
3496       case DW_CFA_val_offset_sf:
3497       case DW_CFA_expression:
3498       case DW_CFA_val_expression:
3499       case DW_CFA_GNU_negative_offset_extended:
3500         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3501           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3502                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3503         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3504         break;
3505       case DW_CFA_def_cfa:
3506       case DW_CFA_def_cfa_sf:
3507       case DW_CFA_def_cfa_expression:
3508         cfi_cfa = cfi;
3509         cfi_cfa_offset = cfi;
3510         break;
3511       case DW_CFA_def_cfa_register:
3512         cfi_cfa = cfi;
3513         break;
3514       case DW_CFA_def_cfa_offset:
3515       case DW_CFA_def_cfa_offset_sf:
3516         cfi_cfa_offset = cfi;
3517         break;
3518       case DW_CFA_nop:
3519         gcc_assert (cfi == NULL);
3520       flush_all:
3521         len = VEC_length (dw_cfi_ref, regs);
3522         for (idx = 0; idx < len; idx++)
3523           {
3524             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3525             if (cfi2 != NULL
3526                 && cfi2->dw_cfi_opc != DW_CFA_restore
3527                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3528               {
3529                 if (do_cfi_asm)
3530                   output_cfi_directive (cfi2);
3531                 else
3532                   output_cfi (cfi2, fde, for_eh);
3533               }
3534           }
3535         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3536           {
3537             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3538             cfi_buf = *cfi_cfa;
3539             switch (cfi_cfa_offset->dw_cfi_opc)
3540               {
3541               case DW_CFA_def_cfa_offset:
3542                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3543                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3544                 break;
3545               case DW_CFA_def_cfa_offset_sf:
3546                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3547                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3548                 break;
3549               case DW_CFA_def_cfa:
3550               case DW_CFA_def_cfa_sf:
3551                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3552                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3553                 break;
3554               default:
3555                 gcc_unreachable ();
3556               }
3557             cfi_cfa = &cfi_buf;
3558           }
3559         else if (cfi_cfa_offset)
3560           cfi_cfa = cfi_cfa_offset;
3561         if (cfi_cfa)
3562           {
3563             if (do_cfi_asm)
3564               output_cfi_directive (cfi_cfa);
3565             else
3566               output_cfi (cfi_cfa, fde, for_eh);
3567           }
3568         cfi_cfa = NULL;
3569         cfi_cfa_offset = NULL;
3570         if (cfi_args_size
3571             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3572           {
3573             if (do_cfi_asm)
3574               output_cfi_directive (cfi_args_size);
3575             else
3576               output_cfi (cfi_args_size, fde, for_eh);
3577           }
3578         cfi_args_size = NULL;
3579         if (cfi == NULL)
3580           {
3581             VEC_free (dw_cfi_ref, heap, regs);
3582             return;
3583           }
3584         else if (do_cfi_asm)
3585           output_cfi_directive (cfi);
3586         else
3587           output_cfi (cfi, fde, for_eh);
3588         break;
3589       default:
3590         gcc_unreachable ();
3591     }
3592 }
3593
3594 /* Output one FDE.  */
3595
3596 static void
3597 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3598             char *section_start_label, int fde_encoding, char *augmentation,
3599             bool any_lsda_needed, int lsda_encoding)
3600 {
3601   const char *begin, *end;
3602   static unsigned int j;
3603   char l1[20], l2[20];
3604   dw_cfi_ref cfi;
3605
3606   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3607                                      /* empty */ 0);
3608   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3609                                   for_eh + j);
3610   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3611   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3612   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3613     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3614                          " indicating 64-bit DWARF extension");
3615   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3616                         "FDE Length");
3617   ASM_OUTPUT_LABEL (asm_out_file, l1);
3618
3619   if (for_eh)
3620     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3621   else
3622     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3623                            debug_frame_section, "FDE CIE offset");
3624
3625   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3626   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3627
3628   if (for_eh)
3629     {
3630       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3631       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3632       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3633                                        "FDE initial location");
3634       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3635                             end, begin, "FDE address range");
3636     }
3637   else
3638     {
3639       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3640       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3641     }
3642
3643   if (augmentation[0])
3644     {
3645       if (any_lsda_needed)
3646         {
3647           int size = size_of_encoded_value (lsda_encoding);
3648
3649           if (lsda_encoding == DW_EH_PE_aligned)
3650             {
3651               int offset = (  4         /* Length */
3652                             + 4         /* CIE offset */
3653                             + 2 * size_of_encoded_value (fde_encoding)
3654                             + 1         /* Augmentation size */ );
3655               int pad = -offset & (PTR_SIZE - 1);
3656
3657               size += pad;
3658               gcc_assert (size_of_uleb128 (size) == 1);
3659             }
3660
3661           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3662
3663           if (fde->uses_eh_lsda)
3664             {
3665               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3666                                            fde->funcdef_number);
3667               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3668                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3669                                                false,
3670                                                "Language Specific Data Area");
3671             }
3672           else
3673             {
3674               if (lsda_encoding == DW_EH_PE_aligned)
3675                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3676               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3677                                    "Language Specific Data Area (none)");
3678             }
3679         }
3680       else
3681         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3682     }
3683
3684   /* Loop through the Call Frame Instructions associated with
3685      this FDE.  */
3686   fde->dw_fde_current_label = begin;
3687   if (fde->dw_fde_second_begin == NULL)
3688     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3689       output_cfi (cfi, fde, for_eh);
3690   else if (!second)
3691     {
3692       if (fde->dw_fde_switch_cfi)
3693         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3694           {
3695             output_cfi (cfi, fde, for_eh);
3696             if (cfi == fde->dw_fde_switch_cfi)
3697               break;
3698           }
3699     }
3700   else
3701     {
3702       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3703
3704       if (fde->dw_fde_switch_cfi)
3705         {
3706           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3707           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3708           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3709           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3710         }
3711       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3712         output_cfi (cfi, fde, for_eh);
3713     }
3714
3715   /* If we are to emit a ref/link from function bodies to their frame tables,
3716      do it now.  This is typically performed to make sure that tables
3717      associated with functions are dragged with them and not discarded in
3718      garbage collecting links. We need to do this on a per function basis to
3719      cope with -ffunction-sections.  */
3720
3721 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3722   /* Switch to the function section, emit the ref to the tables, and
3723      switch *back* into the table section.  */
3724   switch_to_section (function_section (fde->decl));
3725   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3726   switch_to_frame_table_section (for_eh, true);
3727 #endif
3728
3729   /* Pad the FDE out to an address sized boundary.  */
3730   ASM_OUTPUT_ALIGN (asm_out_file,
3731                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3732   ASM_OUTPUT_LABEL (asm_out_file, l2);
3733
3734   j += 2;
3735 }
3736
3737 /* Return true if frame description entry FDE is needed for EH.  */
3738
3739 static bool
3740 fde_needed_for_eh_p (dw_fde_ref fde)
3741 {
3742   if (flag_asynchronous_unwind_tables)
3743     return true;
3744
3745   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3746     return true;
3747
3748   if (fde->uses_eh_lsda)
3749     return true;
3750
3751   /* If exceptions are enabled, we have collected nothrow info.  */
3752   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3753     return false;
3754
3755   return true;
3756 }
3757
3758 /* Output the call frame information used to record information
3759    that relates to calculating the frame pointer, and records the
3760    location of saved registers.  */
3761
3762 static void
3763 output_call_frame_info (int for_eh)
3764 {
3765   unsigned int i;
3766   dw_fde_ref fde;
3767   dw_cfi_ref cfi;
3768   char l1[20], l2[20], section_start_label[20];
3769   bool any_lsda_needed = false;
3770   char augmentation[6];
3771   int augmentation_size;
3772   int fde_encoding = DW_EH_PE_absptr;
3773   int per_encoding = DW_EH_PE_absptr;
3774   int lsda_encoding = DW_EH_PE_absptr;
3775   int return_reg;
3776   rtx personality = NULL;
3777   int dw_cie_version;
3778
3779   /* Don't emit a CIE if there won't be any FDEs.  */
3780   if (fde_table_in_use == 0)
3781     return;
3782
3783   /* Nothing to do if the assembler's doing it all.  */
3784   if (dwarf2out_do_cfi_asm ())
3785     return;
3786
3787   /* If we don't have any functions we'll want to unwind out of, don't emit
3788      any EH unwind information.  If we make FDEs linkonce, we may have to
3789      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3790      want to avoid having an FDE kept around when the function it refers to
3791      is discarded.  Example where this matters: a primary function template
3792      in C++ requires EH information, an explicit specialization doesn't.  */
3793   if (for_eh)
3794     {
3795       bool any_eh_needed = false;
3796
3797       for (i = 0; i < fde_table_in_use; i++)
3798         if (fde_table[i].uses_eh_lsda)
3799           any_eh_needed = any_lsda_needed = true;
3800         else if (fde_needed_for_eh_p (&fde_table[i]))
3801           any_eh_needed = true;
3802         else if (TARGET_USES_WEAK_UNWIND_INFO)
3803           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3804                                              1, 1);
3805
3806       if (!any_eh_needed)
3807         return;
3808     }
3809
3810   /* We're going to be generating comments, so turn on app.  */
3811   if (flag_debug_asm)
3812     app_enable ();
3813
3814   /* Switch to the proper frame section, first time.  */
3815   switch_to_frame_table_section (for_eh, false);
3816
3817   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3818   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3819
3820   /* Output the CIE.  */
3821   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3822   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3823   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3824     dw2_asm_output_data (4, 0xffffffff,
3825       "Initial length escape value indicating 64-bit DWARF extension");
3826   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3827                         "Length of Common Information Entry");
3828   ASM_OUTPUT_LABEL (asm_out_file, l1);
3829
3830   /* Now that the CIE pointer is PC-relative for EH,
3831      use 0 to identify the CIE.  */
3832   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3833                        (for_eh ? 0 : DWARF_CIE_ID),
3834                        "CIE Identifier Tag");
3835
3836   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3837      use CIE version 1, unless that would produce incorrect results
3838      due to overflowing the return register column.  */
3839   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3840   dw_cie_version = 1;
3841   if (return_reg >= 256 || dwarf_version > 2)
3842     dw_cie_version = 3;
3843   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3844
3845   augmentation[0] = 0;
3846   augmentation_size = 0;
3847
3848   personality = current_unit_personality;
3849   if (for_eh)
3850     {
3851       char *p;
3852
3853       /* Augmentation:
3854          z      Indicates that a uleb128 is present to size the
3855                 augmentation section.
3856          L      Indicates the encoding (and thus presence) of
3857                 an LSDA pointer in the FDE augmentation.
3858          R      Indicates a non-default pointer encoding for
3859                 FDE code pointers.
3860          P      Indicates the presence of an encoding + language
3861                 personality routine in the CIE augmentation.  */
3862
3863       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3864       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3865       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3866
3867       p = augmentation + 1;
3868       if (personality)
3869         {
3870           *p++ = 'P';
3871           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3872           assemble_external_libcall (personality);
3873         }
3874       if (any_lsda_needed)
3875         {
3876           *p++ = 'L';
3877           augmentation_size += 1;
3878         }
3879       if (fde_encoding != DW_EH_PE_absptr)
3880         {
3881           *p++ = 'R';
3882           augmentation_size += 1;
3883         }
3884       if (p > augmentation + 1)
3885         {
3886           augmentation[0] = 'z';
3887           *p = '\0';
3888         }
3889
3890       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3891       if (personality && per_encoding == DW_EH_PE_aligned)
3892         {
3893           int offset = (  4             /* Length */
3894                         + 4             /* CIE Id */
3895                         + 1             /* CIE version */
3896                         + strlen (augmentation) + 1     /* Augmentation */
3897                         + size_of_uleb128 (1)           /* Code alignment */
3898                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3899                         + 1             /* RA column */
3900                         + 1             /* Augmentation size */
3901                         + 1             /* Personality encoding */ );
3902           int pad = -offset & (PTR_SIZE - 1);
3903
3904           augmentation_size += pad;
3905
3906           /* Augmentations should be small, so there's scarce need to
3907              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3908           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3909         }
3910     }
3911
3912   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3913   if (dw_cie_version >= 4)
3914     {
3915       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3916       dw2_asm_output_data (1, 0, "CIE Segment Size");
3917     }
3918   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3919   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3920                                "CIE Data Alignment Factor");
3921
3922   if (dw_cie_version == 1)
3923     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3924   else
3925     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3926
3927   if (augmentation[0])
3928     {
3929       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3930       if (personality)
3931         {
3932           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3933                                eh_data_format_name (per_encoding));
3934           dw2_asm_output_encoded_addr_rtx (per_encoding,
3935                                            personality,
3936                                            true, NULL);
3937         }
3938
3939       if (any_lsda_needed)
3940         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3941                              eh_data_format_name (lsda_encoding));
3942
3943       if (fde_encoding != DW_EH_PE_absptr)
3944         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3945                              eh_data_format_name (fde_encoding));
3946     }
3947
3948   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3949     output_cfi (cfi, NULL, for_eh);
3950
3951   /* Pad the CIE out to an address sized boundary.  */
3952   ASM_OUTPUT_ALIGN (asm_out_file,
3953                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3954   ASM_OUTPUT_LABEL (asm_out_file, l2);
3955
3956   /* Loop through all of the FDE's.  */
3957   for (i = 0; i < fde_table_in_use; i++)
3958     {
3959       unsigned int k;
3960       fde = &fde_table[i];
3961
3962       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3963       if (for_eh && !fde_needed_for_eh_p (fde))
3964         continue;
3965
3966       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
3967         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3968                     augmentation, any_lsda_needed, lsda_encoding);
3969     }
3970
3971   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3972     dw2_asm_output_data (4, 0, "End of Table");
3973 #ifdef MIPS_DEBUGGING_INFO
3974   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3975      get a value of 0.  Putting .align 0 after the label fixes it.  */
3976   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3977 #endif
3978
3979   /* Turn off app to make assembly quicker.  */
3980   if (flag_debug_asm)
3981     app_disable ();
3982 }
3983
3984 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3985
3986 static void
3987 dwarf2out_do_cfi_startproc (bool second)
3988 {
3989   int enc;
3990   rtx ref;
3991   rtx personality = get_personality_function (current_function_decl);
3992
3993   fprintf (asm_out_file, "\t.cfi_startproc\n");
3994
3995   if (personality)
3996     {
3997       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3998       ref = personality;
3999
4000       /* ??? The GAS support isn't entirely consistent.  We have to
4001          handle indirect support ourselves, but PC-relative is done
4002          in the assembler.  Further, the assembler can't handle any
4003          of the weirder relocation types.  */
4004       if (enc & DW_EH_PE_indirect)
4005         ref = dw2_force_const_mem (ref, true);
4006
4007       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4008       output_addr_const (asm_out_file, ref);
4009       fputc ('\n', asm_out_file);
4010     }
4011
4012   if (crtl->uses_eh_lsda)
4013     {
4014       char lab[20];
4015
4016       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4017       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4018                                    current_function_funcdef_no);
4019       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4020       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4021
4022       if (enc & DW_EH_PE_indirect)
4023         ref = dw2_force_const_mem (ref, true);
4024
4025       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4026       output_addr_const (asm_out_file, ref);
4027       fputc ('\n', asm_out_file);
4028     }
4029 }
4030
4031 /* Output a marker (i.e. a label) for the beginning of a function, before
4032    the prologue.  */
4033
4034 void
4035 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4036                           const char *file ATTRIBUTE_UNUSED)
4037 {
4038   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4039   char * dup_label;
4040   dw_fde_ref fde;
4041   section *fnsec;
4042   bool do_frame;
4043
4044   current_function_func_begin_label = NULL;
4045
4046   do_frame = dwarf2out_do_frame ();
4047
4048   /* ??? current_function_func_begin_label is also used by except.c for
4049      call-site information.  We must emit this label if it might be used.  */
4050   if (!do_frame
4051       && (!flag_exceptions
4052           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4053     return;
4054
4055   fnsec = function_section (current_function_decl);
4056   switch_to_section (fnsec);
4057   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4058                                current_function_funcdef_no);
4059   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4060                           current_function_funcdef_no);
4061   dup_label = xstrdup (label);
4062   current_function_func_begin_label = dup_label;
4063
4064   /* We can elide the fde allocation if we're not emitting debug info.  */
4065   if (!do_frame)
4066     return;
4067
4068   /* Expand the fde table if necessary.  */
4069   if (fde_table_in_use == fde_table_allocated)
4070     {
4071       fde_table_allocated += FDE_TABLE_INCREMENT;
4072       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4073       memset (fde_table + fde_table_in_use, 0,
4074               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4075     }
4076
4077   /* Record the FDE associated with this function.  */
4078   current_funcdef_fde = fde_table_in_use;
4079
4080   /* Add the new FDE at the end of the fde_table.  */
4081   fde = &fde_table[fde_table_in_use++];
4082   fde->decl = current_function_decl;
4083   fde->dw_fde_begin = dup_label;
4084   fde->dw_fde_end = NULL;
4085   fde->dw_fde_current_label = dup_label;
4086   fde->dw_fde_second_begin = NULL;
4087   fde->dw_fde_second_end = NULL;
4088   fde->dw_fde_vms_end_prologue = NULL;
4089   fde->dw_fde_vms_begin_epilogue = NULL;
4090   fde->dw_fde_cfi = NULL;
4091   fde->dw_fde_switch_cfi = NULL;
4092   fde->funcdef_number = current_function_funcdef_no;
4093   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4094   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4095   fde->nothrow = crtl->nothrow;
4096   fde->drap_reg = INVALID_REGNUM;
4097   fde->vdrap_reg = INVALID_REGNUM;
4098   fde->in_std_section = (fnsec == text_section
4099                          || (cold_text_section && fnsec == cold_text_section));
4100   fde->second_in_std_section = 0;
4101
4102   args_size = old_args_size = 0;
4103
4104   /* We only want to output line number information for the genuine dwarf2
4105      prologue case, not the eh frame case.  */
4106 #ifdef DWARF2_DEBUGGING_INFO
4107   if (file)
4108     dwarf2out_source_line (line, file, 0, true);
4109 #endif
4110
4111   if (dwarf2out_do_cfi_asm ())
4112     dwarf2out_do_cfi_startproc (false);
4113   else
4114     {
4115       rtx personality = get_personality_function (current_function_decl);
4116       if (!current_unit_personality)
4117         current_unit_personality = personality;
4118
4119       /* We cannot keep a current personality per function as without CFI
4120          asm, at the point where we emit the CFI data, there is no current
4121          function anymore.  */
4122       if (personality && current_unit_personality != personality)
4123         sorry ("multiple EH personalities are supported only with assemblers "
4124                "supporting .cfi_personality directive");
4125     }
4126 }
4127
4128 /* Output a marker (i.e. a label) for the end of the generated code
4129    for a function prologue.  This gets called *after* the prologue code has
4130    been generated.  */
4131
4132 void
4133 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4134                         const char *file ATTRIBUTE_UNUSED)
4135 {
4136   dw_fde_ref fde;
4137   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4138
4139   /* Output a label to mark the endpoint of the code generated for this
4140      function.  */
4141   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4142                                current_function_funcdef_no);
4143   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4144                           current_function_funcdef_no);
4145   fde = &fde_table[fde_table_in_use - 1];
4146   fde->dw_fde_vms_end_prologue = xstrdup (label);
4147 }
4148
4149 /* Output a marker (i.e. a label) for the beginning of the generated code
4150    for a function epilogue.  This gets called *before* the prologue code has
4151    been generated.  */
4152
4153 void
4154 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4155                           const char *file ATTRIBUTE_UNUSED)
4156 {
4157   dw_fde_ref fde;
4158   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4159
4160   fde = &fde_table[fde_table_in_use - 1];
4161   if (fde->dw_fde_vms_begin_epilogue)
4162     return;
4163
4164   /* Output a label to mark the endpoint of the code generated for this
4165      function.  */
4166   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4167                                current_function_funcdef_no);
4168   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4169                           current_function_funcdef_no);
4170   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4171 }
4172
4173 /* Output a marker (i.e. a label) for the absolute end of the generated code
4174    for a function definition.  This gets called *after* the epilogue code has
4175    been generated.  */
4176
4177 void
4178 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4179                         const char *file ATTRIBUTE_UNUSED)
4180 {
4181   dw_fde_ref fde;
4182   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4183
4184   last_var_location_insn = NULL_RTX;
4185
4186   if (dwarf2out_do_cfi_asm ())
4187     fprintf (asm_out_file, "\t.cfi_endproc\n");
4188
4189   /* Output a label to mark the endpoint of the code generated for this
4190      function.  */
4191   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4192                                current_function_funcdef_no);
4193   ASM_OUTPUT_LABEL (asm_out_file, label);
4194   fde = current_fde ();
4195   gcc_assert (fde != NULL);
4196   if (fde->dw_fde_second_begin == NULL)
4197     fde->dw_fde_end = xstrdup (label);
4198 }
4199
4200 void
4201 dwarf2out_frame_init (void)
4202 {
4203   /* Allocate the initial hunk of the fde_table.  */
4204   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4205   fde_table_allocated = FDE_TABLE_INCREMENT;
4206   fde_table_in_use = 0;
4207
4208   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4209      sake of lookup_cfa.  */
4210
4211   /* On entry, the Canonical Frame Address is at SP.  */
4212   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4213
4214   if (targetm.debug_unwind_info () == UI_DWARF2
4215       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4216     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4217 }
4218
4219 void
4220 dwarf2out_frame_finish (void)
4221 {
4222   /* Output call frame information.  */
4223   if (targetm.debug_unwind_info () == UI_DWARF2)
4224     output_call_frame_info (0);
4225
4226   /* Output another copy for the unwinder.  */
4227   if ((flag_unwind_tables || flag_exceptions)
4228       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4229     output_call_frame_info (1);
4230 }
4231
4232 /* Note that the current function section is being used for code.  */
4233
4234 static void
4235 dwarf2out_note_section_used (void)
4236 {
4237   section *sec = current_function_section ();
4238   if (sec == text_section)
4239     text_section_used = true;
4240   else if (sec == cold_text_section)
4241     cold_text_section_used = true;
4242 }
4243
4244 static void var_location_switch_text_section (void);
4245
4246 void
4247 dwarf2out_switch_text_section (void)
4248 {
4249   section *sect;
4250   dw_fde_ref fde = current_fde ();
4251   dw_cfi_ref cfi;
4252
4253   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4254
4255   if (!in_cold_section_p)
4256     {
4257       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4258       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4259       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4260     }
4261   else
4262     {
4263       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4264       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4265       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4266     }
4267   have_multiple_function_sections = true;
4268
4269   /* Reset the current label on switching text sections, so that we
4270      don't attempt to advance_loc4 between labels in different sections.  */
4271   fde->dw_fde_current_label = NULL;
4272
4273   /* There is no need to mark used sections when not debugging.  */
4274   if (cold_text_section != NULL)
4275     dwarf2out_note_section_used ();
4276
4277   if (dwarf2out_do_cfi_asm ())
4278     fprintf (asm_out_file, "\t.cfi_endproc\n");
4279
4280   /* Now do the real section switch.  */
4281   sect = current_function_section ();
4282   switch_to_section (sect);
4283
4284   fde->second_in_std_section
4285     = (sect == text_section
4286        || (cold_text_section && sect == cold_text_section));
4287
4288   if (dwarf2out_do_cfi_asm ())
4289     {
4290       dwarf2out_do_cfi_startproc (true);
4291       /* As this is a different FDE, insert all current CFI instructions
4292          again.  */
4293       output_cfis (fde->dw_fde_cfi, true, fde, true);
4294     }
4295   cfi = fde->dw_fde_cfi;
4296   if (cfi)
4297     while (cfi->dw_cfi_next != NULL)
4298       cfi = cfi->dw_cfi_next;
4299   fde->dw_fde_switch_cfi = cfi;
4300   var_location_switch_text_section ();
4301 }
4302 \f
4303 /* And now, the subset of the debugging information support code necessary
4304    for emitting location expressions.  */
4305
4306 /* Data about a single source file.  */
4307 struct GTY(()) dwarf_file_data {
4308   const char * filename;
4309   int emitted_number;
4310 };
4311
4312 typedef struct dw_val_struct *dw_val_ref;
4313 typedef struct die_struct *dw_die_ref;
4314 typedef const struct die_struct *const_dw_die_ref;
4315 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4316 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4317
4318 typedef struct GTY(()) deferred_locations_struct
4319 {
4320   tree variable;
4321   dw_die_ref die;
4322 } deferred_locations;
4323
4324 DEF_VEC_O(deferred_locations);
4325 DEF_VEC_ALLOC_O(deferred_locations,gc);
4326
4327 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4328
4329 DEF_VEC_P(dw_die_ref);
4330 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4331
4332 /* Each DIE may have a series of attribute/value pairs.  Values
4333    can take on several forms.  The forms that are used in this
4334    implementation are listed below.  */
4335
4336 enum dw_val_class
4337 {
4338   dw_val_class_addr,
4339   dw_val_class_offset,
4340   dw_val_class_loc,
4341   dw_val_class_loc_list,
4342   dw_val_class_range_list,
4343   dw_val_class_const,
4344   dw_val_class_unsigned_const,
4345   dw_val_class_const_double,
4346   dw_val_class_vec,
4347   dw_val_class_flag,
4348   dw_val_class_die_ref,
4349   dw_val_class_fde_ref,
4350   dw_val_class_lbl_id,
4351   dw_val_class_lineptr,
4352   dw_val_class_str,
4353   dw_val_class_macptr,
4354   dw_val_class_file,
4355   dw_val_class_data8,
4356   dw_val_class_decl_ref,
4357   dw_val_class_vms_delta
4358 };
4359
4360 /* Describe a floating point constant value, or a vector constant value.  */
4361
4362 typedef struct GTY(()) dw_vec_struct {
4363   unsigned char * GTY((length ("%h.length"))) array;
4364   unsigned length;
4365   unsigned elt_size;
4366 }
4367 dw_vec_const;
4368
4369 /* The dw_val_node describes an attribute's value, as it is
4370    represented internally.  */
4371
4372 typedef struct GTY(()) dw_val_struct {
4373   enum dw_val_class val_class;
4374   union dw_val_struct_union
4375     {
4376       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4377       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4378       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4379       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4380       HOST_WIDE_INT GTY ((default)) val_int;
4381       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4382       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4383       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4384       struct dw_val_die_union
4385         {
4386           dw_die_ref die;
4387           int external;
4388         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4389       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4390       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4391       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4392       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4393       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4394       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4395       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4396       struct dw_val_vms_delta_union
4397         {
4398           char * lbl1;
4399           char * lbl2;
4400         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4401     }
4402   GTY ((desc ("%1.val_class"))) v;
4403 }
4404 dw_val_node;
4405
4406 /* Locations in memory are described using a sequence of stack machine
4407    operations.  */
4408
4409 typedef struct GTY(()) dw_loc_descr_struct {
4410   dw_loc_descr_ref dw_loc_next;
4411   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4412   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4413      from DW_OP_addr with a dtp-relative symbol relocation.  */
4414   unsigned int dtprel : 1;
4415   int dw_loc_addr;
4416   dw_val_node dw_loc_oprnd1;
4417   dw_val_node dw_loc_oprnd2;
4418 }
4419 dw_loc_descr_node;
4420
4421 /* Location lists are ranges + location descriptions for that range,
4422    so you can track variables that are in different places over
4423    their entire life.  */
4424 typedef struct GTY(()) dw_loc_list_struct {
4425   dw_loc_list_ref dw_loc_next;
4426   const char *begin; /* Label for begin address of range */
4427   const char *end;  /* Label for end address of range */
4428   char *ll_symbol; /* Label for beginning of location list.
4429                       Only on head of list */
4430   const char *section; /* Section this loclist is relative to */
4431   dw_loc_descr_ref expr;
4432   hashval_t hash;
4433   /* True if all addresses in this and subsequent lists are known to be
4434      resolved.  */
4435   bool resolved_addr;
4436   /* True if this list has been replaced by dw_loc_next.  */
4437   bool replaced;
4438   bool emitted;
4439 } dw_loc_list_node;
4440
4441 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4442
4443 /* Convert a DWARF stack opcode into its string name.  */
4444
4445 static const char *
4446 dwarf_stack_op_name (unsigned int op)
4447 {
4448   switch (op)
4449     {
4450     case DW_OP_addr:
4451       return "DW_OP_addr";
4452     case DW_OP_deref:
4453       return "DW_OP_deref";
4454     case DW_OP_const1u:
4455       return "DW_OP_const1u";
4456     case DW_OP_const1s:
4457       return "DW_OP_const1s";
4458     case DW_OP_const2u:
4459       return "DW_OP_const2u";
4460     case DW_OP_const2s:
4461       return "DW_OP_const2s";
4462     case DW_OP_const4u:
4463       return "DW_OP_const4u";
4464     case DW_OP_const4s:
4465       return "DW_OP_const4s";
4466     case DW_OP_const8u:
4467       return "DW_OP_const8u";
4468     case DW_OP_const8s:
4469       return "DW_OP_const8s";
4470     case DW_OP_constu:
4471       return "DW_OP_constu";
4472     case DW_OP_consts:
4473       return "DW_OP_consts";
4474     case DW_OP_dup:
4475       return "DW_OP_dup";
4476     case DW_OP_drop:
4477       return "DW_OP_drop";
4478     case DW_OP_over:
4479       return "DW_OP_over";
4480     case DW_OP_pick:
4481       return "DW_OP_pick";
4482     case DW_OP_swap:
4483       return "DW_OP_swap";
4484     case DW_OP_rot:
4485       return "DW_OP_rot";
4486     case DW_OP_xderef:
4487       return "DW_OP_xderef";
4488     case DW_OP_abs:
4489       return "DW_OP_abs";
4490     case DW_OP_and:
4491       return "DW_OP_and";
4492     case DW_OP_div:
4493       return "DW_OP_div";
4494     case DW_OP_minus:
4495       return "DW_OP_minus";
4496     case DW_OP_mod:
4497       return "DW_OP_mod";
4498     case DW_OP_mul:
4499       return "DW_OP_mul";
4500     case DW_OP_neg:
4501       return "DW_OP_neg";
4502     case DW_OP_not:
4503       return "DW_OP_not";
4504     case DW_OP_or:
4505       return "DW_OP_or";
4506     case DW_OP_plus:
4507       return "DW_OP_plus";
4508     case DW_OP_plus_uconst:
4509       return "DW_OP_plus_uconst";
4510     case DW_OP_shl:
4511       return "DW_OP_shl";
4512     case DW_OP_shr:
4513       return "DW_OP_shr";
4514     case DW_OP_shra:
4515       return "DW_OP_shra";
4516     case DW_OP_xor:
4517       return "DW_OP_xor";
4518     case DW_OP_bra:
4519       return "DW_OP_bra";
4520     case DW_OP_eq:
4521       return "DW_OP_eq";
4522     case DW_OP_ge:
4523       return "DW_OP_ge";
4524     case DW_OP_gt:
4525       return "DW_OP_gt";
4526     case DW_OP_le:
4527       return "DW_OP_le";
4528     case DW_OP_lt:
4529       return "DW_OP_lt";
4530     case DW_OP_ne:
4531       return "DW_OP_ne";
4532     case DW_OP_skip:
4533       return "DW_OP_skip";
4534     case DW_OP_lit0:
4535       return "DW_OP_lit0";
4536     case DW_OP_lit1:
4537       return "DW_OP_lit1";
4538     case DW_OP_lit2:
4539       return "DW_OP_lit2";
4540     case DW_OP_lit3:
4541       return "DW_OP_lit3";
4542     case DW_OP_lit4:
4543       return "DW_OP_lit4";
4544     case DW_OP_lit5:
4545       return "DW_OP_lit5";
4546     case DW_OP_lit6:
4547       return "DW_OP_lit6";
4548     case DW_OP_lit7:
4549       return "DW_OP_lit7";
4550     case DW_OP_lit8:
4551       return "DW_OP_lit8";
4552     case DW_OP_lit9:
4553       return "DW_OP_lit9";
4554     case DW_OP_lit10:
4555       return "DW_OP_lit10";
4556     case DW_OP_lit11:
4557       return "DW_OP_lit11";
4558     case DW_OP_lit12:
4559       return "DW_OP_lit12";
4560     case DW_OP_lit13:
4561       return "DW_OP_lit13";
4562     case DW_OP_lit14:
4563       return "DW_OP_lit14";
4564     case DW_OP_lit15:
4565       return "DW_OP_lit15";
4566     case DW_OP_lit16:
4567       return "DW_OP_lit16";
4568     case DW_OP_lit17:
4569       return "DW_OP_lit17";
4570     case DW_OP_lit18:
4571       return "DW_OP_lit18";
4572     case DW_OP_lit19:
4573       return "DW_OP_lit19";
4574     case DW_OP_lit20:
4575       return "DW_OP_lit20";
4576     case DW_OP_lit21:
4577       return "DW_OP_lit21";
4578     case DW_OP_lit22:
4579       return "DW_OP_lit22";
4580     case DW_OP_lit23:
4581       return "DW_OP_lit23";
4582     case DW_OP_lit24:
4583       return "DW_OP_lit24";
4584     case DW_OP_lit25:
4585       return "DW_OP_lit25";
4586     case DW_OP_lit26:
4587       return "DW_OP_lit26";
4588     case DW_OP_lit27:
4589       return "DW_OP_lit27";
4590     case DW_OP_lit28:
4591       return "DW_OP_lit28";
4592     case DW_OP_lit29:
4593       return "DW_OP_lit29";
4594     case DW_OP_lit30:
4595       return "DW_OP_lit30";
4596     case DW_OP_lit31:
4597       return "DW_OP_lit31";
4598     case DW_OP_reg0:
4599       return "DW_OP_reg0";
4600     case DW_OP_reg1:
4601       return "DW_OP_reg1";
4602     case DW_OP_reg2:
4603       return "DW_OP_reg2";
4604     case DW_OP_reg3:
4605       return "DW_OP_reg3";
4606     case DW_OP_reg4:
4607       return "DW_OP_reg4";
4608     case DW_OP_reg5:
4609       return "DW_OP_reg5";
4610     case DW_OP_reg6:
4611       return "DW_OP_reg6";
4612     case DW_OP_reg7:
4613       return "DW_OP_reg7";
4614     case DW_OP_reg8:
4615       return "DW_OP_reg8";
4616     case DW_OP_reg9:
4617       return "DW_OP_reg9";
4618     case DW_OP_reg10:
4619       return "DW_OP_reg10";
4620     case DW_OP_reg11:
4621       return "DW_OP_reg11";
4622     case DW_OP_reg12:
4623       return "DW_OP_reg12";
4624     case DW_OP_reg13:
4625       return "DW_OP_reg13";
4626     case DW_OP_reg14:
4627       return "DW_OP_reg14";
4628     case DW_OP_reg15:
4629       return "DW_OP_reg15";
4630     case DW_OP_reg16:
4631       return "DW_OP_reg16";
4632     case DW_OP_reg17:
4633       return "DW_OP_reg17";
4634     case DW_OP_reg18:
4635       return "DW_OP_reg18";
4636     case DW_OP_reg19:
4637       return "DW_OP_reg19";
4638     case DW_OP_reg20:
4639       return "DW_OP_reg20";
4640     case DW_OP_reg21:
4641       return "DW_OP_reg21";
4642     case DW_OP_reg22:
4643       return "DW_OP_reg22";
4644     case DW_OP_reg23:
4645       return "DW_OP_reg23";
4646     case DW_OP_reg24:
4647       return "DW_OP_reg24";
4648     case DW_OP_reg25:
4649       return "DW_OP_reg25";
4650     case DW_OP_reg26:
4651       return "DW_OP_reg26";
4652     case DW_OP_reg27:
4653       return "DW_OP_reg27";
4654     case DW_OP_reg28:
4655       return "DW_OP_reg28";
4656     case DW_OP_reg29:
4657       return "DW_OP_reg29";
4658     case DW_OP_reg30:
4659       return "DW_OP_reg30";
4660     case DW_OP_reg31:
4661       return "DW_OP_reg31";
4662     case DW_OP_breg0:
4663       return "DW_OP_breg0";
4664     case DW_OP_breg1:
4665       return "DW_OP_breg1";
4666     case DW_OP_breg2:
4667       return "DW_OP_breg2";
4668     case DW_OP_breg3:
4669       return "DW_OP_breg3";
4670     case DW_OP_breg4:
4671       return "DW_OP_breg4";
4672     case DW_OP_breg5:
4673       return "DW_OP_breg5";
4674     case DW_OP_breg6:
4675       return "DW_OP_breg6";
4676     case DW_OP_breg7:
4677       return "DW_OP_breg7";
4678     case DW_OP_breg8:
4679       return "DW_OP_breg8";
4680     case DW_OP_breg9:
4681       return "DW_OP_breg9";
4682     case DW_OP_breg10:
4683       return "DW_OP_breg10";
4684     case DW_OP_breg11:
4685       return "DW_OP_breg11";
4686     case DW_OP_breg12:
4687       return "DW_OP_breg12";
4688     case DW_OP_breg13:
4689       return "DW_OP_breg13";
4690     case DW_OP_breg14:
4691       return "DW_OP_breg14";
4692     case DW_OP_breg15:
4693       return "DW_OP_breg15";
4694     case DW_OP_breg16:
4695       return "DW_OP_breg16";
4696     case DW_OP_breg17:
4697       return "DW_OP_breg17";
4698     case DW_OP_breg18:
4699       return "DW_OP_breg18";
4700     case DW_OP_breg19:
4701       return "DW_OP_breg19";
4702     case DW_OP_breg20:
4703       return "DW_OP_breg20";
4704     case DW_OP_breg21:
4705       return "DW_OP_breg21";
4706     case DW_OP_breg22:
4707       return "DW_OP_breg22";
4708     case DW_OP_breg23:
4709       return "DW_OP_breg23";
4710     case DW_OP_breg24:
4711       return "DW_OP_breg24";
4712     case DW_OP_breg25:
4713       return "DW_OP_breg25";
4714     case DW_OP_breg26:
4715       return "DW_OP_breg26";
4716     case DW_OP_breg27:
4717       return "DW_OP_breg27";
4718     case DW_OP_breg28:
4719       return "DW_OP_breg28";
4720     case DW_OP_breg29:
4721       return "DW_OP_breg29";
4722     case DW_OP_breg30:
4723       return "DW_OP_breg30";
4724     case DW_OP_breg31:
4725       return "DW_OP_breg31";
4726     case DW_OP_regx:
4727       return "DW_OP_regx";
4728     case DW_OP_fbreg:
4729       return "DW_OP_fbreg";
4730     case DW_OP_bregx:
4731       return "DW_OP_bregx";
4732     case DW_OP_piece:
4733       return "DW_OP_piece";
4734     case DW_OP_deref_size:
4735       return "DW_OP_deref_size";
4736     case DW_OP_xderef_size:
4737       return "DW_OP_xderef_size";
4738     case DW_OP_nop:
4739       return "DW_OP_nop";
4740
4741     case DW_OP_push_object_address:
4742       return "DW_OP_push_object_address";
4743     case DW_OP_call2:
4744       return "DW_OP_call2";
4745     case DW_OP_call4:
4746       return "DW_OP_call4";
4747     case DW_OP_call_ref:
4748       return "DW_OP_call_ref";
4749     case DW_OP_implicit_value:
4750       return "DW_OP_implicit_value";
4751     case DW_OP_stack_value:
4752       return "DW_OP_stack_value";
4753     case DW_OP_form_tls_address:
4754       return "DW_OP_form_tls_address";
4755     case DW_OP_call_frame_cfa:
4756       return "DW_OP_call_frame_cfa";
4757     case DW_OP_bit_piece:
4758       return "DW_OP_bit_piece";
4759
4760     case DW_OP_GNU_push_tls_address:
4761       return "DW_OP_GNU_push_tls_address";
4762     case DW_OP_GNU_uninit:
4763       return "DW_OP_GNU_uninit";
4764     case DW_OP_GNU_encoded_addr:
4765       return "DW_OP_GNU_encoded_addr";
4766     case DW_OP_GNU_implicit_pointer:
4767       return "DW_OP_GNU_implicit_pointer";
4768     case DW_OP_GNU_entry_value:
4769       return "DW_OP_GNU_entry_value";
4770
4771     default:
4772       return "OP_<unknown>";
4773     }
4774 }
4775
4776 /* Return a pointer to a newly allocated location description.  Location
4777    descriptions are simple expression terms that can be strung
4778    together to form more complicated location (address) descriptions.  */
4779
4780 static inline dw_loc_descr_ref
4781 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4782                unsigned HOST_WIDE_INT oprnd2)
4783 {
4784   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4785
4786   descr->dw_loc_opc = op;
4787   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4788   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4789   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4790   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4791
4792   return descr;
4793 }
4794
4795 /* Return a pointer to a newly allocated location description for
4796    REG and OFFSET.  */
4797
4798 static inline dw_loc_descr_ref
4799 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4800 {
4801   if (reg <= 31)
4802     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4803                           offset, 0);
4804   else
4805     return new_loc_descr (DW_OP_bregx, reg, offset);
4806 }
4807
4808 /* Add a location description term to a location description expression.  */
4809
4810 static inline void
4811 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4812 {
4813   dw_loc_descr_ref *d;
4814
4815   /* Find the end of the chain.  */
4816   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4817     ;
4818
4819   *d = descr;
4820 }
4821
4822 /* Add a constant OFFSET to a location expression.  */
4823
4824 static void
4825 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4826 {
4827   dw_loc_descr_ref loc;
4828   HOST_WIDE_INT *p;
4829
4830   gcc_assert (*list_head != NULL);
4831
4832   if (!offset)
4833     return;
4834
4835   /* Find the end of the chain.  */
4836   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4837     ;
4838
4839   p = NULL;
4840   if (loc->dw_loc_opc == DW_OP_fbreg
4841       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4842     p = &loc->dw_loc_oprnd1.v.val_int;
4843   else if (loc->dw_loc_opc == DW_OP_bregx)
4844     p = &loc->dw_loc_oprnd2.v.val_int;
4845
4846   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4847      offset.  Don't optimize if an signed integer overflow would happen.  */
4848   if (p != NULL
4849       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4850           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4851     *p += offset;
4852
4853   else if (offset > 0)
4854     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4855
4856   else
4857     {
4858       loc->dw_loc_next = int_loc_descriptor (-offset);
4859       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4860     }
4861 }
4862
4863 /* Add a constant OFFSET to a location list.  */
4864
4865 static void
4866 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4867 {
4868   dw_loc_list_ref d;
4869   for (d = list_head; d != NULL; d = d->dw_loc_next)
4870     loc_descr_plus_const (&d->expr, offset);
4871 }
4872
4873 #define DWARF_REF_SIZE  \
4874   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4875
4876 static unsigned long size_of_locs (dw_loc_descr_ref);
4877
4878 /* Return the size of a location descriptor.  */
4879
4880 static unsigned long
4881 size_of_loc_descr (dw_loc_descr_ref loc)
4882 {
4883   unsigned long size = 1;
4884
4885   switch (loc->dw_loc_opc)
4886     {
4887     case DW_OP_addr:
4888       size += DWARF2_ADDR_SIZE;
4889       break;
4890     case DW_OP_const1u:
4891     case DW_OP_const1s:
4892       size += 1;
4893       break;
4894     case DW_OP_const2u:
4895     case DW_OP_const2s:
4896       size += 2;
4897       break;
4898     case DW_OP_const4u:
4899     case DW_OP_const4s:
4900       size += 4;
4901       break;
4902     case DW_OP_const8u:
4903     case DW_OP_const8s:
4904       size += 8;
4905       break;
4906     case DW_OP_constu:
4907       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4908       break;
4909     case DW_OP_consts:
4910       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4911       break;
4912     case DW_OP_pick:
4913       size += 1;
4914       break;
4915     case DW_OP_plus_uconst:
4916       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4917       break;
4918     case DW_OP_skip:
4919     case DW_OP_bra:
4920       size += 2;
4921       break;
4922     case DW_OP_breg0:
4923     case DW_OP_breg1:
4924     case DW_OP_breg2:
4925     case DW_OP_breg3:
4926     case DW_OP_breg4:
4927     case DW_OP_breg5:
4928     case DW_OP_breg6:
4929     case DW_OP_breg7:
4930     case DW_OP_breg8:
4931     case DW_OP_breg9:
4932     case DW_OP_breg10:
4933     case DW_OP_breg11:
4934     case DW_OP_breg12:
4935     case DW_OP_breg13:
4936     case DW_OP_breg14:
4937     case DW_OP_breg15:
4938     case DW_OP_breg16:
4939     case DW_OP_breg17:
4940     case DW_OP_breg18:
4941     case DW_OP_breg19:
4942     case DW_OP_breg20:
4943     case DW_OP_breg21:
4944     case DW_OP_breg22:
4945     case DW_OP_breg23:
4946     case DW_OP_breg24:
4947     case DW_OP_breg25:
4948     case DW_OP_breg26:
4949     case DW_OP_breg27:
4950     case DW_OP_breg28:
4951     case DW_OP_breg29:
4952     case DW_OP_breg30:
4953     case DW_OP_breg31:
4954       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4955       break;
4956     case DW_OP_regx:
4957       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4958       break;
4959     case DW_OP_fbreg:
4960       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4961       break;
4962     case DW_OP_bregx:
4963       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4964       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4965       break;
4966     case DW_OP_piece:
4967       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4968       break;
4969     case DW_OP_bit_piece:
4970       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4971       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4972       break;
4973     case DW_OP_deref_size:
4974     case DW_OP_xderef_size:
4975       size += 1;
4976       break;
4977     case DW_OP_call2:
4978       size += 2;
4979       break;
4980     case DW_OP_call4:
4981       size += 4;
4982       break;
4983     case DW_OP_call_ref:
4984       size += DWARF_REF_SIZE;
4985       break;
4986     case DW_OP_implicit_value:
4987       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4988               + loc->dw_loc_oprnd1.v.val_unsigned;
4989       break;
4990     case DW_OP_GNU_implicit_pointer:
4991       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4992       break;
4993     case DW_OP_GNU_entry_value:
4994       {
4995         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
4996         size += size_of_uleb128 (op_size) + op_size;
4997         break;
4998       }
4999     default:
5000       break;
5001     }
5002
5003   return size;
5004 }
5005
5006 /* Return the size of a series of location descriptors.  */
5007
5008 static unsigned long
5009 size_of_locs (dw_loc_descr_ref loc)
5010 {
5011   dw_loc_descr_ref l;
5012   unsigned long size;
5013
5014   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5015      field, to avoid writing to a PCH file.  */
5016   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5017     {
5018       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5019         break;
5020       size += size_of_loc_descr (l);
5021     }
5022   if (! l)
5023     return size;
5024
5025   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5026     {
5027       l->dw_loc_addr = size;
5028       size += size_of_loc_descr (l);
5029     }
5030
5031   return size;
5032 }
5033
5034 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5035 static void get_ref_die_offset_label (char *, dw_die_ref);
5036 static void output_loc_sequence (dw_loc_descr_ref, int);
5037
5038 /* Output location description stack opcode's operands (if any).
5039    The for_eh_or_skip parameter controls whether register numbers are
5040    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5041    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5042    info).  This should be suppressed for the cases that have not been converted
5043    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5044
5045 static void
5046 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5047 {
5048   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5049   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5050
5051   switch (loc->dw_loc_opc)
5052     {
5053 #ifdef DWARF2_DEBUGGING_INFO
5054     case DW_OP_const2u:
5055     case DW_OP_const2s:
5056       dw2_asm_output_data (2, val1->v.val_int, NULL);
5057       break;
5058     case DW_OP_const4u:
5059       if (loc->dtprel)
5060         {
5061           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5062           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5063                                                val1->v.val_addr);
5064           fputc ('\n', asm_out_file);
5065           break;
5066         }
5067       /* FALLTHRU */
5068     case DW_OP_const4s:
5069       dw2_asm_output_data (4, val1->v.val_int, NULL);
5070       break;
5071     case DW_OP_const8u:
5072       if (loc->dtprel)
5073         {
5074           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5075           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5076                                                val1->v.val_addr);
5077           fputc ('\n', asm_out_file);
5078           break;
5079         }
5080       /* FALLTHRU */
5081     case DW_OP_const8s:
5082       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5083       dw2_asm_output_data (8, val1->v.val_int, NULL);
5084       break;
5085     case DW_OP_skip:
5086     case DW_OP_bra:
5087       {
5088         int offset;
5089
5090         gcc_assert (val1->val_class == dw_val_class_loc);
5091         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5092
5093         dw2_asm_output_data (2, offset, NULL);
5094       }
5095       break;
5096     case DW_OP_implicit_value:
5097       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5098       switch (val2->val_class)
5099         {
5100         case dw_val_class_const:
5101           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5102           break;
5103         case dw_val_class_vec:
5104           {
5105             unsigned int elt_size = val2->v.val_vec.elt_size;
5106             unsigned int len = val2->v.val_vec.length;
5107             unsigned int i;
5108             unsigned char *p;
5109
5110             if (elt_size > sizeof (HOST_WIDE_INT))
5111               {
5112                 elt_size /= 2;
5113                 len *= 2;
5114               }
5115             for (i = 0, p = val2->v.val_vec.array;
5116                  i < len;
5117                  i++, p += elt_size)
5118               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5119                                    "fp or vector constant word %u", i);
5120           }
5121           break;
5122         case dw_val_class_const_double:
5123           {
5124             unsigned HOST_WIDE_INT first, second;
5125
5126             if (WORDS_BIG_ENDIAN)
5127               {
5128                 first = val2->v.val_double.high;
5129                 second = val2->v.val_double.low;
5130               }
5131             else
5132               {
5133                 first = val2->v.val_double.low;
5134                 second = val2->v.val_double.high;
5135               }
5136             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5137                                  first, NULL);
5138             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5139                                  second, NULL);
5140           }
5141           break;
5142         case dw_val_class_addr:
5143           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5144           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5145           break;
5146         default:
5147           gcc_unreachable ();
5148         }
5149       break;
5150 #else
5151     case DW_OP_const2u:
5152     case DW_OP_const2s:
5153     case DW_OP_const4u:
5154     case DW_OP_const4s:
5155     case DW_OP_const8u:
5156     case DW_OP_const8s:
5157     case DW_OP_skip:
5158     case DW_OP_bra:
5159     case DW_OP_implicit_value:
5160       /* We currently don't make any attempt to make sure these are
5161          aligned properly like we do for the main unwind info, so
5162          don't support emitting things larger than a byte if we're
5163          only doing unwinding.  */
5164       gcc_unreachable ();
5165 #endif
5166     case DW_OP_const1u:
5167     case DW_OP_const1s:
5168       dw2_asm_output_data (1, val1->v.val_int, NULL);
5169       break;
5170     case DW_OP_constu:
5171       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5172       break;
5173     case DW_OP_consts:
5174       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5175       break;
5176     case DW_OP_pick:
5177       dw2_asm_output_data (1, val1->v.val_int, NULL);
5178       break;
5179     case DW_OP_plus_uconst:
5180       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5181       break;
5182     case DW_OP_breg0:
5183     case DW_OP_breg1:
5184     case DW_OP_breg2:
5185     case DW_OP_breg3:
5186     case DW_OP_breg4:
5187     case DW_OP_breg5:
5188     case DW_OP_breg6:
5189     case DW_OP_breg7:
5190     case DW_OP_breg8:
5191     case DW_OP_breg9:
5192     case DW_OP_breg10:
5193     case DW_OP_breg11:
5194     case DW_OP_breg12:
5195     case DW_OP_breg13:
5196     case DW_OP_breg14:
5197     case DW_OP_breg15:
5198     case DW_OP_breg16:
5199     case DW_OP_breg17:
5200     case DW_OP_breg18:
5201     case DW_OP_breg19:
5202     case DW_OP_breg20:
5203     case DW_OP_breg21:
5204     case DW_OP_breg22:
5205     case DW_OP_breg23:
5206     case DW_OP_breg24:
5207     case DW_OP_breg25:
5208     case DW_OP_breg26:
5209     case DW_OP_breg27:
5210     case DW_OP_breg28:
5211     case DW_OP_breg29:
5212     case DW_OP_breg30:
5213     case DW_OP_breg31:
5214       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5215       break;
5216     case DW_OP_regx:
5217       {
5218         unsigned r = val1->v.val_unsigned;
5219         if (for_eh_or_skip >= 0)
5220           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5221         gcc_assert (size_of_uleb128 (r) 
5222                     == size_of_uleb128 (val1->v.val_unsigned));
5223         dw2_asm_output_data_uleb128 (r, NULL);  
5224       }
5225       break;
5226     case DW_OP_fbreg:
5227       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5228       break;
5229     case DW_OP_bregx:
5230       {
5231         unsigned r = val1->v.val_unsigned;
5232         if (for_eh_or_skip >= 0)
5233           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5234         gcc_assert (size_of_uleb128 (r) 
5235                     == size_of_uleb128 (val1->v.val_unsigned));
5236         dw2_asm_output_data_uleb128 (r, NULL);  
5237         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5238       }
5239       break;
5240     case DW_OP_piece:
5241       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5242       break;
5243     case DW_OP_bit_piece:
5244       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5245       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5246       break;
5247     case DW_OP_deref_size:
5248     case DW_OP_xderef_size:
5249       dw2_asm_output_data (1, val1->v.val_int, NULL);
5250       break;
5251
5252     case DW_OP_addr:
5253       if (loc->dtprel)
5254         {
5255           if (targetm.asm_out.output_dwarf_dtprel)
5256             {
5257               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5258                                                    DWARF2_ADDR_SIZE,
5259                                                    val1->v.val_addr);
5260               fputc ('\n', asm_out_file);
5261             }
5262           else
5263             gcc_unreachable ();
5264         }
5265       else
5266         {
5267 #ifdef DWARF2_DEBUGGING_INFO
5268           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5269 #else
5270           gcc_unreachable ();
5271 #endif
5272         }
5273       break;
5274
5275     case DW_OP_GNU_implicit_pointer:
5276       {
5277         char label[MAX_ARTIFICIAL_LABEL_BYTES
5278                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5279         gcc_assert (val1->val_class == dw_val_class_die_ref);
5280         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5281         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5282         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5283       }
5284       break;
5285
5286     case DW_OP_GNU_entry_value:
5287       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5288       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5289       break;
5290
5291     default:
5292       /* Other codes have no operands.  */
5293       break;
5294     }
5295 }
5296
5297 /* Output a sequence of location operations.  
5298    The for_eh_or_skip parameter controls whether register numbers are
5299    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5300    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5301    info).  This should be suppressed for the cases that have not been converted
5302    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5303
5304 static void
5305 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5306 {
5307   for (; loc != NULL; loc = loc->dw_loc_next)
5308     {
5309       enum dwarf_location_atom opc = loc->dw_loc_opc;
5310       /* Output the opcode.  */
5311       if (for_eh_or_skip >= 0 
5312           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5313         {
5314           unsigned r = (opc - DW_OP_breg0);
5315           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5316           gcc_assert (r <= 31);
5317           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5318         }
5319       else if (for_eh_or_skip >= 0 
5320                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5321         {
5322           unsigned r = (opc - DW_OP_reg0);
5323           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5324           gcc_assert (r <= 31);
5325           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5326         }
5327
5328       dw2_asm_output_data (1, opc,
5329                              "%s", dwarf_stack_op_name (opc));
5330
5331       /* Output the operand(s) (if any).  */
5332       output_loc_operands (loc, for_eh_or_skip);
5333     }
5334 }
5335
5336 /* Output location description stack opcode's operands (if any).
5337    The output is single bytes on a line, suitable for .cfi_escape.  */
5338
5339 static void
5340 output_loc_operands_raw (dw_loc_descr_ref loc)
5341 {
5342   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5343   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5344
5345   switch (loc->dw_loc_opc)
5346     {
5347     case DW_OP_addr:
5348     case DW_OP_implicit_value:
5349       /* We cannot output addresses in .cfi_escape, only bytes.  */
5350       gcc_unreachable ();
5351
5352     case DW_OP_const1u:
5353     case DW_OP_const1s:
5354     case DW_OP_pick:
5355     case DW_OP_deref_size:
5356     case DW_OP_xderef_size:
5357       fputc (',', asm_out_file);
5358       dw2_asm_output_data_raw (1, val1->v.val_int);
5359       break;
5360
5361     case DW_OP_const2u:
5362     case DW_OP_const2s:
5363       fputc (',', asm_out_file);
5364       dw2_asm_output_data_raw (2, val1->v.val_int);
5365       break;
5366
5367     case DW_OP_const4u:
5368     case DW_OP_const4s:
5369       fputc (',', asm_out_file);
5370       dw2_asm_output_data_raw (4, val1->v.val_int);
5371       break;
5372
5373     case DW_OP_const8u:
5374     case DW_OP_const8s:
5375       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5376       fputc (',', asm_out_file);
5377       dw2_asm_output_data_raw (8, val1->v.val_int);
5378       break;
5379
5380     case DW_OP_skip:
5381     case DW_OP_bra:
5382       {
5383         int offset;
5384
5385         gcc_assert (val1->val_class == dw_val_class_loc);
5386         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5387
5388         fputc (',', asm_out_file);
5389         dw2_asm_output_data_raw (2, offset);
5390       }
5391       break;
5392
5393     case DW_OP_regx:
5394       {
5395         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5396         gcc_assert (size_of_uleb128 (r) 
5397                     == size_of_uleb128 (val1->v.val_unsigned));
5398         fputc (',', asm_out_file);
5399         dw2_asm_output_data_uleb128_raw (r);
5400       }
5401       break;
5402       
5403     case DW_OP_constu:
5404     case DW_OP_plus_uconst:
5405     case DW_OP_piece:
5406       fputc (',', asm_out_file);
5407       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5408       break;
5409
5410     case DW_OP_bit_piece:
5411       fputc (',', asm_out_file);
5412       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5413       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5414       break;
5415
5416     case DW_OP_consts:
5417     case DW_OP_breg0:
5418     case DW_OP_breg1:
5419     case DW_OP_breg2:
5420     case DW_OP_breg3:
5421     case DW_OP_breg4:
5422     case DW_OP_breg5:
5423     case DW_OP_breg6:
5424     case DW_OP_breg7:
5425     case DW_OP_breg8:
5426     case DW_OP_breg9:
5427     case DW_OP_breg10:
5428     case DW_OP_breg11:
5429     case DW_OP_breg12:
5430     case DW_OP_breg13:
5431     case DW_OP_breg14:
5432     case DW_OP_breg15:
5433     case DW_OP_breg16:
5434     case DW_OP_breg17:
5435     case DW_OP_breg18:
5436     case DW_OP_breg19:
5437     case DW_OP_breg20:
5438     case DW_OP_breg21:
5439     case DW_OP_breg22:
5440     case DW_OP_breg23:
5441     case DW_OP_breg24:
5442     case DW_OP_breg25:
5443     case DW_OP_breg26:
5444     case DW_OP_breg27:
5445     case DW_OP_breg28:
5446     case DW_OP_breg29:
5447     case DW_OP_breg30:
5448     case DW_OP_breg31:
5449     case DW_OP_fbreg:
5450       fputc (',', asm_out_file);
5451       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5452       break;
5453
5454     case DW_OP_bregx:
5455       {
5456         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5457         gcc_assert (size_of_uleb128 (r) 
5458                     == size_of_uleb128 (val1->v.val_unsigned));
5459         fputc (',', asm_out_file);
5460         dw2_asm_output_data_uleb128_raw (r);
5461         fputc (',', asm_out_file);
5462         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5463       }
5464       break;
5465
5466     case DW_OP_GNU_implicit_pointer:
5467     case DW_OP_GNU_entry_value:
5468       gcc_unreachable ();
5469       break;
5470
5471     default:
5472       /* Other codes have no operands.  */
5473       break;
5474     }
5475 }
5476
5477 static void
5478 output_loc_sequence_raw (dw_loc_descr_ref loc)
5479 {
5480   while (1)
5481     {
5482       enum dwarf_location_atom opc = loc->dw_loc_opc;
5483       /* Output the opcode.  */
5484       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5485         {
5486           unsigned r = (opc - DW_OP_breg0);
5487           r = DWARF2_FRAME_REG_OUT (r, 1);
5488           gcc_assert (r <= 31);
5489           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5490         }
5491       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5492         {
5493           unsigned r = (opc - DW_OP_reg0);
5494           r = DWARF2_FRAME_REG_OUT (r, 1);
5495           gcc_assert (r <= 31);
5496           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5497         }
5498       /* Output the opcode.  */
5499       fprintf (asm_out_file, "%#x", opc);
5500       output_loc_operands_raw (loc);
5501
5502       if (!loc->dw_loc_next)
5503         break;
5504       loc = loc->dw_loc_next;
5505
5506       fputc (',', asm_out_file);
5507     }
5508 }
5509
5510 /* This routine will generate the correct assembly data for a location
5511    description based on a cfi entry with a complex address.  */
5512
5513 static void
5514 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5515 {
5516   dw_loc_descr_ref loc;
5517   unsigned long size;
5518
5519   if (cfi->dw_cfi_opc == DW_CFA_expression)
5520     {
5521       unsigned r = 
5522         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5523       dw2_asm_output_data (1, r, NULL);
5524       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5525     }
5526   else
5527     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5528
5529   /* Output the size of the block.  */
5530   size = size_of_locs (loc);
5531   dw2_asm_output_data_uleb128 (size, NULL);
5532
5533   /* Now output the operations themselves.  */
5534   output_loc_sequence (loc, for_eh);
5535 }
5536
5537 /* Similar, but used for .cfi_escape.  */
5538
5539 static void
5540 output_cfa_loc_raw (dw_cfi_ref cfi)
5541 {
5542   dw_loc_descr_ref loc;
5543   unsigned long size;
5544
5545   if (cfi->dw_cfi_opc == DW_CFA_expression)
5546     {
5547       unsigned r = 
5548         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5549       fprintf (asm_out_file, "%#x,", r);
5550       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5551     }
5552   else
5553     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5554
5555   /* Output the size of the block.  */
5556   size = size_of_locs (loc);
5557   dw2_asm_output_data_uleb128_raw (size);
5558   fputc (',', asm_out_file);
5559
5560   /* Now output the operations themselves.  */
5561   output_loc_sequence_raw (loc);
5562 }
5563
5564 /* This function builds a dwarf location descriptor sequence from a
5565    dw_cfa_location, adding the given OFFSET to the result of the
5566    expression.  */
5567
5568 static struct dw_loc_descr_struct *
5569 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5570 {
5571   struct dw_loc_descr_struct *head, *tmp;
5572
5573   offset += cfa->offset;
5574
5575   if (cfa->indirect)
5576     {
5577       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5578       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5579       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5580       add_loc_descr (&head, tmp);
5581       if (offset != 0)
5582         {
5583           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5584           add_loc_descr (&head, tmp);
5585         }
5586     }
5587   else
5588     head = new_reg_loc_descr (cfa->reg, offset);
5589
5590   return head;
5591 }
5592
5593 /* This function builds a dwarf location descriptor sequence for
5594    the address at OFFSET from the CFA when stack is aligned to
5595    ALIGNMENT byte.  */
5596
5597 static struct dw_loc_descr_struct *
5598 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5599 {
5600   struct dw_loc_descr_struct *head;
5601   unsigned int dwarf_fp
5602     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5603
5604  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5605   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5606     {
5607       head = new_reg_loc_descr (dwarf_fp, 0);
5608       add_loc_descr (&head, int_loc_descriptor (alignment));
5609       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5610       loc_descr_plus_const (&head, offset);
5611     }
5612   else
5613     head = new_reg_loc_descr (dwarf_fp, offset);
5614   return head;
5615 }
5616
5617 /* This function fills in aa dw_cfa_location structure from a dwarf location
5618    descriptor sequence.  */
5619
5620 static void
5621 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5622 {
5623   struct dw_loc_descr_struct *ptr;
5624   cfa->offset = 0;
5625   cfa->base_offset = 0;
5626   cfa->indirect = 0;
5627   cfa->reg = -1;
5628
5629   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5630     {
5631       enum dwarf_location_atom op = ptr->dw_loc_opc;
5632
5633       switch (op)
5634         {
5635         case DW_OP_reg0:
5636         case DW_OP_reg1:
5637         case DW_OP_reg2:
5638         case DW_OP_reg3:
5639         case DW_OP_reg4:
5640         case DW_OP_reg5:
5641         case DW_OP_reg6:
5642         case DW_OP_reg7:
5643         case DW_OP_reg8:
5644         case DW_OP_reg9:
5645         case DW_OP_reg10:
5646         case DW_OP_reg11:
5647         case DW_OP_reg12:
5648         case DW_OP_reg13:
5649         case DW_OP_reg14:
5650         case DW_OP_reg15:
5651         case DW_OP_reg16:
5652         case DW_OP_reg17:
5653         case DW_OP_reg18:
5654         case DW_OP_reg19:
5655         case DW_OP_reg20:
5656         case DW_OP_reg21:
5657         case DW_OP_reg22:
5658         case DW_OP_reg23:
5659         case DW_OP_reg24:
5660         case DW_OP_reg25:
5661         case DW_OP_reg26:
5662         case DW_OP_reg27:
5663         case DW_OP_reg28:
5664         case DW_OP_reg29:
5665         case DW_OP_reg30:
5666         case DW_OP_reg31:
5667           cfa->reg = op - DW_OP_reg0;
5668           break;
5669         case DW_OP_regx:
5670           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5671           break;
5672         case DW_OP_breg0:
5673         case DW_OP_breg1:
5674         case DW_OP_breg2:
5675         case DW_OP_breg3:
5676         case DW_OP_breg4:
5677         case DW_OP_breg5:
5678         case DW_OP_breg6:
5679         case DW_OP_breg7:
5680         case DW_OP_breg8:
5681         case DW_OP_breg9:
5682         case DW_OP_breg10:
5683         case DW_OP_breg11:
5684         case DW_OP_breg12:
5685         case DW_OP_breg13:
5686         case DW_OP_breg14:
5687         case DW_OP_breg15:
5688         case DW_OP_breg16:
5689         case DW_OP_breg17:
5690         case DW_OP_breg18:
5691         case DW_OP_breg19:
5692         case DW_OP_breg20:
5693         case DW_OP_breg21:
5694         case DW_OP_breg22:
5695         case DW_OP_breg23:
5696         case DW_OP_breg24:
5697         case DW_OP_breg25:
5698         case DW_OP_breg26:
5699         case DW_OP_breg27:
5700         case DW_OP_breg28:
5701         case DW_OP_breg29:
5702         case DW_OP_breg30:
5703         case DW_OP_breg31:
5704           cfa->reg = op - DW_OP_breg0;
5705           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5706           break;
5707         case DW_OP_bregx:
5708           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5709           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5710           break;
5711         case DW_OP_deref:
5712           cfa->indirect = 1;
5713           break;
5714         case DW_OP_plus_uconst:
5715           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5716           break;
5717         default:
5718           internal_error ("DW_LOC_OP %s not implemented",
5719                           dwarf_stack_op_name (ptr->dw_loc_opc));
5720         }
5721     }
5722 }
5723 \f
5724 /* And now, the support for symbolic debugging information.  */
5725
5726 /* .debug_str support.  */
5727 static int output_indirect_string (void **, void *);
5728
5729 static void dwarf2out_init (const char *);
5730 static void dwarf2out_finish (const char *);
5731 static void dwarf2out_assembly_start (void);
5732 static void dwarf2out_define (unsigned int, const char *);
5733 static void dwarf2out_undef (unsigned int, const char *);
5734 static void dwarf2out_start_source_file (unsigned, const char *);
5735 static void dwarf2out_end_source_file (unsigned);
5736 static void dwarf2out_function_decl (tree);
5737 static void dwarf2out_begin_block (unsigned, unsigned);
5738 static void dwarf2out_end_block (unsigned, unsigned);
5739 static bool dwarf2out_ignore_block (const_tree);
5740 static void dwarf2out_global_decl (tree);
5741 static void dwarf2out_type_decl (tree, int);
5742 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5743 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5744                                                  dw_die_ref);
5745 static void dwarf2out_abstract_function (tree);
5746 static void dwarf2out_var_location (rtx);
5747 static void dwarf2out_begin_function (tree);
5748 static void dwarf2out_set_name (tree, tree);
5749
5750 /* The debug hooks structure.  */
5751
5752 const struct gcc_debug_hooks dwarf2_debug_hooks =
5753 {
5754   dwarf2out_init,
5755   dwarf2out_finish,
5756   dwarf2out_assembly_start,
5757   dwarf2out_define,
5758   dwarf2out_undef,
5759   dwarf2out_start_source_file,
5760   dwarf2out_end_source_file,
5761   dwarf2out_begin_block,
5762   dwarf2out_end_block,
5763   dwarf2out_ignore_block,
5764   dwarf2out_source_line,
5765   dwarf2out_begin_prologue,
5766 #if VMS_DEBUGGING_INFO
5767   dwarf2out_vms_end_prologue,
5768   dwarf2out_vms_begin_epilogue,
5769 #else
5770   debug_nothing_int_charstar,
5771   debug_nothing_int_charstar,
5772 #endif
5773   dwarf2out_end_epilogue,
5774   dwarf2out_begin_function,
5775   debug_nothing_int,            /* end_function */
5776   dwarf2out_function_decl,      /* function_decl */
5777   dwarf2out_global_decl,
5778   dwarf2out_type_decl,          /* type_decl */
5779   dwarf2out_imported_module_or_decl,
5780   debug_nothing_tree,           /* deferred_inline_function */
5781   /* The DWARF 2 backend tries to reduce debugging bloat by not
5782      emitting the abstract description of inline functions until
5783      something tries to reference them.  */
5784   dwarf2out_abstract_function,  /* outlining_inline_function */
5785   debug_nothing_rtx,            /* label */
5786   debug_nothing_int,            /* handle_pch */
5787   dwarf2out_var_location,
5788   dwarf2out_switch_text_section,
5789   dwarf2out_set_name,
5790   1,                            /* start_end_main_source_file */
5791   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5792 };
5793 \f
5794 /* NOTE: In the comments in this file, many references are made to
5795    "Debugging Information Entries".  This term is abbreviated as `DIE'
5796    throughout the remainder of this file.  */
5797
5798 /* An internal representation of the DWARF output is built, and then
5799    walked to generate the DWARF debugging info.  The walk of the internal
5800    representation is done after the entire program has been compiled.
5801    The types below are used to describe the internal representation.  */
5802
5803 /* Various DIE's use offsets relative to the beginning of the
5804    .debug_info section to refer to each other.  */
5805
5806 typedef long int dw_offset;
5807
5808 /* Define typedefs here to avoid circular dependencies.  */
5809
5810 typedef struct dw_attr_struct *dw_attr_ref;
5811 typedef struct dw_line_info_struct *dw_line_info_ref;
5812 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5813 typedef struct pubname_struct *pubname_ref;
5814 typedef struct dw_ranges_struct *dw_ranges_ref;
5815 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5816 typedef struct comdat_type_struct *comdat_type_node_ref;
5817
5818 /* Each entry in the line_info_table maintains the file and
5819    line number associated with the label generated for that
5820    entry.  The label gives the PC value associated with
5821    the line number entry.  */
5822
5823 typedef struct GTY(()) dw_line_info_struct {
5824   unsigned long dw_file_num;
5825   unsigned long dw_line_num;
5826 }
5827 dw_line_info_entry;
5828
5829 /* Line information for functions in separate sections; each one gets its
5830    own sequence.  */
5831 typedef struct GTY(()) dw_separate_line_info_struct {
5832   unsigned long dw_file_num;
5833   unsigned long dw_line_num;
5834   unsigned long function;
5835 }
5836 dw_separate_line_info_entry;
5837
5838 /* Each DIE attribute has a field specifying the attribute kind,
5839    a link to the next attribute in the chain, and an attribute value.
5840    Attributes are typically linked below the DIE they modify.  */
5841
5842 typedef struct GTY(()) dw_attr_struct {
5843   enum dwarf_attribute dw_attr;
5844   dw_val_node dw_attr_val;
5845 }
5846 dw_attr_node;
5847
5848 DEF_VEC_O(dw_attr_node);
5849 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5850
5851 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5852    The children of each node form a circular list linked by
5853    die_sib.  die_child points to the node *before* the "first" child node.  */
5854
5855 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5856   union die_symbol_or_type_node
5857     {
5858       char * GTY ((tag ("0"))) die_symbol;
5859       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5860     }
5861   GTY ((desc ("dwarf_version >= 4"))) die_id;
5862   VEC(dw_attr_node,gc) * die_attr;
5863   dw_die_ref die_parent;
5864   dw_die_ref die_child;
5865   dw_die_ref die_sib;
5866   dw_die_ref die_definition; /* ref from a specification to its definition */
5867   dw_offset die_offset;
5868   unsigned long die_abbrev;
5869   int die_mark;
5870   /* Die is used and must not be pruned as unused.  */
5871   int die_perennial_p;
5872   unsigned int decl_id;
5873   enum dwarf_tag die_tag;
5874 }
5875 die_node;
5876
5877 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5878 #define FOR_EACH_CHILD(die, c, expr) do {       \
5879   c = die->die_child;                           \
5880   if (c) do {                                   \
5881     c = c->die_sib;                             \
5882     expr;                                       \
5883   } while (c != die->die_child);                \
5884 } while (0)
5885
5886 /* The pubname structure */
5887
5888 typedef struct GTY(()) pubname_struct {
5889   dw_die_ref die;
5890   const char *name;
5891 }
5892 pubname_entry;
5893
5894 DEF_VEC_O(pubname_entry);
5895 DEF_VEC_ALLOC_O(pubname_entry, gc);
5896
5897 struct GTY(()) dw_ranges_struct {
5898   /* If this is positive, it's a block number, otherwise it's a
5899      bitwise-negated index into dw_ranges_by_label.  */
5900   int num;
5901 };
5902
5903 /* A structure to hold a macinfo entry.  */
5904
5905 typedef struct GTY(()) macinfo_struct {
5906   unsigned HOST_WIDE_INT code;
5907   unsigned HOST_WIDE_INT lineno;
5908   const char *info;
5909 }
5910 macinfo_entry;
5911
5912 DEF_VEC_O(macinfo_entry);
5913 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5914
5915 struct GTY(()) dw_ranges_by_label_struct {
5916   const char *begin;
5917   const char *end;
5918 };
5919
5920 /* The comdat type node structure.  */
5921 typedef struct GTY(()) comdat_type_struct
5922 {
5923   dw_die_ref root_die;
5924   dw_die_ref type_die;
5925   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5926   struct comdat_type_struct *next;
5927 }
5928 comdat_type_node;
5929
5930 /* The limbo die list structure.  */
5931 typedef struct GTY(()) limbo_die_struct {
5932   dw_die_ref die;
5933   tree created_for;
5934   struct limbo_die_struct *next;
5935 }
5936 limbo_die_node;
5937
5938 typedef struct skeleton_chain_struct
5939 {
5940   dw_die_ref old_die;
5941   dw_die_ref new_die;
5942   struct skeleton_chain_struct *parent;
5943 }
5944 skeleton_chain_node;
5945
5946 /* How to start an assembler comment.  */
5947 #ifndef ASM_COMMENT_START
5948 #define ASM_COMMENT_START ";#"
5949 #endif
5950
5951 /* Define a macro which returns nonzero for a TYPE_DECL which was
5952    implicitly generated for a tagged type.
5953
5954    Note that unlike the gcc front end (which generates a NULL named
5955    TYPE_DECL node for each complete tagged type, each array type, and
5956    each function type node created) the g++ front end generates a
5957    _named_ TYPE_DECL node for each tagged type node created.
5958    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5959    generate a DW_TAG_typedef DIE for them.  */
5960
5961 #define TYPE_DECL_IS_STUB(decl)                         \
5962   (DECL_NAME (decl) == NULL_TREE                        \
5963    || (DECL_ARTIFICIAL (decl)                           \
5964        && is_tagged_type (TREE_TYPE (decl))             \
5965        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5966            /* This is necessary for stub decls that     \
5967               appear in nested inline functions.  */    \
5968            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5969                && (decl_ultimate_origin (decl)          \
5970                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5971
5972 /* Information concerning the compilation unit's programming
5973    language, and compiler version.  */
5974
5975 /* Fixed size portion of the DWARF compilation unit header.  */
5976 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5977   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5978
5979 /* Fixed size portion of the DWARF comdat type unit header.  */
5980 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5981   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5982    + DWARF_OFFSET_SIZE)
5983
5984 /* Fixed size portion of public names info.  */
5985 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5986
5987 /* Fixed size portion of the address range info.  */
5988 #define DWARF_ARANGES_HEADER_SIZE                                       \
5989   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5990                 DWARF2_ADDR_SIZE * 2)                                   \
5991    - DWARF_INITIAL_LENGTH_SIZE)
5992
5993 /* Size of padding portion in the address range info.  It must be
5994    aligned to twice the pointer size.  */
5995 #define DWARF_ARANGES_PAD_SIZE \
5996   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5997                 DWARF2_ADDR_SIZE * 2)                              \
5998    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5999
6000 /* Use assembler line directives if available.  */
6001 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6002 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6003 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6004 #else
6005 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6006 #endif
6007 #endif
6008
6009 /* Minimum line offset in a special line info. opcode.
6010    This value was chosen to give a reasonable range of values.  */
6011 #define DWARF_LINE_BASE  -10
6012
6013 /* First special line opcode - leave room for the standard opcodes.  */
6014 #define DWARF_LINE_OPCODE_BASE  10
6015
6016 /* Range of line offsets in a special line info. opcode.  */
6017 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6018
6019 /* Flag that indicates the initial value of the is_stmt_start flag.
6020    In the present implementation, we do not mark any lines as
6021    the beginning of a source statement, because that information
6022    is not made available by the GCC front-end.  */
6023 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6024
6025 /* Maximum number of operations per instruction bundle.  */
6026 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6027 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6028 #endif
6029
6030 /* This location is used by calc_die_sizes() to keep track
6031    the offset of each DIE within the .debug_info section.  */
6032 static unsigned long next_die_offset;
6033
6034 /* Record the root of the DIE's built for the current compilation unit.  */
6035 static GTY(()) dw_die_ref single_comp_unit_die;
6036
6037 /* A list of type DIEs that have been separated into comdat sections.  */
6038 static GTY(()) comdat_type_node *comdat_type_list;
6039
6040 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6041 static GTY(()) limbo_die_node *limbo_die_list;
6042
6043 /* A list of DIEs for which we may have to generate
6044    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6045 static GTY(()) limbo_die_node *deferred_asm_name;
6046
6047 /* Filenames referenced by this compilation unit.  */
6048 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6049
6050 /* A hash table of references to DIE's that describe declarations.
6051    The key is a DECL_UID() which is a unique number identifying each decl.  */
6052 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6053
6054 /* A hash table of references to DIE's that describe COMMON blocks.
6055    The key is DECL_UID() ^ die_parent.  */
6056 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6057
6058 typedef struct GTY(()) die_arg_entry_struct {
6059     dw_die_ref die;
6060     tree arg;
6061 } die_arg_entry;
6062
6063 DEF_VEC_O(die_arg_entry);
6064 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6065
6066 /* Node of the variable location list.  */
6067 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6068   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6069      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6070      in mode of the EXPR_LIST node and first EXPR_LIST operand
6071      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6072      location or NULL for padding.  For larger bitsizes,
6073      mode is 0 and first operand is a CONCAT with bitsize
6074      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6075      NULL as second operand.  */
6076   rtx GTY (()) loc;
6077   const char * GTY (()) label;
6078   struct var_loc_node * GTY (()) next;
6079 };
6080
6081 /* Variable location list.  */
6082 struct GTY (()) var_loc_list_def {
6083   struct var_loc_node * GTY (()) first;
6084
6085   /* Pointer to the last but one or last element of the
6086      chained list.  If the list is empty, both first and
6087      last are NULL, if the list contains just one node
6088      or the last node certainly is not redundant, it points
6089      to the last node, otherwise points to the last but one.
6090      Do not mark it for GC because it is marked through the chain.  */
6091   struct var_loc_node * GTY ((skip ("%h"))) last;
6092
6093   /* Pointer to the last element before section switch,
6094      if NULL, either sections weren't switched or first
6095      is after section switch.  */
6096   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6097
6098   /* DECL_UID of the variable decl.  */
6099   unsigned int decl_id;
6100 };
6101 typedef struct var_loc_list_def var_loc_list;
6102
6103 /* Call argument location list.  */
6104 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6105   rtx GTY (()) call_arg_loc_note;
6106   const char * GTY (()) label;
6107   tree GTY (()) block;
6108   bool tail_call_p;
6109   rtx GTY (()) symbol_ref;
6110   struct call_arg_loc_node * GTY (()) next;
6111 };
6112
6113
6114 /* Table of decl location linked lists.  */
6115 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6116
6117 /* Head and tail of call_arg_loc chain.  */
6118 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6119 static struct call_arg_loc_node *call_arg_loc_last;
6120
6121 /* Number of call sites in the current function.  */
6122 static int call_site_count = -1;
6123 /* Number of tail call sites in the current function.  */
6124 static int tail_call_site_count = -1;
6125
6126 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6127    DIEs.  */
6128 static VEC (dw_die_ref, heap) *block_map;
6129
6130 /* A cached location list.  */
6131 struct GTY (()) cached_dw_loc_list_def {
6132   /* The DECL_UID of the decl that this entry describes.  */
6133   unsigned int decl_id;
6134
6135   /* The cached location list.  */
6136   dw_loc_list_ref loc_list;
6137 };
6138 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6139
6140 /* Table of cached location lists.  */
6141 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6142
6143 /* A pointer to the base of a list of references to DIE's that
6144    are uniquely identified by their tag, presence/absence of
6145    children DIE's, and list of attribute/value pairs.  */
6146 static GTY((length ("abbrev_die_table_allocated")))
6147   dw_die_ref *abbrev_die_table;
6148
6149 /* Number of elements currently allocated for abbrev_die_table.  */
6150 static GTY(()) unsigned abbrev_die_table_allocated;
6151
6152 /* Number of elements in type_die_table currently in use.  */
6153 static GTY(()) unsigned abbrev_die_table_in_use;
6154
6155 /* Size (in elements) of increments by which we may expand the
6156    abbrev_die_table.  */
6157 #define ABBREV_DIE_TABLE_INCREMENT 256
6158
6159 /* A pointer to the base of a table that contains line information
6160    for each source code line in .text in the compilation unit.  */
6161 static GTY((length ("line_info_table_allocated")))
6162      dw_line_info_ref line_info_table;
6163
6164 /* Number of elements currently allocated for line_info_table.  */
6165 static GTY(()) unsigned line_info_table_allocated;
6166
6167 /* Number of elements in line_info_table currently in use.  */
6168 static GTY(()) unsigned line_info_table_in_use;
6169
6170 /* A pointer to the base of a table that contains line information
6171    for each source code line outside of .text in the compilation unit.  */
6172 static GTY ((length ("separate_line_info_table_allocated")))
6173      dw_separate_line_info_ref separate_line_info_table;
6174
6175 /* Number of elements currently allocated for separate_line_info_table.  */
6176 static GTY(()) unsigned separate_line_info_table_allocated;
6177
6178 /* Number of elements in separate_line_info_table currently in use.  */
6179 static GTY(()) unsigned separate_line_info_table_in_use;
6180
6181 /* Size (in elements) of increments by which we may expand the
6182    line_info_table.  */
6183 #define LINE_INFO_TABLE_INCREMENT 1024
6184
6185 /* A flag to tell pubnames/types export if there is an info section to
6186    refer to.  */
6187 static bool info_section_emitted;
6188
6189 /* A pointer to the base of a table that contains a list of publicly
6190    accessible names.  */
6191 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6192
6193 /* A pointer to the base of a table that contains a list of publicly
6194    accessible types.  */
6195 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6196
6197 /* A pointer to the base of a table that contains a list of macro
6198    defines/undefines (and file start/end markers).  */
6199 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6200
6201 /* Array of dies for which we should generate .debug_ranges info.  */
6202 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6203
6204 /* Number of elements currently allocated for ranges_table.  */
6205 static GTY(()) unsigned ranges_table_allocated;
6206
6207 /* Number of elements in ranges_table currently in use.  */
6208 static GTY(()) unsigned ranges_table_in_use;
6209
6210 /* Array of pairs of labels referenced in ranges_table.  */
6211 static GTY ((length ("ranges_by_label_allocated")))
6212      dw_ranges_by_label_ref ranges_by_label;
6213
6214 /* Number of elements currently allocated for ranges_by_label.  */
6215 static GTY(()) unsigned ranges_by_label_allocated;
6216
6217 /* Number of elements in ranges_by_label currently in use.  */
6218 static GTY(()) unsigned ranges_by_label_in_use;
6219
6220 /* Size (in elements) of increments by which we may expand the
6221    ranges_table.  */
6222 #define RANGES_TABLE_INCREMENT 64
6223
6224 /* Whether we have location lists that need outputting */
6225 static GTY(()) bool have_location_lists;
6226
6227 /* Unique label counter.  */
6228 static GTY(()) unsigned int loclabel_num;
6229
6230 /* Unique label counter for point-of-call tables.  */
6231 static GTY(()) unsigned int poc_label_num;
6232
6233 /* Record whether the function being analyzed contains inlined functions.  */
6234 static int current_function_has_inlines;
6235
6236 /* The last file entry emitted by maybe_emit_file().  */
6237 static GTY(()) struct dwarf_file_data * last_emitted_file;
6238
6239 /* Number of internal labels generated by gen_internal_sym().  */
6240 static GTY(()) int label_num;
6241
6242 /* Cached result of previous call to lookup_filename.  */
6243 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6244
6245 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6246
6247 /* Instances of generic types for which we need to generate debug
6248    info that describe their generic parameters and arguments. That
6249    generation needs to happen once all types are properly laid out so
6250    we do it at the end of compilation.  */
6251 static GTY(()) VEC(tree,gc) *generic_type_instances;
6252
6253 /* Offset from the "steady-state frame pointer" to the frame base,
6254    within the current function.  */
6255 static HOST_WIDE_INT frame_pointer_fb_offset;
6256
6257 /* Forward declarations for functions defined in this file.  */
6258
6259 static int is_pseudo_reg (const_rtx);
6260 static tree type_main_variant (tree);
6261 static int is_tagged_type (const_tree);
6262 static const char *dwarf_tag_name (unsigned);
6263 static const char *dwarf_attr_name (unsigned);
6264 static const char *dwarf_form_name (unsigned);
6265 static tree decl_ultimate_origin (const_tree);
6266 static tree decl_class_context (tree);
6267 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6268 static inline enum dw_val_class AT_class (dw_attr_ref);
6269 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6270 static inline unsigned AT_flag (dw_attr_ref);
6271 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6272 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6273 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6274 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6275 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6276                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6277 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6278                                unsigned int, unsigned char *);
6279 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6280 static hashval_t debug_str_do_hash (const void *);
6281 static int debug_str_eq (const void *, const void *);
6282 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6283 static inline const char *AT_string (dw_attr_ref);
6284 static enum dwarf_form AT_string_form (dw_attr_ref);
6285 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6286 static void add_AT_specification (dw_die_ref, dw_die_ref);
6287 static inline dw_die_ref AT_ref (dw_attr_ref);
6288 static inline int AT_ref_external (dw_attr_ref);
6289 static inline void set_AT_ref_external (dw_attr_ref, int);
6290 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6291 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6292 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6293 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6294                              dw_loc_list_ref);
6295 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6296 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6297 static inline rtx AT_addr (dw_attr_ref);
6298 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6299 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6300 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6301 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6302                            unsigned HOST_WIDE_INT);
6303 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6304                                unsigned long);
6305 static inline const char *AT_lbl (dw_attr_ref);
6306 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6307 static const char *get_AT_low_pc (dw_die_ref);
6308 static const char *get_AT_hi_pc (dw_die_ref);
6309 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6310 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6311 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6312 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6313 static bool is_cxx (void);
6314 static bool is_fortran (void);
6315 static bool is_ada (void);
6316 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6317 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6318 static void add_child_die (dw_die_ref, dw_die_ref);
6319 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6320 static dw_die_ref lookup_type_die (tree);
6321 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6322 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6323 static void equate_type_number_to_die (tree, dw_die_ref);
6324 static hashval_t decl_die_table_hash (const void *);
6325 static int decl_die_table_eq (const void *, const void *);
6326 static dw_die_ref lookup_decl_die (tree);
6327 static hashval_t common_block_die_table_hash (const void *);
6328 static int common_block_die_table_eq (const void *, const void *);
6329 static hashval_t decl_loc_table_hash (const void *);
6330 static int decl_loc_table_eq (const void *, const void *);
6331 static var_loc_list *lookup_decl_loc (const_tree);
6332 static void equate_decl_number_to_die (tree, dw_die_ref);
6333 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6334 static void print_spaces (FILE *);
6335 static void print_die (dw_die_ref, FILE *);
6336 static void print_dwarf_line_table (FILE *);
6337 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6338 static dw_die_ref pop_compile_unit (dw_die_ref);
6339 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6340 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6341 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6342 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6343 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6344 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6345 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6346                                    struct md5_ctx *, int *);
6347 struct checksum_attributes;
6348 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6349 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6350 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6351 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6352 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6353 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6354 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6355 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6356 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6357 static void compute_section_prefix (dw_die_ref);
6358 static int is_type_die (dw_die_ref);
6359 static int is_comdat_die (dw_die_ref);
6360 static int is_symbol_die (dw_die_ref);
6361 static void assign_symbol_names (dw_die_ref);
6362 static void break_out_includes (dw_die_ref);
6363 static int is_declaration_die (dw_die_ref);
6364 static int should_move_die_to_comdat (dw_die_ref);
6365 static dw_die_ref clone_as_declaration (dw_die_ref);
6366 static dw_die_ref clone_die (dw_die_ref);
6367 static dw_die_ref clone_tree (dw_die_ref);
6368 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6369 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6370 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6371 static dw_die_ref generate_skeleton (dw_die_ref);
6372 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6373                                                          dw_die_ref);
6374 static void break_out_comdat_types (dw_die_ref);
6375 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6376 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6377 static void copy_decls_for_unworthy_types (dw_die_ref);
6378
6379 static hashval_t htab_cu_hash (const void *);
6380 static int htab_cu_eq (const void *, const void *);
6381 static void htab_cu_del (void *);
6382 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6383 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6384 static void add_sibling_attributes (dw_die_ref);
6385 static void build_abbrev_table (dw_die_ref);
6386 static void output_location_lists (dw_die_ref);
6387 static int constant_size (unsigned HOST_WIDE_INT);
6388 static unsigned long size_of_die (dw_die_ref);
6389 static void calc_die_sizes (dw_die_ref);
6390 static void mark_dies (dw_die_ref);
6391 static void unmark_dies (dw_die_ref);
6392 static void unmark_all_dies (dw_die_ref);
6393 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6394 static unsigned long size_of_aranges (void);
6395 static enum dwarf_form value_format (dw_attr_ref);
6396 static void output_value_format (dw_attr_ref);
6397 static void output_abbrev_section (void);
6398 static void output_die_symbol (dw_die_ref);
6399 static void output_die (dw_die_ref);
6400 static void output_compilation_unit_header (void);
6401 static void output_comp_unit (dw_die_ref, int);
6402 static void output_comdat_type_unit (comdat_type_node *);
6403 static const char *dwarf2_name (tree, int);
6404 static void add_pubname (tree, dw_die_ref);
6405 static void add_pubname_string (const char *, dw_die_ref);
6406 static void add_pubtype (tree, dw_die_ref);
6407 static void output_pubnames (VEC (pubname_entry,gc) *);
6408 static void output_aranges (unsigned long);
6409 static unsigned int add_ranges_num (int);
6410 static unsigned int add_ranges (const_tree);
6411 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6412                                   bool *);
6413 static void output_ranges (void);
6414 static void output_line_info (void);
6415 static void output_file_names (void);
6416 static dw_die_ref base_type_die (tree);
6417 static int is_base_type (tree);
6418 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6419 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6420 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6421 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6422 static int type_is_enum (const_tree);
6423 static unsigned int dbx_reg_number (const_rtx);
6424 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6425 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6426 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6427                                                 enum var_init_status);
6428 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6429                                                      enum var_init_status);
6430 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6431                                          enum var_init_status);
6432 static int is_based_loc (const_rtx);
6433 static int resolve_one_addr (rtx *, void *);
6434 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6435                                                enum var_init_status);
6436 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6437                                         enum var_init_status);
6438 static dw_loc_list_ref loc_list_from_tree (tree, int);
6439 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6440 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6441 static tree field_type (const_tree);
6442 static unsigned int simple_type_align_in_bits (const_tree);
6443 static unsigned int simple_decl_align_in_bits (const_tree);
6444 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6445 static HOST_WIDE_INT field_byte_offset (const_tree);
6446 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6447                                          dw_loc_list_ref);
6448 static void add_data_member_location_attribute (dw_die_ref, tree);
6449 static bool add_const_value_attribute (dw_die_ref, rtx);
6450 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6451 static void insert_double (double_int, unsigned char *);
6452 static void insert_float (const_rtx, unsigned char *);
6453 static rtx rtl_for_decl_location (tree);
6454 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6455                                                    enum dwarf_attribute);
6456 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6457 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6458 static void add_name_attribute (dw_die_ref, const char *);
6459 static void add_comp_dir_attribute (dw_die_ref);
6460 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6461 static void add_subscript_info (dw_die_ref, tree, bool);
6462 static void add_byte_size_attribute (dw_die_ref, tree);
6463 static void add_bit_offset_attribute (dw_die_ref, tree);
6464 static void add_bit_size_attribute (dw_die_ref, tree);
6465 static void add_prototyped_attribute (dw_die_ref, tree);
6466 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6467 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6468 static void add_src_coords_attributes (dw_die_ref, tree);
6469 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6470 static void push_decl_scope (tree);
6471 static void pop_decl_scope (void);
6472 static dw_die_ref scope_die_for (tree, dw_die_ref);
6473 static inline int local_scope_p (dw_die_ref);
6474 static inline int class_scope_p (dw_die_ref);
6475 static inline int class_or_namespace_scope_p (dw_die_ref);
6476 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6477 static void add_calling_convention_attribute (dw_die_ref, tree);
6478 static const char *type_tag (const_tree);
6479 static tree member_declared_type (const_tree);
6480 #if 0
6481 static const char *decl_start_label (tree);
6482 #endif
6483 static void gen_array_type_die (tree, dw_die_ref);
6484 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6485 #if 0
6486 static void gen_entry_point_die (tree, dw_die_ref);
6487 #endif
6488 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6489 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6490 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6491 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6492 static void gen_formal_types_die (tree, dw_die_ref);
6493 static void gen_subprogram_die (tree, dw_die_ref);
6494 static void gen_variable_die (tree, tree, dw_die_ref);
6495 static void gen_const_die (tree, dw_die_ref);
6496 static void gen_label_die (tree, dw_die_ref);
6497 static void gen_lexical_block_die (tree, dw_die_ref, int);
6498 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6499 static void gen_field_die (tree, dw_die_ref);
6500 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6501 static dw_die_ref gen_compile_unit_die (const char *);
6502 static void gen_inheritance_die (tree, tree, dw_die_ref);
6503 static void gen_member_die (tree, dw_die_ref);
6504 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6505                                                 enum debug_info_usage);
6506 static void gen_subroutine_type_die (tree, dw_die_ref);
6507 static void gen_typedef_die (tree, dw_die_ref);
6508 static void gen_type_die (tree, dw_die_ref);
6509 static void gen_block_die (tree, dw_die_ref, int);
6510 static void decls_for_scope (tree, dw_die_ref, int);
6511 static int is_redundant_typedef (const_tree);
6512 static bool is_naming_typedef_decl (const_tree);
6513 static inline dw_die_ref get_context_die (tree);
6514 static void gen_namespace_die (tree, dw_die_ref);
6515 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6516 static dw_die_ref force_decl_die (tree);
6517 static dw_die_ref force_type_die (tree);
6518 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6519 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6520 static struct dwarf_file_data * lookup_filename (const char *);
6521 static void retry_incomplete_types (void);
6522 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6523 static void gen_generic_params_dies (tree);
6524 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6525 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6526 static void splice_child_die (dw_die_ref, dw_die_ref);
6527 static int file_info_cmp (const void *, const void *);
6528 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6529                                      const char *, const char *);
6530 static void output_loc_list (dw_loc_list_ref);
6531 static char *gen_internal_sym (const char *);
6532
6533 static void prune_unmark_dies (dw_die_ref);
6534 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6535 static void prune_unused_types_mark (dw_die_ref, int);
6536 static void prune_unused_types_walk (dw_die_ref);
6537 static void prune_unused_types_walk_attribs (dw_die_ref);
6538 static void prune_unused_types_prune (dw_die_ref);
6539 static void prune_unused_types (void);
6540 static int maybe_emit_file (struct dwarf_file_data *fd);
6541 static inline const char *AT_vms_delta1 (dw_attr_ref);
6542 static inline const char *AT_vms_delta2 (dw_attr_ref);
6543 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6544                                      const char *, const char *);
6545 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6546 static void gen_remaining_tmpl_value_param_die_attribute (void);
6547 static bool generic_type_p (tree);
6548 static void schedule_generic_params_dies_gen (tree t);
6549 static void gen_scheduled_generic_parms_dies (void);
6550
6551 /* Section names used to hold DWARF debugging information.  */
6552 #ifndef DEBUG_INFO_SECTION
6553 #define DEBUG_INFO_SECTION      ".debug_info"
6554 #endif
6555 #ifndef DEBUG_ABBREV_SECTION
6556 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6557 #endif
6558 #ifndef DEBUG_ARANGES_SECTION
6559 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6560 #endif
6561 #ifndef DEBUG_MACINFO_SECTION
6562 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6563 #endif
6564 #ifndef DEBUG_LINE_SECTION
6565 #define DEBUG_LINE_SECTION      ".debug_line"
6566 #endif
6567 #ifndef DEBUG_LOC_SECTION
6568 #define DEBUG_LOC_SECTION       ".debug_loc"
6569 #endif
6570 #ifndef DEBUG_PUBNAMES_SECTION
6571 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6572 #endif
6573 #ifndef DEBUG_PUBTYPES_SECTION
6574 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6575 #endif
6576 #ifndef DEBUG_STR_SECTION
6577 #define DEBUG_STR_SECTION       ".debug_str"
6578 #endif
6579 #ifndef DEBUG_RANGES_SECTION
6580 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6581 #endif
6582
6583 /* Standard ELF section names for compiled code and data.  */
6584 #ifndef TEXT_SECTION_NAME
6585 #define TEXT_SECTION_NAME       ".text"
6586 #endif
6587
6588 /* Section flags for .debug_str section.  */
6589 #define DEBUG_STR_SECTION_FLAGS \
6590   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6591    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6592    : SECTION_DEBUG)
6593
6594 /* Labels we insert at beginning sections we can reference instead of
6595    the section names themselves.  */
6596
6597 #ifndef TEXT_SECTION_LABEL
6598 #define TEXT_SECTION_LABEL              "Ltext"
6599 #endif
6600 #ifndef COLD_TEXT_SECTION_LABEL
6601 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6602 #endif
6603 #ifndef DEBUG_LINE_SECTION_LABEL
6604 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6605 #endif
6606 #ifndef DEBUG_INFO_SECTION_LABEL
6607 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6608 #endif
6609 #ifndef DEBUG_ABBREV_SECTION_LABEL
6610 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6611 #endif
6612 #ifndef DEBUG_LOC_SECTION_LABEL
6613 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6614 #endif
6615 #ifndef DEBUG_RANGES_SECTION_LABEL
6616 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6617 #endif
6618 #ifndef DEBUG_MACINFO_SECTION_LABEL
6619 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6620 #endif
6621
6622
6623 /* Definitions of defaults for formats and names of various special
6624    (artificial) labels which may be generated within this file (when the -g
6625    options is used and DWARF2_DEBUGGING_INFO is in effect.
6626    If necessary, these may be overridden from within the tm.h file, but
6627    typically, overriding these defaults is unnecessary.  */
6628
6629 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6630 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6631 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6632 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6633 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6634 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6635 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6636 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6637 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6638 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6639
6640 #ifndef TEXT_END_LABEL
6641 #define TEXT_END_LABEL          "Letext"
6642 #endif
6643 #ifndef COLD_END_LABEL
6644 #define COLD_END_LABEL          "Letext_cold"
6645 #endif
6646 #ifndef BLOCK_BEGIN_LABEL
6647 #define BLOCK_BEGIN_LABEL       "LBB"
6648 #endif
6649 #ifndef BLOCK_END_LABEL
6650 #define BLOCK_END_LABEL         "LBE"
6651 #endif
6652 #ifndef LINE_CODE_LABEL
6653 #define LINE_CODE_LABEL         "LM"
6654 #endif
6655 #ifndef SEPARATE_LINE_CODE_LABEL
6656 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6657 #endif
6658
6659 \f
6660 /* Return the root of the DIE's built for the current compilation unit.  */
6661 static dw_die_ref
6662 comp_unit_die (void)
6663 {
6664   if (!single_comp_unit_die)
6665     single_comp_unit_die = gen_compile_unit_die (NULL);
6666   return single_comp_unit_die;
6667 }
6668
6669 /* We allow a language front-end to designate a function that is to be
6670    called to "demangle" any name before it is put into a DIE.  */
6671
6672 static const char *(*demangle_name_func) (const char *);
6673
6674 void
6675 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6676 {
6677   demangle_name_func = func;
6678 }
6679
6680 /* Test if rtl node points to a pseudo register.  */
6681
6682 static inline int
6683 is_pseudo_reg (const_rtx rtl)
6684 {
6685   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6686           || (GET_CODE (rtl) == SUBREG
6687               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6688 }
6689
6690 /* Return a reference to a type, with its const and volatile qualifiers
6691    removed.  */
6692
6693 static inline tree
6694 type_main_variant (tree type)
6695 {
6696   type = TYPE_MAIN_VARIANT (type);
6697
6698   /* ??? There really should be only one main variant among any group of
6699      variants of a given type (and all of the MAIN_VARIANT values for all
6700      members of the group should point to that one type) but sometimes the C
6701      front-end messes this up for array types, so we work around that bug
6702      here.  */
6703   if (TREE_CODE (type) == ARRAY_TYPE)
6704     while (type != TYPE_MAIN_VARIANT (type))
6705       type = TYPE_MAIN_VARIANT (type);
6706
6707   return type;
6708 }
6709
6710 /* Return nonzero if the given type node represents a tagged type.  */
6711
6712 static inline int
6713 is_tagged_type (const_tree type)
6714 {
6715   enum tree_code code = TREE_CODE (type);
6716
6717   return (code == RECORD_TYPE || code == UNION_TYPE
6718           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6719 }
6720
6721 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6722
6723 static void
6724 get_ref_die_offset_label (char *label, dw_die_ref ref)
6725 {
6726   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6727 }
6728
6729 /* Convert a DIE tag into its string name.  */
6730
6731 static const char *
6732 dwarf_tag_name (unsigned int tag)
6733 {
6734   switch (tag)
6735     {
6736     case DW_TAG_padding:
6737       return "DW_TAG_padding";
6738     case DW_TAG_array_type:
6739       return "DW_TAG_array_type";
6740     case DW_TAG_class_type:
6741       return "DW_TAG_class_type";
6742     case DW_TAG_entry_point:
6743       return "DW_TAG_entry_point";
6744     case DW_TAG_enumeration_type:
6745       return "DW_TAG_enumeration_type";
6746     case DW_TAG_formal_parameter:
6747       return "DW_TAG_formal_parameter";
6748     case DW_TAG_imported_declaration:
6749       return "DW_TAG_imported_declaration";
6750     case DW_TAG_label:
6751       return "DW_TAG_label";
6752     case DW_TAG_lexical_block:
6753       return "DW_TAG_lexical_block";
6754     case DW_TAG_member:
6755       return "DW_TAG_member";
6756     case DW_TAG_pointer_type:
6757       return "DW_TAG_pointer_type";
6758     case DW_TAG_reference_type:
6759       return "DW_TAG_reference_type";
6760     case DW_TAG_compile_unit:
6761       return "DW_TAG_compile_unit";
6762     case DW_TAG_string_type:
6763       return "DW_TAG_string_type";
6764     case DW_TAG_structure_type:
6765       return "DW_TAG_structure_type";
6766     case DW_TAG_subroutine_type:
6767       return "DW_TAG_subroutine_type";
6768     case DW_TAG_typedef:
6769       return "DW_TAG_typedef";
6770     case DW_TAG_union_type:
6771       return "DW_TAG_union_type";
6772     case DW_TAG_unspecified_parameters:
6773       return "DW_TAG_unspecified_parameters";
6774     case DW_TAG_variant:
6775       return "DW_TAG_variant";
6776     case DW_TAG_common_block:
6777       return "DW_TAG_common_block";
6778     case DW_TAG_common_inclusion:
6779       return "DW_TAG_common_inclusion";
6780     case DW_TAG_inheritance:
6781       return "DW_TAG_inheritance";
6782     case DW_TAG_inlined_subroutine:
6783       return "DW_TAG_inlined_subroutine";
6784     case DW_TAG_module:
6785       return "DW_TAG_module";
6786     case DW_TAG_ptr_to_member_type:
6787       return "DW_TAG_ptr_to_member_type";
6788     case DW_TAG_set_type:
6789       return "DW_TAG_set_type";
6790     case DW_TAG_subrange_type:
6791       return "DW_TAG_subrange_type";
6792     case DW_TAG_with_stmt:
6793       return "DW_TAG_with_stmt";
6794     case DW_TAG_access_declaration:
6795       return "DW_TAG_access_declaration";
6796     case DW_TAG_base_type:
6797       return "DW_TAG_base_type";
6798     case DW_TAG_catch_block:
6799       return "DW_TAG_catch_block";
6800     case DW_TAG_const_type:
6801       return "DW_TAG_const_type";
6802     case DW_TAG_constant:
6803       return "DW_TAG_constant";
6804     case DW_TAG_enumerator:
6805       return "DW_TAG_enumerator";
6806     case DW_TAG_file_type:
6807       return "DW_TAG_file_type";
6808     case DW_TAG_friend:
6809       return "DW_TAG_friend";
6810     case DW_TAG_namelist:
6811       return "DW_TAG_namelist";
6812     case DW_TAG_namelist_item:
6813       return "DW_TAG_namelist_item";
6814     case DW_TAG_packed_type:
6815       return "DW_TAG_packed_type";
6816     case DW_TAG_subprogram:
6817       return "DW_TAG_subprogram";
6818     case DW_TAG_template_type_param:
6819       return "DW_TAG_template_type_param";
6820     case DW_TAG_template_value_param:
6821       return "DW_TAG_template_value_param";
6822     case DW_TAG_thrown_type:
6823       return "DW_TAG_thrown_type";
6824     case DW_TAG_try_block:
6825       return "DW_TAG_try_block";
6826     case DW_TAG_variant_part:
6827       return "DW_TAG_variant_part";
6828     case DW_TAG_variable:
6829       return "DW_TAG_variable";
6830     case DW_TAG_volatile_type:
6831       return "DW_TAG_volatile_type";
6832     case DW_TAG_dwarf_procedure:
6833       return "DW_TAG_dwarf_procedure";
6834     case DW_TAG_restrict_type:
6835       return "DW_TAG_restrict_type";
6836     case DW_TAG_interface_type:
6837       return "DW_TAG_interface_type";
6838     case DW_TAG_namespace:
6839       return "DW_TAG_namespace";
6840     case DW_TAG_imported_module:
6841       return "DW_TAG_imported_module";
6842     case DW_TAG_unspecified_type:
6843       return "DW_TAG_unspecified_type";
6844     case DW_TAG_partial_unit:
6845       return "DW_TAG_partial_unit";
6846     case DW_TAG_imported_unit:
6847       return "DW_TAG_imported_unit";
6848     case DW_TAG_condition:
6849       return "DW_TAG_condition";
6850     case DW_TAG_shared_type:
6851       return "DW_TAG_shared_type";
6852     case DW_TAG_type_unit:
6853       return "DW_TAG_type_unit";
6854     case DW_TAG_rvalue_reference_type:
6855       return "DW_TAG_rvalue_reference_type";
6856     case DW_TAG_template_alias:
6857       return "DW_TAG_template_alias";
6858     case DW_TAG_GNU_template_parameter_pack:
6859       return "DW_TAG_GNU_template_parameter_pack";
6860     case DW_TAG_GNU_formal_parameter_pack:
6861       return "DW_TAG_GNU_formal_parameter_pack";
6862     case DW_TAG_MIPS_loop:
6863       return "DW_TAG_MIPS_loop";
6864     case DW_TAG_format_label:
6865       return "DW_TAG_format_label";
6866     case DW_TAG_function_template:
6867       return "DW_TAG_function_template";
6868     case DW_TAG_class_template:
6869       return "DW_TAG_class_template";
6870     case DW_TAG_GNU_BINCL:
6871       return "DW_TAG_GNU_BINCL";
6872     case DW_TAG_GNU_EINCL:
6873       return "DW_TAG_GNU_EINCL";
6874     case DW_TAG_GNU_template_template_param:
6875       return "DW_TAG_GNU_template_template_param";
6876     case DW_TAG_GNU_call_site:
6877       return "DW_TAG_GNU_call_site";
6878     case DW_TAG_GNU_call_site_parameter:
6879       return "DW_TAG_GNU_call_site_parameter";
6880     default:
6881       return "DW_TAG_<unknown>";
6882     }
6883 }
6884
6885 /* Convert a DWARF attribute code into its string name.  */
6886
6887 static const char *
6888 dwarf_attr_name (unsigned int attr)
6889 {
6890   switch (attr)
6891     {
6892     case DW_AT_sibling:
6893       return "DW_AT_sibling";
6894     case DW_AT_location:
6895       return "DW_AT_location";
6896     case DW_AT_name:
6897       return "DW_AT_name";
6898     case DW_AT_ordering:
6899       return "DW_AT_ordering";
6900     case DW_AT_subscr_data:
6901       return "DW_AT_subscr_data";
6902     case DW_AT_byte_size:
6903       return "DW_AT_byte_size";
6904     case DW_AT_bit_offset:
6905       return "DW_AT_bit_offset";
6906     case DW_AT_bit_size:
6907       return "DW_AT_bit_size";
6908     case DW_AT_element_list:
6909       return "DW_AT_element_list";
6910     case DW_AT_stmt_list:
6911       return "DW_AT_stmt_list";
6912     case DW_AT_low_pc:
6913       return "DW_AT_low_pc";
6914     case DW_AT_high_pc:
6915       return "DW_AT_high_pc";
6916     case DW_AT_language:
6917       return "DW_AT_language";
6918     case DW_AT_member:
6919       return "DW_AT_member";
6920     case DW_AT_discr:
6921       return "DW_AT_discr";
6922     case DW_AT_discr_value:
6923       return "DW_AT_discr_value";
6924     case DW_AT_visibility:
6925       return "DW_AT_visibility";
6926     case DW_AT_import:
6927       return "DW_AT_import";
6928     case DW_AT_string_length:
6929       return "DW_AT_string_length";
6930     case DW_AT_common_reference:
6931       return "DW_AT_common_reference";
6932     case DW_AT_comp_dir:
6933       return "DW_AT_comp_dir";
6934     case DW_AT_const_value:
6935       return "DW_AT_const_value";
6936     case DW_AT_containing_type:
6937       return "DW_AT_containing_type";
6938     case DW_AT_default_value:
6939       return "DW_AT_default_value";
6940     case DW_AT_inline:
6941       return "DW_AT_inline";
6942     case DW_AT_is_optional:
6943       return "DW_AT_is_optional";
6944     case DW_AT_lower_bound:
6945       return "DW_AT_lower_bound";
6946     case DW_AT_producer:
6947       return "DW_AT_producer";
6948     case DW_AT_prototyped:
6949       return "DW_AT_prototyped";
6950     case DW_AT_return_addr:
6951       return "DW_AT_return_addr";
6952     case DW_AT_start_scope:
6953       return "DW_AT_start_scope";
6954     case DW_AT_bit_stride:
6955       return "DW_AT_bit_stride";
6956     case DW_AT_upper_bound:
6957       return "DW_AT_upper_bound";
6958     case DW_AT_abstract_origin:
6959       return "DW_AT_abstract_origin";
6960     case DW_AT_accessibility:
6961       return "DW_AT_accessibility";
6962     case DW_AT_address_class:
6963       return "DW_AT_address_class";
6964     case DW_AT_artificial:
6965       return "DW_AT_artificial";
6966     case DW_AT_base_types:
6967       return "DW_AT_base_types";
6968     case DW_AT_calling_convention:
6969       return "DW_AT_calling_convention";
6970     case DW_AT_count:
6971       return "DW_AT_count";
6972     case DW_AT_data_member_location:
6973       return "DW_AT_data_member_location";
6974     case DW_AT_decl_column:
6975       return "DW_AT_decl_column";
6976     case DW_AT_decl_file:
6977       return "DW_AT_decl_file";
6978     case DW_AT_decl_line:
6979       return "DW_AT_decl_line";
6980     case DW_AT_declaration:
6981       return "DW_AT_declaration";
6982     case DW_AT_discr_list:
6983       return "DW_AT_discr_list";
6984     case DW_AT_encoding:
6985       return "DW_AT_encoding";
6986     case DW_AT_external:
6987       return "DW_AT_external";
6988     case DW_AT_explicit:
6989       return "DW_AT_explicit";
6990     case DW_AT_frame_base:
6991       return "DW_AT_frame_base";
6992     case DW_AT_friend:
6993       return "DW_AT_friend";
6994     case DW_AT_identifier_case:
6995       return "DW_AT_identifier_case";
6996     case DW_AT_macro_info:
6997       return "DW_AT_macro_info";
6998     case DW_AT_namelist_items:
6999       return "DW_AT_namelist_items";
7000     case DW_AT_priority:
7001       return "DW_AT_priority";
7002     case DW_AT_segment:
7003       return "DW_AT_segment";
7004     case DW_AT_specification:
7005       return "DW_AT_specification";
7006     case DW_AT_static_link:
7007       return "DW_AT_static_link";
7008     case DW_AT_type:
7009       return "DW_AT_type";
7010     case DW_AT_use_location:
7011       return "DW_AT_use_location";
7012     case DW_AT_variable_parameter:
7013       return "DW_AT_variable_parameter";
7014     case DW_AT_virtuality:
7015       return "DW_AT_virtuality";
7016     case DW_AT_vtable_elem_location:
7017       return "DW_AT_vtable_elem_location";
7018
7019     case DW_AT_allocated:
7020       return "DW_AT_allocated";
7021     case DW_AT_associated:
7022       return "DW_AT_associated";
7023     case DW_AT_data_location:
7024       return "DW_AT_data_location";
7025     case DW_AT_byte_stride:
7026       return "DW_AT_byte_stride";
7027     case DW_AT_entry_pc:
7028       return "DW_AT_entry_pc";
7029     case DW_AT_use_UTF8:
7030       return "DW_AT_use_UTF8";
7031     case DW_AT_extension:
7032       return "DW_AT_extension";
7033     case DW_AT_ranges:
7034       return "DW_AT_ranges";
7035     case DW_AT_trampoline:
7036       return "DW_AT_trampoline";
7037     case DW_AT_call_column:
7038       return "DW_AT_call_column";
7039     case DW_AT_call_file:
7040       return "DW_AT_call_file";
7041     case DW_AT_call_line:
7042       return "DW_AT_call_line";
7043     case DW_AT_object_pointer:
7044       return "DW_AT_object_pointer";
7045
7046     case DW_AT_signature:
7047       return "DW_AT_signature";
7048     case DW_AT_main_subprogram:
7049       return "DW_AT_main_subprogram";
7050     case DW_AT_data_bit_offset:
7051       return "DW_AT_data_bit_offset";
7052     case DW_AT_const_expr:
7053       return "DW_AT_const_expr";
7054     case DW_AT_enum_class:
7055       return "DW_AT_enum_class";
7056     case DW_AT_linkage_name:
7057       return "DW_AT_linkage_name";
7058
7059     case DW_AT_MIPS_fde:
7060       return "DW_AT_MIPS_fde";
7061     case DW_AT_MIPS_loop_begin:
7062       return "DW_AT_MIPS_loop_begin";
7063     case DW_AT_MIPS_tail_loop_begin:
7064       return "DW_AT_MIPS_tail_loop_begin";
7065     case DW_AT_MIPS_epilog_begin:
7066       return "DW_AT_MIPS_epilog_begin";
7067 #if VMS_DEBUGGING_INFO
7068     case DW_AT_HP_prologue:
7069       return "DW_AT_HP_prologue";
7070 #else
7071     case DW_AT_MIPS_loop_unroll_factor:
7072       return "DW_AT_MIPS_loop_unroll_factor";
7073 #endif
7074     case DW_AT_MIPS_software_pipeline_depth:
7075       return "DW_AT_MIPS_software_pipeline_depth";
7076     case DW_AT_MIPS_linkage_name:
7077       return "DW_AT_MIPS_linkage_name";
7078 #if VMS_DEBUGGING_INFO
7079     case DW_AT_HP_epilogue:
7080       return "DW_AT_HP_epilogue";
7081 #else
7082     case DW_AT_MIPS_stride:
7083       return "DW_AT_MIPS_stride";
7084 #endif
7085     case DW_AT_MIPS_abstract_name:
7086       return "DW_AT_MIPS_abstract_name";
7087     case DW_AT_MIPS_clone_origin:
7088       return "DW_AT_MIPS_clone_origin";
7089     case DW_AT_MIPS_has_inlines:
7090       return "DW_AT_MIPS_has_inlines";
7091
7092     case DW_AT_sf_names:
7093       return "DW_AT_sf_names";
7094     case DW_AT_src_info:
7095       return "DW_AT_src_info";
7096     case DW_AT_mac_info:
7097       return "DW_AT_mac_info";
7098     case DW_AT_src_coords:
7099       return "DW_AT_src_coords";
7100     case DW_AT_body_begin:
7101       return "DW_AT_body_begin";
7102     case DW_AT_body_end:
7103       return "DW_AT_body_end";
7104     case DW_AT_GNU_vector:
7105       return "DW_AT_GNU_vector";
7106     case DW_AT_GNU_guarded_by:
7107       return "DW_AT_GNU_guarded_by";
7108     case DW_AT_GNU_pt_guarded_by:
7109       return "DW_AT_GNU_pt_guarded_by";
7110     case DW_AT_GNU_guarded:
7111       return "DW_AT_GNU_guarded";
7112     case DW_AT_GNU_pt_guarded:
7113       return "DW_AT_GNU_pt_guarded";
7114     case DW_AT_GNU_locks_excluded:
7115       return "DW_AT_GNU_locks_excluded";
7116     case DW_AT_GNU_exclusive_locks_required:
7117       return "DW_AT_GNU_exclusive_locks_required";
7118     case DW_AT_GNU_shared_locks_required:
7119       return "DW_AT_GNU_shared_locks_required";
7120     case DW_AT_GNU_odr_signature:
7121       return "DW_AT_GNU_odr_signature";
7122     case DW_AT_GNU_template_name:
7123       return "DW_AT_GNU_template_name";
7124     case DW_AT_GNU_call_site_value:
7125       return "DW_AT_GNU_call_site_value";
7126     case DW_AT_GNU_call_site_data_value:
7127       return "DW_AT_GNU_call_site_data_value";
7128     case DW_AT_GNU_call_site_target:
7129       return "DW_AT_GNU_call_site_target";
7130     case DW_AT_GNU_call_site_target_clobbered:
7131       return "DW_AT_GNU_call_site_target_clobbered";
7132     case DW_AT_GNU_tail_call:
7133       return "DW_AT_GNU_tail_call";
7134     case DW_AT_GNU_all_tail_call_sites:
7135       return "DW_AT_GNU_all_tail_call_sites";
7136     case DW_AT_GNU_all_call_sites:
7137       return "DW_AT_GNU_all_call_sites";
7138     case DW_AT_GNU_all_source_call_sites:
7139       return "DW_AT_GNU_all_source_call_sites";
7140
7141     case DW_AT_VMS_rtnbeg_pd_address:
7142       return "DW_AT_VMS_rtnbeg_pd_address";
7143
7144     default:
7145       return "DW_AT_<unknown>";
7146     }
7147 }
7148
7149 /* Convert a DWARF value form code into its string name.  */
7150
7151 static const char *
7152 dwarf_form_name (unsigned int form)
7153 {
7154   switch (form)
7155     {
7156     case DW_FORM_addr:
7157       return "DW_FORM_addr";
7158     case DW_FORM_block2:
7159       return "DW_FORM_block2";
7160     case DW_FORM_block4:
7161       return "DW_FORM_block4";
7162     case DW_FORM_data2:
7163       return "DW_FORM_data2";
7164     case DW_FORM_data4:
7165       return "DW_FORM_data4";
7166     case DW_FORM_data8:
7167       return "DW_FORM_data8";
7168     case DW_FORM_string:
7169       return "DW_FORM_string";
7170     case DW_FORM_block:
7171       return "DW_FORM_block";
7172     case DW_FORM_block1:
7173       return "DW_FORM_block1";
7174     case DW_FORM_data1:
7175       return "DW_FORM_data1";
7176     case DW_FORM_flag:
7177       return "DW_FORM_flag";
7178     case DW_FORM_sdata:
7179       return "DW_FORM_sdata";
7180     case DW_FORM_strp:
7181       return "DW_FORM_strp";
7182     case DW_FORM_udata:
7183       return "DW_FORM_udata";
7184     case DW_FORM_ref_addr:
7185       return "DW_FORM_ref_addr";
7186     case DW_FORM_ref1:
7187       return "DW_FORM_ref1";
7188     case DW_FORM_ref2:
7189       return "DW_FORM_ref2";
7190     case DW_FORM_ref4:
7191       return "DW_FORM_ref4";
7192     case DW_FORM_ref8:
7193       return "DW_FORM_ref8";
7194     case DW_FORM_ref_udata:
7195       return "DW_FORM_ref_udata";
7196     case DW_FORM_indirect:
7197       return "DW_FORM_indirect";
7198     case DW_FORM_sec_offset:
7199       return "DW_FORM_sec_offset";
7200     case DW_FORM_exprloc:
7201       return "DW_FORM_exprloc";
7202     case DW_FORM_flag_present:
7203       return "DW_FORM_flag_present";
7204     case DW_FORM_ref_sig8:
7205       return "DW_FORM_ref_sig8";
7206     default:
7207       return "DW_FORM_<unknown>";
7208     }
7209 }
7210 \f
7211 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7212    instance of an inlined instance of a decl which is local to an inline
7213    function, so we have to trace all of the way back through the origin chain
7214    to find out what sort of node actually served as the original seed for the
7215    given block.  */
7216
7217 static tree
7218 decl_ultimate_origin (const_tree decl)
7219 {
7220   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7221     return NULL_TREE;
7222
7223   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7224      nodes in the function to point to themselves; ignore that if
7225      we're trying to output the abstract instance of this function.  */
7226   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7227     return NULL_TREE;
7228
7229   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7230      most distant ancestor, this should never happen.  */
7231   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7232
7233   return DECL_ABSTRACT_ORIGIN (decl);
7234 }
7235
7236 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7237    of a virtual function may refer to a base class, so we check the 'this'
7238    parameter.  */
7239
7240 static tree
7241 decl_class_context (tree decl)
7242 {
7243   tree context = NULL_TREE;
7244
7245   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7246     context = DECL_CONTEXT (decl);
7247   else
7248     context = TYPE_MAIN_VARIANT
7249       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7250
7251   if (context && !TYPE_P (context))
7252     context = NULL_TREE;
7253
7254   return context;
7255 }
7256 \f
7257 /* Add an attribute/value pair to a DIE.  */
7258
7259 static inline void
7260 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7261 {
7262   /* Maybe this should be an assert?  */
7263   if (die == NULL)
7264     return;
7265
7266   if (die->die_attr == NULL)
7267     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7268   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7269 }
7270
7271 static inline enum dw_val_class
7272 AT_class (dw_attr_ref a)
7273 {
7274   return a->dw_attr_val.val_class;
7275 }
7276
7277 /* Add a flag value attribute to a DIE.  */
7278
7279 static inline void
7280 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7281 {
7282   dw_attr_node attr;
7283
7284   attr.dw_attr = attr_kind;
7285   attr.dw_attr_val.val_class = dw_val_class_flag;
7286   attr.dw_attr_val.v.val_flag = flag;
7287   add_dwarf_attr (die, &attr);
7288 }
7289
7290 static inline unsigned
7291 AT_flag (dw_attr_ref a)
7292 {
7293   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7294   return a->dw_attr_val.v.val_flag;
7295 }
7296
7297 /* Add a signed integer attribute value to a DIE.  */
7298
7299 static inline void
7300 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7301 {
7302   dw_attr_node attr;
7303
7304   attr.dw_attr = attr_kind;
7305   attr.dw_attr_val.val_class = dw_val_class_const;
7306   attr.dw_attr_val.v.val_int = int_val;
7307   add_dwarf_attr (die, &attr);
7308 }
7309
7310 static inline HOST_WIDE_INT
7311 AT_int (dw_attr_ref a)
7312 {
7313   gcc_assert (a && AT_class (a) == dw_val_class_const);
7314   return a->dw_attr_val.v.val_int;
7315 }
7316
7317 /* Add an unsigned integer attribute value to a DIE.  */
7318
7319 static inline void
7320 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7321                  unsigned HOST_WIDE_INT unsigned_val)
7322 {
7323   dw_attr_node attr;
7324
7325   attr.dw_attr = attr_kind;
7326   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7327   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7328   add_dwarf_attr (die, &attr);
7329 }
7330
7331 static inline unsigned HOST_WIDE_INT
7332 AT_unsigned (dw_attr_ref a)
7333 {
7334   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7335   return a->dw_attr_val.v.val_unsigned;
7336 }
7337
7338 /* Add an unsigned double integer attribute value to a DIE.  */
7339
7340 static inline void
7341 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7342                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7343 {
7344   dw_attr_node attr;
7345
7346   attr.dw_attr = attr_kind;
7347   attr.dw_attr_val.val_class = dw_val_class_const_double;
7348   attr.dw_attr_val.v.val_double.high = high;
7349   attr.dw_attr_val.v.val_double.low = low;
7350   add_dwarf_attr (die, &attr);
7351 }
7352
7353 /* Add a floating point attribute value to a DIE and return it.  */
7354
7355 static inline void
7356 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7357             unsigned int length, unsigned int elt_size, unsigned char *array)
7358 {
7359   dw_attr_node attr;
7360
7361   attr.dw_attr = attr_kind;
7362   attr.dw_attr_val.val_class = dw_val_class_vec;
7363   attr.dw_attr_val.v.val_vec.length = length;
7364   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7365   attr.dw_attr_val.v.val_vec.array = array;
7366   add_dwarf_attr (die, &attr);
7367 }
7368
7369 /* Add an 8-byte data attribute value to a DIE.  */
7370
7371 static inline void
7372 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7373               unsigned char data8[8])
7374 {
7375   dw_attr_node attr;
7376
7377   attr.dw_attr = attr_kind;
7378   attr.dw_attr_val.val_class = dw_val_class_data8;
7379   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7380   add_dwarf_attr (die, &attr);
7381 }
7382
7383 /* Hash and equality functions for debug_str_hash.  */
7384
7385 static hashval_t
7386 debug_str_do_hash (const void *x)
7387 {
7388   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7389 }
7390
7391 static int
7392 debug_str_eq (const void *x1, const void *x2)
7393 {
7394   return strcmp ((((const struct indirect_string_node *)x1)->str),
7395                  (const char *)x2) == 0;
7396 }
7397
7398 /* Add STR to the indirect string hash table.  */
7399
7400 static struct indirect_string_node *
7401 find_AT_string (const char *str)
7402 {
7403   struct indirect_string_node *node;
7404   void **slot;
7405
7406   if (! debug_str_hash)
7407     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7408                                       debug_str_eq, NULL);
7409
7410   slot = htab_find_slot_with_hash (debug_str_hash, str,
7411                                    htab_hash_string (str), INSERT);
7412   if (*slot == NULL)
7413     {
7414       node = ggc_alloc_cleared_indirect_string_node ();
7415       node->str = ggc_strdup (str);
7416       *slot = node;
7417     }
7418   else
7419     node = (struct indirect_string_node *) *slot;
7420
7421   node->refcount++;
7422   return node;
7423 }
7424
7425 /* Add a string attribute value to a DIE.  */
7426
7427 static inline void
7428 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7429 {
7430   dw_attr_node attr;
7431   struct indirect_string_node *node;
7432
7433   node = find_AT_string (str);
7434
7435   attr.dw_attr = attr_kind;
7436   attr.dw_attr_val.val_class = dw_val_class_str;
7437   attr.dw_attr_val.v.val_str = node;
7438   add_dwarf_attr (die, &attr);
7439 }
7440
7441 /* Create a label for an indirect string node, ensuring it is going to
7442    be output, unless its reference count goes down to zero.  */
7443
7444 static inline void
7445 gen_label_for_indirect_string (struct indirect_string_node *node)
7446 {
7447   char label[32];
7448
7449   if (node->label)
7450     return;
7451
7452   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7453   ++dw2_string_counter;
7454   node->label = xstrdup (label);
7455 }
7456
7457 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7458    debug string STR.  */
7459
7460 static inline rtx
7461 get_debug_string_label (const char *str)
7462 {
7463   struct indirect_string_node *node = find_AT_string (str);
7464
7465   debug_str_hash_forced = true;
7466
7467   gen_label_for_indirect_string (node);
7468
7469   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7470 }
7471
7472 static inline const char *
7473 AT_string (dw_attr_ref a)
7474 {
7475   gcc_assert (a && AT_class (a) == dw_val_class_str);
7476   return a->dw_attr_val.v.val_str->str;
7477 }
7478
7479 /* Find out whether a string should be output inline in DIE
7480    or out-of-line in .debug_str section.  */
7481
7482 static enum dwarf_form
7483 AT_string_form (dw_attr_ref a)
7484 {
7485   struct indirect_string_node *node;
7486   unsigned int len;
7487
7488   gcc_assert (a && AT_class (a) == dw_val_class_str);
7489
7490   node = a->dw_attr_val.v.val_str;
7491   if (node->form)
7492     return node->form;
7493
7494   len = strlen (node->str) + 1;
7495
7496   /* If the string is shorter or equal to the size of the reference, it is
7497      always better to put it inline.  */
7498   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7499     return node->form = DW_FORM_string;
7500
7501   /* If we cannot expect the linker to merge strings in .debug_str
7502      section, only put it into .debug_str if it is worth even in this
7503      single module.  */
7504   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7505       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7506       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7507     return node->form = DW_FORM_string;
7508
7509   gen_label_for_indirect_string (node);
7510
7511   return node->form = DW_FORM_strp;
7512 }
7513
7514 /* Add a DIE reference attribute value to a DIE.  */
7515
7516 static inline void
7517 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7518 {
7519   dw_attr_node attr;
7520
7521 #ifdef ENABLE_CHECKING
7522   gcc_assert (targ_die != NULL);
7523 #else
7524   /* With LTO we can end up trying to reference something we didn't create
7525      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7526   if (targ_die == NULL)
7527     return;
7528 #endif
7529
7530   attr.dw_attr = attr_kind;
7531   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7532   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7533   attr.dw_attr_val.v.val_die_ref.external = 0;
7534   add_dwarf_attr (die, &attr);
7535 }
7536
7537 /* Add an AT_specification attribute to a DIE, and also make the back
7538    pointer from the specification to the definition.  */
7539
7540 static inline void
7541 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7542 {
7543   add_AT_die_ref (die, DW_AT_specification, targ_die);
7544   gcc_assert (!targ_die->die_definition);
7545   targ_die->die_definition = die;
7546 }
7547
7548 static inline dw_die_ref
7549 AT_ref (dw_attr_ref a)
7550 {
7551   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7552   return a->dw_attr_val.v.val_die_ref.die;
7553 }
7554
7555 static inline int
7556 AT_ref_external (dw_attr_ref a)
7557 {
7558   if (a && AT_class (a) == dw_val_class_die_ref)
7559     return a->dw_attr_val.v.val_die_ref.external;
7560
7561   return 0;
7562 }
7563
7564 static inline void
7565 set_AT_ref_external (dw_attr_ref a, int i)
7566 {
7567   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7568   a->dw_attr_val.v.val_die_ref.external = i;
7569 }
7570
7571 /* Add an FDE reference attribute value to a DIE.  */
7572
7573 static inline void
7574 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7575 {
7576   dw_attr_node attr;
7577
7578   attr.dw_attr = attr_kind;
7579   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7580   attr.dw_attr_val.v.val_fde_index = targ_fde;
7581   add_dwarf_attr (die, &attr);
7582 }
7583
7584 /* Add a location description attribute value to a DIE.  */
7585
7586 static inline void
7587 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7588 {
7589   dw_attr_node attr;
7590
7591   attr.dw_attr = attr_kind;
7592   attr.dw_attr_val.val_class = dw_val_class_loc;
7593   attr.dw_attr_val.v.val_loc = loc;
7594   add_dwarf_attr (die, &attr);
7595 }
7596
7597 static inline dw_loc_descr_ref
7598 AT_loc (dw_attr_ref a)
7599 {
7600   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7601   return a->dw_attr_val.v.val_loc;
7602 }
7603
7604 static inline void
7605 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7606 {
7607   dw_attr_node attr;
7608
7609   attr.dw_attr = attr_kind;
7610   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7611   attr.dw_attr_val.v.val_loc_list = loc_list;
7612   add_dwarf_attr (die, &attr);
7613   have_location_lists = true;
7614 }
7615
7616 static inline dw_loc_list_ref
7617 AT_loc_list (dw_attr_ref a)
7618 {
7619   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7620   return a->dw_attr_val.v.val_loc_list;
7621 }
7622
7623 static inline dw_loc_list_ref *
7624 AT_loc_list_ptr (dw_attr_ref a)
7625 {
7626   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7627   return &a->dw_attr_val.v.val_loc_list;
7628 }
7629
7630 /* Add an address constant attribute value to a DIE.  */
7631
7632 static inline void
7633 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7634 {
7635   dw_attr_node attr;
7636
7637   attr.dw_attr = attr_kind;
7638   attr.dw_attr_val.val_class = dw_val_class_addr;
7639   attr.dw_attr_val.v.val_addr = addr;
7640   add_dwarf_attr (die, &attr);
7641 }
7642
7643 /* Get the RTX from to an address DIE attribute.  */
7644
7645 static inline rtx
7646 AT_addr (dw_attr_ref a)
7647 {
7648   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7649   return a->dw_attr_val.v.val_addr;
7650 }
7651
7652 /* Add a file attribute value to a DIE.  */
7653
7654 static inline void
7655 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7656              struct dwarf_file_data *fd)
7657 {
7658   dw_attr_node attr;
7659
7660   attr.dw_attr = attr_kind;
7661   attr.dw_attr_val.val_class = dw_val_class_file;
7662   attr.dw_attr_val.v.val_file = fd;
7663   add_dwarf_attr (die, &attr);
7664 }
7665
7666 /* Get the dwarf_file_data from a file DIE attribute.  */
7667
7668 static inline struct dwarf_file_data *
7669 AT_file (dw_attr_ref a)
7670 {
7671   gcc_assert (a && AT_class (a) == dw_val_class_file);
7672   return a->dw_attr_val.v.val_file;
7673 }
7674
7675 /* Add a vms delta attribute value to a DIE.  */
7676
7677 static inline void
7678 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7679                   const char *lbl1, const char *lbl2)
7680 {
7681   dw_attr_node attr;
7682
7683   attr.dw_attr = attr_kind;
7684   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7685   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7686   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7687   add_dwarf_attr (die, &attr);
7688 }
7689
7690 /* Add a label identifier attribute value to a DIE.  */
7691
7692 static inline void
7693 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7694 {
7695   dw_attr_node attr;
7696
7697   attr.dw_attr = attr_kind;
7698   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7699   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7700   add_dwarf_attr (die, &attr);
7701 }
7702
7703 /* Add a section offset attribute value to a DIE, an offset into the
7704    debug_line section.  */
7705
7706 static inline void
7707 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7708                 const char *label)
7709 {
7710   dw_attr_node attr;
7711
7712   attr.dw_attr = attr_kind;
7713   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7714   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7715   add_dwarf_attr (die, &attr);
7716 }
7717
7718 /* Add a section offset attribute value to a DIE, an offset into the
7719    debug_macinfo section.  */
7720
7721 static inline void
7722 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7723                const char *label)
7724 {
7725   dw_attr_node attr;
7726
7727   attr.dw_attr = attr_kind;
7728   attr.dw_attr_val.val_class = dw_val_class_macptr;
7729   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7730   add_dwarf_attr (die, &attr);
7731 }
7732
7733 /* Add an offset attribute value to a DIE.  */
7734
7735 static inline void
7736 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7737                unsigned HOST_WIDE_INT offset)
7738 {
7739   dw_attr_node attr;
7740
7741   attr.dw_attr = attr_kind;
7742   attr.dw_attr_val.val_class = dw_val_class_offset;
7743   attr.dw_attr_val.v.val_offset = offset;
7744   add_dwarf_attr (die, &attr);
7745 }
7746
7747 /* Add an range_list attribute value to a DIE.  */
7748
7749 static void
7750 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7751                    long unsigned int offset)
7752 {
7753   dw_attr_node attr;
7754
7755   attr.dw_attr = attr_kind;
7756   attr.dw_attr_val.val_class = dw_val_class_range_list;
7757   attr.dw_attr_val.v.val_offset = offset;
7758   add_dwarf_attr (die, &attr);
7759 }
7760
7761 /* Return the start label of a delta attribute.  */
7762
7763 static inline const char *
7764 AT_vms_delta1 (dw_attr_ref a)
7765 {
7766   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7767   return a->dw_attr_val.v.val_vms_delta.lbl1;
7768 }
7769
7770 /* Return the end label of a delta attribute.  */
7771
7772 static inline const char *
7773 AT_vms_delta2 (dw_attr_ref a)
7774 {
7775   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7776   return a->dw_attr_val.v.val_vms_delta.lbl2;
7777 }
7778
7779 static inline const char *
7780 AT_lbl (dw_attr_ref a)
7781 {
7782   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7783                     || AT_class (a) == dw_val_class_lineptr
7784                     || AT_class (a) == dw_val_class_macptr));
7785   return a->dw_attr_val.v.val_lbl_id;
7786 }
7787
7788 /* Get the attribute of type attr_kind.  */
7789
7790 static dw_attr_ref
7791 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7792 {
7793   dw_attr_ref a;
7794   unsigned ix;
7795   dw_die_ref spec = NULL;
7796
7797   if (! die)
7798     return NULL;
7799
7800   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7801     if (a->dw_attr == attr_kind)
7802       return a;
7803     else if (a->dw_attr == DW_AT_specification
7804              || a->dw_attr == DW_AT_abstract_origin)
7805       spec = AT_ref (a);
7806
7807   if (spec)
7808     return get_AT (spec, attr_kind);
7809
7810   return NULL;
7811 }
7812
7813 /* Return the "low pc" attribute value, typically associated with a subprogram
7814    DIE.  Return null if the "low pc" attribute is either not present, or if it
7815    cannot be represented as an assembler label identifier.  */
7816
7817 static inline const char *
7818 get_AT_low_pc (dw_die_ref die)
7819 {
7820   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7821
7822   return a ? AT_lbl (a) : NULL;
7823 }
7824
7825 /* Return the "high pc" attribute value, typically associated with a subprogram
7826    DIE.  Return null if the "high pc" attribute is either not present, or if it
7827    cannot be represented as an assembler label identifier.  */
7828
7829 static inline const char *
7830 get_AT_hi_pc (dw_die_ref die)
7831 {
7832   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7833
7834   return a ? AT_lbl (a) : NULL;
7835 }
7836
7837 /* Return the value of the string attribute designated by ATTR_KIND, or
7838    NULL if it is not present.  */
7839
7840 static inline const char *
7841 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7842 {
7843   dw_attr_ref a = get_AT (die, attr_kind);
7844
7845   return a ? AT_string (a) : NULL;
7846 }
7847
7848 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7849    if it is not present.  */
7850
7851 static inline int
7852 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7853 {
7854   dw_attr_ref a = get_AT (die, attr_kind);
7855
7856   return a ? AT_flag (a) : 0;
7857 }
7858
7859 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7860    if it is not present.  */
7861
7862 static inline unsigned
7863 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7864 {
7865   dw_attr_ref a = get_AT (die, attr_kind);
7866
7867   return a ? AT_unsigned (a) : 0;
7868 }
7869
7870 static inline dw_die_ref
7871 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7872 {
7873   dw_attr_ref a = get_AT (die, attr_kind);
7874
7875   return a ? AT_ref (a) : NULL;
7876 }
7877
7878 static inline struct dwarf_file_data *
7879 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7880 {
7881   dw_attr_ref a = get_AT (die, attr_kind);
7882
7883   return a ? AT_file (a) : NULL;
7884 }
7885
7886 /* Return TRUE if the language is C++.  */
7887
7888 static inline bool
7889 is_cxx (void)
7890 {
7891   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7892
7893   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7894 }
7895
7896 /* Return TRUE if the language is Fortran.  */
7897
7898 static inline bool
7899 is_fortran (void)
7900 {
7901   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7902
7903   return (lang == DW_LANG_Fortran77
7904           || lang == DW_LANG_Fortran90
7905           || lang == DW_LANG_Fortran95);
7906 }
7907
7908 /* Return TRUE if the language is Ada.  */
7909
7910 static inline bool
7911 is_ada (void)
7912 {
7913   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7914
7915   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7916 }
7917
7918 /* Remove the specified attribute if present.  */
7919
7920 static void
7921 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7922 {
7923   dw_attr_ref a;
7924   unsigned ix;
7925
7926   if (! die)
7927     return;
7928
7929   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7930     if (a->dw_attr == attr_kind)
7931       {
7932         if (AT_class (a) == dw_val_class_str)
7933           if (a->dw_attr_val.v.val_str->refcount)
7934             a->dw_attr_val.v.val_str->refcount--;
7935
7936         /* VEC_ordered_remove should help reduce the number of abbrevs
7937            that are needed.  */
7938         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7939         return;
7940       }
7941 }
7942
7943 /* Remove CHILD from its parent.  PREV must have the property that
7944    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7945
7946 static void
7947 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7948 {
7949   gcc_assert (child->die_parent == prev->die_parent);
7950   gcc_assert (prev->die_sib == child);
7951   if (prev == child)
7952     {
7953       gcc_assert (child->die_parent->die_child == child);
7954       prev = NULL;
7955     }
7956   else
7957     prev->die_sib = child->die_sib;
7958   if (child->die_parent->die_child == child)
7959     child->die_parent->die_child = prev;
7960 }
7961
7962 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7963    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7964
7965 static void
7966 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7967 {
7968   dw_die_ref parent = old_child->die_parent;
7969
7970   gcc_assert (parent == prev->die_parent);
7971   gcc_assert (prev->die_sib == old_child);
7972
7973   new_child->die_parent = parent;
7974   if (prev == old_child)
7975     {
7976       gcc_assert (parent->die_child == old_child);
7977       new_child->die_sib = new_child;
7978     }
7979   else
7980     {
7981       prev->die_sib = new_child;
7982       new_child->die_sib = old_child->die_sib;
7983     }
7984   if (old_child->die_parent->die_child == old_child)
7985     old_child->die_parent->die_child = new_child;
7986 }
7987
7988 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7989
7990 static void
7991 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7992 {
7993   dw_die_ref c;
7994   new_parent->die_child = old_parent->die_child;
7995   old_parent->die_child = NULL;
7996   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7997 }
7998
7999 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8000    matches TAG.  */
8001
8002 static void
8003 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8004 {
8005   dw_die_ref c;
8006
8007   c = die->die_child;
8008   if (c) do {
8009     dw_die_ref prev = c;
8010     c = c->die_sib;
8011     while (c->die_tag == tag)
8012       {
8013         remove_child_with_prev (c, prev);
8014         /* Might have removed every child.  */
8015         if (c == c->die_sib)
8016           return;
8017         c = c->die_sib;
8018       }
8019   } while (c != die->die_child);
8020 }
8021
8022 /* Add a CHILD_DIE as the last child of DIE.  */
8023
8024 static void
8025 add_child_die (dw_die_ref die, dw_die_ref child_die)
8026 {
8027   /* FIXME this should probably be an assert.  */
8028   if (! die || ! child_die)
8029     return;
8030   gcc_assert (die != child_die);
8031
8032   child_die->die_parent = die;
8033   if (die->die_child)
8034     {
8035       child_die->die_sib = die->die_child->die_sib;
8036       die->die_child->die_sib = child_die;
8037     }
8038   else
8039     child_die->die_sib = child_die;
8040   die->die_child = child_die;
8041 }
8042
8043 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8044    is the specification, to the end of PARENT's list of children.
8045    This is done by removing and re-adding it.  */
8046
8047 static void
8048 splice_child_die (dw_die_ref parent, dw_die_ref child)
8049 {
8050   dw_die_ref p;
8051
8052   /* We want the declaration DIE from inside the class, not the
8053      specification DIE at toplevel.  */
8054   if (child->die_parent != parent)
8055     {
8056       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8057
8058       if (tmp)
8059         child = tmp;
8060     }
8061
8062   gcc_assert (child->die_parent == parent
8063               || (child->die_parent
8064                   == get_AT_ref (parent, DW_AT_specification)));
8065
8066   for (p = child->die_parent->die_child; ; p = p->die_sib)
8067     if (p->die_sib == child)
8068       {
8069         remove_child_with_prev (child, p);
8070         break;
8071       }
8072
8073   add_child_die (parent, child);
8074 }
8075
8076 /* Return a pointer to a newly created DIE node.  */
8077
8078 static inline dw_die_ref
8079 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8080 {
8081   dw_die_ref die = ggc_alloc_cleared_die_node ();
8082
8083   die->die_tag = tag_value;
8084
8085   if (parent_die != NULL)
8086     add_child_die (parent_die, die);
8087   else
8088     {
8089       limbo_die_node *limbo_node;
8090
8091       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8092       limbo_node->die = die;
8093       limbo_node->created_for = t;
8094       limbo_node->next = limbo_die_list;
8095       limbo_die_list = limbo_node;
8096     }
8097
8098   return die;
8099 }
8100
8101 /* Return the DIE associated with the given type specifier.  */
8102
8103 static inline dw_die_ref
8104 lookup_type_die (tree type)
8105 {
8106   return TYPE_SYMTAB_DIE (type);
8107 }
8108
8109 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8110    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8111    anonymous type instead the one of the naming typedef.  */
8112
8113 static inline dw_die_ref
8114 strip_naming_typedef (tree type, dw_die_ref type_die)
8115 {
8116   if (type
8117       && TREE_CODE (type) == RECORD_TYPE
8118       && type_die
8119       && type_die->die_tag == DW_TAG_typedef
8120       && is_naming_typedef_decl (TYPE_NAME (type)))
8121     type_die = get_AT_ref (type_die, DW_AT_type);
8122   return type_die;
8123 }
8124
8125 /* Like lookup_type_die, but if type is an anonymous type named by a
8126    typedef[1], return the DIE of the anonymous type instead the one of
8127    the naming typedef.  This is because in gen_typedef_die, we did
8128    equate the anonymous struct named by the typedef with the DIE of
8129    the naming typedef. So by default, lookup_type_die on an anonymous
8130    struct yields the DIE of the naming typedef.
8131
8132    [1]: Read the comment of is_naming_typedef_decl to learn about what
8133    a naming typedef is.  */
8134
8135 static inline dw_die_ref
8136 lookup_type_die_strip_naming_typedef (tree type)
8137 {
8138   dw_die_ref die = lookup_type_die (type);
8139   return strip_naming_typedef (type, die);
8140 }
8141
8142 /* Equate a DIE to a given type specifier.  */
8143
8144 static inline void
8145 equate_type_number_to_die (tree type, dw_die_ref type_die)
8146 {
8147   TYPE_SYMTAB_DIE (type) = type_die;
8148 }
8149
8150 /* Returns a hash value for X (which really is a die_struct).  */
8151
8152 static hashval_t
8153 decl_die_table_hash (const void *x)
8154 {
8155   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8156 }
8157
8158 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8159
8160 static int
8161 decl_die_table_eq (const void *x, const void *y)
8162 {
8163   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8164 }
8165
8166 /* Return the DIE associated with a given declaration.  */
8167
8168 static inline dw_die_ref
8169 lookup_decl_die (tree decl)
8170 {
8171   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8172 }
8173
8174 /* Returns a hash value for X (which really is a var_loc_list).  */
8175
8176 static hashval_t
8177 decl_loc_table_hash (const void *x)
8178 {
8179   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8180 }
8181
8182 /* Return nonzero if decl_id of var_loc_list X is the same as
8183    UID of decl *Y.  */
8184
8185 static int
8186 decl_loc_table_eq (const void *x, const void *y)
8187 {
8188   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8189 }
8190
8191 /* Return the var_loc list associated with a given declaration.  */
8192
8193 static inline var_loc_list *
8194 lookup_decl_loc (const_tree decl)
8195 {
8196   if (!decl_loc_table)
8197     return NULL;
8198   return (var_loc_list *)
8199     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8200 }
8201
8202 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8203
8204 static hashval_t
8205 cached_dw_loc_list_table_hash (const void *x)
8206 {
8207   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8208 }
8209
8210 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8211    UID of decl *Y.  */
8212
8213 static int
8214 cached_dw_loc_list_table_eq (const void *x, const void *y)
8215 {
8216   return (((const cached_dw_loc_list *) x)->decl_id
8217           == DECL_UID ((const_tree) y));
8218 }
8219
8220 /* Equate a DIE to a particular declaration.  */
8221
8222 static void
8223 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8224 {
8225   unsigned int decl_id = DECL_UID (decl);
8226   void **slot;
8227
8228   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8229   *slot = decl_die;
8230   decl_die->decl_id = decl_id;
8231 }
8232
8233 /* Return how many bits covers PIECE EXPR_LIST.  */
8234
8235 static int
8236 decl_piece_bitsize (rtx piece)
8237 {
8238   int ret = (int) GET_MODE (piece);
8239   if (ret)
8240     return ret;
8241   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8242               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8243   return INTVAL (XEXP (XEXP (piece, 0), 0));
8244 }
8245
8246 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8247
8248 static rtx *
8249 decl_piece_varloc_ptr (rtx piece)
8250 {
8251   if ((int) GET_MODE (piece))
8252     return &XEXP (piece, 0);
8253   else
8254     return &XEXP (XEXP (piece, 0), 1);
8255 }
8256
8257 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8258    Next is the chain of following piece nodes.  */
8259
8260 static rtx
8261 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8262 {
8263   if (bitsize <= (int) MAX_MACHINE_MODE)
8264     return alloc_EXPR_LIST (bitsize, loc_note, next);
8265   else
8266     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8267                                                GEN_INT (bitsize),
8268                                                loc_note), next);
8269 }
8270
8271 /* Return rtx that should be stored into loc field for
8272    LOC_NOTE and BITPOS/BITSIZE.  */
8273
8274 static rtx
8275 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8276                       HOST_WIDE_INT bitsize)
8277 {
8278   if (bitsize != -1)
8279     {
8280       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8281       if (bitpos != 0)
8282         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8283     }
8284   return loc_note;
8285 }
8286
8287 /* This function either modifies location piece list *DEST in
8288    place (if SRC and INNER is NULL), or copies location piece list
8289    *SRC to *DEST while modifying it.  Location BITPOS is modified
8290    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8291    not copied and if needed some padding around it is added.
8292    When modifying in place, DEST should point to EXPR_LIST where
8293    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8294    to the start of the whole list and INNER points to the EXPR_LIST
8295    where earlier pieces cover PIECE_BITPOS bits.  */
8296
8297 static void
8298 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8299                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8300                    HOST_WIDE_INT bitsize, rtx loc_note)
8301 {
8302   int diff;
8303   bool copy = inner != NULL;
8304
8305   if (copy)
8306     {
8307       /* First copy all nodes preceeding the current bitpos.  */
8308       while (src != inner)
8309         {
8310           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8311                                    decl_piece_bitsize (*src), NULL_RTX);
8312           dest = &XEXP (*dest, 1);
8313           src = &XEXP (*src, 1);
8314         }
8315     }
8316   /* Add padding if needed.  */
8317   if (bitpos != piece_bitpos)
8318     {
8319       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8320                                copy ? NULL_RTX : *dest);
8321       dest = &XEXP (*dest, 1);
8322     }
8323   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8324     {
8325       gcc_assert (!copy);
8326       /* A piece with correct bitpos and bitsize already exist,
8327          just update the location for it and return.  */
8328       *decl_piece_varloc_ptr (*dest) = loc_note;
8329       return;
8330     }
8331   /* Add the piece that changed.  */
8332   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8333   dest = &XEXP (*dest, 1);
8334   /* Skip over pieces that overlap it.  */
8335   diff = bitpos - piece_bitpos + bitsize;
8336   if (!copy)
8337     src = dest;
8338   while (diff > 0 && *src)
8339     {
8340       rtx piece = *src;
8341       diff -= decl_piece_bitsize (piece);
8342       if (copy)
8343         src = &XEXP (piece, 1);
8344       else
8345         {
8346           *src = XEXP (piece, 1);
8347           free_EXPR_LIST_node (piece);
8348         }
8349     }
8350   /* Add padding if needed.  */
8351   if (diff < 0 && *src)
8352     {
8353       if (!copy)
8354         dest = src;
8355       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8356       dest = &XEXP (*dest, 1);
8357     }
8358   if (!copy)
8359     return;
8360   /* Finally copy all nodes following it.  */
8361   while (*src)
8362     {
8363       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8364                                decl_piece_bitsize (*src), NULL_RTX);
8365       dest = &XEXP (*dest, 1);
8366       src = &XEXP (*src, 1);
8367     }
8368 }
8369
8370 /* Add a variable location node to the linked list for DECL.  */
8371
8372 static struct var_loc_node *
8373 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8374 {
8375   unsigned int decl_id;
8376   var_loc_list *temp;
8377   void **slot;
8378   struct var_loc_node *loc = NULL;
8379   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8380
8381   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8382     {
8383       tree realdecl = DECL_DEBUG_EXPR (decl);
8384       if (realdecl && handled_component_p (realdecl))
8385         {
8386           HOST_WIDE_INT maxsize;
8387           tree innerdecl;
8388           innerdecl
8389             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8390           if (!DECL_P (innerdecl)
8391               || DECL_IGNORED_P (innerdecl)
8392               || TREE_STATIC (innerdecl)
8393               || bitsize <= 0
8394               || bitpos + bitsize > 256
8395               || bitsize != maxsize)
8396             return NULL;
8397           decl = innerdecl;
8398         }
8399     }
8400
8401   decl_id = DECL_UID (decl);
8402   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8403   if (*slot == NULL)
8404     {
8405       temp = ggc_alloc_cleared_var_loc_list ();
8406       temp->decl_id = decl_id;
8407       *slot = temp;
8408     }
8409   else
8410     temp = (var_loc_list *) *slot;
8411
8412   if (temp->last)
8413     {
8414       struct var_loc_node *last = temp->last, *unused = NULL;
8415       rtx *piece_loc = NULL, last_loc_note;
8416       int piece_bitpos = 0;
8417       if (last->next)
8418         {
8419           last = last->next;
8420           gcc_assert (last->next == NULL);
8421         }
8422       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8423         {
8424           piece_loc = &last->loc;
8425           do
8426             {
8427               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8428               if (piece_bitpos + cur_bitsize > bitpos)
8429                 break;
8430               piece_bitpos += cur_bitsize;
8431               piece_loc = &XEXP (*piece_loc, 1);
8432             }
8433           while (*piece_loc);
8434         }
8435       /* TEMP->LAST here is either pointer to the last but one or
8436          last element in the chained list, LAST is pointer to the
8437          last element.  */
8438       if (label && strcmp (last->label, label) == 0)
8439         {
8440           /* For SRA optimized variables if there weren't any real
8441              insns since last note, just modify the last node.  */
8442           if (piece_loc != NULL)
8443             {
8444               adjust_piece_list (piece_loc, NULL, NULL,
8445                                  bitpos, piece_bitpos, bitsize, loc_note);
8446               return NULL;
8447             }
8448           /* If the last note doesn't cover any instructions, remove it.  */
8449           if (temp->last != last)
8450             {
8451               temp->last->next = NULL;
8452               unused = last;
8453               last = temp->last;
8454               gcc_assert (strcmp (last->label, label) != 0);
8455             }
8456           else
8457             {
8458               gcc_assert (temp->first == temp->last);
8459               memset (temp->last, '\0', sizeof (*temp->last));
8460               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8461               return temp->last;
8462             }
8463         }
8464       if (bitsize == -1 && NOTE_P (last->loc))
8465         last_loc_note = last->loc;
8466       else if (piece_loc != NULL
8467                && *piece_loc != NULL_RTX
8468                && piece_bitpos == bitpos
8469                && decl_piece_bitsize (*piece_loc) == bitsize)
8470         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8471       else
8472         last_loc_note = NULL_RTX;
8473       /* If the current location is the same as the end of the list,
8474          and either both or neither of the locations is uninitialized,
8475          we have nothing to do.  */
8476       if (last_loc_note == NULL_RTX
8477           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8478                             NOTE_VAR_LOCATION_LOC (loc_note)))
8479           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8480                != NOTE_VAR_LOCATION_STATUS (loc_note))
8481               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8482                    == VAR_INIT_STATUS_UNINITIALIZED)
8483                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8484                       == VAR_INIT_STATUS_UNINITIALIZED))))
8485         {
8486           /* Add LOC to the end of list and update LAST.  If the last
8487              element of the list has been removed above, reuse its
8488              memory for the new node, otherwise allocate a new one.  */
8489           if (unused)
8490             {
8491               loc = unused;
8492               memset (loc, '\0', sizeof (*loc));
8493             }
8494           else
8495             loc = ggc_alloc_cleared_var_loc_node ();
8496           if (bitsize == -1 || piece_loc == NULL)
8497             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8498           else
8499             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8500                                bitpos, piece_bitpos, bitsize, loc_note);
8501           last->next = loc;
8502           /* Ensure TEMP->LAST will point either to the new last but one
8503              element of the chain, or to the last element in it.  */
8504           if (last != temp->last)
8505             temp->last = last;
8506         }
8507       else if (unused)
8508         ggc_free (unused);
8509     }
8510   else
8511     {
8512       loc = ggc_alloc_cleared_var_loc_node ();
8513       temp->first = loc;
8514       temp->last = loc;
8515       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8516     }
8517   return loc;
8518 }
8519 \f
8520 /* Keep track of the number of spaces used to indent the
8521    output of the debugging routines that print the structure of
8522    the DIE internal representation.  */
8523 static int print_indent;
8524
8525 /* Indent the line the number of spaces given by print_indent.  */
8526
8527 static inline void
8528 print_spaces (FILE *outfile)
8529 {
8530   fprintf (outfile, "%*s", print_indent, "");
8531 }
8532
8533 /* Print a type signature in hex.  */
8534
8535 static inline void
8536 print_signature (FILE *outfile, char *sig)
8537 {
8538   int i;
8539
8540   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8541     fprintf (outfile, "%02x", sig[i] & 0xff);
8542 }
8543
8544 /* Print the information associated with a given DIE, and its children.
8545    This routine is a debugging aid only.  */
8546
8547 static void
8548 print_die (dw_die_ref die, FILE *outfile)
8549 {
8550   dw_attr_ref a;
8551   dw_die_ref c;
8552   unsigned ix;
8553
8554   print_spaces (outfile);
8555   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8556            die->die_offset, dwarf_tag_name (die->die_tag),
8557            (void*) die);
8558   print_spaces (outfile);
8559   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8560   fprintf (outfile, " offset: %ld", die->die_offset);
8561   fprintf (outfile, " mark: %d\n", die->die_mark);
8562
8563   if (dwarf_version >= 4 && die->die_id.die_type_node)
8564     {
8565       print_spaces (outfile);
8566       fprintf (outfile, "  signature: ");
8567       print_signature (outfile, die->die_id.die_type_node->signature);
8568       fprintf (outfile, "\n");
8569     }
8570
8571   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8572     {
8573       print_spaces (outfile);
8574       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8575
8576       switch (AT_class (a))
8577         {
8578         case dw_val_class_addr:
8579           fprintf (outfile, "address");
8580           break;
8581         case dw_val_class_offset:
8582           fprintf (outfile, "offset");
8583           break;
8584         case dw_val_class_loc:
8585           fprintf (outfile, "location descriptor");
8586           break;
8587         case dw_val_class_loc_list:
8588           fprintf (outfile, "location list -> label:%s",
8589                    AT_loc_list (a)->ll_symbol);
8590           break;
8591         case dw_val_class_range_list:
8592           fprintf (outfile, "range list");
8593           break;
8594         case dw_val_class_const:
8595           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8596           break;
8597         case dw_val_class_unsigned_const:
8598           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8599           break;
8600         case dw_val_class_const_double:
8601           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8602                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8603                    a->dw_attr_val.v.val_double.high,
8604                    a->dw_attr_val.v.val_double.low);
8605           break;
8606         case dw_val_class_vec:
8607           fprintf (outfile, "floating-point or vector constant");
8608           break;
8609         case dw_val_class_flag:
8610           fprintf (outfile, "%u", AT_flag (a));
8611           break;
8612         case dw_val_class_die_ref:
8613           if (AT_ref (a) != NULL)
8614             {
8615               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8616                 {
8617                   fprintf (outfile, "die -> signature: ");
8618                   print_signature (outfile,
8619                                    AT_ref (a)->die_id.die_type_node->signature);
8620                 }
8621               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8622                 fprintf (outfile, "die -> label: %s",
8623                          AT_ref (a)->die_id.die_symbol);
8624               else
8625                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8626               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8627             }
8628           else
8629             fprintf (outfile, "die -> <null>");
8630           break;
8631         case dw_val_class_vms_delta:
8632           fprintf (outfile, "delta: @slotcount(%s-%s)",
8633                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8634           break;
8635         case dw_val_class_lbl_id:
8636         case dw_val_class_lineptr:
8637         case dw_val_class_macptr:
8638           fprintf (outfile, "label: %s", AT_lbl (a));
8639           break;
8640         case dw_val_class_str:
8641           if (AT_string (a) != NULL)
8642             fprintf (outfile, "\"%s\"", AT_string (a));
8643           else
8644             fprintf (outfile, "<null>");
8645           break;
8646         case dw_val_class_file:
8647           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8648                    AT_file (a)->emitted_number);
8649           break;
8650         case dw_val_class_data8:
8651           {
8652             int i;
8653
8654             for (i = 0; i < 8; i++)
8655               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8656             break;
8657           }
8658         default:
8659           break;
8660         }
8661
8662       fprintf (outfile, "\n");
8663     }
8664
8665   if (die->die_child != NULL)
8666     {
8667       print_indent += 4;
8668       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8669       print_indent -= 4;
8670     }
8671   if (print_indent == 0)
8672     fprintf (outfile, "\n");
8673 }
8674
8675 /* Print the contents of the source code line number correspondence table.
8676    This routine is a debugging aid only.  */
8677
8678 static void
8679 print_dwarf_line_table (FILE *outfile)
8680 {
8681   unsigned i;
8682   dw_line_info_ref line_info;
8683
8684   fprintf (outfile, "\n\nDWARF source line information\n");
8685   for (i = 1; i < line_info_table_in_use; i++)
8686     {
8687       line_info = &line_info_table[i];
8688       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8689                line_info->dw_file_num,
8690                line_info->dw_line_num);
8691     }
8692
8693   fprintf (outfile, "\n\n");
8694 }
8695
8696 /* Print the information collected for a given DIE.  */
8697
8698 DEBUG_FUNCTION void
8699 debug_dwarf_die (dw_die_ref die)
8700 {
8701   print_die (die, stderr);
8702 }
8703
8704 /* Print all DWARF information collected for the compilation unit.
8705    This routine is a debugging aid only.  */
8706
8707 DEBUG_FUNCTION void
8708 debug_dwarf (void)
8709 {
8710   print_indent = 0;
8711   print_die (comp_unit_die (), stderr);
8712   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8713     print_dwarf_line_table (stderr);
8714 }
8715 \f
8716 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8717    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8718    DIE that marks the start of the DIEs for this include file.  */
8719
8720 static dw_die_ref
8721 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8722 {
8723   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8724   dw_die_ref new_unit = gen_compile_unit_die (filename);
8725
8726   new_unit->die_sib = old_unit;
8727   return new_unit;
8728 }
8729
8730 /* Close an include-file CU and reopen the enclosing one.  */
8731
8732 static dw_die_ref
8733 pop_compile_unit (dw_die_ref old_unit)
8734 {
8735   dw_die_ref new_unit = old_unit->die_sib;
8736
8737   old_unit->die_sib = NULL;
8738   return new_unit;
8739 }
8740
8741 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8742 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8743
8744 /* Calculate the checksum of a location expression.  */
8745
8746 static inline void
8747 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8748 {
8749   int tem;
8750
8751   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8752   CHECKSUM (tem);
8753   CHECKSUM (loc->dw_loc_oprnd1);
8754   CHECKSUM (loc->dw_loc_oprnd2);
8755 }
8756
8757 /* Calculate the checksum of an attribute.  */
8758
8759 static void
8760 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8761 {
8762   dw_loc_descr_ref loc;
8763   rtx r;
8764
8765   CHECKSUM (at->dw_attr);
8766
8767   /* We don't care that this was compiled with a different compiler
8768      snapshot; if the output is the same, that's what matters.  */
8769   if (at->dw_attr == DW_AT_producer)
8770     return;
8771
8772   switch (AT_class (at))
8773     {
8774     case dw_val_class_const:
8775       CHECKSUM (at->dw_attr_val.v.val_int);
8776       break;
8777     case dw_val_class_unsigned_const:
8778       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8779       break;
8780     case dw_val_class_const_double:
8781       CHECKSUM (at->dw_attr_val.v.val_double);
8782       break;
8783     case dw_val_class_vec:
8784       CHECKSUM (at->dw_attr_val.v.val_vec);
8785       break;
8786     case dw_val_class_flag:
8787       CHECKSUM (at->dw_attr_val.v.val_flag);
8788       break;
8789     case dw_val_class_str:
8790       CHECKSUM_STRING (AT_string (at));
8791       break;
8792
8793     case dw_val_class_addr:
8794       r = AT_addr (at);
8795       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8796       CHECKSUM_STRING (XSTR (r, 0));
8797       break;
8798
8799     case dw_val_class_offset:
8800       CHECKSUM (at->dw_attr_val.v.val_offset);
8801       break;
8802
8803     case dw_val_class_loc:
8804       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8805         loc_checksum (loc, ctx);
8806       break;
8807
8808     case dw_val_class_die_ref:
8809       die_checksum (AT_ref (at), ctx, mark);
8810       break;
8811
8812     case dw_val_class_fde_ref:
8813     case dw_val_class_vms_delta:
8814     case dw_val_class_lbl_id:
8815     case dw_val_class_lineptr:
8816     case dw_val_class_macptr:
8817       break;
8818
8819     case dw_val_class_file:
8820       CHECKSUM_STRING (AT_file (at)->filename);
8821       break;
8822
8823     case dw_val_class_data8:
8824       CHECKSUM (at->dw_attr_val.v.val_data8);
8825       break;
8826
8827     default:
8828       break;
8829     }
8830 }
8831
8832 /* Calculate the checksum of a DIE.  */
8833
8834 static void
8835 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8836 {
8837   dw_die_ref c;
8838   dw_attr_ref a;
8839   unsigned ix;
8840
8841   /* To avoid infinite recursion.  */
8842   if (die->die_mark)
8843     {
8844       CHECKSUM (die->die_mark);
8845       return;
8846     }
8847   die->die_mark = ++(*mark);
8848
8849   CHECKSUM (die->die_tag);
8850
8851   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8852     attr_checksum (a, ctx, mark);
8853
8854   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8855 }
8856
8857 #undef CHECKSUM
8858 #undef CHECKSUM_STRING
8859
8860 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8861 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8862 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8863 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8864 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8865 #define CHECKSUM_ATTR(FOO) \
8866   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8867
8868 /* Calculate the checksum of a number in signed LEB128 format.  */
8869
8870 static void
8871 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8872 {
8873   unsigned char byte;
8874   bool more;
8875
8876   while (1)
8877     {
8878       byte = (value & 0x7f);
8879       value >>= 7;
8880       more = !((value == 0 && (byte & 0x40) == 0)
8881                 || (value == -1 && (byte & 0x40) != 0));
8882       if (more)
8883         byte |= 0x80;
8884       CHECKSUM (byte);
8885       if (!more)
8886         break;
8887     }
8888 }
8889
8890 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8891
8892 static void
8893 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8894 {
8895   while (1)
8896     {
8897       unsigned char byte = (value & 0x7f);
8898       value >>= 7;
8899       if (value != 0)
8900         /* More bytes to follow.  */
8901         byte |= 0x80;
8902       CHECKSUM (byte);
8903       if (value == 0)
8904         break;
8905     }
8906 }
8907
8908 /* Checksum the context of the DIE.  This adds the names of any
8909    surrounding namespaces or structures to the checksum.  */
8910
8911 static void
8912 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8913 {
8914   const char *name;
8915   dw_die_ref spec;
8916   int tag = die->die_tag;
8917
8918   if (tag != DW_TAG_namespace
8919       && tag != DW_TAG_structure_type
8920       && tag != DW_TAG_class_type)
8921     return;
8922
8923   name = get_AT_string (die, DW_AT_name);
8924
8925   spec = get_AT_ref (die, DW_AT_specification);
8926   if (spec != NULL)
8927     die = spec;
8928
8929   if (die->die_parent != NULL)
8930     checksum_die_context (die->die_parent, ctx);
8931
8932   CHECKSUM_ULEB128 ('C');
8933   CHECKSUM_ULEB128 (tag);
8934   if (name != NULL)
8935     CHECKSUM_STRING (name);
8936 }
8937
8938 /* Calculate the checksum of a location expression.  */
8939
8940 static inline void
8941 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8942 {
8943   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8944      were emitted as a DW_FORM_sdata instead of a location expression.  */
8945   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8946     {
8947       CHECKSUM_ULEB128 (DW_FORM_sdata);
8948       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8949       return;
8950     }
8951
8952   /* Otherwise, just checksum the raw location expression.  */
8953   while (loc != NULL)
8954     {
8955       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8956       CHECKSUM (loc->dw_loc_oprnd1);
8957       CHECKSUM (loc->dw_loc_oprnd2);
8958       loc = loc->dw_loc_next;
8959     }
8960 }
8961
8962 /* Calculate the checksum of an attribute.  */
8963
8964 static void
8965 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8966                        struct md5_ctx *ctx, int *mark)
8967 {
8968   dw_loc_descr_ref loc;
8969   rtx r;
8970
8971   if (AT_class (at) == dw_val_class_die_ref)
8972     {
8973       dw_die_ref target_die = AT_ref (at);
8974
8975       /* For pointer and reference types, we checksum only the (qualified)
8976          name of the target type (if there is a name).  For friend entries,
8977          we checksum only the (qualified) name of the target type or function.
8978          This allows the checksum to remain the same whether the target type
8979          is complete or not.  */
8980       if ((at->dw_attr == DW_AT_type
8981            && (tag == DW_TAG_pointer_type
8982                || tag == DW_TAG_reference_type
8983                || tag == DW_TAG_rvalue_reference_type
8984                || tag == DW_TAG_ptr_to_member_type))
8985           || (at->dw_attr == DW_AT_friend
8986               && tag == DW_TAG_friend))
8987         {
8988           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8989
8990           if (name_attr != NULL)
8991             {
8992               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8993
8994               if (decl == NULL)
8995                 decl = target_die;
8996               CHECKSUM_ULEB128 ('N');
8997               CHECKSUM_ULEB128 (at->dw_attr);
8998               if (decl->die_parent != NULL)
8999                 checksum_die_context (decl->die_parent, ctx);
9000               CHECKSUM_ULEB128 ('E');
9001               CHECKSUM_STRING (AT_string (name_attr));
9002               return;
9003             }
9004         }
9005
9006       /* For all other references to another DIE, we check to see if the
9007          target DIE has already been visited.  If it has, we emit a
9008          backward reference; if not, we descend recursively.  */
9009       if (target_die->die_mark > 0)
9010         {
9011           CHECKSUM_ULEB128 ('R');
9012           CHECKSUM_ULEB128 (at->dw_attr);
9013           CHECKSUM_ULEB128 (target_die->die_mark);
9014         }
9015       else
9016         {
9017           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9018
9019           if (decl == NULL)
9020             decl = target_die;
9021           target_die->die_mark = ++(*mark);
9022           CHECKSUM_ULEB128 ('T');
9023           CHECKSUM_ULEB128 (at->dw_attr);
9024           if (decl->die_parent != NULL)
9025             checksum_die_context (decl->die_parent, ctx);
9026           die_checksum_ordered (target_die, ctx, mark);
9027         }
9028       return;
9029     }
9030
9031   CHECKSUM_ULEB128 ('A');
9032   CHECKSUM_ULEB128 (at->dw_attr);
9033
9034   switch (AT_class (at))
9035     {
9036     case dw_val_class_const:
9037       CHECKSUM_ULEB128 (DW_FORM_sdata);
9038       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9039       break;
9040
9041     case dw_val_class_unsigned_const:
9042       CHECKSUM_ULEB128 (DW_FORM_sdata);
9043       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9044       break;
9045
9046     case dw_val_class_const_double:
9047       CHECKSUM_ULEB128 (DW_FORM_block);
9048       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9049       CHECKSUM (at->dw_attr_val.v.val_double);
9050       break;
9051
9052     case dw_val_class_vec:
9053       CHECKSUM_ULEB128 (DW_FORM_block);
9054       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9055       CHECKSUM (at->dw_attr_val.v.val_vec);
9056       break;
9057
9058     case dw_val_class_flag:
9059       CHECKSUM_ULEB128 (DW_FORM_flag);
9060       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9061       break;
9062
9063     case dw_val_class_str:
9064       CHECKSUM_ULEB128 (DW_FORM_string);
9065       CHECKSUM_STRING (AT_string (at));
9066       break;
9067
9068     case dw_val_class_addr:
9069       r = AT_addr (at);
9070       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9071       CHECKSUM_ULEB128 (DW_FORM_string);
9072       CHECKSUM_STRING (XSTR (r, 0));
9073       break;
9074
9075     case dw_val_class_offset:
9076       CHECKSUM_ULEB128 (DW_FORM_sdata);
9077       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9078       break;
9079
9080     case dw_val_class_loc:
9081       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9082         loc_checksum_ordered (loc, ctx);
9083       break;
9084
9085     case dw_val_class_fde_ref:
9086     case dw_val_class_lbl_id:
9087     case dw_val_class_lineptr:
9088     case dw_val_class_macptr:
9089       break;
9090
9091     case dw_val_class_file:
9092       CHECKSUM_ULEB128 (DW_FORM_string);
9093       CHECKSUM_STRING (AT_file (at)->filename);
9094       break;
9095
9096     case dw_val_class_data8:
9097       CHECKSUM (at->dw_attr_val.v.val_data8);
9098       break;
9099
9100     default:
9101       break;
9102     }
9103 }
9104
9105 struct checksum_attributes
9106 {
9107   dw_attr_ref at_name;
9108   dw_attr_ref at_type;
9109   dw_attr_ref at_friend;
9110   dw_attr_ref at_accessibility;
9111   dw_attr_ref at_address_class;
9112   dw_attr_ref at_allocated;
9113   dw_attr_ref at_artificial;
9114   dw_attr_ref at_associated;
9115   dw_attr_ref at_binary_scale;
9116   dw_attr_ref at_bit_offset;
9117   dw_attr_ref at_bit_size;
9118   dw_attr_ref at_bit_stride;
9119   dw_attr_ref at_byte_size;
9120   dw_attr_ref at_byte_stride;
9121   dw_attr_ref at_const_value;
9122   dw_attr_ref at_containing_type;
9123   dw_attr_ref at_count;
9124   dw_attr_ref at_data_location;
9125   dw_attr_ref at_data_member_location;
9126   dw_attr_ref at_decimal_scale;
9127   dw_attr_ref at_decimal_sign;
9128   dw_attr_ref at_default_value;
9129   dw_attr_ref at_digit_count;
9130   dw_attr_ref at_discr;
9131   dw_attr_ref at_discr_list;
9132   dw_attr_ref at_discr_value;
9133   dw_attr_ref at_encoding;
9134   dw_attr_ref at_endianity;
9135   dw_attr_ref at_explicit;
9136   dw_attr_ref at_is_optional;
9137   dw_attr_ref at_location;
9138   dw_attr_ref at_lower_bound;
9139   dw_attr_ref at_mutable;
9140   dw_attr_ref at_ordering;
9141   dw_attr_ref at_picture_string;
9142   dw_attr_ref at_prototyped;
9143   dw_attr_ref at_small;
9144   dw_attr_ref at_segment;
9145   dw_attr_ref at_string_length;
9146   dw_attr_ref at_threads_scaled;
9147   dw_attr_ref at_upper_bound;
9148   dw_attr_ref at_use_location;
9149   dw_attr_ref at_use_UTF8;
9150   dw_attr_ref at_variable_parameter;
9151   dw_attr_ref at_virtuality;
9152   dw_attr_ref at_visibility;
9153   dw_attr_ref at_vtable_elem_location;
9154 };
9155
9156 /* Collect the attributes that we will want to use for the checksum.  */
9157
9158 static void
9159 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9160 {
9161   dw_attr_ref a;
9162   unsigned ix;
9163
9164   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9165     {
9166       switch (a->dw_attr)
9167         {
9168         case DW_AT_name:
9169           attrs->at_name = a;
9170           break;
9171         case DW_AT_type:
9172           attrs->at_type = a;
9173           break;
9174         case DW_AT_friend:
9175           attrs->at_friend = a;
9176           break;
9177         case DW_AT_accessibility:
9178           attrs->at_accessibility = a;
9179           break;
9180         case DW_AT_address_class:
9181           attrs->at_address_class = a;
9182           break;
9183         case DW_AT_allocated:
9184           attrs->at_allocated = a;
9185           break;
9186         case DW_AT_artificial:
9187           attrs->at_artificial = a;
9188           break;
9189         case DW_AT_associated:
9190           attrs->at_associated = a;
9191           break;
9192         case DW_AT_binary_scale:
9193           attrs->at_binary_scale = a;
9194           break;
9195         case DW_AT_bit_offset:
9196           attrs->at_bit_offset = a;
9197           break;
9198         case DW_AT_bit_size:
9199           attrs->at_bit_size = a;
9200           break;
9201         case DW_AT_bit_stride:
9202           attrs->at_bit_stride = a;
9203           break;
9204         case DW_AT_byte_size:
9205           attrs->at_byte_size = a;
9206           break;
9207         case DW_AT_byte_stride:
9208           attrs->at_byte_stride = a;
9209           break;
9210         case DW_AT_const_value:
9211           attrs->at_const_value = a;
9212           break;
9213         case DW_AT_containing_type:
9214           attrs->at_containing_type = a;
9215           break;
9216         case DW_AT_count:
9217           attrs->at_count = a;
9218           break;
9219         case DW_AT_data_location:
9220           attrs->at_data_location = a;
9221           break;
9222         case DW_AT_data_member_location:
9223           attrs->at_data_member_location = a;
9224           break;
9225         case DW_AT_decimal_scale:
9226           attrs->at_decimal_scale = a;
9227           break;
9228         case DW_AT_decimal_sign:
9229           attrs->at_decimal_sign = a;
9230           break;
9231         case DW_AT_default_value:
9232           attrs->at_default_value = a;
9233           break;
9234         case DW_AT_digit_count:
9235           attrs->at_digit_count = a;
9236           break;
9237         case DW_AT_discr:
9238           attrs->at_discr = a;
9239           break;
9240         case DW_AT_discr_list:
9241           attrs->at_discr_list = a;
9242           break;
9243         case DW_AT_discr_value:
9244           attrs->at_discr_value = a;
9245           break;
9246         case DW_AT_encoding:
9247           attrs->at_encoding = a;
9248           break;
9249         case DW_AT_endianity:
9250           attrs->at_endianity = a;
9251           break;
9252         case DW_AT_explicit:
9253           attrs->at_explicit = a;
9254           break;
9255         case DW_AT_is_optional:
9256           attrs->at_is_optional = a;
9257           break;
9258         case DW_AT_location:
9259           attrs->at_location = a;
9260           break;
9261         case DW_AT_lower_bound:
9262           attrs->at_lower_bound = a;
9263           break;
9264         case DW_AT_mutable:
9265           attrs->at_mutable = a;
9266           break;
9267         case DW_AT_ordering:
9268           attrs->at_ordering = a;
9269           break;
9270         case DW_AT_picture_string:
9271           attrs->at_picture_string = a;
9272           break;
9273         case DW_AT_prototyped:
9274           attrs->at_prototyped = a;
9275           break;
9276         case DW_AT_small:
9277           attrs->at_small = a;
9278           break;
9279         case DW_AT_segment:
9280           attrs->at_segment = a;
9281           break;
9282         case DW_AT_string_length:
9283           attrs->at_string_length = a;
9284           break;
9285         case DW_AT_threads_scaled:
9286           attrs->at_threads_scaled = a;
9287           break;
9288         case DW_AT_upper_bound:
9289           attrs->at_upper_bound = a;
9290           break;
9291         case DW_AT_use_location:
9292           attrs->at_use_location = a;
9293           break;
9294         case DW_AT_use_UTF8:
9295           attrs->at_use_UTF8 = a;
9296           break;
9297         case DW_AT_variable_parameter:
9298           attrs->at_variable_parameter = a;
9299           break;
9300         case DW_AT_virtuality:
9301           attrs->at_virtuality = a;
9302           break;
9303         case DW_AT_visibility:
9304           attrs->at_visibility = a;
9305           break;
9306         case DW_AT_vtable_elem_location:
9307           attrs->at_vtable_elem_location = a;
9308           break;
9309         default:
9310           break;
9311         }
9312     }
9313 }
9314
9315 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9316
9317 static void
9318 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9319 {
9320   dw_die_ref c;
9321   dw_die_ref decl;
9322   struct checksum_attributes attrs;
9323
9324   CHECKSUM_ULEB128 ('D');
9325   CHECKSUM_ULEB128 (die->die_tag);
9326
9327   memset (&attrs, 0, sizeof (attrs));
9328
9329   decl = get_AT_ref (die, DW_AT_specification);
9330   if (decl != NULL)
9331     collect_checksum_attributes (&attrs, decl);
9332   collect_checksum_attributes (&attrs, die);
9333
9334   CHECKSUM_ATTR (attrs.at_name);
9335   CHECKSUM_ATTR (attrs.at_accessibility);
9336   CHECKSUM_ATTR (attrs.at_address_class);
9337   CHECKSUM_ATTR (attrs.at_allocated);
9338   CHECKSUM_ATTR (attrs.at_artificial);
9339   CHECKSUM_ATTR (attrs.at_associated);
9340   CHECKSUM_ATTR (attrs.at_binary_scale);
9341   CHECKSUM_ATTR (attrs.at_bit_offset);
9342   CHECKSUM_ATTR (attrs.at_bit_size);
9343   CHECKSUM_ATTR (attrs.at_bit_stride);
9344   CHECKSUM_ATTR (attrs.at_byte_size);
9345   CHECKSUM_ATTR (attrs.at_byte_stride);
9346   CHECKSUM_ATTR (attrs.at_const_value);
9347   CHECKSUM_ATTR (attrs.at_containing_type);
9348   CHECKSUM_ATTR (attrs.at_count);
9349   CHECKSUM_ATTR (attrs.at_data_location);
9350   CHECKSUM_ATTR (attrs.at_data_member_location);
9351   CHECKSUM_ATTR (attrs.at_decimal_scale);
9352   CHECKSUM_ATTR (attrs.at_decimal_sign);
9353   CHECKSUM_ATTR (attrs.at_default_value);
9354   CHECKSUM_ATTR (attrs.at_digit_count);
9355   CHECKSUM_ATTR (attrs.at_discr);
9356   CHECKSUM_ATTR (attrs.at_discr_list);
9357   CHECKSUM_ATTR (attrs.at_discr_value);
9358   CHECKSUM_ATTR (attrs.at_encoding);
9359   CHECKSUM_ATTR (attrs.at_endianity);
9360   CHECKSUM_ATTR (attrs.at_explicit);
9361   CHECKSUM_ATTR (attrs.at_is_optional);
9362   CHECKSUM_ATTR (attrs.at_location);
9363   CHECKSUM_ATTR (attrs.at_lower_bound);
9364   CHECKSUM_ATTR (attrs.at_mutable);
9365   CHECKSUM_ATTR (attrs.at_ordering);
9366   CHECKSUM_ATTR (attrs.at_picture_string);
9367   CHECKSUM_ATTR (attrs.at_prototyped);
9368   CHECKSUM_ATTR (attrs.at_small);
9369   CHECKSUM_ATTR (attrs.at_segment);
9370   CHECKSUM_ATTR (attrs.at_string_length);
9371   CHECKSUM_ATTR (attrs.at_threads_scaled);
9372   CHECKSUM_ATTR (attrs.at_upper_bound);
9373   CHECKSUM_ATTR (attrs.at_use_location);
9374   CHECKSUM_ATTR (attrs.at_use_UTF8);
9375   CHECKSUM_ATTR (attrs.at_variable_parameter);
9376   CHECKSUM_ATTR (attrs.at_virtuality);
9377   CHECKSUM_ATTR (attrs.at_visibility);
9378   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9379   CHECKSUM_ATTR (attrs.at_type);
9380   CHECKSUM_ATTR (attrs.at_friend);
9381
9382   /* Checksum the child DIEs, except for nested types and member functions.  */
9383   c = die->die_child;
9384   if (c) do {
9385     dw_attr_ref name_attr;
9386
9387     c = c->die_sib;
9388     name_attr = get_AT (c, DW_AT_name);
9389     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9390         && name_attr != NULL)
9391       {
9392         CHECKSUM_ULEB128 ('S');
9393         CHECKSUM_ULEB128 (c->die_tag);
9394         CHECKSUM_STRING (AT_string (name_attr));
9395       }
9396     else
9397       {
9398         /* Mark this DIE so it gets processed when unmarking.  */
9399         if (c->die_mark == 0)
9400           c->die_mark = -1;
9401         die_checksum_ordered (c, ctx, mark);
9402       }
9403   } while (c != die->die_child);
9404
9405   CHECKSUM_ULEB128 (0);
9406 }
9407
9408 #undef CHECKSUM
9409 #undef CHECKSUM_STRING
9410 #undef CHECKSUM_ATTR
9411 #undef CHECKSUM_LEB128
9412 #undef CHECKSUM_ULEB128
9413
9414 /* Generate the type signature for DIE.  This is computed by generating an
9415    MD5 checksum over the DIE's tag, its relevant attributes, and its
9416    children.  Attributes that are references to other DIEs are processed
9417    by recursion, using the MARK field to prevent infinite recursion.
9418    If the DIE is nested inside a namespace or another type, we also
9419    need to include that context in the signature.  The lower 64 bits
9420    of the resulting MD5 checksum comprise the signature.  */
9421
9422 static void
9423 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9424 {
9425   int mark;
9426   const char *name;
9427   unsigned char checksum[16];
9428   struct md5_ctx ctx;
9429   dw_die_ref decl;
9430
9431   name = get_AT_string (die, DW_AT_name);
9432   decl = get_AT_ref (die, DW_AT_specification);
9433
9434   /* First, compute a signature for just the type name (and its surrounding
9435      context, if any.  This is stored in the type unit DIE for link-time
9436      ODR (one-definition rule) checking.  */
9437
9438   if (is_cxx() && name != NULL)
9439     {
9440       md5_init_ctx (&ctx);
9441
9442       /* Checksum the names of surrounding namespaces and structures.  */
9443       if (decl != NULL && decl->die_parent != NULL)
9444         checksum_die_context (decl->die_parent, &ctx);
9445
9446       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9447       md5_process_bytes (name, strlen (name) + 1, &ctx);
9448       md5_finish_ctx (&ctx, checksum);
9449
9450       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9451     }
9452
9453   /* Next, compute the complete type signature.  */
9454
9455   md5_init_ctx (&ctx);
9456   mark = 1;
9457   die->die_mark = mark;
9458
9459   /* Checksum the names of surrounding namespaces and structures.  */
9460   if (decl != NULL && decl->die_parent != NULL)
9461     checksum_die_context (decl->die_parent, &ctx);
9462
9463   /* Checksum the DIE and its children.  */
9464   die_checksum_ordered (die, &ctx, &mark);
9465   unmark_all_dies (die);
9466   md5_finish_ctx (&ctx, checksum);
9467
9468   /* Store the signature in the type node and link the type DIE and the
9469      type node together.  */
9470   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9471           DWARF_TYPE_SIGNATURE_SIZE);
9472   die->die_id.die_type_node = type_node;
9473   type_node->type_die = die;
9474
9475   /* If the DIE is a specification, link its declaration to the type node
9476      as well.  */
9477   if (decl != NULL)
9478     decl->die_id.die_type_node = type_node;
9479 }
9480
9481 /* Do the location expressions look same?  */
9482 static inline int
9483 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9484 {
9485   return loc1->dw_loc_opc == loc2->dw_loc_opc
9486          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9487          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9488 }
9489
9490 /* Do the values look the same?  */
9491 static int
9492 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9493 {
9494   dw_loc_descr_ref loc1, loc2;
9495   rtx r1, r2;
9496
9497   if (v1->val_class != v2->val_class)
9498     return 0;
9499
9500   switch (v1->val_class)
9501     {
9502     case dw_val_class_const:
9503       return v1->v.val_int == v2->v.val_int;
9504     case dw_val_class_unsigned_const:
9505       return v1->v.val_unsigned == v2->v.val_unsigned;
9506     case dw_val_class_const_double:
9507       return v1->v.val_double.high == v2->v.val_double.high
9508              && v1->v.val_double.low == v2->v.val_double.low;
9509     case dw_val_class_vec:
9510       if (v1->v.val_vec.length != v2->v.val_vec.length
9511           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9512         return 0;
9513       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9514                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9515         return 0;
9516       return 1;
9517     case dw_val_class_flag:
9518       return v1->v.val_flag == v2->v.val_flag;
9519     case dw_val_class_str:
9520       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9521
9522     case dw_val_class_addr:
9523       r1 = v1->v.val_addr;
9524       r2 = v2->v.val_addr;
9525       if (GET_CODE (r1) != GET_CODE (r2))
9526         return 0;
9527       return !rtx_equal_p (r1, r2);
9528
9529     case dw_val_class_offset:
9530       return v1->v.val_offset == v2->v.val_offset;
9531
9532     case dw_val_class_loc:
9533       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9534            loc1 && loc2;
9535            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9536         if (!same_loc_p (loc1, loc2, mark))
9537           return 0;
9538       return !loc1 && !loc2;
9539
9540     case dw_val_class_die_ref:
9541       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9542
9543     case dw_val_class_fde_ref:
9544     case dw_val_class_vms_delta:
9545     case dw_val_class_lbl_id:
9546     case dw_val_class_lineptr:
9547     case dw_val_class_macptr:
9548       return 1;
9549
9550     case dw_val_class_file:
9551       return v1->v.val_file == v2->v.val_file;
9552
9553     case dw_val_class_data8:
9554       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9555
9556     default:
9557       return 1;
9558     }
9559 }
9560
9561 /* Do the attributes look the same?  */
9562
9563 static int
9564 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9565 {
9566   if (at1->dw_attr != at2->dw_attr)
9567     return 0;
9568
9569   /* We don't care that this was compiled with a different compiler
9570      snapshot; if the output is the same, that's what matters. */
9571   if (at1->dw_attr == DW_AT_producer)
9572     return 1;
9573
9574   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9575 }
9576
9577 /* Do the dies look the same?  */
9578
9579 static int
9580 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9581 {
9582   dw_die_ref c1, c2;
9583   dw_attr_ref a1;
9584   unsigned ix;
9585
9586   /* To avoid infinite recursion.  */
9587   if (die1->die_mark)
9588     return die1->die_mark == die2->die_mark;
9589   die1->die_mark = die2->die_mark = ++(*mark);
9590
9591   if (die1->die_tag != die2->die_tag)
9592     return 0;
9593
9594   if (VEC_length (dw_attr_node, die1->die_attr)
9595       != VEC_length (dw_attr_node, die2->die_attr))
9596     return 0;
9597
9598   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9599     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9600       return 0;
9601
9602   c1 = die1->die_child;
9603   c2 = die2->die_child;
9604   if (! c1)
9605     {
9606       if (c2)
9607         return 0;
9608     }
9609   else
9610     for (;;)
9611       {
9612         if (!same_die_p (c1, c2, mark))
9613           return 0;
9614         c1 = c1->die_sib;
9615         c2 = c2->die_sib;
9616         if (c1 == die1->die_child)
9617           {
9618             if (c2 == die2->die_child)
9619               break;
9620             else
9621               return 0;
9622           }
9623     }
9624
9625   return 1;
9626 }
9627
9628 /* Do the dies look the same?  Wrapper around same_die_p.  */
9629
9630 static int
9631 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9632 {
9633   int mark = 0;
9634   int ret = same_die_p (die1, die2, &mark);
9635
9636   unmark_all_dies (die1);
9637   unmark_all_dies (die2);
9638
9639   return ret;
9640 }
9641
9642 /* The prefix to attach to symbols on DIEs in the current comdat debug
9643    info section.  */
9644 static char *comdat_symbol_id;
9645
9646 /* The index of the current symbol within the current comdat CU.  */
9647 static unsigned int comdat_symbol_number;
9648
9649 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9650    children, and set comdat_symbol_id accordingly.  */
9651
9652 static void
9653 compute_section_prefix (dw_die_ref unit_die)
9654 {
9655   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9656   const char *base = die_name ? lbasename (die_name) : "anonymous";
9657   char *name = XALLOCAVEC (char, strlen (base) + 64);
9658   char *p;
9659   int i, mark;
9660   unsigned char checksum[16];
9661   struct md5_ctx ctx;
9662
9663   /* Compute the checksum of the DIE, then append part of it as hex digits to
9664      the name filename of the unit.  */
9665
9666   md5_init_ctx (&ctx);
9667   mark = 0;
9668   die_checksum (unit_die, &ctx, &mark);
9669   unmark_all_dies (unit_die);
9670   md5_finish_ctx (&ctx, checksum);
9671
9672   sprintf (name, "%s.", base);
9673   clean_symbol_name (name);
9674
9675   p = name + strlen (name);
9676   for (i = 0; i < 4; i++)
9677     {
9678       sprintf (p, "%.2x", checksum[i]);
9679       p += 2;
9680     }
9681
9682   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9683   comdat_symbol_number = 0;
9684 }
9685
9686 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9687
9688 static int
9689 is_type_die (dw_die_ref die)
9690 {
9691   switch (die->die_tag)
9692     {
9693     case DW_TAG_array_type:
9694     case DW_TAG_class_type:
9695     case DW_TAG_interface_type:
9696     case DW_TAG_enumeration_type:
9697     case DW_TAG_pointer_type:
9698     case DW_TAG_reference_type:
9699     case DW_TAG_rvalue_reference_type:
9700     case DW_TAG_string_type:
9701     case DW_TAG_structure_type:
9702     case DW_TAG_subroutine_type:
9703     case DW_TAG_union_type:
9704     case DW_TAG_ptr_to_member_type:
9705     case DW_TAG_set_type:
9706     case DW_TAG_subrange_type:
9707     case DW_TAG_base_type:
9708     case DW_TAG_const_type:
9709     case DW_TAG_file_type:
9710     case DW_TAG_packed_type:
9711     case DW_TAG_volatile_type:
9712     case DW_TAG_typedef:
9713       return 1;
9714     default:
9715       return 0;
9716     }
9717 }
9718
9719 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9720    Basically, we want to choose the bits that are likely to be shared between
9721    compilations (types) and leave out the bits that are specific to individual
9722    compilations (functions).  */
9723
9724 static int
9725 is_comdat_die (dw_die_ref c)
9726 {
9727   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9728      we do for stabs.  The advantage is a greater likelihood of sharing between
9729      objects that don't include headers in the same order (and therefore would
9730      put the base types in a different comdat).  jason 8/28/00 */
9731
9732   if (c->die_tag == DW_TAG_base_type)
9733     return 0;
9734
9735   if (c->die_tag == DW_TAG_pointer_type
9736       || c->die_tag == DW_TAG_reference_type
9737       || c->die_tag == DW_TAG_rvalue_reference_type
9738       || c->die_tag == DW_TAG_const_type
9739       || c->die_tag == DW_TAG_volatile_type)
9740     {
9741       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9742
9743       return t ? is_comdat_die (t) : 0;
9744     }
9745
9746   return is_type_die (c);
9747 }
9748
9749 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9750    compilation unit.  */
9751
9752 static int
9753 is_symbol_die (dw_die_ref c)
9754 {
9755   return (is_type_die (c)
9756           || is_declaration_die (c)
9757           || c->die_tag == DW_TAG_namespace
9758           || c->die_tag == DW_TAG_module);
9759 }
9760
9761 /* Returns true iff C is a compile-unit DIE.  */
9762
9763 static inline bool
9764 is_cu_die (dw_die_ref c)
9765 {
9766   return c && c->die_tag == DW_TAG_compile_unit;
9767 }
9768
9769 static char *
9770 gen_internal_sym (const char *prefix)
9771 {
9772   char buf[256];
9773
9774   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9775   return xstrdup (buf);
9776 }
9777
9778 /* Assign symbols to all worthy DIEs under DIE.  */
9779
9780 static void
9781 assign_symbol_names (dw_die_ref die)
9782 {
9783   dw_die_ref c;
9784
9785   if (is_symbol_die (die))
9786     {
9787       if (comdat_symbol_id)
9788         {
9789           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9790
9791           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9792                    comdat_symbol_id, comdat_symbol_number++);
9793           die->die_id.die_symbol = xstrdup (p);
9794         }
9795       else
9796         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9797     }
9798
9799   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9800 }
9801
9802 struct cu_hash_table_entry
9803 {
9804   dw_die_ref cu;
9805   unsigned min_comdat_num, max_comdat_num;
9806   struct cu_hash_table_entry *next;
9807 };
9808
9809 /* Routines to manipulate hash table of CUs.  */
9810 static hashval_t
9811 htab_cu_hash (const void *of)
9812 {
9813   const struct cu_hash_table_entry *const entry =
9814     (const struct cu_hash_table_entry *) of;
9815
9816   return htab_hash_string (entry->cu->die_id.die_symbol);
9817 }
9818
9819 static int
9820 htab_cu_eq (const void *of1, const void *of2)
9821 {
9822   const struct cu_hash_table_entry *const entry1 =
9823     (const struct cu_hash_table_entry *) of1;
9824   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9825
9826   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9827 }
9828
9829 static void
9830 htab_cu_del (void *what)
9831 {
9832   struct cu_hash_table_entry *next,
9833     *entry = (struct cu_hash_table_entry *) what;
9834
9835   while (entry)
9836     {
9837       next = entry->next;
9838       free (entry);
9839       entry = next;
9840     }
9841 }
9842
9843 /* Check whether we have already seen this CU and set up SYM_NUM
9844    accordingly.  */
9845 static int
9846 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9847 {
9848   struct cu_hash_table_entry dummy;
9849   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9850
9851   dummy.max_comdat_num = 0;
9852
9853   slot = (struct cu_hash_table_entry **)
9854     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9855         INSERT);
9856   entry = *slot;
9857
9858   for (; entry; last = entry, entry = entry->next)
9859     {
9860       if (same_die_p_wrap (cu, entry->cu))
9861         break;
9862     }
9863
9864   if (entry)
9865     {
9866       *sym_num = entry->min_comdat_num;
9867       return 1;
9868     }
9869
9870   entry = XCNEW (struct cu_hash_table_entry);
9871   entry->cu = cu;
9872   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9873   entry->next = *slot;
9874   *slot = entry;
9875
9876   return 0;
9877 }
9878
9879 /* Record SYM_NUM to record of CU in HTABLE.  */
9880 static void
9881 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9882 {
9883   struct cu_hash_table_entry **slot, *entry;
9884
9885   slot = (struct cu_hash_table_entry **)
9886     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9887         NO_INSERT);
9888   entry = *slot;
9889
9890   entry->max_comdat_num = sym_num;
9891 }
9892
9893 /* Traverse the DIE (which is always comp_unit_die), and set up
9894    additional compilation units for each of the include files we see
9895    bracketed by BINCL/EINCL.  */
9896
9897 static void
9898 break_out_includes (dw_die_ref die)
9899 {
9900   dw_die_ref c;
9901   dw_die_ref unit = NULL;
9902   limbo_die_node *node, **pnode;
9903   htab_t cu_hash_table;
9904
9905   c = die->die_child;
9906   if (c) do {
9907     dw_die_ref prev = c;
9908     c = c->die_sib;
9909     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9910            || (unit && is_comdat_die (c)))
9911       {
9912         dw_die_ref next = c->die_sib;
9913
9914         /* This DIE is for a secondary CU; remove it from the main one.  */
9915         remove_child_with_prev (c, prev);
9916
9917         if (c->die_tag == DW_TAG_GNU_BINCL)
9918           unit = push_new_compile_unit (unit, c);
9919         else if (c->die_tag == DW_TAG_GNU_EINCL)
9920           unit = pop_compile_unit (unit);
9921         else
9922           add_child_die (unit, c);
9923         c = next;
9924         if (c == die->die_child)
9925           break;
9926       }
9927   } while (c != die->die_child);
9928
9929 #if 0
9930   /* We can only use this in debugging, since the frontend doesn't check
9931      to make sure that we leave every include file we enter.  */
9932   gcc_assert (!unit);
9933 #endif
9934
9935   assign_symbol_names (die);
9936   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9937   for (node = limbo_die_list, pnode = &limbo_die_list;
9938        node;
9939        node = node->next)
9940     {
9941       int is_dupl;
9942
9943       compute_section_prefix (node->die);
9944       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9945                         &comdat_symbol_number);
9946       assign_symbol_names (node->die);
9947       if (is_dupl)
9948         *pnode = node->next;
9949       else
9950         {
9951           pnode = &node->next;
9952           record_comdat_symbol_number (node->die, cu_hash_table,
9953                 comdat_symbol_number);
9954         }
9955     }
9956   htab_delete (cu_hash_table);
9957 }
9958
9959 /* Return non-zero if this DIE is a declaration.  */
9960
9961 static int
9962 is_declaration_die (dw_die_ref die)
9963 {
9964   dw_attr_ref a;
9965   unsigned ix;
9966
9967   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9968     if (a->dw_attr == DW_AT_declaration)
9969       return 1;
9970
9971   return 0;
9972 }
9973
9974 /* Return non-zero if this DIE is nested inside a subprogram.  */
9975
9976 static int
9977 is_nested_in_subprogram (dw_die_ref die)
9978 {
9979   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9980
9981   if (decl == NULL)
9982     decl = die;
9983   return local_scope_p (decl);
9984 }
9985
9986 /* Return non-zero if this is a type DIE that should be moved to a
9987    COMDAT .debug_types section.  */
9988
9989 static int
9990 should_move_die_to_comdat (dw_die_ref die)
9991 {
9992   switch (die->die_tag)
9993     {
9994     case DW_TAG_class_type:
9995     case DW_TAG_structure_type:
9996     case DW_TAG_enumeration_type:
9997     case DW_TAG_union_type:
9998       /* Don't move declarations, inlined instances, or types nested in a
9999          subprogram.  */
10000       if (is_declaration_die (die)
10001           || get_AT (die, DW_AT_abstract_origin)
10002           || is_nested_in_subprogram (die))
10003         return 0;
10004       return 1;
10005     case DW_TAG_array_type:
10006     case DW_TAG_interface_type:
10007     case DW_TAG_pointer_type:
10008     case DW_TAG_reference_type:
10009     case DW_TAG_rvalue_reference_type:
10010     case DW_TAG_string_type:
10011     case DW_TAG_subroutine_type:
10012     case DW_TAG_ptr_to_member_type:
10013     case DW_TAG_set_type:
10014     case DW_TAG_subrange_type:
10015     case DW_TAG_base_type:
10016     case DW_TAG_const_type:
10017     case DW_TAG_file_type:
10018     case DW_TAG_packed_type:
10019     case DW_TAG_volatile_type:
10020     case DW_TAG_typedef:
10021     default:
10022       return 0;
10023     }
10024 }
10025
10026 /* Make a clone of DIE.  */
10027
10028 static dw_die_ref
10029 clone_die (dw_die_ref die)
10030 {
10031   dw_die_ref clone;
10032   dw_attr_ref a;
10033   unsigned ix;
10034
10035   clone = ggc_alloc_cleared_die_node ();
10036   clone->die_tag = die->die_tag;
10037
10038   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10039     add_dwarf_attr (clone, a);
10040
10041   return clone;
10042 }
10043
10044 /* Make a clone of the tree rooted at DIE.  */
10045
10046 static dw_die_ref
10047 clone_tree (dw_die_ref die)
10048 {
10049   dw_die_ref c;
10050   dw_die_ref clone = clone_die (die);
10051
10052   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10053
10054   return clone;
10055 }
10056
10057 /* Make a clone of DIE as a declaration.  */
10058
10059 static dw_die_ref
10060 clone_as_declaration (dw_die_ref die)
10061 {
10062   dw_die_ref clone;
10063   dw_die_ref decl;
10064   dw_attr_ref a;
10065   unsigned ix;
10066
10067   /* If the DIE is already a declaration, just clone it.  */
10068   if (is_declaration_die (die))
10069     return clone_die (die);
10070
10071   /* If the DIE is a specification, just clone its declaration DIE.  */
10072   decl = get_AT_ref (die, DW_AT_specification);
10073   if (decl != NULL)
10074     return clone_die (decl);
10075
10076   clone = ggc_alloc_cleared_die_node ();
10077   clone->die_tag = die->die_tag;
10078
10079   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10080     {
10081       /* We don't want to copy over all attributes.
10082          For example we don't want DW_AT_byte_size because otherwise we will no
10083          longer have a declaration and GDB will treat it as a definition.  */
10084
10085       switch (a->dw_attr)
10086         {
10087         case DW_AT_artificial:
10088         case DW_AT_containing_type:
10089         case DW_AT_external:
10090         case DW_AT_name:
10091         case DW_AT_type:
10092         case DW_AT_virtuality:
10093         case DW_AT_linkage_name:
10094         case DW_AT_MIPS_linkage_name:
10095           add_dwarf_attr (clone, a);
10096           break;
10097         case DW_AT_byte_size:
10098         default:
10099           break;
10100         }
10101     }
10102
10103   if (die->die_id.die_type_node)
10104     add_AT_die_ref (clone, DW_AT_signature, die);
10105
10106   add_AT_flag (clone, DW_AT_declaration, 1);
10107   return clone;
10108 }
10109
10110 /* Copy the declaration context to the new compile unit DIE.  This includes
10111    any surrounding namespace or type declarations.  If the DIE has an
10112    AT_specification attribute, it also includes attributes and children
10113    attached to the specification.  */
10114
10115 static void
10116 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10117 {
10118   dw_die_ref decl;
10119   dw_die_ref new_decl;
10120
10121   decl = get_AT_ref (die, DW_AT_specification);
10122   if (decl == NULL)
10123     decl = die;
10124   else
10125     {
10126       unsigned ix;
10127       dw_die_ref c;
10128       dw_attr_ref a;
10129
10130       /* Copy the type node pointer from the new DIE to the original
10131          declaration DIE so we can forward references later.  */
10132       decl->die_id.die_type_node = die->die_id.die_type_node;
10133
10134       remove_AT (die, DW_AT_specification);
10135
10136       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10137         {
10138           if (a->dw_attr != DW_AT_name
10139               && a->dw_attr != DW_AT_declaration
10140               && a->dw_attr != DW_AT_external)
10141             add_dwarf_attr (die, a);
10142         }
10143
10144       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10145     }
10146
10147   if (decl->die_parent != NULL
10148       && decl->die_parent->die_tag != DW_TAG_compile_unit
10149       && decl->die_parent->die_tag != DW_TAG_type_unit)
10150     {
10151       new_decl = copy_ancestor_tree (unit, decl, NULL);
10152       if (new_decl != NULL)
10153         {
10154           remove_AT (new_decl, DW_AT_signature);
10155           add_AT_specification (die, new_decl);
10156         }
10157     }
10158 }
10159
10160 /* Generate the skeleton ancestor tree for the given NODE, then clone
10161    the DIE and add the clone into the tree.  */
10162
10163 static void
10164 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10165 {
10166   if (node->new_die != NULL)
10167     return;
10168
10169   node->new_die = clone_as_declaration (node->old_die);
10170
10171   if (node->parent != NULL)
10172     {
10173       generate_skeleton_ancestor_tree (node->parent);
10174       add_child_die (node->parent->new_die, node->new_die);
10175     }
10176 }
10177
10178 /* Generate a skeleton tree of DIEs containing any declarations that are
10179    found in the original tree.  We traverse the tree looking for declaration
10180    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10181
10182 static void
10183 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10184 {
10185   skeleton_chain_node node;
10186   dw_die_ref c;
10187   dw_die_ref first;
10188   dw_die_ref prev = NULL;
10189   dw_die_ref next = NULL;
10190
10191   node.parent = parent;
10192
10193   first = c = parent->old_die->die_child;
10194   if (c)
10195     next = c->die_sib;
10196   if (c) do {
10197     if (prev == NULL || prev->die_sib == c)
10198       prev = c;
10199     c = next;
10200     next = (c == first ? NULL : c->die_sib);
10201     node.old_die = c;
10202     node.new_die = NULL;
10203     if (is_declaration_die (c))
10204       {
10205         /* Clone the existing DIE, move the original to the skeleton
10206            tree (which is in the main CU), and put the clone, with
10207            all the original's children, where the original came from.  */
10208         dw_die_ref clone = clone_die (c);
10209         move_all_children (c, clone);
10210
10211         replace_child (c, clone, prev);
10212         generate_skeleton_ancestor_tree (parent);
10213         add_child_die (parent->new_die, c);
10214         node.new_die = c;
10215         c = clone;
10216       }
10217     generate_skeleton_bottom_up (&node);
10218   } while (next != NULL);
10219 }
10220
10221 /* Wrapper function for generate_skeleton_bottom_up.  */
10222
10223 static dw_die_ref
10224 generate_skeleton (dw_die_ref die)
10225 {
10226   skeleton_chain_node node;
10227
10228   node.old_die = die;
10229   node.new_die = NULL;
10230   node.parent = NULL;
10231
10232   /* If this type definition is nested inside another type,
10233      always leave at least a declaration in its place.  */
10234   if (die->die_parent != NULL && is_type_die (die->die_parent))
10235     node.new_die = clone_as_declaration (die);
10236
10237   generate_skeleton_bottom_up (&node);
10238   return node.new_die;
10239 }
10240
10241 /* Remove the DIE from its parent, possibly replacing it with a cloned
10242    declaration.  The original DIE will be moved to a new compile unit
10243    so that existing references to it follow it to the new location.  If
10244    any of the original DIE's descendants is a declaration, we need to
10245    replace the original DIE with a skeleton tree and move the
10246    declarations back into the skeleton tree.  */
10247
10248 static dw_die_ref
10249 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10250 {
10251   dw_die_ref skeleton;
10252
10253   skeleton = generate_skeleton (child);
10254   if (skeleton == NULL)
10255     remove_child_with_prev (child, prev);
10256   else
10257     {
10258       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10259       replace_child (child, skeleton, prev);
10260     }
10261
10262   return skeleton;
10263 }
10264
10265 /* Traverse the DIE and set up additional .debug_types sections for each
10266    type worthy of being placed in a COMDAT section.  */
10267
10268 static void
10269 break_out_comdat_types (dw_die_ref die)
10270 {
10271   dw_die_ref c;
10272   dw_die_ref first;
10273   dw_die_ref prev = NULL;
10274   dw_die_ref next = NULL;
10275   dw_die_ref unit = NULL;
10276
10277   first = c = die->die_child;
10278   if (c)
10279     next = c->die_sib;
10280   if (c) do {
10281     if (prev == NULL || prev->die_sib == c)
10282       prev = c;
10283     c = next;
10284     next = (c == first ? NULL : c->die_sib);
10285     if (should_move_die_to_comdat (c))
10286       {
10287         dw_die_ref replacement;
10288         comdat_type_node_ref type_node;
10289
10290         /* Create a new type unit DIE as the root for the new tree, and
10291            add it to the list of comdat types.  */
10292         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10293         add_AT_unsigned (unit, DW_AT_language,
10294                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10295         type_node = ggc_alloc_cleared_comdat_type_node ();
10296         type_node->root_die = unit;
10297         type_node->next = comdat_type_list;
10298         comdat_type_list = type_node;
10299
10300         /* Generate the type signature.  */
10301         generate_type_signature (c, type_node);
10302
10303         /* Copy the declaration context, attributes, and children of the
10304            declaration into the new compile unit DIE.  */
10305         copy_declaration_context (unit, c);
10306
10307         /* Remove this DIE from the main CU.  */
10308         replacement = remove_child_or_replace_with_skeleton (c, prev);
10309
10310         /* Break out nested types into their own type units.  */
10311         break_out_comdat_types (c);
10312
10313         /* Add the DIE to the new compunit.  */
10314         add_child_die (unit, c);
10315
10316         if (replacement != NULL)
10317           c = replacement;
10318       }
10319     else if (c->die_tag == DW_TAG_namespace
10320              || c->die_tag == DW_TAG_class_type
10321              || c->die_tag == DW_TAG_structure_type
10322              || c->die_tag == DW_TAG_union_type)
10323       {
10324         /* Look for nested types that can be broken out.  */
10325         break_out_comdat_types (c);
10326       }
10327   } while (next != NULL);
10328 }
10329
10330 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10331
10332 struct decl_table_entry
10333 {
10334   dw_die_ref orig;
10335   dw_die_ref copy;
10336 };
10337
10338 /* Routines to manipulate hash table of copied declarations.  */
10339
10340 static hashval_t
10341 htab_decl_hash (const void *of)
10342 {
10343   const struct decl_table_entry *const entry =
10344     (const struct decl_table_entry *) of;
10345
10346   return htab_hash_pointer (entry->orig);
10347 }
10348
10349 static int
10350 htab_decl_eq (const void *of1, const void *of2)
10351 {
10352   const struct decl_table_entry *const entry1 =
10353     (const struct decl_table_entry *) of1;
10354   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10355
10356   return entry1->orig == entry2;
10357 }
10358
10359 static void
10360 htab_decl_del (void *what)
10361 {
10362   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10363
10364   free (entry);
10365 }
10366
10367 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10368    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10369    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10370    to check if the ancestor has already been copied into UNIT.  */
10371
10372 static dw_die_ref
10373 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10374 {
10375   dw_die_ref parent = die->die_parent;
10376   dw_die_ref new_parent = unit;
10377   dw_die_ref copy;
10378   void **slot = NULL;
10379   struct decl_table_entry *entry = NULL;
10380
10381   if (decl_table)
10382     {
10383       /* Check if the entry has already been copied to UNIT.  */
10384       slot = htab_find_slot_with_hash (decl_table, die,
10385                                        htab_hash_pointer (die), INSERT);
10386       if (*slot != HTAB_EMPTY_ENTRY)
10387         {
10388           entry = (struct decl_table_entry *) *slot;
10389           return entry->copy;
10390         }
10391
10392       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10393       entry = XCNEW (struct decl_table_entry);
10394       entry->orig = die;
10395       entry->copy = NULL;
10396       *slot = entry;
10397     }
10398
10399   if (parent != NULL)
10400     {
10401       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10402       if (spec != NULL)
10403         parent = spec;
10404       if (parent->die_tag != DW_TAG_compile_unit
10405           && parent->die_tag != DW_TAG_type_unit)
10406         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10407     }
10408
10409   copy = clone_as_declaration (die);
10410   add_child_die (new_parent, copy);
10411
10412   if (decl_table != NULL)
10413     {
10414       /* Record the pointer to the copy.  */
10415       entry->copy = copy;
10416     }
10417
10418   return copy;
10419 }
10420
10421 /* Walk the DIE and its children, looking for references to incomplete
10422    or trivial types that are unmarked (i.e., that are not in the current
10423    type_unit).  */
10424
10425 static void
10426 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10427 {
10428   dw_die_ref c;
10429   dw_attr_ref a;
10430   unsigned ix;
10431
10432   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10433     {
10434       if (AT_class (a) == dw_val_class_die_ref)
10435         {
10436           dw_die_ref targ = AT_ref (a);
10437           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10438           void **slot;
10439           struct decl_table_entry *entry;
10440
10441           if (targ->die_mark != 0 || type_node != NULL)
10442             continue;
10443
10444           slot = htab_find_slot_with_hash (decl_table, targ,
10445                                            htab_hash_pointer (targ), INSERT);
10446
10447           if (*slot != HTAB_EMPTY_ENTRY)
10448             {
10449               /* TARG has already been copied, so we just need to
10450                  modify the reference to point to the copy.  */
10451               entry = (struct decl_table_entry *) *slot;
10452               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10453             }
10454           else
10455             {
10456               dw_die_ref parent = unit;
10457               dw_die_ref copy = clone_tree (targ);
10458
10459               /* Make sure the cloned tree is marked as part of the
10460                  type unit.  */
10461               mark_dies (copy);
10462
10463               /* Record in DECL_TABLE that TARG has been copied.
10464                  Need to do this now, before the recursive call,
10465                  because DECL_TABLE may be expanded and SLOT
10466                  would no longer be a valid pointer.  */
10467               entry = XCNEW (struct decl_table_entry);
10468               entry->orig = targ;
10469               entry->copy = copy;
10470               *slot = entry;
10471
10472               /* If TARG has surrounding context, copy its ancestor tree
10473                  into the new type unit.  */
10474               if (targ->die_parent != NULL
10475                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10476                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10477                 parent = copy_ancestor_tree (unit, targ->die_parent,
10478                                              decl_table);
10479
10480               add_child_die (parent, copy);
10481               a->dw_attr_val.v.val_die_ref.die = copy;
10482
10483               /* Make sure the newly-copied DIE is walked.  If it was
10484                  installed in a previously-added context, it won't
10485                  get visited otherwise.  */
10486               if (parent != unit)
10487                 {
10488                   /* Find the highest point of the newly-added tree,
10489                      mark each node along the way, and walk from there.  */
10490                   parent->die_mark = 1;
10491                   while (parent->die_parent
10492                          && parent->die_parent->die_mark == 0)
10493                     {
10494                       parent = parent->die_parent;
10495                       parent->die_mark = 1;
10496                     }
10497                   copy_decls_walk (unit, parent, decl_table);
10498                 }
10499             }
10500         }
10501     }
10502
10503   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10504 }
10505
10506 /* Copy declarations for "unworthy" types into the new comdat section.
10507    Incomplete types, modified types, and certain other types aren't broken
10508    out into comdat sections of their own, so they don't have a signature,
10509    and we need to copy the declaration into the same section so that we
10510    don't have an external reference.  */
10511
10512 static void
10513 copy_decls_for_unworthy_types (dw_die_ref unit)
10514 {
10515   htab_t decl_table;
10516
10517   mark_dies (unit);
10518   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10519   copy_decls_walk (unit, unit, decl_table);
10520   htab_delete (decl_table);
10521   unmark_dies (unit);
10522 }
10523
10524 /* Traverse the DIE and add a sibling attribute if it may have the
10525    effect of speeding up access to siblings.  To save some space,
10526    avoid generating sibling attributes for DIE's without children.  */
10527
10528 static void
10529 add_sibling_attributes (dw_die_ref die)
10530 {
10531   dw_die_ref c;
10532
10533   if (! die->die_child)
10534     return;
10535
10536   if (die->die_parent && die != die->die_parent->die_child)
10537     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10538
10539   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10540 }
10541
10542 /* Output all location lists for the DIE and its children.  */
10543
10544 static void
10545 output_location_lists (dw_die_ref die)
10546 {
10547   dw_die_ref c;
10548   dw_attr_ref a;
10549   unsigned ix;
10550
10551   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10552     if (AT_class (a) == dw_val_class_loc_list)
10553       output_loc_list (AT_loc_list (a));
10554
10555   FOR_EACH_CHILD (die, c, output_location_lists (c));
10556 }
10557
10558 /* The format of each DIE (and its attribute value pairs) is encoded in an
10559    abbreviation table.  This routine builds the abbreviation table and assigns
10560    a unique abbreviation id for each abbreviation entry.  The children of each
10561    die are visited recursively.  */
10562
10563 static void
10564 build_abbrev_table (dw_die_ref die)
10565 {
10566   unsigned long abbrev_id;
10567   unsigned int n_alloc;
10568   dw_die_ref c;
10569   dw_attr_ref a;
10570   unsigned ix;
10571
10572   /* Scan the DIE references, and mark as external any that refer to
10573      DIEs from other CUs (i.e. those which are not marked).  */
10574   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10575     if (AT_class (a) == dw_val_class_die_ref
10576         && AT_ref (a)->die_mark == 0)
10577       {
10578         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10579         set_AT_ref_external (a, 1);
10580       }
10581
10582   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10583     {
10584       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10585       dw_attr_ref die_a, abbrev_a;
10586       unsigned ix;
10587       bool ok = true;
10588
10589       if (abbrev->die_tag != die->die_tag)
10590         continue;
10591       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10592         continue;
10593
10594       if (VEC_length (dw_attr_node, abbrev->die_attr)
10595           != VEC_length (dw_attr_node, die->die_attr))
10596         continue;
10597
10598       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10599         {
10600           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10601           if ((abbrev_a->dw_attr != die_a->dw_attr)
10602               || (value_format (abbrev_a) != value_format (die_a)))
10603             {
10604               ok = false;
10605               break;
10606             }
10607         }
10608       if (ok)
10609         break;
10610     }
10611
10612   if (abbrev_id >= abbrev_die_table_in_use)
10613     {
10614       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10615         {
10616           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10617           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10618                                             n_alloc);
10619
10620           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10621                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10622           abbrev_die_table_allocated = n_alloc;
10623         }
10624
10625       ++abbrev_die_table_in_use;
10626       abbrev_die_table[abbrev_id] = die;
10627     }
10628
10629   die->die_abbrev = abbrev_id;
10630   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10631 }
10632 \f
10633 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10634
10635 static int
10636 constant_size (unsigned HOST_WIDE_INT value)
10637 {
10638   int log;
10639
10640   if (value == 0)
10641     log = 0;
10642   else
10643     log = floor_log2 (value);
10644
10645   log = log / 8;
10646   log = 1 << (floor_log2 (log) + 1);
10647
10648   return log;
10649 }
10650
10651 /* Return the size of a DIE as it is represented in the
10652    .debug_info section.  */
10653
10654 static unsigned long
10655 size_of_die (dw_die_ref die)
10656 {
10657   unsigned long size = 0;
10658   dw_attr_ref a;
10659   unsigned ix;
10660
10661   size += size_of_uleb128 (die->die_abbrev);
10662   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10663     {
10664       switch (AT_class (a))
10665         {
10666         case dw_val_class_addr:
10667           size += DWARF2_ADDR_SIZE;
10668           break;
10669         case dw_val_class_offset:
10670           size += DWARF_OFFSET_SIZE;
10671           break;
10672         case dw_val_class_loc:
10673           {
10674             unsigned long lsize = size_of_locs (AT_loc (a));
10675
10676             /* Block length.  */
10677             if (dwarf_version >= 4)
10678               size += size_of_uleb128 (lsize);
10679             else
10680               size += constant_size (lsize);
10681             size += lsize;
10682           }
10683           break;
10684         case dw_val_class_loc_list:
10685           size += DWARF_OFFSET_SIZE;
10686           break;
10687         case dw_val_class_range_list:
10688           size += DWARF_OFFSET_SIZE;
10689           break;
10690         case dw_val_class_const:
10691           size += size_of_sleb128 (AT_int (a));
10692           break;
10693         case dw_val_class_unsigned_const:
10694           size += constant_size (AT_unsigned (a));
10695           break;
10696         case dw_val_class_const_double:
10697           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10698           if (HOST_BITS_PER_WIDE_INT >= 64)
10699             size++; /* block */
10700           break;
10701         case dw_val_class_vec:
10702           size += constant_size (a->dw_attr_val.v.val_vec.length
10703                                  * a->dw_attr_val.v.val_vec.elt_size)
10704                   + a->dw_attr_val.v.val_vec.length
10705                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10706           break;
10707         case dw_val_class_flag:
10708           if (dwarf_version >= 4)
10709             /* Currently all add_AT_flag calls pass in 1 as last argument,
10710                so DW_FORM_flag_present can be used.  If that ever changes,
10711                we'll need to use DW_FORM_flag and have some optimization
10712                in build_abbrev_table that will change those to
10713                DW_FORM_flag_present if it is set to 1 in all DIEs using
10714                the same abbrev entry.  */
10715             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10716           else
10717             size += 1;
10718           break;
10719         case dw_val_class_die_ref:
10720           if (AT_ref_external (a))
10721             {
10722               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10723                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10724                  is sized by target address length, whereas in DWARF3
10725                  it's always sized as an offset.  */
10726               if (dwarf_version >= 4)
10727                 size += DWARF_TYPE_SIGNATURE_SIZE;
10728               else if (dwarf_version == 2)
10729                 size += DWARF2_ADDR_SIZE;
10730               else
10731                 size += DWARF_OFFSET_SIZE;
10732             }
10733           else
10734             size += DWARF_OFFSET_SIZE;
10735           break;
10736         case dw_val_class_fde_ref:
10737           size += DWARF_OFFSET_SIZE;
10738           break;
10739         case dw_val_class_lbl_id:
10740           size += DWARF2_ADDR_SIZE;
10741           break;
10742         case dw_val_class_lineptr:
10743         case dw_val_class_macptr:
10744           size += DWARF_OFFSET_SIZE;
10745           break;
10746         case dw_val_class_str:
10747           if (AT_string_form (a) == DW_FORM_strp)
10748             size += DWARF_OFFSET_SIZE;
10749           else
10750             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10751           break;
10752         case dw_val_class_file:
10753           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10754           break;
10755         case dw_val_class_data8:
10756           size += 8;
10757           break;
10758         case dw_val_class_vms_delta:
10759           size += DWARF_OFFSET_SIZE;
10760           break;
10761         default:
10762           gcc_unreachable ();
10763         }
10764     }
10765
10766   return size;
10767 }
10768
10769 /* Size the debugging information associated with a given DIE.  Visits the
10770    DIE's children recursively.  Updates the global variable next_die_offset, on
10771    each time through.  Uses the current value of next_die_offset to update the
10772    die_offset field in each DIE.  */
10773
10774 static void
10775 calc_die_sizes (dw_die_ref die)
10776 {
10777   dw_die_ref c;
10778
10779   die->die_offset = next_die_offset;
10780   next_die_offset += size_of_die (die);
10781
10782   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10783
10784   if (die->die_child != NULL)
10785     /* Count the null byte used to terminate sibling lists.  */
10786     next_die_offset += 1;
10787 }
10788
10789 /* Set the marks for a die and its children.  We do this so
10790    that we know whether or not a reference needs to use FORM_ref_addr; only
10791    DIEs in the same CU will be marked.  We used to clear out the offset
10792    and use that as the flag, but ran into ordering problems.  */
10793
10794 static void
10795 mark_dies (dw_die_ref die)
10796 {
10797   dw_die_ref c;
10798
10799   gcc_assert (!die->die_mark);
10800
10801   die->die_mark = 1;
10802   FOR_EACH_CHILD (die, c, mark_dies (c));
10803 }
10804
10805 /* Clear the marks for a die and its children.  */
10806
10807 static void
10808 unmark_dies (dw_die_ref die)
10809 {
10810   dw_die_ref c;
10811
10812   if (dwarf_version < 4)
10813     gcc_assert (die->die_mark);
10814
10815   die->die_mark = 0;
10816   FOR_EACH_CHILD (die, c, unmark_dies (c));
10817 }
10818
10819 /* Clear the marks for a die, its children and referred dies.  */
10820
10821 static void
10822 unmark_all_dies (dw_die_ref die)
10823 {
10824   dw_die_ref c;
10825   dw_attr_ref a;
10826   unsigned ix;
10827
10828   if (!die->die_mark)
10829     return;
10830   die->die_mark = 0;
10831
10832   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10833
10834   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10835     if (AT_class (a) == dw_val_class_die_ref)
10836       unmark_all_dies (AT_ref (a));
10837 }
10838
10839 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10840    generated for the compilation unit.  */
10841
10842 static unsigned long
10843 size_of_pubnames (VEC (pubname_entry, gc) * names)
10844 {
10845   unsigned long size;
10846   unsigned i;
10847   pubname_ref p;
10848
10849   size = DWARF_PUBNAMES_HEADER_SIZE;
10850   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10851     if (names != pubtype_table
10852         || p->die->die_offset != 0
10853         || !flag_eliminate_unused_debug_types)
10854       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10855
10856   size += DWARF_OFFSET_SIZE;
10857   return size;
10858 }
10859
10860 /* Return the size of the information in the .debug_aranges section.  */
10861
10862 static unsigned long
10863 size_of_aranges (void)
10864 {
10865   unsigned long size;
10866
10867   size = DWARF_ARANGES_HEADER_SIZE;
10868
10869   /* Count the address/length pair for this compilation unit.  */
10870   if (text_section_used)
10871     size += 2 * DWARF2_ADDR_SIZE;
10872   if (cold_text_section_used)
10873     size += 2 * DWARF2_ADDR_SIZE;
10874   if (have_multiple_function_sections)
10875     {
10876       unsigned fde_idx = 0;
10877
10878       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
10879         {
10880           dw_fde_ref fde = &fde_table[fde_idx];
10881
10882           if (!fde->in_std_section)
10883             size += 2 * DWARF2_ADDR_SIZE;
10884           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
10885             size += 2 * DWARF2_ADDR_SIZE;
10886         }
10887     }
10888
10889   /* Count the two zero words used to terminated the address range table.  */
10890   size += 2 * DWARF2_ADDR_SIZE;
10891   return size;
10892 }
10893 \f
10894 /* Select the encoding of an attribute value.  */
10895
10896 static enum dwarf_form
10897 value_format (dw_attr_ref a)
10898 {
10899   switch (a->dw_attr_val.val_class)
10900     {
10901     case dw_val_class_addr:
10902       /* Only very few attributes allow DW_FORM_addr.  */
10903       switch (a->dw_attr)
10904         {
10905         case DW_AT_low_pc:
10906         case DW_AT_high_pc:
10907         case DW_AT_entry_pc:
10908         case DW_AT_trampoline:
10909           return DW_FORM_addr;
10910         default:
10911           break;
10912         }
10913       switch (DWARF2_ADDR_SIZE)
10914         {
10915         case 1:
10916           return DW_FORM_data1;
10917         case 2:
10918           return DW_FORM_data2;
10919         case 4:
10920           return DW_FORM_data4;
10921         case 8:
10922           return DW_FORM_data8;
10923         default:
10924           gcc_unreachable ();
10925         }
10926     case dw_val_class_range_list:
10927     case dw_val_class_loc_list:
10928       if (dwarf_version >= 4)
10929         return DW_FORM_sec_offset;
10930       /* FALLTHRU */
10931     case dw_val_class_vms_delta:
10932     case dw_val_class_offset:
10933       switch (DWARF_OFFSET_SIZE)
10934         {
10935         case 4:
10936           return DW_FORM_data4;
10937         case 8:
10938           return DW_FORM_data8;
10939         default:
10940           gcc_unreachable ();
10941         }
10942     case dw_val_class_loc:
10943       if (dwarf_version >= 4)
10944         return DW_FORM_exprloc;
10945       switch (constant_size (size_of_locs (AT_loc (a))))
10946         {
10947         case 1:
10948           return DW_FORM_block1;
10949         case 2:
10950           return DW_FORM_block2;
10951         default:
10952           gcc_unreachable ();
10953         }
10954     case dw_val_class_const:
10955       return DW_FORM_sdata;
10956     case dw_val_class_unsigned_const:
10957       switch (constant_size (AT_unsigned (a)))
10958         {
10959         case 1:
10960           return DW_FORM_data1;
10961         case 2:
10962           return DW_FORM_data2;
10963         case 4:
10964           return DW_FORM_data4;
10965         case 8:
10966           return DW_FORM_data8;
10967         default:
10968           gcc_unreachable ();
10969         }
10970     case dw_val_class_const_double:
10971       switch (HOST_BITS_PER_WIDE_INT)
10972         {
10973         case 8:
10974           return DW_FORM_data2;
10975         case 16:
10976           return DW_FORM_data4;
10977         case 32:
10978           return DW_FORM_data8;
10979         case 64:
10980         default:
10981           return DW_FORM_block1;
10982         }
10983     case dw_val_class_vec:
10984       switch (constant_size (a->dw_attr_val.v.val_vec.length
10985                              * a->dw_attr_val.v.val_vec.elt_size))
10986         {
10987         case 1:
10988           return DW_FORM_block1;
10989         case 2:
10990           return DW_FORM_block2;
10991         case 4:
10992           return DW_FORM_block4;
10993         default:
10994           gcc_unreachable ();
10995         }
10996     case dw_val_class_flag:
10997       if (dwarf_version >= 4)
10998         {
10999           /* Currently all add_AT_flag calls pass in 1 as last argument,
11000              so DW_FORM_flag_present can be used.  If that ever changes,
11001              we'll need to use DW_FORM_flag and have some optimization
11002              in build_abbrev_table that will change those to
11003              DW_FORM_flag_present if it is set to 1 in all DIEs using
11004              the same abbrev entry.  */
11005           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11006           return DW_FORM_flag_present;
11007         }
11008       return DW_FORM_flag;
11009     case dw_val_class_die_ref:
11010       if (AT_ref_external (a))
11011         return dwarf_version >= 4 ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11012       else
11013         return DW_FORM_ref;
11014     case dw_val_class_fde_ref:
11015       return DW_FORM_data;
11016     case dw_val_class_lbl_id:
11017       return DW_FORM_addr;
11018     case dw_val_class_lineptr:
11019     case dw_val_class_macptr:
11020       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11021     case dw_val_class_str:
11022       return AT_string_form (a);
11023     case dw_val_class_file:
11024       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11025         {
11026         case 1:
11027           return DW_FORM_data1;
11028         case 2:
11029           return DW_FORM_data2;
11030         case 4:
11031           return DW_FORM_data4;
11032         default:
11033           gcc_unreachable ();
11034         }
11035
11036     case dw_val_class_data8:
11037       return DW_FORM_data8;
11038
11039     default:
11040       gcc_unreachable ();
11041     }
11042 }
11043
11044 /* Output the encoding of an attribute value.  */
11045
11046 static void
11047 output_value_format (dw_attr_ref a)
11048 {
11049   enum dwarf_form form = value_format (a);
11050
11051   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11052 }
11053
11054 /* Output the .debug_abbrev section which defines the DIE abbreviation
11055    table.  */
11056
11057 static void
11058 output_abbrev_section (void)
11059 {
11060   unsigned long abbrev_id;
11061
11062   if (abbrev_die_table_in_use == 1)
11063     return;
11064
11065   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11066     {
11067       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11068       unsigned ix;
11069       dw_attr_ref a_attr;
11070
11071       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11072       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11073                                    dwarf_tag_name (abbrev->die_tag));
11074
11075       if (abbrev->die_child != NULL)
11076         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11077       else
11078         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11079
11080       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11081            ix++)
11082         {
11083           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11084                                        dwarf_attr_name (a_attr->dw_attr));
11085           output_value_format (a_attr);
11086         }
11087
11088       dw2_asm_output_data (1, 0, NULL);
11089       dw2_asm_output_data (1, 0, NULL);
11090     }
11091
11092   /* Terminate the table.  */
11093   dw2_asm_output_data (1, 0, NULL);
11094 }
11095
11096 /* Output a symbol we can use to refer to this DIE from another CU.  */
11097
11098 static inline void
11099 output_die_symbol (dw_die_ref die)
11100 {
11101   char *sym = die->die_id.die_symbol;
11102
11103   if (sym == 0)
11104     return;
11105
11106   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11107     /* We make these global, not weak; if the target doesn't support
11108        .linkonce, it doesn't support combining the sections, so debugging
11109        will break.  */
11110     targetm.asm_out.globalize_label (asm_out_file, sym);
11111
11112   ASM_OUTPUT_LABEL (asm_out_file, sym);
11113 }
11114
11115 /* Return a new location list, given the begin and end range, and the
11116    expression.  */
11117
11118 static inline dw_loc_list_ref
11119 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11120               const char *section)
11121 {
11122   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11123
11124   retlist->begin = begin;
11125   retlist->end = end;
11126   retlist->expr = expr;
11127   retlist->section = section;
11128
11129   return retlist;
11130 }
11131
11132 /* Generate a new internal symbol for this location list node, if it
11133    hasn't got one yet.  */
11134
11135 static inline void
11136 gen_llsym (dw_loc_list_ref list)
11137 {
11138   gcc_assert (!list->ll_symbol);
11139   list->ll_symbol = gen_internal_sym ("LLST");
11140 }
11141
11142 /* Output the location list given to us.  */
11143
11144 static void
11145 output_loc_list (dw_loc_list_ref list_head)
11146 {
11147   dw_loc_list_ref curr = list_head;
11148
11149   if (list_head->emitted)
11150     return;
11151   list_head->emitted = true;
11152
11153   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11154
11155   /* Walk the location list, and output each range + expression.  */
11156   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11157     {
11158       unsigned long size;
11159       /* Don't output an entry that starts and ends at the same address.  */
11160       if (strcmp (curr->begin, curr->end) == 0)
11161         continue;
11162       if (!have_multiple_function_sections)
11163         {
11164           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11165                                 "Location list begin address (%s)",
11166                                 list_head->ll_symbol);
11167           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11168                                 "Location list end address (%s)",
11169                                 list_head->ll_symbol);
11170         }
11171       else
11172         {
11173           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11174                                "Location list begin address (%s)",
11175                                list_head->ll_symbol);
11176           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11177                                "Location list end address (%s)",
11178                                list_head->ll_symbol);
11179         }
11180       size = size_of_locs (curr->expr);
11181
11182       /* Output the block length for this list of location operations.  */
11183       gcc_assert (size <= 0xffff);
11184       dw2_asm_output_data (2, size, "%s", "Location expression size");
11185
11186       output_loc_sequence (curr->expr, -1);
11187     }
11188
11189   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11190                        "Location list terminator begin (%s)",
11191                        list_head->ll_symbol);
11192   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11193                        "Location list terminator end (%s)",
11194                        list_head->ll_symbol);
11195 }
11196
11197 /* Output a type signature.  */
11198
11199 static inline void
11200 output_signature (const char *sig, const char *name)
11201 {
11202   int i;
11203
11204   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11205     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11206 }
11207
11208 /* Output the DIE and its attributes.  Called recursively to generate
11209    the definitions of each child DIE.  */
11210
11211 static void
11212 output_die (dw_die_ref die)
11213 {
11214   dw_attr_ref a;
11215   dw_die_ref c;
11216   unsigned long size;
11217   unsigned ix;
11218
11219   /* If someone in another CU might refer to us, set up a symbol for
11220      them to point to.  */
11221   if (dwarf_version < 4 && die->die_id.die_symbol)
11222     output_die_symbol (die);
11223
11224   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11225                                (unsigned long)die->die_offset,
11226                                dwarf_tag_name (die->die_tag));
11227
11228   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11229     {
11230       const char *name = dwarf_attr_name (a->dw_attr);
11231
11232       switch (AT_class (a))
11233         {
11234         case dw_val_class_addr:
11235           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11236           break;
11237
11238         case dw_val_class_offset:
11239           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11240                                "%s", name);
11241           break;
11242
11243         case dw_val_class_range_list:
11244           {
11245             char *p = strchr (ranges_section_label, '\0');
11246
11247             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11248                      a->dw_attr_val.v.val_offset);
11249             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11250                                    debug_ranges_section, "%s", name);
11251             *p = '\0';
11252           }
11253           break;
11254
11255         case dw_val_class_loc:
11256           size = size_of_locs (AT_loc (a));
11257
11258           /* Output the block length for this list of location operations.  */
11259           if (dwarf_version >= 4)
11260             dw2_asm_output_data_uleb128 (size, "%s", name);
11261           else
11262             dw2_asm_output_data (constant_size (size), size, "%s", name);
11263
11264           output_loc_sequence (AT_loc (a), -1);
11265           break;
11266
11267         case dw_val_class_const:
11268           /* ??? It would be slightly more efficient to use a scheme like is
11269              used for unsigned constants below, but gdb 4.x does not sign
11270              extend.  Gdb 5.x does sign extend.  */
11271           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11272           break;
11273
11274         case dw_val_class_unsigned_const:
11275           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11276                                AT_unsigned (a), "%s", name);
11277           break;
11278
11279         case dw_val_class_const_double:
11280           {
11281             unsigned HOST_WIDE_INT first, second;
11282
11283             if (HOST_BITS_PER_WIDE_INT >= 64)
11284               dw2_asm_output_data (1,
11285                                    2 * HOST_BITS_PER_WIDE_INT
11286                                    / HOST_BITS_PER_CHAR,
11287                                    NULL);
11288
11289             if (WORDS_BIG_ENDIAN)
11290               {
11291                 first = a->dw_attr_val.v.val_double.high;
11292                 second = a->dw_attr_val.v.val_double.low;
11293               }
11294             else
11295               {
11296                 first = a->dw_attr_val.v.val_double.low;
11297                 second = a->dw_attr_val.v.val_double.high;
11298               }
11299
11300             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11301                                  first, name);
11302             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11303                                  second, NULL);
11304           }
11305           break;
11306
11307         case dw_val_class_vec:
11308           {
11309             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11310             unsigned int len = a->dw_attr_val.v.val_vec.length;
11311             unsigned int i;
11312             unsigned char *p;
11313
11314             dw2_asm_output_data (constant_size (len * elt_size),
11315                                  len * elt_size, "%s", name);
11316             if (elt_size > sizeof (HOST_WIDE_INT))
11317               {
11318                 elt_size /= 2;
11319                 len *= 2;
11320               }
11321             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11322                  i < len;
11323                  i++, p += elt_size)
11324               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11325                                    "fp or vector constant word %u", i);
11326             break;
11327           }
11328
11329         case dw_val_class_flag:
11330           if (dwarf_version >= 4)
11331             {
11332               /* Currently all add_AT_flag calls pass in 1 as last argument,
11333                  so DW_FORM_flag_present can be used.  If that ever changes,
11334                  we'll need to use DW_FORM_flag and have some optimization
11335                  in build_abbrev_table that will change those to
11336                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11337                  the same abbrev entry.  */
11338               gcc_assert (AT_flag (a) == 1);
11339               if (flag_debug_asm)
11340                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11341                          ASM_COMMENT_START, name);
11342               break;
11343             }
11344           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11345           break;
11346
11347         case dw_val_class_loc_list:
11348           {
11349             char *sym = AT_loc_list (a)->ll_symbol;
11350
11351             gcc_assert (sym);
11352             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11353                                    "%s", name);
11354           }
11355           break;
11356
11357         case dw_val_class_die_ref:
11358           if (AT_ref_external (a))
11359             {
11360               if (dwarf_version >= 4)
11361                 {
11362                   comdat_type_node_ref type_node =
11363                     AT_ref (a)->die_id.die_type_node;
11364
11365                   gcc_assert (type_node);
11366                   output_signature (type_node->signature, name);
11367                 }
11368               else
11369                 {
11370                   char *sym = AT_ref (a)->die_id.die_symbol;
11371                   int size;
11372
11373                   gcc_assert (sym);
11374                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11375                      length, whereas in DWARF3 it's always sized as an
11376                      offset.  */
11377                   if (dwarf_version == 2)
11378                     size = DWARF2_ADDR_SIZE;
11379                   else
11380                     size = DWARF_OFFSET_SIZE;
11381                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11382                                          name);
11383                 }
11384             }
11385           else
11386             {
11387               gcc_assert (AT_ref (a)->die_offset);
11388               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11389                                    "%s", name);
11390             }
11391           break;
11392
11393         case dw_val_class_fde_ref:
11394           {
11395             char l1[20];
11396
11397             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11398                                          a->dw_attr_val.v.val_fde_index * 2);
11399             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11400                                    "%s", name);
11401           }
11402           break;
11403
11404         case dw_val_class_vms_delta:
11405           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11406                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11407                                     "%s", name);
11408           break;
11409
11410         case dw_val_class_lbl_id:
11411           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11412           break;
11413
11414         case dw_val_class_lineptr:
11415           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11416                                  debug_line_section, "%s", name);
11417           break;
11418
11419         case dw_val_class_macptr:
11420           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11421                                  debug_macinfo_section, "%s", name);
11422           break;
11423
11424         case dw_val_class_str:
11425           if (AT_string_form (a) == DW_FORM_strp)
11426             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11427                                    a->dw_attr_val.v.val_str->label,
11428                                    debug_str_section,
11429                                    "%s: \"%s\"", name, AT_string (a));
11430           else
11431             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11432           break;
11433
11434         case dw_val_class_file:
11435           {
11436             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11437
11438             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11439                                  a->dw_attr_val.v.val_file->filename);
11440             break;
11441           }
11442
11443         case dw_val_class_data8:
11444           {
11445             int i;
11446
11447             for (i = 0; i < 8; i++)
11448               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11449                                    i == 0 ? "%s" : NULL, name);
11450             break;
11451           }
11452
11453         default:
11454           gcc_unreachable ();
11455         }
11456     }
11457
11458   FOR_EACH_CHILD (die, c, output_die (c));
11459
11460   /* Add null byte to terminate sibling list.  */
11461   if (die->die_child != NULL)
11462     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11463                          (unsigned long) die->die_offset);
11464 }
11465
11466 /* Output the compilation unit that appears at the beginning of the
11467    .debug_info section, and precedes the DIE descriptions.  */
11468
11469 static void
11470 output_compilation_unit_header (void)
11471 {
11472   int ver = dwarf_version;
11473
11474   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11475     dw2_asm_output_data (4, 0xffffffff,
11476       "Initial length escape value indicating 64-bit DWARF extension");
11477   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11478                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11479                        "Length of Compilation Unit Info");
11480   dw2_asm_output_data (2, ver, "DWARF version number");
11481   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11482                          debug_abbrev_section,
11483                          "Offset Into Abbrev. Section");
11484   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11485 }
11486
11487 /* Output the compilation unit DIE and its children.  */
11488
11489 static void
11490 output_comp_unit (dw_die_ref die, int output_if_empty)
11491 {
11492   const char *secname;
11493   char *oldsym, *tmp;
11494
11495   /* Unless we are outputting main CU, we may throw away empty ones.  */
11496   if (!output_if_empty && die->die_child == NULL)
11497     return;
11498
11499   /* Even if there are no children of this DIE, we must output the information
11500      about the compilation unit.  Otherwise, on an empty translation unit, we
11501      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11502      will then complain when examining the file.  First mark all the DIEs in
11503      this CU so we know which get local refs.  */
11504   mark_dies (die);
11505
11506   build_abbrev_table (die);
11507
11508   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11509   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11510   calc_die_sizes (die);
11511
11512   oldsym = die->die_id.die_symbol;
11513   if (oldsym)
11514     {
11515       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11516
11517       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11518       secname = tmp;
11519       die->die_id.die_symbol = NULL;
11520       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11521     }
11522   else
11523     {
11524       switch_to_section (debug_info_section);
11525       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11526       info_section_emitted = true;
11527     }
11528
11529   /* Output debugging information.  */
11530   output_compilation_unit_header ();
11531   output_die (die);
11532
11533   /* Leave the marks on the main CU, so we can check them in
11534      output_pubnames.  */
11535   if (oldsym)
11536     {
11537       unmark_dies (die);
11538       die->die_id.die_symbol = oldsym;
11539     }
11540 }
11541
11542 /* Output a comdat type unit DIE and its children.  */
11543
11544 static void
11545 output_comdat_type_unit (comdat_type_node *node)
11546 {
11547   const char *secname;
11548   char *tmp;
11549   int i;
11550 #if defined (OBJECT_FORMAT_ELF)
11551   tree comdat_key;
11552 #endif
11553
11554   /* First mark all the DIEs in this CU so we know which get local refs.  */
11555   mark_dies (node->root_die);
11556
11557   build_abbrev_table (node->root_die);
11558
11559   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11560   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11561   calc_die_sizes (node->root_die);
11562
11563 #if defined (OBJECT_FORMAT_ELF)
11564   secname = ".debug_types";
11565   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11566   sprintf (tmp, "wt.");
11567   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11568     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11569   comdat_key = get_identifier (tmp);
11570   targetm.asm_out.named_section (secname,
11571                                  SECTION_DEBUG | SECTION_LINKONCE,
11572                                  comdat_key);
11573 #else
11574   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11575   sprintf (tmp, ".gnu.linkonce.wt.");
11576   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11577     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11578   secname = tmp;
11579   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11580 #endif
11581
11582   /* Output debugging information.  */
11583   output_compilation_unit_header ();
11584   output_signature (node->signature, "Type Signature");
11585   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11586                        "Offset to Type DIE");
11587   output_die (node->root_die);
11588
11589   unmark_dies (node->root_die);
11590 }
11591
11592 /* Return the DWARF2/3 pubname associated with a decl.  */
11593
11594 static const char *
11595 dwarf2_name (tree decl, int scope)
11596 {
11597   if (DECL_NAMELESS (decl))
11598     return NULL;
11599   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11600 }
11601
11602 /* Add a new entry to .debug_pubnames if appropriate.  */
11603
11604 static void
11605 add_pubname_string (const char *str, dw_die_ref die)
11606 {
11607   if (targetm.want_debug_pub_sections)
11608     {
11609       pubname_entry e;
11610
11611       e.die = die;
11612       e.name = xstrdup (str);
11613       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11614     }
11615 }
11616
11617 static void
11618 add_pubname (tree decl, dw_die_ref die)
11619 {
11620   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11621     {
11622       const char *name = dwarf2_name (decl, 1);
11623       if (name)
11624         add_pubname_string (name, die);
11625     }
11626 }
11627
11628 /* Add a new entry to .debug_pubtypes if appropriate.  */
11629
11630 static void
11631 add_pubtype (tree decl, dw_die_ref die)
11632 {
11633   pubname_entry e;
11634
11635   if (!targetm.want_debug_pub_sections)
11636     return;
11637
11638   e.name = NULL;
11639   if ((TREE_PUBLIC (decl)
11640        || is_cu_die (die->die_parent))
11641       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11642     {
11643       e.die = die;
11644       if (TYPE_P (decl))
11645         {
11646           if (TYPE_NAME (decl))
11647             {
11648               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11649                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11650               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11651                        && DECL_NAME (TYPE_NAME (decl)))
11652                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11653               else
11654                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11655             }
11656         }
11657       else
11658         {
11659           e.name = dwarf2_name (decl, 1);
11660           if (e.name)
11661             e.name = xstrdup (e.name);
11662         }
11663
11664       /* If we don't have a name for the type, there's no point in adding
11665          it to the table.  */
11666       if (e.name && e.name[0] != '\0')
11667         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11668     }
11669 }
11670
11671 /* Output the public names table used to speed up access to externally
11672    visible names; or the public types table used to find type definitions.  */
11673
11674 static void
11675 output_pubnames (VEC (pubname_entry, gc) * names)
11676 {
11677   unsigned i;
11678   unsigned long pubnames_length = size_of_pubnames (names);
11679   pubname_ref pub;
11680
11681   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11682     dw2_asm_output_data (4, 0xffffffff,
11683       "Initial length escape value indicating 64-bit DWARF extension");
11684   if (names == pubname_table)
11685     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11686                          "Length of Public Names Info");
11687   else
11688     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11689                          "Length of Public Type Names Info");
11690   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11691   dw2_asm_output_data (2, 2, "DWARF Version");
11692   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11693                          debug_info_section,
11694                          "Offset of Compilation Unit Info");
11695   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11696                        "Compilation Unit Length");
11697
11698   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11699     {
11700       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11701       if (names == pubname_table)
11702         gcc_assert (pub->die->die_mark);
11703
11704       if (names != pubtype_table
11705           || pub->die->die_offset != 0
11706           || !flag_eliminate_unused_debug_types)
11707         {
11708           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11709                                "DIE offset");
11710
11711           dw2_asm_output_nstring (pub->name, -1, "external name");
11712         }
11713     }
11714
11715   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11716 }
11717
11718 /* Output the information that goes into the .debug_aranges table.
11719    Namely, define the beginning and ending address range of the
11720    text section generated for this compilation unit.  */
11721
11722 static void
11723 output_aranges (unsigned long aranges_length)
11724 {
11725   unsigned i;
11726
11727   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11728     dw2_asm_output_data (4, 0xffffffff,
11729       "Initial length escape value indicating 64-bit DWARF extension");
11730   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11731                        "Length of Address Ranges Info");
11732   /* Version number for aranges is still 2, even in DWARF3.  */
11733   dw2_asm_output_data (2, 2, "DWARF Version");
11734   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11735                          debug_info_section,
11736                          "Offset of Compilation Unit Info");
11737   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11738   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11739
11740   /* We need to align to twice the pointer size here.  */
11741   if (DWARF_ARANGES_PAD_SIZE)
11742     {
11743       /* Pad using a 2 byte words so that padding is correct for any
11744          pointer size.  */
11745       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11746                            2 * DWARF2_ADDR_SIZE);
11747       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11748         dw2_asm_output_data (2, 0, NULL);
11749     }
11750
11751   /* It is necessary not to output these entries if the sections were
11752      not used; if the sections were not used, the length will be 0 and
11753      the address may end up as 0 if the section is discarded by ld
11754      --gc-sections, leaving an invalid (0, 0) entry that can be
11755      confused with the terminator.  */
11756   if (text_section_used)
11757     {
11758       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11759       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11760                             text_section_label, "Length");
11761     }
11762   if (cold_text_section_used)
11763     {
11764       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11765                            "Address");
11766       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11767                             cold_text_section_label, "Length");
11768     }
11769
11770   if (have_multiple_function_sections)
11771     {
11772       unsigned fde_idx = 0;
11773
11774       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11775         {
11776           dw_fde_ref fde = &fde_table[fde_idx];
11777
11778           if (!fde->in_std_section)
11779             {
11780               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
11781                                    "Address");
11782               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
11783                                     fde->dw_fde_begin, "Length");
11784             }
11785           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11786             {
11787               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
11788                                    "Address");
11789               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
11790                                     fde->dw_fde_second_begin, "Length");
11791             }
11792         }
11793     }
11794
11795   /* Output the terminator words.  */
11796   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11797   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11798 }
11799
11800 /* Add a new entry to .debug_ranges.  Return the offset at which it
11801    was placed.  */
11802
11803 static unsigned int
11804 add_ranges_num (int num)
11805 {
11806   unsigned int in_use = ranges_table_in_use;
11807
11808   if (in_use == ranges_table_allocated)
11809     {
11810       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11811       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11812                                     ranges_table_allocated);
11813       memset (ranges_table + ranges_table_in_use, 0,
11814               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11815     }
11816
11817   ranges_table[in_use].num = num;
11818   ranges_table_in_use = in_use + 1;
11819
11820   return in_use * 2 * DWARF2_ADDR_SIZE;
11821 }
11822
11823 /* Add a new entry to .debug_ranges corresponding to a block, or a
11824    range terminator if BLOCK is NULL.  */
11825
11826 static unsigned int
11827 add_ranges (const_tree block)
11828 {
11829   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11830 }
11831
11832 /* Add a new entry to .debug_ranges corresponding to a pair of
11833    labels.  */
11834
11835 static void
11836 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11837                       bool *added)
11838 {
11839   unsigned int in_use = ranges_by_label_in_use;
11840   unsigned int offset;
11841
11842   if (in_use == ranges_by_label_allocated)
11843     {
11844       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11845       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11846                                        ranges_by_label,
11847                                        ranges_by_label_allocated);
11848       memset (ranges_by_label + ranges_by_label_in_use, 0,
11849               RANGES_TABLE_INCREMENT
11850               * sizeof (struct dw_ranges_by_label_struct));
11851     }
11852
11853   ranges_by_label[in_use].begin = begin;
11854   ranges_by_label[in_use].end = end;
11855   ranges_by_label_in_use = in_use + 1;
11856
11857   offset = add_ranges_num (-(int)in_use - 1);
11858   if (!*added)
11859     {
11860       add_AT_range_list (die, DW_AT_ranges, offset);
11861       *added = true;
11862     }
11863 }
11864
11865 static void
11866 output_ranges (void)
11867 {
11868   unsigned i;
11869   static const char *const start_fmt = "Offset %#x";
11870   const char *fmt = start_fmt;
11871
11872   for (i = 0; i < ranges_table_in_use; i++)
11873     {
11874       int block_num = ranges_table[i].num;
11875
11876       if (block_num > 0)
11877         {
11878           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11879           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11880
11881           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11882           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11883
11884           /* If all code is in the text section, then the compilation
11885              unit base address defaults to DW_AT_low_pc, which is the
11886              base of the text section.  */
11887           if (!have_multiple_function_sections)
11888             {
11889               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11890                                     text_section_label,
11891                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11892               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11893                                     text_section_label, NULL);
11894             }
11895
11896           /* Otherwise, the compilation unit base address is zero,
11897              which allows us to use absolute addresses, and not worry
11898              about whether the target supports cross-section
11899              arithmetic.  */
11900           else
11901             {
11902               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11903                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11904               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11905             }
11906
11907           fmt = NULL;
11908         }
11909
11910       /* Negative block_num stands for an index into ranges_by_label.  */
11911       else if (block_num < 0)
11912         {
11913           int lab_idx = - block_num - 1;
11914
11915           if (!have_multiple_function_sections)
11916             {
11917               gcc_unreachable ();
11918 #if 0
11919               /* If we ever use add_ranges_by_labels () for a single
11920                  function section, all we have to do is to take out
11921                  the #if 0 above.  */
11922               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11923                                     ranges_by_label[lab_idx].begin,
11924                                     text_section_label,
11925                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11926               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11927                                     ranges_by_label[lab_idx].end,
11928                                     text_section_label, NULL);
11929 #endif
11930             }
11931           else
11932             {
11933               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11934                                    ranges_by_label[lab_idx].begin,
11935                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11936               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11937                                    ranges_by_label[lab_idx].end,
11938                                    NULL);
11939             }
11940         }
11941       else
11942         {
11943           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11944           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11945           fmt = start_fmt;
11946         }
11947     }
11948 }
11949
11950 /* Data structure containing information about input files.  */
11951 struct file_info
11952 {
11953   const char *path;     /* Complete file name.  */
11954   const char *fname;    /* File name part.  */
11955   int length;           /* Length of entire string.  */
11956   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11957   int dir_idx;          /* Index in directory table.  */
11958 };
11959
11960 /* Data structure containing information about directories with source
11961    files.  */
11962 struct dir_info
11963 {
11964   const char *path;     /* Path including directory name.  */
11965   int length;           /* Path length.  */
11966   int prefix;           /* Index of directory entry which is a prefix.  */
11967   int count;            /* Number of files in this directory.  */
11968   int dir_idx;          /* Index of directory used as base.  */
11969 };
11970
11971 /* Callback function for file_info comparison.  We sort by looking at
11972    the directories in the path.  */
11973
11974 static int
11975 file_info_cmp (const void *p1, const void *p2)
11976 {
11977   const struct file_info *const s1 = (const struct file_info *) p1;
11978   const struct file_info *const s2 = (const struct file_info *) p2;
11979   const unsigned char *cp1;
11980   const unsigned char *cp2;
11981
11982   /* Take care of file names without directories.  We need to make sure that
11983      we return consistent values to qsort since some will get confused if
11984      we return the same value when identical operands are passed in opposite
11985      orders.  So if neither has a directory, return 0 and otherwise return
11986      1 or -1 depending on which one has the directory.  */
11987   if ((s1->path == s1->fname || s2->path == s2->fname))
11988     return (s2->path == s2->fname) - (s1->path == s1->fname);
11989
11990   cp1 = (const unsigned char *) s1->path;
11991   cp2 = (const unsigned char *) s2->path;
11992
11993   while (1)
11994     {
11995       ++cp1;
11996       ++cp2;
11997       /* Reached the end of the first path?  If so, handle like above.  */
11998       if ((cp1 == (const unsigned char *) s1->fname)
11999           || (cp2 == (const unsigned char *) s2->fname))
12000         return ((cp2 == (const unsigned char *) s2->fname)
12001                 - (cp1 == (const unsigned char *) s1->fname));
12002
12003       /* Character of current path component the same?  */
12004       else if (*cp1 != *cp2)
12005         return *cp1 - *cp2;
12006     }
12007 }
12008
12009 struct file_name_acquire_data
12010 {
12011   struct file_info *files;
12012   int used_files;
12013   int max_files;
12014 };
12015
12016 /* Traversal function for the hash table.  */
12017
12018 static int
12019 file_name_acquire (void ** slot, void *data)
12020 {
12021   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12022   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12023   struct file_info *fi;
12024   const char *f;
12025
12026   gcc_assert (fnad->max_files >= d->emitted_number);
12027
12028   if (! d->emitted_number)
12029     return 1;
12030
12031   gcc_assert (fnad->max_files != fnad->used_files);
12032
12033   fi = fnad->files + fnad->used_files++;
12034
12035   /* Skip all leading "./".  */
12036   f = d->filename;
12037   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12038     f += 2;
12039
12040   /* Create a new array entry.  */
12041   fi->path = f;
12042   fi->length = strlen (f);
12043   fi->file_idx = d;
12044
12045   /* Search for the file name part.  */
12046   f = strrchr (f, DIR_SEPARATOR);
12047 #if defined (DIR_SEPARATOR_2)
12048   {
12049     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12050
12051     if (g != NULL)
12052       {
12053         if (f == NULL || f < g)
12054           f = g;
12055       }
12056   }
12057 #endif
12058
12059   fi->fname = f == NULL ? fi->path : f + 1;
12060   return 1;
12061 }
12062
12063 /* Output the directory table and the file name table.  We try to minimize
12064    the total amount of memory needed.  A heuristic is used to avoid large
12065    slowdowns with many input files.  */
12066
12067 static void
12068 output_file_names (void)
12069 {
12070   struct file_name_acquire_data fnad;
12071   int numfiles;
12072   struct file_info *files;
12073   struct dir_info *dirs;
12074   int *saved;
12075   int *savehere;
12076   int *backmap;
12077   int ndirs;
12078   int idx_offset;
12079   int i;
12080
12081   if (!last_emitted_file)
12082     {
12083       dw2_asm_output_data (1, 0, "End directory table");
12084       dw2_asm_output_data (1, 0, "End file name table");
12085       return;
12086     }
12087
12088   numfiles = last_emitted_file->emitted_number;
12089
12090   /* Allocate the various arrays we need.  */
12091   files = XALLOCAVEC (struct file_info, numfiles);
12092   dirs = XALLOCAVEC (struct dir_info, numfiles);
12093
12094   fnad.files = files;
12095   fnad.used_files = 0;
12096   fnad.max_files = numfiles;
12097   htab_traverse (file_table, file_name_acquire, &fnad);
12098   gcc_assert (fnad.used_files == fnad.max_files);
12099
12100   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12101
12102   /* Find all the different directories used.  */
12103   dirs[0].path = files[0].path;
12104   dirs[0].length = files[0].fname - files[0].path;
12105   dirs[0].prefix = -1;
12106   dirs[0].count = 1;
12107   dirs[0].dir_idx = 0;
12108   files[0].dir_idx = 0;
12109   ndirs = 1;
12110
12111   for (i = 1; i < numfiles; i++)
12112     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12113         && memcmp (dirs[ndirs - 1].path, files[i].path,
12114                    dirs[ndirs - 1].length) == 0)
12115       {
12116         /* Same directory as last entry.  */
12117         files[i].dir_idx = ndirs - 1;
12118         ++dirs[ndirs - 1].count;
12119       }
12120     else
12121       {
12122         int j;
12123
12124         /* This is a new directory.  */
12125         dirs[ndirs].path = files[i].path;
12126         dirs[ndirs].length = files[i].fname - files[i].path;
12127         dirs[ndirs].count = 1;
12128         dirs[ndirs].dir_idx = ndirs;
12129         files[i].dir_idx = ndirs;
12130
12131         /* Search for a prefix.  */
12132         dirs[ndirs].prefix = -1;
12133         for (j = 0; j < ndirs; j++)
12134           if (dirs[j].length < dirs[ndirs].length
12135               && dirs[j].length > 1
12136               && (dirs[ndirs].prefix == -1
12137                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12138               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12139             dirs[ndirs].prefix = j;
12140
12141         ++ndirs;
12142       }
12143
12144   /* Now to the actual work.  We have to find a subset of the directories which
12145      allow expressing the file name using references to the directory table
12146      with the least amount of characters.  We do not do an exhaustive search
12147      where we would have to check out every combination of every single
12148      possible prefix.  Instead we use a heuristic which provides nearly optimal
12149      results in most cases and never is much off.  */
12150   saved = XALLOCAVEC (int, ndirs);
12151   savehere = XALLOCAVEC (int, ndirs);
12152
12153   memset (saved, '\0', ndirs * sizeof (saved[0]));
12154   for (i = 0; i < ndirs; i++)
12155     {
12156       int j;
12157       int total;
12158
12159       /* We can always save some space for the current directory.  But this
12160          does not mean it will be enough to justify adding the directory.  */
12161       savehere[i] = dirs[i].length;
12162       total = (savehere[i] - saved[i]) * dirs[i].count;
12163
12164       for (j = i + 1; j < ndirs; j++)
12165         {
12166           savehere[j] = 0;
12167           if (saved[j] < dirs[i].length)
12168             {
12169               /* Determine whether the dirs[i] path is a prefix of the
12170                  dirs[j] path.  */
12171               int k;
12172
12173               k = dirs[j].prefix;
12174               while (k != -1 && k != (int) i)
12175                 k = dirs[k].prefix;
12176
12177               if (k == (int) i)
12178                 {
12179                   /* Yes it is.  We can possibly save some memory by
12180                      writing the filenames in dirs[j] relative to
12181                      dirs[i].  */
12182                   savehere[j] = dirs[i].length;
12183                   total += (savehere[j] - saved[j]) * dirs[j].count;
12184                 }
12185             }
12186         }
12187
12188       /* Check whether we can save enough to justify adding the dirs[i]
12189          directory.  */
12190       if (total > dirs[i].length + 1)
12191         {
12192           /* It's worthwhile adding.  */
12193           for (j = i; j < ndirs; j++)
12194             if (savehere[j] > 0)
12195               {
12196                 /* Remember how much we saved for this directory so far.  */
12197                 saved[j] = savehere[j];
12198
12199                 /* Remember the prefix directory.  */
12200                 dirs[j].dir_idx = i;
12201               }
12202         }
12203     }
12204
12205   /* Emit the directory name table.  */
12206   idx_offset = dirs[0].length > 0 ? 1 : 0;
12207   for (i = 1 - idx_offset; i < ndirs; i++)
12208     dw2_asm_output_nstring (dirs[i].path,
12209                             dirs[i].length
12210                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12211                             "Directory Entry: %#x", i + idx_offset);
12212
12213   dw2_asm_output_data (1, 0, "End directory table");
12214
12215   /* We have to emit them in the order of emitted_number since that's
12216      used in the debug info generation.  To do this efficiently we
12217      generate a back-mapping of the indices first.  */
12218   backmap = XALLOCAVEC (int, numfiles);
12219   for (i = 0; i < numfiles; i++)
12220     backmap[files[i].file_idx->emitted_number - 1] = i;
12221
12222   /* Now write all the file names.  */
12223   for (i = 0; i < numfiles; i++)
12224     {
12225       int file_idx = backmap[i];
12226       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12227
12228 #ifdef VMS_DEBUGGING_INFO
12229 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12230
12231       /* Setting these fields can lead to debugger miscomparisons,
12232          but VMS Debug requires them to be set correctly.  */
12233
12234       int ver;
12235       long long cdt;
12236       long siz;
12237       int maxfilelen = strlen (files[file_idx].path)
12238                                + dirs[dir_idx].length
12239                                + MAX_VMS_VERSION_LEN + 1;
12240       char *filebuf = XALLOCAVEC (char, maxfilelen);
12241
12242       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12243       snprintf (filebuf, maxfilelen, "%s;%d",
12244                 files[file_idx].path + dirs[dir_idx].length, ver);
12245
12246       dw2_asm_output_nstring
12247         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12248
12249       /* Include directory index.  */
12250       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12251
12252       /* Modification time.  */
12253       dw2_asm_output_data_uleb128
12254         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12255           ? cdt : 0,
12256          NULL);
12257
12258       /* File length in bytes.  */
12259       dw2_asm_output_data_uleb128
12260         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12261           ? siz : 0,
12262          NULL);
12263 #else
12264       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12265                               "File Entry: %#x", (unsigned) i + 1);
12266
12267       /* Include directory index.  */
12268       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12269
12270       /* Modification time.  */
12271       dw2_asm_output_data_uleb128 (0, NULL);
12272
12273       /* File length in bytes.  */
12274       dw2_asm_output_data_uleb128 (0, NULL);
12275 #endif /* VMS_DEBUGGING_INFO */
12276     }
12277
12278   dw2_asm_output_data (1, 0, "End file name table");
12279 }
12280
12281
12282 /* Output the source line number correspondence information.  This
12283    information goes into the .debug_line section.  */
12284
12285 static void
12286 output_line_info (void)
12287 {
12288   char l1[20], l2[20], p1[20], p2[20];
12289   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12290   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12291   unsigned opc;
12292   unsigned n_op_args;
12293   unsigned long lt_index;
12294   unsigned long current_line;
12295   long line_offset;
12296   long line_delta;
12297   unsigned long current_file;
12298   unsigned long function;
12299   int ver = dwarf_version;
12300
12301   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12302   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12303   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12304   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12305
12306   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12307     dw2_asm_output_data (4, 0xffffffff,
12308       "Initial length escape value indicating 64-bit DWARF extension");
12309   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12310                         "Length of Source Line Info");
12311   ASM_OUTPUT_LABEL (asm_out_file, l1);
12312
12313   dw2_asm_output_data (2, ver, "DWARF Version");
12314   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12315   ASM_OUTPUT_LABEL (asm_out_file, p1);
12316
12317   /* Define the architecture-dependent minimum instruction length (in
12318    bytes).  In this implementation of DWARF, this field is used for
12319    information purposes only.  Since GCC generates assembly language,
12320    we have no a priori knowledge of how many instruction bytes are
12321    generated for each source line, and therefore can use only the
12322    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12323    commands.  Accordingly, we fix this as `1', which is "correct
12324    enough" for all architectures, and don't let the target override.  */
12325   dw2_asm_output_data (1, 1,
12326                        "Minimum Instruction Length");
12327
12328   if (ver >= 4)
12329     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12330                          "Maximum Operations Per Instruction");
12331   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12332                        "Default is_stmt_start flag");
12333   dw2_asm_output_data (1, DWARF_LINE_BASE,
12334                        "Line Base Value (Special Opcodes)");
12335   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12336                        "Line Range Value (Special Opcodes)");
12337   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12338                        "Special Opcode Base");
12339
12340   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12341     {
12342       switch (opc)
12343         {
12344         case DW_LNS_advance_pc:
12345         case DW_LNS_advance_line:
12346         case DW_LNS_set_file:
12347         case DW_LNS_set_column:
12348         case DW_LNS_fixed_advance_pc:
12349           n_op_args = 1;
12350           break;
12351         default:
12352           n_op_args = 0;
12353           break;
12354         }
12355
12356       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12357                            opc, n_op_args);
12358     }
12359
12360   /* Write out the information about the files we use.  */
12361   output_file_names ();
12362   ASM_OUTPUT_LABEL (asm_out_file, p2);
12363
12364   /* We used to set the address register to the first location in the text
12365      section here, but that didn't accomplish anything since we already
12366      have a line note for the opening brace of the first function.  */
12367
12368   /* Generate the line number to PC correspondence table, encoded as
12369      a series of state machine operations.  */
12370   current_file = 1;
12371   current_line = 1;
12372
12373   if (cfun && in_cold_section_p)
12374     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12375   else
12376     strcpy (prev_line_label, text_section_label);
12377   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12378     {
12379       dw_line_info_ref line_info = &line_info_table[lt_index];
12380
12381 #if 0
12382       /* Disable this optimization for now; GDB wants to see two line notes
12383          at the beginning of a function so it can find the end of the
12384          prologue.  */
12385
12386       /* Don't emit anything for redundant notes.  Just updating the
12387          address doesn't accomplish anything, because we already assume
12388          that anything after the last address is this line.  */
12389       if (line_info->dw_line_num == current_line
12390           && line_info->dw_file_num == current_file)
12391         continue;
12392 #endif
12393
12394       /* Emit debug info for the address of the current line.
12395
12396          Unfortunately, we have little choice here currently, and must always
12397          use the most general form.  GCC does not know the address delta
12398          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12399          attributes which will give an upper bound on the address range.  We
12400          could perhaps use length attributes to determine when it is safe to
12401          use DW_LNS_fixed_advance_pc.  */
12402
12403       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12404       if (0)
12405         {
12406           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12407           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12408                                "DW_LNS_fixed_advance_pc");
12409           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12410         }
12411       else
12412         {
12413           /* This can handle any delta.  This takes
12414              4+DWARF2_ADDR_SIZE bytes.  */
12415           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12416           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12417           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12418           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12419         }
12420
12421       strcpy (prev_line_label, line_label);
12422
12423       /* Emit debug info for the source file of the current line, if
12424          different from the previous line.  */
12425       if (line_info->dw_file_num != current_file)
12426         {
12427           current_file = line_info->dw_file_num;
12428           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12429           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12430         }
12431
12432       /* Emit debug info for the current line number, choosing the encoding
12433          that uses the least amount of space.  */
12434       if (line_info->dw_line_num != current_line)
12435         {
12436           line_offset = line_info->dw_line_num - current_line;
12437           line_delta = line_offset - DWARF_LINE_BASE;
12438           current_line = line_info->dw_line_num;
12439           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12440             /* This can handle deltas from -10 to 234, using the current
12441                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12442                takes 1 byte.  */
12443             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12444                                  "line %lu", current_line);
12445           else
12446             {
12447               /* This can handle any delta.  This takes at least 4 bytes,
12448                  depending on the value being encoded.  */
12449               dw2_asm_output_data (1, DW_LNS_advance_line,
12450                                    "advance to line %lu", current_line);
12451               dw2_asm_output_data_sleb128 (line_offset, NULL);
12452               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12453             }
12454         }
12455       else
12456         /* We still need to start a new row, so output a copy insn.  */
12457         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12458     }
12459
12460   /* Emit debug info for the address of the end of the function.  */
12461   if (0)
12462     {
12463       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12464                            "DW_LNS_fixed_advance_pc");
12465       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12466     }
12467   else
12468     {
12469       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12470       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12471       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12472       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12473     }
12474
12475   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12476   dw2_asm_output_data_uleb128 (1, NULL);
12477   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12478
12479   function = 0;
12480   current_file = 1;
12481   current_line = 1;
12482   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12483     {
12484       dw_separate_line_info_ref line_info
12485         = &separate_line_info_table[lt_index];
12486
12487 #if 0
12488       /* Don't emit anything for redundant notes.  */
12489       if (line_info->dw_line_num == current_line
12490           && line_info->dw_file_num == current_file
12491           && line_info->function == function)
12492         goto cont;
12493 #endif
12494
12495       /* Emit debug info for the address of the current line.  If this is
12496          a new function, or the first line of a function, then we need
12497          to handle it differently.  */
12498       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12499                                    lt_index);
12500       if (function != line_info->function)
12501         {
12502           function = line_info->function;
12503
12504           /* Set the address register to the first line in the function.  */
12505           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12506           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12507           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12508           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12509         }
12510       else
12511         {
12512           /* ??? See the DW_LNS_advance_pc comment above.  */
12513           if (0)
12514             {
12515               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12516                                    "DW_LNS_fixed_advance_pc");
12517               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12518             }
12519           else
12520             {
12521               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12522               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12523               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12524               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12525             }
12526         }
12527
12528       strcpy (prev_line_label, line_label);
12529
12530       /* Emit debug info for the source file of the current line, if
12531          different from the previous line.  */
12532       if (line_info->dw_file_num != current_file)
12533         {
12534           current_file = line_info->dw_file_num;
12535           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12536           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12537         }
12538
12539       /* Emit debug info for the current line number, choosing the encoding
12540          that uses the least amount of space.  */
12541       if (line_info->dw_line_num != current_line)
12542         {
12543           line_offset = line_info->dw_line_num - current_line;
12544           line_delta = line_offset - DWARF_LINE_BASE;
12545           current_line = line_info->dw_line_num;
12546           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12547             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12548                                  "line %lu", current_line);
12549           else
12550             {
12551               dw2_asm_output_data (1, DW_LNS_advance_line,
12552                                    "advance to line %lu", current_line);
12553               dw2_asm_output_data_sleb128 (line_offset, NULL);
12554               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12555             }
12556         }
12557       else
12558         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12559
12560 #if 0
12561     cont:
12562 #endif
12563
12564       lt_index++;
12565
12566       /* If we're done with a function, end its sequence.  */
12567       if (lt_index == separate_line_info_table_in_use
12568           || separate_line_info_table[lt_index].function != function)
12569         {
12570           current_file = 1;
12571           current_line = 1;
12572
12573           /* Emit debug info for the address of the end of the function.  */
12574           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12575           if (0)
12576             {
12577               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12578                                    "DW_LNS_fixed_advance_pc");
12579               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12580             }
12581           else
12582             {
12583               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12584               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12585               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12586               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12587             }
12588
12589           /* Output the marker for the end of this sequence.  */
12590           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12591           dw2_asm_output_data_uleb128 (1, NULL);
12592           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12593         }
12594     }
12595
12596   /* Output the marker for the end of the line number info.  */
12597   ASM_OUTPUT_LABEL (asm_out_file, l2);
12598 }
12599 \f
12600 /* Given a pointer to a tree node for some base type, return a pointer to
12601    a DIE that describes the given type.
12602
12603    This routine must only be called for GCC type nodes that correspond to
12604    Dwarf base (fundamental) types.  */
12605
12606 static dw_die_ref
12607 base_type_die (tree type)
12608 {
12609   dw_die_ref base_type_result;
12610   enum dwarf_type encoding;
12611
12612   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12613     return 0;
12614
12615   /* If this is a subtype that should not be emitted as a subrange type,
12616      use the base type.  See subrange_type_for_debug_p.  */
12617   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12618     type = TREE_TYPE (type);
12619
12620   switch (TREE_CODE (type))
12621     {
12622     case INTEGER_TYPE:
12623       if ((dwarf_version >= 4 || !dwarf_strict)
12624           && TYPE_NAME (type)
12625           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12626           && DECL_IS_BUILTIN (TYPE_NAME (type))
12627           && DECL_NAME (TYPE_NAME (type)))
12628         {
12629           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12630           if (strcmp (name, "char16_t") == 0
12631               || strcmp (name, "char32_t") == 0)
12632             {
12633               encoding = DW_ATE_UTF;
12634               break;
12635             }
12636         }
12637       if (TYPE_STRING_FLAG (type))
12638         {
12639           if (TYPE_UNSIGNED (type))
12640             encoding = DW_ATE_unsigned_char;
12641           else
12642             encoding = DW_ATE_signed_char;
12643         }
12644       else if (TYPE_UNSIGNED (type))
12645         encoding = DW_ATE_unsigned;
12646       else
12647         encoding = DW_ATE_signed;
12648       break;
12649
12650     case REAL_TYPE:
12651       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12652         {
12653           if (dwarf_version >= 3 || !dwarf_strict)
12654             encoding = DW_ATE_decimal_float;
12655           else
12656             encoding = DW_ATE_lo_user;
12657         }
12658       else
12659         encoding = DW_ATE_float;
12660       break;
12661
12662     case FIXED_POINT_TYPE:
12663       if (!(dwarf_version >= 3 || !dwarf_strict))
12664         encoding = DW_ATE_lo_user;
12665       else if (TYPE_UNSIGNED (type))
12666         encoding = DW_ATE_unsigned_fixed;
12667       else
12668         encoding = DW_ATE_signed_fixed;
12669       break;
12670
12671       /* Dwarf2 doesn't know anything about complex ints, so use
12672          a user defined type for it.  */
12673     case COMPLEX_TYPE:
12674       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12675         encoding = DW_ATE_complex_float;
12676       else
12677         encoding = DW_ATE_lo_user;
12678       break;
12679
12680     case BOOLEAN_TYPE:
12681       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12682       encoding = DW_ATE_boolean;
12683       break;
12684
12685     default:
12686       /* No other TREE_CODEs are Dwarf fundamental types.  */
12687       gcc_unreachable ();
12688     }
12689
12690   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12691
12692   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12693                    int_size_in_bytes (type));
12694   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12695
12696   return base_type_result;
12697 }
12698
12699 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12700    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12701
12702 static inline int
12703 is_base_type (tree type)
12704 {
12705   switch (TREE_CODE (type))
12706     {
12707     case ERROR_MARK:
12708     case VOID_TYPE:
12709     case INTEGER_TYPE:
12710     case REAL_TYPE:
12711     case FIXED_POINT_TYPE:
12712     case COMPLEX_TYPE:
12713     case BOOLEAN_TYPE:
12714       return 1;
12715
12716     case ARRAY_TYPE:
12717     case RECORD_TYPE:
12718     case UNION_TYPE:
12719     case QUAL_UNION_TYPE:
12720     case ENUMERAL_TYPE:
12721     case FUNCTION_TYPE:
12722     case METHOD_TYPE:
12723     case POINTER_TYPE:
12724     case REFERENCE_TYPE:
12725     case NULLPTR_TYPE:
12726     case OFFSET_TYPE:
12727     case LANG_TYPE:
12728     case VECTOR_TYPE:
12729       return 0;
12730
12731     default:
12732       gcc_unreachable ();
12733     }
12734
12735   return 0;
12736 }
12737
12738 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12739    node, return the size in bits for the type if it is a constant, or else
12740    return the alignment for the type if the type's size is not constant, or
12741    else return BITS_PER_WORD if the type actually turns out to be an
12742    ERROR_MARK node.  */
12743
12744 static inline unsigned HOST_WIDE_INT
12745 simple_type_size_in_bits (const_tree type)
12746 {
12747   if (TREE_CODE (type) == ERROR_MARK)
12748     return BITS_PER_WORD;
12749   else if (TYPE_SIZE (type) == NULL_TREE)
12750     return 0;
12751   else if (host_integerp (TYPE_SIZE (type), 1))
12752     return tree_low_cst (TYPE_SIZE (type), 1);
12753   else
12754     return TYPE_ALIGN (type);
12755 }
12756
12757 /* Similarly, but return a double_int instead of UHWI.  */
12758
12759 static inline double_int
12760 double_int_type_size_in_bits (const_tree type)
12761 {
12762   if (TREE_CODE (type) == ERROR_MARK)
12763     return uhwi_to_double_int (BITS_PER_WORD);
12764   else if (TYPE_SIZE (type) == NULL_TREE)
12765     return double_int_zero;
12766   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12767     return tree_to_double_int (TYPE_SIZE (type));
12768   else
12769     return uhwi_to_double_int (TYPE_ALIGN (type));
12770 }
12771
12772 /*  Given a pointer to a tree node for a subrange type, return a pointer
12773     to a DIE that describes the given type.  */
12774
12775 static dw_die_ref
12776 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12777 {
12778   dw_die_ref subrange_die;
12779   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12780
12781   if (context_die == NULL)
12782     context_die = comp_unit_die ();
12783
12784   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12785
12786   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12787     {
12788       /* The size of the subrange type and its base type do not match,
12789          so we need to generate a size attribute for the subrange type.  */
12790       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12791     }
12792
12793   if (low)
12794     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12795   if (high)
12796     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12797
12798   return subrange_die;
12799 }
12800
12801 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12802    entry that chains various modifiers in front of the given type.  */
12803
12804 static dw_die_ref
12805 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12806                    dw_die_ref context_die)
12807 {
12808   enum tree_code code = TREE_CODE (type);
12809   dw_die_ref mod_type_die;
12810   dw_die_ref sub_die = NULL;
12811   tree item_type = NULL;
12812   tree qualified_type;
12813   tree name, low, high;
12814
12815   if (code == ERROR_MARK)
12816     return NULL;
12817
12818   /* See if we already have the appropriately qualified variant of
12819      this type.  */
12820   qualified_type
12821     = get_qualified_type (type,
12822                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12823                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12824
12825   if (qualified_type == sizetype
12826       && TYPE_NAME (qualified_type)
12827       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12828     {
12829       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12830
12831       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12832                            && TYPE_PRECISION (t)
12833                            == TYPE_PRECISION (qualified_type)
12834                            && TYPE_UNSIGNED (t)
12835                            == TYPE_UNSIGNED (qualified_type));
12836       qualified_type = t;
12837     }
12838
12839   /* If we do, then we can just use its DIE, if it exists.  */
12840   if (qualified_type)
12841     {
12842       mod_type_die = lookup_type_die (qualified_type);
12843       if (mod_type_die)
12844         return mod_type_die;
12845     }
12846
12847   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12848
12849   /* Handle C typedef types.  */
12850   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12851       && !DECL_ARTIFICIAL (name))
12852     {
12853       tree dtype = TREE_TYPE (name);
12854
12855       if (qualified_type == dtype)
12856         {
12857           /* For a named type, use the typedef.  */
12858           gen_type_die (qualified_type, context_die);
12859           return lookup_type_die (qualified_type);
12860         }
12861       else if (is_const_type < TYPE_READONLY (dtype)
12862                || is_volatile_type < TYPE_VOLATILE (dtype)
12863                || (is_const_type <= TYPE_READONLY (dtype)
12864                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12865                    && DECL_ORIGINAL_TYPE (name) != type))
12866         /* cv-unqualified version of named type.  Just use the unnamed
12867            type to which it refers.  */
12868         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12869                                   is_const_type, is_volatile_type,
12870                                   context_die);
12871       /* Else cv-qualified version of named type; fall through.  */
12872     }
12873
12874   if (is_const_type
12875       /* If both is_const_type and is_volatile_type, prefer the path
12876          which leads to a qualified type.  */
12877       && (!is_volatile_type
12878           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
12879           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
12880     {
12881       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
12882       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12883     }
12884   else if (is_volatile_type)
12885     {
12886       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
12887       sub_die = modified_type_die (type, is_const_type, 0, context_die);
12888     }
12889   else if (code == POINTER_TYPE)
12890     {
12891       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
12892       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12893                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12894       item_type = TREE_TYPE (type);
12895       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12896         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12897                          TYPE_ADDR_SPACE (item_type));
12898     }
12899   else if (code == REFERENCE_TYPE)
12900     {
12901       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12902         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
12903                                 type);
12904       else
12905         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
12906       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12907                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12908       item_type = TREE_TYPE (type);
12909       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12910         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12911                          TYPE_ADDR_SPACE (item_type));
12912     }
12913   else if (code == INTEGER_TYPE
12914            && TREE_TYPE (type) != NULL_TREE
12915            && subrange_type_for_debug_p (type, &low, &high))
12916     {
12917       mod_type_die = subrange_type_die (type, low, high, context_die);
12918       item_type = TREE_TYPE (type);
12919     }
12920   else if (is_base_type (type))
12921     mod_type_die = base_type_die (type);
12922   else
12923     {
12924       gen_type_die (type, context_die);
12925
12926       /* We have to get the type_main_variant here (and pass that to the
12927          `lookup_type_die' routine) because the ..._TYPE node we have
12928          might simply be a *copy* of some original type node (where the
12929          copy was created to help us keep track of typedef names) and
12930          that copy might have a different TYPE_UID from the original
12931          ..._TYPE node.  */
12932       if (TREE_CODE (type) != VECTOR_TYPE)
12933         return lookup_type_die (type_main_variant (type));
12934       else
12935         /* Vectors have the debugging information in the type,
12936            not the main variant.  */
12937         return lookup_type_die (type);
12938     }
12939
12940   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12941      don't output a DW_TAG_typedef, since there isn't one in the
12942      user's program; just attach a DW_AT_name to the type.
12943      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12944      if the base type already has the same name.  */
12945   if (name
12946       && ((TREE_CODE (name) != TYPE_DECL
12947            && (qualified_type == TYPE_MAIN_VARIANT (type)
12948                || (!is_const_type && !is_volatile_type)))
12949           || (TREE_CODE (name) == TYPE_DECL
12950               && TREE_TYPE (name) == qualified_type
12951               && DECL_NAME (name))))
12952     {
12953       if (TREE_CODE (name) == TYPE_DECL)
12954         /* Could just call add_name_and_src_coords_attributes here,
12955            but since this is a builtin type it doesn't have any
12956            useful source coordinates anyway.  */
12957         name = DECL_NAME (name);
12958       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12959     }
12960   /* This probably indicates a bug.  */
12961   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12962     add_name_attribute (mod_type_die, "__unknown__");
12963
12964   if (qualified_type)
12965     equate_type_number_to_die (qualified_type, mod_type_die);
12966
12967   if (item_type)
12968     /* We must do this after the equate_type_number_to_die call, in case
12969        this is a recursive type.  This ensures that the modified_type_die
12970        recursion will terminate even if the type is recursive.  Recursive
12971        types are possible in Ada.  */
12972     sub_die = modified_type_die (item_type,
12973                                  TYPE_READONLY (item_type),
12974                                  TYPE_VOLATILE (item_type),
12975                                  context_die);
12976
12977   if (sub_die != NULL)
12978     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12979
12980   return mod_type_die;
12981 }
12982
12983 /* Generate DIEs for the generic parameters of T.
12984    T must be either a generic type or a generic function.
12985    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12986
12987 static void
12988 gen_generic_params_dies (tree t)
12989 {
12990   tree parms, args;
12991   int parms_num, i;
12992   dw_die_ref die = NULL;
12993
12994   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12995     return;
12996
12997   if (TYPE_P (t))
12998     die = lookup_type_die (t);
12999   else if (DECL_P (t))
13000     die = lookup_decl_die (t);
13001
13002   gcc_assert (die);
13003
13004   parms = lang_hooks.get_innermost_generic_parms (t);
13005   if (!parms)
13006     /* T has no generic parameter. It means T is neither a generic type
13007        or function. End of story.  */
13008     return;
13009
13010   parms_num = TREE_VEC_LENGTH (parms);
13011   args = lang_hooks.get_innermost_generic_args (t);
13012   for (i = 0; i < parms_num; i++)
13013     {
13014       tree parm, arg, arg_pack_elems;
13015
13016       parm = TREE_VEC_ELT (parms, i);
13017       arg = TREE_VEC_ELT (args, i);
13018       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13019       gcc_assert (parm && TREE_VALUE (parm) && arg);
13020
13021       if (parm && TREE_VALUE (parm) && arg)
13022         {
13023           /* If PARM represents a template parameter pack,
13024              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13025              by DW_TAG_template_*_parameter DIEs for the argument
13026              pack elements of ARG. Note that ARG would then be
13027              an argument pack.  */
13028           if (arg_pack_elems)
13029             template_parameter_pack_die (TREE_VALUE (parm),
13030                                          arg_pack_elems,
13031                                          die);
13032           else
13033             generic_parameter_die (TREE_VALUE (parm), arg,
13034                                    true /* Emit DW_AT_name */, die);
13035         }
13036     }
13037 }
13038
13039 /* Create and return a DIE for PARM which should be
13040    the representation of a generic type parameter.
13041    For instance, in the C++ front end, PARM would be a template parameter.
13042    ARG is the argument to PARM.
13043    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13044    name of the PARM.
13045    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13046    as a child node.  */
13047
13048 static dw_die_ref
13049 generic_parameter_die (tree parm, tree arg,
13050                        bool emit_name_p,
13051                        dw_die_ref parent_die)
13052 {
13053   dw_die_ref tmpl_die = NULL;
13054   const char *name = NULL;
13055
13056   if (!parm || !DECL_NAME (parm) || !arg)
13057     return NULL;
13058
13059   /* We support non-type generic parameters and arguments,
13060      type generic parameters and arguments, as well as
13061      generic generic parameters (a.k.a. template template parameters in C++)
13062      and arguments.  */
13063   if (TREE_CODE (parm) == PARM_DECL)
13064     /* PARM is a nontype generic parameter  */
13065     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13066   else if (TREE_CODE (parm) == TYPE_DECL)
13067     /* PARM is a type generic parameter.  */
13068     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13069   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13070     /* PARM is a generic generic parameter.
13071        Its DIE is a GNU extension. It shall have a
13072        DW_AT_name attribute to represent the name of the template template
13073        parameter, and a DW_AT_GNU_template_name attribute to represent the
13074        name of the template template argument.  */
13075     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13076                         parent_die, parm);
13077   else
13078     gcc_unreachable ();
13079
13080   if (tmpl_die)
13081     {
13082       tree tmpl_type;
13083
13084       /* If PARM is a generic parameter pack, it means we are
13085          emitting debug info for a template argument pack element.
13086          In other terms, ARG is a template argument pack element.
13087          In that case, we don't emit any DW_AT_name attribute for
13088          the die.  */
13089       if (emit_name_p)
13090         {
13091           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13092           gcc_assert (name);
13093           add_AT_string (tmpl_die, DW_AT_name, name);
13094         }
13095
13096       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13097         {
13098           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13099              TMPL_DIE should have a child DW_AT_type attribute that is set
13100              to the type of the argument to PARM, which is ARG.
13101              If PARM is a type generic parameter, TMPL_DIE should have a
13102              child DW_AT_type that is set to ARG.  */
13103           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13104           add_type_attribute (tmpl_die, tmpl_type, 0,
13105                               TREE_THIS_VOLATILE (tmpl_type),
13106                               parent_die);
13107         }
13108       else
13109         {
13110           /* So TMPL_DIE is a DIE representing a
13111              a generic generic template parameter, a.k.a template template
13112              parameter in C++ and arg is a template.  */
13113
13114           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13115              to the name of the argument.  */
13116           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13117           if (name)
13118             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13119         }
13120
13121       if (TREE_CODE (parm) == PARM_DECL)
13122         /* So PARM is a non-type generic parameter.
13123            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13124            attribute of TMPL_DIE which value represents the value
13125            of ARG.
13126            We must be careful here:
13127            The value of ARG might reference some function decls.
13128            We might currently be emitting debug info for a generic
13129            type and types are emitted before function decls, we don't
13130            know if the function decls referenced by ARG will actually be
13131            emitted after cgraph computations.
13132            So must defer the generation of the DW_AT_const_value to
13133            after cgraph is ready.  */
13134         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13135     }
13136
13137   return tmpl_die;
13138 }
13139
13140 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13141    PARM_PACK must be a template parameter pack. The returned DIE
13142    will be child DIE of PARENT_DIE.  */
13143
13144 static dw_die_ref
13145 template_parameter_pack_die (tree parm_pack,
13146                              tree parm_pack_args,
13147                              dw_die_ref parent_die)
13148 {
13149   dw_die_ref die;
13150   int j;
13151
13152   gcc_assert (parent_die && parm_pack);
13153
13154   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13155   add_name_and_src_coords_attributes (die, parm_pack);
13156   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13157     generic_parameter_die (parm_pack,
13158                            TREE_VEC_ELT (parm_pack_args, j),
13159                            false /* Don't emit DW_AT_name */,
13160                            die);
13161   return die;
13162 }
13163
13164 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13165    an enumerated type.  */
13166
13167 static inline int
13168 type_is_enum (const_tree type)
13169 {
13170   return TREE_CODE (type) == ENUMERAL_TYPE;
13171 }
13172
13173 /* Return the DBX register number described by a given RTL node.  */
13174
13175 static unsigned int
13176 dbx_reg_number (const_rtx rtl)
13177 {
13178   unsigned regno = REGNO (rtl);
13179
13180   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13181
13182 #ifdef LEAF_REG_REMAP
13183   if (current_function_uses_only_leaf_regs)
13184     {
13185       int leaf_reg = LEAF_REG_REMAP (regno);
13186       if (leaf_reg != -1)
13187         regno = (unsigned) leaf_reg;
13188     }
13189 #endif
13190
13191   return DBX_REGISTER_NUMBER (regno);
13192 }
13193
13194 /* Optionally add a DW_OP_piece term to a location description expression.
13195    DW_OP_piece is only added if the location description expression already
13196    doesn't end with DW_OP_piece.  */
13197
13198 static void
13199 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13200 {
13201   dw_loc_descr_ref loc;
13202
13203   if (*list_head != NULL)
13204     {
13205       /* Find the end of the chain.  */
13206       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13207         ;
13208
13209       if (loc->dw_loc_opc != DW_OP_piece)
13210         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13211     }
13212 }
13213
13214 /* Return a location descriptor that designates a machine register or
13215    zero if there is none.  */
13216
13217 static dw_loc_descr_ref
13218 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13219 {
13220   rtx regs;
13221
13222   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13223     return 0;
13224
13225   /* We only use "frame base" when we're sure we're talking about the
13226      post-prologue local stack frame.  We do this by *not* running
13227      register elimination until this point, and recognizing the special
13228      argument pointer and soft frame pointer rtx's.
13229      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13230   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13231       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13232     {
13233       dw_loc_descr_ref result = NULL;
13234
13235       if (dwarf_version >= 4 || !dwarf_strict)
13236         {
13237           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13238           if (result)
13239             add_loc_descr (&result,
13240                            new_loc_descr (DW_OP_stack_value, 0, 0));
13241         }
13242       return result;
13243     }
13244
13245   regs = targetm.dwarf_register_span (rtl);
13246
13247   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13248     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13249   else
13250     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13251 }
13252
13253 /* Return a location descriptor that designates a machine register for
13254    a given hard register number.  */
13255
13256 static dw_loc_descr_ref
13257 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13258 {
13259   dw_loc_descr_ref reg_loc_descr;
13260
13261   if (regno <= 31)
13262     reg_loc_descr
13263       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13264   else
13265     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13266
13267   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13268     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13269
13270   return reg_loc_descr;
13271 }
13272
13273 /* Given an RTL of a register, return a location descriptor that
13274    designates a value that spans more than one register.  */
13275
13276 static dw_loc_descr_ref
13277 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13278                              enum var_init_status initialized)
13279 {
13280   int nregs, size, i;
13281   unsigned reg;
13282   dw_loc_descr_ref loc_result = NULL;
13283
13284   reg = REGNO (rtl);
13285 #ifdef LEAF_REG_REMAP
13286   if (current_function_uses_only_leaf_regs)
13287     {
13288       int leaf_reg = LEAF_REG_REMAP (reg);
13289       if (leaf_reg != -1)
13290         reg = (unsigned) leaf_reg;
13291     }
13292 #endif
13293   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13294   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13295
13296   /* Simple, contiguous registers.  */
13297   if (regs == NULL_RTX)
13298     {
13299       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13300
13301       loc_result = NULL;
13302       while (nregs--)
13303         {
13304           dw_loc_descr_ref t;
13305
13306           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13307                                       VAR_INIT_STATUS_INITIALIZED);
13308           add_loc_descr (&loc_result, t);
13309           add_loc_descr_op_piece (&loc_result, size);
13310           ++reg;
13311         }
13312       return loc_result;
13313     }
13314
13315   /* Now onto stupid register sets in non contiguous locations.  */
13316
13317   gcc_assert (GET_CODE (regs) == PARALLEL);
13318
13319   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13320   loc_result = NULL;
13321
13322   for (i = 0; i < XVECLEN (regs, 0); ++i)
13323     {
13324       dw_loc_descr_ref t;
13325
13326       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13327                                   VAR_INIT_STATUS_INITIALIZED);
13328       add_loc_descr (&loc_result, t);
13329       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13330       add_loc_descr_op_piece (&loc_result, size);
13331     }
13332
13333   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13334     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13335   return loc_result;
13336 }
13337
13338 /* Return a location descriptor that designates a constant.  */
13339
13340 static dw_loc_descr_ref
13341 int_loc_descriptor (HOST_WIDE_INT i)
13342 {
13343   enum dwarf_location_atom op;
13344
13345   /* Pick the smallest representation of a constant, rather than just
13346      defaulting to the LEB encoding.  */
13347   if (i >= 0)
13348     {
13349       if (i <= 31)
13350         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13351       else if (i <= 0xff)
13352         op = DW_OP_const1u;
13353       else if (i <= 0xffff)
13354         op = DW_OP_const2u;
13355       else if (HOST_BITS_PER_WIDE_INT == 32
13356                || i <= 0xffffffff)
13357         op = DW_OP_const4u;
13358       else
13359         op = DW_OP_constu;
13360     }
13361   else
13362     {
13363       if (i >= -0x80)
13364         op = DW_OP_const1s;
13365       else if (i >= -0x8000)
13366         op = DW_OP_const2s;
13367       else if (HOST_BITS_PER_WIDE_INT == 32
13368                || i >= -0x80000000)
13369         op = DW_OP_const4s;
13370       else
13371         op = DW_OP_consts;
13372     }
13373
13374   return new_loc_descr (op, i, 0);
13375 }
13376
13377 /* Return loc description representing "address" of integer value.
13378    This can appear only as toplevel expression.  */
13379
13380 static dw_loc_descr_ref
13381 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13382 {
13383   int litsize;
13384   dw_loc_descr_ref loc_result = NULL;
13385
13386   if (!(dwarf_version >= 4 || !dwarf_strict))
13387     return NULL;
13388
13389   if (i >= 0)
13390     {
13391       if (i <= 31)
13392         litsize = 1;
13393       else if (i <= 0xff)
13394         litsize = 2;
13395       else if (i <= 0xffff)
13396         litsize = 3;
13397       else if (HOST_BITS_PER_WIDE_INT == 32
13398                || i <= 0xffffffff)
13399         litsize = 5;
13400       else
13401         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13402     }
13403   else
13404     {
13405       if (i >= -0x80)
13406         litsize = 2;
13407       else if (i >= -0x8000)
13408         litsize = 3;
13409       else if (HOST_BITS_PER_WIDE_INT == 32
13410                || i >= -0x80000000)
13411         litsize = 5;
13412       else
13413         litsize = 1 + size_of_sleb128 (i);
13414     }
13415   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13416      is more compact.  For DW_OP_stack_value we need:
13417      litsize + 1 (DW_OP_stack_value)
13418      and for DW_OP_implicit_value:
13419      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13420   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13421     {
13422       loc_result = int_loc_descriptor (i);
13423       add_loc_descr (&loc_result,
13424                      new_loc_descr (DW_OP_stack_value, 0, 0));
13425       return loc_result;
13426     }
13427
13428   loc_result = new_loc_descr (DW_OP_implicit_value,
13429                               size, 0);
13430   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13431   loc_result->dw_loc_oprnd2.v.val_int = i;
13432   return loc_result;
13433 }
13434
13435 /* Return a location descriptor that designates a base+offset location.  */
13436
13437 static dw_loc_descr_ref
13438 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13439                  enum var_init_status initialized)
13440 {
13441   unsigned int regno;
13442   dw_loc_descr_ref result;
13443   dw_fde_ref fde = current_fde ();
13444
13445   /* We only use "frame base" when we're sure we're talking about the
13446      post-prologue local stack frame.  We do this by *not* running
13447      register elimination until this point, and recognizing the special
13448      argument pointer and soft frame pointer rtx's.  */
13449   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13450     {
13451       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13452
13453       if (elim != reg)
13454         {
13455           if (GET_CODE (elim) == PLUS)
13456             {
13457               offset += INTVAL (XEXP (elim, 1));
13458               elim = XEXP (elim, 0);
13459             }
13460           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13461                        && (elim == hard_frame_pointer_rtx
13462                            || elim == stack_pointer_rtx))
13463                       || elim == (frame_pointer_needed
13464                                   ? hard_frame_pointer_rtx
13465                                   : stack_pointer_rtx));
13466
13467           /* If drap register is used to align stack, use frame
13468              pointer + offset to access stack variables.  If stack
13469              is aligned without drap, use stack pointer + offset to
13470              access stack variables.  */
13471           if (crtl->stack_realign_tried
13472               && reg == frame_pointer_rtx)
13473             {
13474               int base_reg
13475                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13476                                       ? HARD_FRAME_POINTER_REGNUM
13477                                       : STACK_POINTER_REGNUM);
13478               return new_reg_loc_descr (base_reg, offset);
13479             }
13480
13481           offset += frame_pointer_fb_offset;
13482           return new_loc_descr (DW_OP_fbreg, offset, 0);
13483         }
13484     }
13485   else if (!optimize
13486            && fde
13487            && (fde->drap_reg == REGNO (reg)
13488                || fde->vdrap_reg == REGNO (reg)))
13489     {
13490       /* Use cfa+offset to represent the location of arguments passed
13491          on the stack when drap is used to align stack.
13492          Only do this when not optimizing, for optimized code var-tracking
13493          is supposed to track where the arguments live and the register
13494          used as vdrap or drap in some spot might be used for something
13495          else in other part of the routine.  */
13496       return new_loc_descr (DW_OP_fbreg, offset, 0);
13497     }
13498
13499   regno = dbx_reg_number (reg);
13500   if (regno <= 31)
13501     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13502                             offset, 0);
13503   else
13504     result = new_loc_descr (DW_OP_bregx, regno, offset);
13505
13506   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13507     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13508
13509   return result;
13510 }
13511
13512 /* Return true if this RTL expression describes a base+offset calculation.  */
13513
13514 static inline int
13515 is_based_loc (const_rtx rtl)
13516 {
13517   return (GET_CODE (rtl) == PLUS
13518           && ((REG_P (XEXP (rtl, 0))
13519                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13520                && CONST_INT_P (XEXP (rtl, 1)))));
13521 }
13522
13523 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13524    failed.  */
13525
13526 static dw_loc_descr_ref
13527 tls_mem_loc_descriptor (rtx mem)
13528 {
13529   tree base;
13530   dw_loc_descr_ref loc_result;
13531
13532   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13533     return NULL;
13534
13535   base = get_base_address (MEM_EXPR (mem));
13536   if (base == NULL
13537       || TREE_CODE (base) != VAR_DECL
13538       || !DECL_THREAD_LOCAL_P (base))
13539     return NULL;
13540
13541   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13542   if (loc_result == NULL)
13543     return NULL;
13544
13545   if (INTVAL (MEM_OFFSET (mem)))
13546     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13547
13548   return loc_result;
13549 }
13550
13551 /* Output debug info about reason why we failed to expand expression as dwarf
13552    expression.  */
13553
13554 static void
13555 expansion_failed (tree expr, rtx rtl, char const *reason)
13556 {
13557   if (dump_file && (dump_flags & TDF_DETAILS))
13558     {
13559       fprintf (dump_file, "Failed to expand as dwarf: ");
13560       if (expr)
13561         print_generic_expr (dump_file, expr, dump_flags);
13562       if (rtl)
13563         {
13564           fprintf (dump_file, "\n");
13565           print_rtl (dump_file, rtl);
13566         }
13567       fprintf (dump_file, "\nReason: %s\n", reason);
13568     }
13569 }
13570
13571 /* Helper function for const_ok_for_output, called either directly
13572    or via for_each_rtx.  */
13573
13574 static int
13575 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13576 {
13577   rtx rtl = *rtlp;
13578
13579   if (GET_CODE (rtl) == UNSPEC)
13580     {
13581       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13582          we can't express it in the debug info.  */
13583 #ifdef ENABLE_CHECKING
13584       /* Don't complain about TLS UNSPECs, those are just too hard to
13585          delegitimize.  */
13586       if (XVECLEN (rtl, 0) != 1
13587           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13588           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13589           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13590           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13591         inform (current_function_decl
13592                 ? DECL_SOURCE_LOCATION (current_function_decl)
13593                 : UNKNOWN_LOCATION,
13594 #if NUM_UNSPEC_VALUES > 0
13595                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13596                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13597                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13598                 XINT (rtl, 1));
13599 #else
13600                 "non-delegitimized UNSPEC %d found in variable location",
13601                 XINT (rtl, 1));
13602 #endif
13603 #endif
13604       expansion_failed (NULL_TREE, rtl,
13605                         "UNSPEC hasn't been delegitimized.\n");
13606       return 1;
13607     }
13608
13609   if (GET_CODE (rtl) != SYMBOL_REF)
13610     return 0;
13611
13612   if (CONSTANT_POOL_ADDRESS_P (rtl))
13613     {
13614       bool marked;
13615       get_pool_constant_mark (rtl, &marked);
13616       /* If all references to this pool constant were optimized away,
13617          it was not output and thus we can't represent it.  */
13618       if (!marked)
13619         {
13620           expansion_failed (NULL_TREE, rtl,
13621                             "Constant was removed from constant pool.\n");
13622           return 1;
13623         }
13624     }
13625
13626   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13627     return 1;
13628
13629   /* Avoid references to external symbols in debug info, on several targets
13630      the linker might even refuse to link when linking a shared library,
13631      and in many other cases the relocations for .debug_info/.debug_loc are
13632      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13633      to be defined within the same shared library or executable are fine.  */
13634   if (SYMBOL_REF_EXTERNAL_P (rtl))
13635     {
13636       tree decl = SYMBOL_REF_DECL (rtl);
13637
13638       if (decl == NULL || !targetm.binds_local_p (decl))
13639         {
13640           expansion_failed (NULL_TREE, rtl,
13641                             "Symbol not defined in current TU.\n");
13642           return 1;
13643         }
13644     }
13645
13646   return 0;
13647 }
13648
13649 /* Return true if constant RTL can be emitted in DW_OP_addr or
13650    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13651    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13652
13653 static bool
13654 const_ok_for_output (rtx rtl)
13655 {
13656   if (GET_CODE (rtl) == SYMBOL_REF)
13657     return const_ok_for_output_1 (&rtl, NULL) == 0;
13658
13659   if (GET_CODE (rtl) == CONST)
13660     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13661
13662   return true;
13663 }
13664
13665 /* The following routine converts the RTL for a variable or parameter
13666    (resident in memory) into an equivalent Dwarf representation of a
13667    mechanism for getting the address of that same variable onto the top of a
13668    hypothetical "address evaluation" stack.
13669
13670    When creating memory location descriptors, we are effectively transforming
13671    the RTL for a memory-resident object into its Dwarf postfix expression
13672    equivalent.  This routine recursively descends an RTL tree, turning
13673    it into Dwarf postfix code as it goes.
13674
13675    MODE is the mode of the memory reference, needed to handle some
13676    autoincrement addressing modes.
13677
13678    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13679    location list for RTL.
13680
13681    Return 0 if we can't represent the location.  */
13682
13683 static dw_loc_descr_ref
13684 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13685                     enum var_init_status initialized)
13686 {
13687   dw_loc_descr_ref mem_loc_result = NULL;
13688   enum dwarf_location_atom op;
13689   dw_loc_descr_ref op0, op1;
13690
13691   /* Note that for a dynamically sized array, the location we will generate a
13692      description of here will be the lowest numbered location which is
13693      actually within the array.  That's *not* necessarily the same as the
13694      zeroth element of the array.  */
13695
13696   rtl = targetm.delegitimize_address (rtl);
13697
13698   switch (GET_CODE (rtl))
13699     {
13700     case POST_INC:
13701     case POST_DEC:
13702     case POST_MODIFY:
13703       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13704
13705     case SUBREG:
13706       /* The case of a subreg may arise when we have a local (register)
13707          variable or a formal (register) parameter which doesn't quite fill
13708          up an entire register.  For now, just assume that it is
13709          legitimate to make the Dwarf info refer to the whole register which
13710          contains the given subreg.  */
13711       if (!subreg_lowpart_p (rtl))
13712         break;
13713       rtl = SUBREG_REG (rtl);
13714       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13715         break;
13716       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13717         break;
13718       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13719       break;
13720
13721     case REG:
13722       /* Whenever a register number forms a part of the description of the
13723          method for calculating the (dynamic) address of a memory resident
13724          object, DWARF rules require the register number be referred to as
13725          a "base register".  This distinction is not based in any way upon
13726          what category of register the hardware believes the given register
13727          belongs to.  This is strictly DWARF terminology we're dealing with
13728          here. Note that in cases where the location of a memory-resident
13729          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13730          OP_CONST (0)) the actual DWARF location descriptor that we generate
13731          may just be OP_BASEREG (basereg).  This may look deceptively like
13732          the object in question was allocated to a register (rather than in
13733          memory) so DWARF consumers need to be aware of the subtle
13734          distinction between OP_REG and OP_BASEREG.  */
13735       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13736         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13737       else if (stack_realign_drap
13738                && crtl->drap_reg
13739                && crtl->args.internal_arg_pointer == rtl
13740                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13741         {
13742           /* If RTL is internal_arg_pointer, which has been optimized
13743              out, use DRAP instead.  */
13744           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13745                                             VAR_INIT_STATUS_INITIALIZED);
13746         }
13747       break;
13748
13749     case SIGN_EXTEND:
13750     case ZERO_EXTEND:
13751       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13752                                 VAR_INIT_STATUS_INITIALIZED);
13753       if (op0 == 0)
13754         break;
13755       else
13756         {
13757           int shift = DWARF2_ADDR_SIZE
13758                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13759           shift *= BITS_PER_UNIT;
13760           if (GET_CODE (rtl) == SIGN_EXTEND)
13761             op = DW_OP_shra;
13762           else
13763             op = DW_OP_shr;
13764           mem_loc_result = op0;
13765           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13766           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13767           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13768           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13769         }
13770       break;
13771
13772     case MEM:
13773       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13774                                            VAR_INIT_STATUS_INITIALIZED);
13775       if (mem_loc_result == NULL)
13776         mem_loc_result = tls_mem_loc_descriptor (rtl);
13777       if (mem_loc_result != 0)
13778         {
13779           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13780             {
13781               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13782               return 0;
13783             }
13784           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13785             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13786           else
13787             add_loc_descr (&mem_loc_result,
13788                            new_loc_descr (DW_OP_deref_size,
13789                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13790         }
13791       else
13792         {
13793           rtx new_rtl = avoid_constant_pool_reference (rtl);
13794           if (new_rtl != rtl)
13795             return mem_loc_descriptor (new_rtl, mode, initialized);
13796         }
13797       break;
13798
13799     case LO_SUM:
13800          rtl = XEXP (rtl, 1);
13801
13802       /* ... fall through ...  */
13803
13804     case LABEL_REF:
13805       /* Some ports can transform a symbol ref into a label ref, because
13806          the symbol ref is too far away and has to be dumped into a constant
13807          pool.  */
13808     case CONST:
13809     case SYMBOL_REF:
13810       if (GET_CODE (rtl) == SYMBOL_REF
13811           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13812         {
13813           dw_loc_descr_ref temp;
13814
13815           /* If this is not defined, we have no way to emit the data.  */
13816           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13817             break;
13818
13819           /* We used to emit DW_OP_addr here, but that's wrong, since
13820              DW_OP_addr should be relocated by the debug info consumer,
13821              while DW_OP_GNU_push_tls_address operand should not.  */
13822           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13823                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13824           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13825           temp->dw_loc_oprnd1.v.val_addr = rtl;
13826           temp->dtprel = true;
13827
13828           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13829           add_loc_descr (&mem_loc_result, temp);
13830
13831           break;
13832         }
13833
13834       if (!const_ok_for_output (rtl))
13835         break;
13836
13837     symref:
13838       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13839       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13840       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13841       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13842       break;
13843
13844     case CONCAT:
13845     case CONCATN:
13846     case VAR_LOCATION:
13847     case DEBUG_IMPLICIT_PTR:
13848       expansion_failed (NULL_TREE, rtl,
13849                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13850       return 0;
13851
13852     case ENTRY_VALUE:
13853       if (dwarf_strict)
13854         return NULL;
13855       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
13856       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
13857       if (REG_P (ENTRY_VALUE_EXP (rtl)))
13858         mem_loc_result->dw_loc_oprnd1.v.val_loc
13859           = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
13860                                     VAR_INIT_STATUS_INITIALIZED);
13861       else if (MEM_P (ENTRY_VALUE_EXP (rtl)) && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
13862         {
13863           dw_loc_descr_ref ref
13864             = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), GET_MODE (rtl),
13865                                   VAR_INIT_STATUS_INITIALIZED);
13866           if (ref == NULL || ref->dw_loc_opc == DW_OP_fbreg)
13867             return NULL;
13868           mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
13869         }
13870       else
13871         gcc_unreachable ();
13872       return mem_loc_result;
13873
13874     case PRE_MODIFY:
13875       /* Extract the PLUS expression nested inside and fall into
13876          PLUS code below.  */
13877       rtl = XEXP (rtl, 1);
13878       goto plus;
13879
13880     case PRE_INC:
13881     case PRE_DEC:
13882       /* Turn these into a PLUS expression and fall into the PLUS code
13883          below.  */
13884       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13885                           GEN_INT (GET_CODE (rtl) == PRE_INC
13886                                    ? GET_MODE_UNIT_SIZE (mode)
13887                                    : -GET_MODE_UNIT_SIZE (mode)));
13888
13889       /* ... fall through ...  */
13890
13891     case PLUS:
13892     plus:
13893       if (is_based_loc (rtl))
13894         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13895                                           INTVAL (XEXP (rtl, 1)),
13896                                           VAR_INIT_STATUS_INITIALIZED);
13897       else
13898         {
13899           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13900                                                VAR_INIT_STATUS_INITIALIZED);
13901           if (mem_loc_result == 0)
13902             break;
13903
13904           if (CONST_INT_P (XEXP (rtl, 1)))
13905             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13906           else
13907             {
13908               dw_loc_descr_ref mem_loc_result2
13909                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13910                                       VAR_INIT_STATUS_INITIALIZED);
13911               if (mem_loc_result2 == 0)
13912                 break;
13913               add_loc_descr (&mem_loc_result, mem_loc_result2);
13914               add_loc_descr (&mem_loc_result,
13915                              new_loc_descr (DW_OP_plus, 0, 0));
13916             }
13917         }
13918       break;
13919
13920     /* If a pseudo-reg is optimized away, it is possible for it to
13921        be replaced with a MEM containing a multiply or shift.  */
13922     case MINUS:
13923       op = DW_OP_minus;
13924       goto do_binop;
13925
13926     case MULT:
13927       op = DW_OP_mul;
13928       goto do_binop;
13929
13930     case DIV:
13931       op = DW_OP_div;
13932       goto do_binop;
13933
13934     case UMOD:
13935       op = DW_OP_mod;
13936       goto do_binop;
13937
13938     case ASHIFT:
13939       op = DW_OP_shl;
13940       goto do_binop;
13941
13942     case ASHIFTRT:
13943       op = DW_OP_shra;
13944       goto do_binop;
13945
13946     case LSHIFTRT:
13947       op = DW_OP_shr;
13948       goto do_binop;
13949
13950     case AND:
13951       op = DW_OP_and;
13952       goto do_binop;
13953
13954     case IOR:
13955       op = DW_OP_or;
13956       goto do_binop;
13957
13958     case XOR:
13959       op = DW_OP_xor;
13960       goto do_binop;
13961
13962     do_binop:
13963       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13964                                 VAR_INIT_STATUS_INITIALIZED);
13965       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13966                                 VAR_INIT_STATUS_INITIALIZED);
13967
13968       if (op0 == 0 || op1 == 0)
13969         break;
13970
13971       mem_loc_result = op0;
13972       add_loc_descr (&mem_loc_result, op1);
13973       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13974       break;
13975
13976     case MOD:
13977       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13978                                 VAR_INIT_STATUS_INITIALIZED);
13979       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13980                                 VAR_INIT_STATUS_INITIALIZED);
13981
13982       if (op0 == 0 || op1 == 0)
13983         break;
13984
13985       mem_loc_result = op0;
13986       add_loc_descr (&mem_loc_result, op1);
13987       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13988       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13989       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13990       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13991       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13992       break;
13993
13994     case NOT:
13995       op = DW_OP_not;
13996       goto do_unop;
13997
13998     case ABS:
13999       op = DW_OP_abs;
14000       goto do_unop;
14001
14002     case NEG:
14003       op = DW_OP_neg;
14004       goto do_unop;
14005
14006     do_unop:
14007       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14008                                 VAR_INIT_STATUS_INITIALIZED);
14009
14010       if (op0 == 0)
14011         break;
14012
14013       mem_loc_result = op0;
14014       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14015       break;
14016
14017     case CONST_INT:
14018       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14019       break;
14020
14021     case EQ:
14022       op = DW_OP_eq;
14023       goto do_scompare;
14024
14025     case GE:
14026       op = DW_OP_ge;
14027       goto do_scompare;
14028
14029     case GT:
14030       op = DW_OP_gt;
14031       goto do_scompare;
14032
14033     case LE:
14034       op = DW_OP_le;
14035       goto do_scompare;
14036
14037     case LT:
14038       op = DW_OP_lt;
14039       goto do_scompare;
14040
14041     case NE:
14042       op = DW_OP_ne;
14043       goto do_scompare;
14044
14045     do_scompare:
14046       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14047           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14048         break;
14049       else
14050         {
14051           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14052
14053           if (op_mode == VOIDmode)
14054             op_mode = GET_MODE (XEXP (rtl, 1));
14055           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14056             break;
14057
14058           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14059                                     VAR_INIT_STATUS_INITIALIZED);
14060           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14061                                     VAR_INIT_STATUS_INITIALIZED);
14062
14063           if (op0 == 0 || op1 == 0)
14064             break;
14065
14066           if (op_mode != VOIDmode
14067               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14068             {
14069               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14070               shift *= BITS_PER_UNIT;
14071               /* For eq/ne, if the operands are known to be zero-extended,
14072                  there is no need to do the fancy shifting up.  */
14073               if (op == DW_OP_eq || op == DW_OP_ne)
14074                 {
14075                   dw_loc_descr_ref last0, last1;
14076                   for (last0 = op0;
14077                        last0->dw_loc_next != NULL;
14078                        last0 = last0->dw_loc_next)
14079                     ;
14080                   for (last1 = op1;
14081                        last1->dw_loc_next != NULL;
14082                        last1 = last1->dw_loc_next)
14083                     ;
14084                   /* deref_size zero extends, and for constants we can check
14085                      whether they are zero extended or not.  */
14086                   if (((last0->dw_loc_opc == DW_OP_deref_size
14087                         && last0->dw_loc_oprnd1.v.val_int
14088                            <= GET_MODE_SIZE (op_mode))
14089                        || (CONST_INT_P (XEXP (rtl, 0))
14090                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14091                                == (INTVAL (XEXP (rtl, 0))
14092                                    & GET_MODE_MASK (op_mode))))
14093                       && ((last1->dw_loc_opc == DW_OP_deref_size
14094                            && last1->dw_loc_oprnd1.v.val_int
14095                               <= GET_MODE_SIZE (op_mode))
14096                           || (CONST_INT_P (XEXP (rtl, 1))
14097                               && (unsigned HOST_WIDE_INT)
14098                                  INTVAL (XEXP (rtl, 1))
14099                                  == (INTVAL (XEXP (rtl, 1))
14100                                      & GET_MODE_MASK (op_mode)))))
14101                     goto do_compare;
14102                 }
14103               add_loc_descr (&op0, int_loc_descriptor (shift));
14104               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14105               if (CONST_INT_P (XEXP (rtl, 1)))
14106                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14107               else
14108                 {
14109                   add_loc_descr (&op1, int_loc_descriptor (shift));
14110                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14111                 }
14112             }
14113         }
14114
14115     do_compare:
14116       mem_loc_result = op0;
14117       add_loc_descr (&mem_loc_result, op1);
14118       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14119       if (STORE_FLAG_VALUE != 1)
14120         {
14121           add_loc_descr (&mem_loc_result,
14122                          int_loc_descriptor (STORE_FLAG_VALUE));
14123           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14124         }
14125       break;
14126
14127     case GEU:
14128       op = DW_OP_ge;
14129       goto do_ucompare;
14130
14131     case GTU:
14132       op = DW_OP_gt;
14133       goto do_ucompare;
14134
14135     case LEU:
14136       op = DW_OP_le;
14137       goto do_ucompare;
14138
14139     case LTU:
14140       op = DW_OP_lt;
14141       goto do_ucompare;
14142
14143     do_ucompare:
14144       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14145           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14146         break;
14147       else
14148         {
14149           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14150
14151           if (op_mode == VOIDmode)
14152             op_mode = GET_MODE (XEXP (rtl, 1));
14153           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14154             break;
14155
14156           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14157                                     VAR_INIT_STATUS_INITIALIZED);
14158           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14159                                     VAR_INIT_STATUS_INITIALIZED);
14160
14161           if (op0 == 0 || op1 == 0)
14162             break;
14163
14164           if (op_mode != VOIDmode
14165               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14166             {
14167               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14168               dw_loc_descr_ref last0, last1;
14169               for (last0 = op0;
14170                    last0->dw_loc_next != NULL;
14171                    last0 = last0->dw_loc_next)
14172                 ;
14173               for (last1 = op1;
14174                    last1->dw_loc_next != NULL;
14175                    last1 = last1->dw_loc_next)
14176                 ;
14177               if (CONST_INT_P (XEXP (rtl, 0)))
14178                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14179               /* deref_size zero extends, so no need to mask it again.  */
14180               else if (last0->dw_loc_opc != DW_OP_deref_size
14181                        || last0->dw_loc_oprnd1.v.val_int
14182                           > GET_MODE_SIZE (op_mode))
14183                 {
14184                   add_loc_descr (&op0, int_loc_descriptor (mask));
14185                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14186                 }
14187               if (CONST_INT_P (XEXP (rtl, 1)))
14188                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14189               /* deref_size zero extends, so no need to mask it again.  */
14190               else if (last1->dw_loc_opc != DW_OP_deref_size
14191                        || last1->dw_loc_oprnd1.v.val_int
14192                           > GET_MODE_SIZE (op_mode))
14193                 {
14194                   add_loc_descr (&op1, int_loc_descriptor (mask));
14195                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14196                 }
14197             }
14198           else
14199             {
14200               HOST_WIDE_INT bias = 1;
14201               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14202               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14203               if (CONST_INT_P (XEXP (rtl, 1)))
14204                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14205                                           + INTVAL (XEXP (rtl, 1)));
14206               else
14207                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14208                                                     bias, 0));
14209             }
14210         }
14211       goto do_compare;
14212
14213     case SMIN:
14214     case SMAX:
14215     case UMIN:
14216     case UMAX:
14217       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14218           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14219           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14220         break;
14221
14222       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14223                                 VAR_INIT_STATUS_INITIALIZED);
14224       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14225                                 VAR_INIT_STATUS_INITIALIZED);
14226
14227       if (op0 == 0 || op1 == 0)
14228         break;
14229
14230       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14231       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14232       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14233       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14234         {
14235           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14236             {
14237               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14238               add_loc_descr (&op0, int_loc_descriptor (mask));
14239               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14240               add_loc_descr (&op1, int_loc_descriptor (mask));
14241               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14242             }
14243           else
14244             {
14245               HOST_WIDE_INT bias = 1;
14246               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14247               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14248               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14249             }
14250         }
14251       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14252         {
14253           int shift = DWARF2_ADDR_SIZE
14254                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14255           shift *= BITS_PER_UNIT;
14256           add_loc_descr (&op0, int_loc_descriptor (shift));
14257           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14258           add_loc_descr (&op1, int_loc_descriptor (shift));
14259           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14260         }
14261
14262       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14263         op = DW_OP_lt;
14264       else
14265         op = DW_OP_gt;
14266       mem_loc_result = op0;
14267       add_loc_descr (&mem_loc_result, op1);
14268       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14269       {
14270         dw_loc_descr_ref bra_node, drop_node;
14271
14272         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14273         add_loc_descr (&mem_loc_result, bra_node);
14274         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14275         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14276         add_loc_descr (&mem_loc_result, drop_node);
14277         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14278         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14279       }
14280       break;
14281
14282     case ZERO_EXTRACT:
14283     case SIGN_EXTRACT:
14284       if (CONST_INT_P (XEXP (rtl, 1))
14285           && CONST_INT_P (XEXP (rtl, 2))
14286           && ((unsigned) INTVAL (XEXP (rtl, 1))
14287               + (unsigned) INTVAL (XEXP (rtl, 2))
14288               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14289           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14290           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14291         {
14292           int shift, size;
14293           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14294                                     VAR_INIT_STATUS_INITIALIZED);
14295           if (op0 == 0)
14296             break;
14297           if (GET_CODE (rtl) == SIGN_EXTRACT)
14298             op = DW_OP_shra;
14299           else
14300             op = DW_OP_shr;
14301           mem_loc_result = op0;
14302           size = INTVAL (XEXP (rtl, 1));
14303           shift = INTVAL (XEXP (rtl, 2));
14304           if (BITS_BIG_ENDIAN)
14305             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14306                     - shift - size;
14307           if (shift + size != (int) DWARF2_ADDR_SIZE)
14308             {
14309               add_loc_descr (&mem_loc_result,
14310                              int_loc_descriptor (DWARF2_ADDR_SIZE
14311                                                  - shift - size));
14312               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14313             }
14314           if (size != (int) DWARF2_ADDR_SIZE)
14315             {
14316               add_loc_descr (&mem_loc_result,
14317                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14318               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14319             }
14320         }
14321       break;
14322
14323     case IF_THEN_ELSE:
14324       {
14325         dw_loc_descr_ref op2, bra_node, drop_node;
14326         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14327                                   VAR_INIT_STATUS_INITIALIZED);
14328         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14329                                   VAR_INIT_STATUS_INITIALIZED);
14330         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14331                                   VAR_INIT_STATUS_INITIALIZED);
14332         if (op0 == NULL || op1 == NULL || op2 == NULL)
14333           break;
14334
14335         mem_loc_result = op1;
14336         add_loc_descr (&mem_loc_result, op2);
14337         add_loc_descr (&mem_loc_result, op0);
14338         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14339         add_loc_descr (&mem_loc_result, bra_node);
14340         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14341         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14342         add_loc_descr (&mem_loc_result, drop_node);
14343         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14344         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14345       }
14346       break;
14347
14348     case COMPARE:
14349     case ROTATE:
14350     case ROTATERT:
14351     case TRUNCATE:
14352       /* In theory, we could implement the above.  */
14353       /* DWARF cannot represent the unsigned compare operations
14354          natively.  */
14355     case SS_MULT:
14356     case US_MULT:
14357     case SS_DIV:
14358     case US_DIV:
14359     case SS_PLUS:
14360     case US_PLUS:
14361     case SS_MINUS:
14362     case US_MINUS:
14363     case SS_NEG:
14364     case US_NEG:
14365     case SS_ABS:
14366     case SS_ASHIFT:
14367     case US_ASHIFT:
14368     case SS_TRUNCATE:
14369     case US_TRUNCATE:
14370     case UDIV:
14371     case UNORDERED:
14372     case ORDERED:
14373     case UNEQ:
14374     case UNGE:
14375     case UNGT:
14376     case UNLE:
14377     case UNLT:
14378     case LTGT:
14379     case FLOAT_EXTEND:
14380     case FLOAT_TRUNCATE:
14381     case FLOAT:
14382     case UNSIGNED_FLOAT:
14383     case FIX:
14384     case UNSIGNED_FIX:
14385     case FRACT_CONVERT:
14386     case UNSIGNED_FRACT_CONVERT:
14387     case SAT_FRACT:
14388     case UNSIGNED_SAT_FRACT:
14389     case SQRT:
14390     case BSWAP:
14391     case FFS:
14392     case CLZ:
14393     case CTZ:
14394     case POPCOUNT:
14395     case PARITY:
14396     case ASM_OPERANDS:
14397     case VEC_MERGE:
14398     case VEC_SELECT:
14399     case VEC_CONCAT:
14400     case VEC_DUPLICATE:
14401     case UNSPEC:
14402     case HIGH:
14403       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14404          can't express it in the debug info.  This can happen e.g. with some
14405          TLS UNSPECs.  */
14406       break;
14407
14408     case CONST_STRING:
14409       resolve_one_addr (&rtl, NULL);
14410       goto symref;
14411
14412     default:
14413 #ifdef ENABLE_CHECKING
14414       print_rtl (stderr, rtl);
14415       gcc_unreachable ();
14416 #else
14417       break;
14418 #endif
14419     }
14420
14421   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14422     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14423
14424   return mem_loc_result;
14425 }
14426
14427 /* Return a descriptor that describes the concatenation of two locations.
14428    This is typically a complex variable.  */
14429
14430 static dw_loc_descr_ref
14431 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14432 {
14433   dw_loc_descr_ref cc_loc_result = NULL;
14434   dw_loc_descr_ref x0_ref
14435     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14436   dw_loc_descr_ref x1_ref
14437     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14438
14439   if (x0_ref == 0 || x1_ref == 0)
14440     return 0;
14441
14442   cc_loc_result = x0_ref;
14443   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14444
14445   add_loc_descr (&cc_loc_result, x1_ref);
14446   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14447
14448   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14449     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14450
14451   return cc_loc_result;
14452 }
14453
14454 /* Return a descriptor that describes the concatenation of N
14455    locations.  */
14456
14457 static dw_loc_descr_ref
14458 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14459 {
14460   unsigned int i;
14461   dw_loc_descr_ref cc_loc_result = NULL;
14462   unsigned int n = XVECLEN (concatn, 0);
14463
14464   for (i = 0; i < n; ++i)
14465     {
14466       dw_loc_descr_ref ref;
14467       rtx x = XVECEXP (concatn, 0, i);
14468
14469       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14470       if (ref == NULL)
14471         return NULL;
14472
14473       add_loc_descr (&cc_loc_result, ref);
14474       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14475     }
14476
14477   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14478     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14479
14480   return cc_loc_result;
14481 }
14482
14483 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14484    for DEBUG_IMPLICIT_PTR RTL.  */
14485
14486 static dw_loc_descr_ref
14487 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14488 {
14489   dw_loc_descr_ref ret;
14490   dw_die_ref ref;
14491
14492   if (dwarf_strict)
14493     return NULL;
14494   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14495               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14496               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14497   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14498   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14499   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14500   if (ref)
14501     {
14502       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14503       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14504       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14505     }
14506   else
14507     {
14508       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14509       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14510     }
14511   return ret;
14512 }
14513
14514 /* Output a proper Dwarf location descriptor for a variable or parameter
14515    which is either allocated in a register or in a memory location.  For a
14516    register, we just generate an OP_REG and the register number.  For a
14517    memory location we provide a Dwarf postfix expression describing how to
14518    generate the (dynamic) address of the object onto the address stack.
14519
14520    MODE is mode of the decl if this loc_descriptor is going to be used in
14521    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14522    allowed, VOIDmode otherwise.
14523
14524    If we don't know how to describe it, return 0.  */
14525
14526 static dw_loc_descr_ref
14527 loc_descriptor (rtx rtl, enum machine_mode mode,
14528                 enum var_init_status initialized)
14529 {
14530   dw_loc_descr_ref loc_result = NULL;
14531
14532   switch (GET_CODE (rtl))
14533     {
14534     case SUBREG:
14535       /* The case of a subreg may arise when we have a local (register)
14536          variable or a formal (register) parameter which doesn't quite fill
14537          up an entire register.  For now, just assume that it is
14538          legitimate to make the Dwarf info refer to the whole register which
14539          contains the given subreg.  */
14540       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14541       break;
14542
14543     case REG:
14544       loc_result = reg_loc_descriptor (rtl, initialized);
14545       break;
14546
14547     case MEM:
14548       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14549                                        initialized);
14550       if (loc_result == NULL)
14551         loc_result = tls_mem_loc_descriptor (rtl);
14552       if (loc_result == NULL)
14553         {
14554           rtx new_rtl = avoid_constant_pool_reference (rtl);
14555           if (new_rtl != rtl)
14556             loc_result = loc_descriptor (new_rtl, mode, initialized);
14557         }
14558       break;
14559
14560     case CONCAT:
14561       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14562                                           initialized);
14563       break;
14564
14565     case CONCATN:
14566       loc_result = concatn_loc_descriptor (rtl, initialized);
14567       break;
14568
14569     case VAR_LOCATION:
14570       /* Single part.  */
14571       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14572         {
14573           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14574           if (GET_CODE (loc) == EXPR_LIST)
14575             loc = XEXP (loc, 0);
14576           loc_result = loc_descriptor (loc, mode, initialized);
14577           break;
14578         }
14579
14580       rtl = XEXP (rtl, 1);
14581       /* FALLTHRU */
14582
14583     case PARALLEL:
14584       {
14585         rtvec par_elems = XVEC (rtl, 0);
14586         int num_elem = GET_NUM_ELEM (par_elems);
14587         enum machine_mode mode;
14588         int i;
14589
14590         /* Create the first one, so we have something to add to.  */
14591         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14592                                      VOIDmode, initialized);
14593         if (loc_result == NULL)
14594           return NULL;
14595         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14596         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14597         for (i = 1; i < num_elem; i++)
14598           {
14599             dw_loc_descr_ref temp;
14600
14601             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14602                                    VOIDmode, initialized);
14603             if (temp == NULL)
14604               return NULL;
14605             add_loc_descr (&loc_result, temp);
14606             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14607             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14608           }
14609       }
14610       break;
14611
14612     case CONST_INT:
14613       if (mode != VOIDmode && mode != BLKmode)
14614         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14615                                                     INTVAL (rtl));
14616       break;
14617
14618     case CONST_DOUBLE:
14619       if (mode == VOIDmode)
14620         mode = GET_MODE (rtl);
14621
14622       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14623         {
14624           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14625
14626           /* Note that a CONST_DOUBLE rtx could represent either an integer
14627              or a floating-point constant.  A CONST_DOUBLE is used whenever
14628              the constant requires more than one word in order to be
14629              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14630           loc_result = new_loc_descr (DW_OP_implicit_value,
14631                                       GET_MODE_SIZE (mode), 0);
14632           if (SCALAR_FLOAT_MODE_P (mode))
14633             {
14634               unsigned int length = GET_MODE_SIZE (mode);
14635               unsigned char *array
14636                   = (unsigned char*) ggc_alloc_atomic (length);
14637
14638               insert_float (rtl, array);
14639               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14640               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14641               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14642               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14643             }
14644           else
14645             {
14646               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14647               loc_result->dw_loc_oprnd2.v.val_double
14648                 = rtx_to_double_int (rtl);
14649             }
14650         }
14651       break;
14652
14653     case CONST_VECTOR:
14654       if (mode == VOIDmode)
14655         mode = GET_MODE (rtl);
14656
14657       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14658         {
14659           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14660           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14661           unsigned char *array = (unsigned char *)
14662             ggc_alloc_atomic (length * elt_size);
14663           unsigned int i;
14664           unsigned char *p;
14665
14666           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14667           switch (GET_MODE_CLASS (mode))
14668             {
14669             case MODE_VECTOR_INT:
14670               for (i = 0, p = array; i < length; i++, p += elt_size)
14671                 {
14672                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14673                   double_int val = rtx_to_double_int (elt);
14674
14675                   if (elt_size <= sizeof (HOST_WIDE_INT))
14676                     insert_int (double_int_to_shwi (val), elt_size, p);
14677                   else
14678                     {
14679                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14680                       insert_double (val, p);
14681                     }
14682                 }
14683               break;
14684
14685             case MODE_VECTOR_FLOAT:
14686               for (i = 0, p = array; i < length; i++, p += elt_size)
14687                 {
14688                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14689                   insert_float (elt, p);
14690                 }
14691               break;
14692
14693             default:
14694               gcc_unreachable ();
14695             }
14696
14697           loc_result = new_loc_descr (DW_OP_implicit_value,
14698                                       length * elt_size, 0);
14699           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14700           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14701           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14702           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14703         }
14704       break;
14705
14706     case CONST:
14707       if (mode == VOIDmode
14708           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14709           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14710           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14711         {
14712           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14713           break;
14714         }
14715       /* FALLTHROUGH */
14716     case SYMBOL_REF:
14717       if (!const_ok_for_output (rtl))
14718         break;
14719     case LABEL_REF:
14720       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14721           && (dwarf_version >= 4 || !dwarf_strict))
14722         {
14723           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14724           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14725           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14726           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14727           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14728         }
14729       break;
14730
14731     case DEBUG_IMPLICIT_PTR:
14732       loc_result = implicit_ptr_descriptor (rtl, 0);
14733       break;
14734
14735     case PLUS:
14736       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14737           && CONST_INT_P (XEXP (rtl, 1)))
14738         {
14739           loc_result
14740             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14741           break;
14742         }
14743       /* FALLTHRU */
14744     default:
14745       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14746           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14747           && (dwarf_version >= 4 || !dwarf_strict))
14748         {
14749           /* Value expression.  */
14750           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14751           if (loc_result)
14752             add_loc_descr (&loc_result,
14753                            new_loc_descr (DW_OP_stack_value, 0, 0));
14754         }
14755       break;
14756     }
14757
14758   return loc_result;
14759 }
14760
14761 /* We need to figure out what section we should use as the base for the
14762    address ranges where a given location is valid.
14763    1. If this particular DECL has a section associated with it, use that.
14764    2. If this function has a section associated with it, use that.
14765    3. Otherwise, use the text section.
14766    XXX: If you split a variable across multiple sections, we won't notice.  */
14767
14768 static const char *
14769 secname_for_decl (const_tree decl)
14770 {
14771   const char *secname;
14772
14773   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14774     {
14775       tree sectree = DECL_SECTION_NAME (decl);
14776       secname = TREE_STRING_POINTER (sectree);
14777     }
14778   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14779     {
14780       tree sectree = DECL_SECTION_NAME (current_function_decl);
14781       secname = TREE_STRING_POINTER (sectree);
14782     }
14783   else if (cfun && in_cold_section_p)
14784     secname = crtl->subsections.cold_section_label;
14785   else
14786     secname = text_section_label;
14787
14788   return secname;
14789 }
14790
14791 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14792
14793 static bool
14794 decl_by_reference_p (tree decl)
14795 {
14796   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14797            || TREE_CODE (decl) == VAR_DECL)
14798           && DECL_BY_REFERENCE (decl));
14799 }
14800
14801 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14802    for VARLOC.  */
14803
14804 static dw_loc_descr_ref
14805 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14806                enum var_init_status initialized)
14807 {
14808   int have_address = 0;
14809   dw_loc_descr_ref descr;
14810   enum machine_mode mode;
14811
14812   if (want_address != 2)
14813     {
14814       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14815       /* Single part.  */
14816       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14817         {
14818           varloc = PAT_VAR_LOCATION_LOC (varloc);
14819           if (GET_CODE (varloc) == EXPR_LIST)
14820             varloc = XEXP (varloc, 0);
14821           mode = GET_MODE (varloc);
14822           if (MEM_P (varloc))
14823             {
14824               rtx addr = XEXP (varloc, 0);
14825               descr = mem_loc_descriptor (addr, mode, initialized);
14826               if (descr)
14827                 have_address = 1;
14828               else
14829                 {
14830                   rtx x = avoid_constant_pool_reference (varloc);
14831                   if (x != varloc)
14832                     descr = mem_loc_descriptor (x, mode, initialized);
14833                 }
14834             }
14835           else
14836             descr = mem_loc_descriptor (varloc, mode, initialized);
14837         }
14838       else
14839         return 0;
14840     }
14841   else
14842     {
14843       if (GET_CODE (varloc) == VAR_LOCATION)
14844         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14845       else
14846         mode = DECL_MODE (loc);
14847       descr = loc_descriptor (varloc, mode, initialized);
14848       have_address = 1;
14849     }
14850
14851   if (!descr)
14852     return 0;
14853
14854   if (want_address == 2 && !have_address
14855       && (dwarf_version >= 4 || !dwarf_strict))
14856     {
14857       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14858         {
14859           expansion_failed (loc, NULL_RTX,
14860                             "DWARF address size mismatch");
14861           return 0;
14862         }
14863       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14864       have_address = 1;
14865     }
14866   /* Show if we can't fill the request for an address.  */
14867   if (want_address && !have_address)
14868     {
14869       expansion_failed (loc, NULL_RTX,
14870                         "Want address and only have value");
14871       return 0;
14872     }
14873
14874   /* If we've got an address and don't want one, dereference.  */
14875   if (!want_address && have_address)
14876     {
14877       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14878       enum dwarf_location_atom op;
14879
14880       if (size > DWARF2_ADDR_SIZE || size == -1)
14881         {
14882           expansion_failed (loc, NULL_RTX,
14883                             "DWARF address size mismatch");
14884           return 0;
14885         }
14886       else if (size == DWARF2_ADDR_SIZE)
14887         op = DW_OP_deref;
14888       else
14889         op = DW_OP_deref_size;
14890
14891       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14892     }
14893
14894   return descr;
14895 }
14896
14897 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14898    if it is not possible.  */
14899
14900 static dw_loc_descr_ref
14901 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14902 {
14903   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14904     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14905   else if (dwarf_version >= 3 || !dwarf_strict)
14906     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14907   else
14908     return NULL;
14909 }
14910
14911 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14912    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14913
14914 static dw_loc_descr_ref
14915 dw_sra_loc_expr (tree decl, rtx loc)
14916 {
14917   rtx p;
14918   unsigned int padsize = 0;
14919   dw_loc_descr_ref descr, *descr_tail;
14920   unsigned HOST_WIDE_INT decl_size;
14921   rtx varloc;
14922   enum var_init_status initialized;
14923
14924   if (DECL_SIZE (decl) == NULL
14925       || !host_integerp (DECL_SIZE (decl), 1))
14926     return NULL;
14927
14928   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14929   descr = NULL;
14930   descr_tail = &descr;
14931
14932   for (p = loc; p; p = XEXP (p, 1))
14933     {
14934       unsigned int bitsize = decl_piece_bitsize (p);
14935       rtx loc_note = *decl_piece_varloc_ptr (p);
14936       dw_loc_descr_ref cur_descr;
14937       dw_loc_descr_ref *tail, last = NULL;
14938       unsigned int opsize = 0;
14939
14940       if (loc_note == NULL_RTX
14941           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14942         {
14943           padsize += bitsize;
14944           continue;
14945         }
14946       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14947       varloc = NOTE_VAR_LOCATION (loc_note);
14948       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14949       if (cur_descr == NULL)
14950         {
14951           padsize += bitsize;
14952           continue;
14953         }
14954
14955       /* Check that cur_descr either doesn't use
14956          DW_OP_*piece operations, or their sum is equal
14957          to bitsize.  Otherwise we can't embed it.  */
14958       for (tail = &cur_descr; *tail != NULL;
14959            tail = &(*tail)->dw_loc_next)
14960         if ((*tail)->dw_loc_opc == DW_OP_piece)
14961           {
14962             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14963                       * BITS_PER_UNIT;
14964             last = *tail;
14965           }
14966         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14967           {
14968             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14969             last = *tail;
14970           }
14971
14972       if (last != NULL && opsize != bitsize)
14973         {
14974           padsize += bitsize;
14975           continue;
14976         }
14977
14978       /* If there is a hole, add DW_OP_*piece after empty DWARF
14979          expression, which means that those bits are optimized out.  */
14980       if (padsize)
14981         {
14982           if (padsize > decl_size)
14983             return NULL;
14984           decl_size -= padsize;
14985           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14986           if (*descr_tail == NULL)
14987             return NULL;
14988           descr_tail = &(*descr_tail)->dw_loc_next;
14989           padsize = 0;
14990         }
14991       *descr_tail = cur_descr;
14992       descr_tail = tail;
14993       if (bitsize > decl_size)
14994         return NULL;
14995       decl_size -= bitsize;
14996       if (last == NULL)
14997         {
14998           HOST_WIDE_INT offset = 0;
14999           if (GET_CODE (varloc) == VAR_LOCATION
15000               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15001             {
15002               varloc = PAT_VAR_LOCATION_LOC (varloc);
15003               if (GET_CODE (varloc) == EXPR_LIST)
15004                 varloc = XEXP (varloc, 0);
15005             }
15006           do 
15007             {
15008               if (GET_CODE (varloc) == CONST
15009                   || GET_CODE (varloc) == SIGN_EXTEND
15010                   || GET_CODE (varloc) == ZERO_EXTEND)
15011                 varloc = XEXP (varloc, 0);
15012               else if (GET_CODE (varloc) == SUBREG)
15013                 varloc = SUBREG_REG (varloc);
15014               else
15015                 break;
15016             }
15017           while (1);
15018           /* DW_OP_bit_size offset should be zero for register
15019              or implicit location descriptions and empty location
15020              descriptions, but for memory addresses needs big endian
15021              adjustment.  */
15022           if (MEM_P (varloc))
15023             {
15024               unsigned HOST_WIDE_INT memsize
15025                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15026               if (memsize != bitsize)
15027                 {
15028                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15029                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15030                     return NULL;
15031                   if (memsize < bitsize)
15032                     return NULL;
15033                   if (BITS_BIG_ENDIAN)
15034                     offset = memsize - bitsize;
15035                 }
15036             }
15037
15038           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15039           if (*descr_tail == NULL)
15040             return NULL;
15041           descr_tail = &(*descr_tail)->dw_loc_next;
15042         }
15043     }
15044
15045   /* If there were any non-empty expressions, add padding till the end of
15046      the decl.  */
15047   if (descr != NULL && decl_size != 0)
15048     {
15049       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15050       if (*descr_tail == NULL)
15051         return NULL;
15052     }
15053   return descr;
15054 }
15055
15056 /* Return the dwarf representation of the location list LOC_LIST of
15057    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15058    function.  */
15059
15060 static dw_loc_list_ref
15061 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15062 {
15063   const char *endname, *secname;
15064   rtx varloc;
15065   enum var_init_status initialized;
15066   struct var_loc_node *node;
15067   dw_loc_descr_ref descr;
15068   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15069   dw_loc_list_ref list = NULL;
15070   dw_loc_list_ref *listp = &list;
15071
15072   /* Now that we know what section we are using for a base,
15073      actually construct the list of locations.
15074      The first location information is what is passed to the
15075      function that creates the location list, and the remaining
15076      locations just get added on to that list.
15077      Note that we only know the start address for a location
15078      (IE location changes), so to build the range, we use
15079      the range [current location start, next location start].
15080      This means we have to special case the last node, and generate
15081      a range of [last location start, end of function label].  */
15082
15083   secname = secname_for_decl (decl);
15084
15085   for (node = loc_list->first; node; node = node->next)
15086     if (GET_CODE (node->loc) == EXPR_LIST
15087         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15088       {
15089         if (GET_CODE (node->loc) == EXPR_LIST)
15090           {
15091             /* This requires DW_OP_{,bit_}piece, which is not usable
15092                inside DWARF expressions.  */
15093             if (want_address != 2)
15094               continue;
15095             descr = dw_sra_loc_expr (decl, node->loc);
15096             if (descr == NULL)
15097               continue;
15098           }
15099         else
15100           {
15101             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15102             varloc = NOTE_VAR_LOCATION (node->loc);
15103             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15104           }
15105         if (descr)
15106           {
15107             bool range_across_switch = false;
15108             /* If section switch happens in between node->label
15109                and node->next->label (or end of function) and
15110                we can't emit it as a single entry list,
15111                emit two ranges, first one ending at the end
15112                of first partition and second one starting at the
15113                beginning of second partition.  */
15114             if (node == loc_list->last_before_switch
15115                 && (node != loc_list->first || loc_list->first->next)
15116                 && current_function_decl)
15117               {
15118                 endname = current_fde ()->dw_fde_end;
15119                 range_across_switch = true;
15120               }
15121             /* The variable has a location between NODE->LABEL and
15122                NODE->NEXT->LABEL.  */
15123             else if (node->next)
15124               endname = node->next->label;
15125             /* If the variable has a location at the last label
15126                it keeps its location until the end of function.  */
15127             else if (!current_function_decl)
15128               endname = text_end_label;
15129             else
15130               {
15131                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15132                                              current_function_funcdef_no);
15133                 endname = ggc_strdup (label_id);
15134               }
15135
15136             *listp = new_loc_list (descr, node->label, endname, secname);
15137             listp = &(*listp)->dw_loc_next;
15138
15139             if (range_across_switch)
15140               {
15141                 if (GET_CODE (node->loc) == EXPR_LIST)
15142                   descr = dw_sra_loc_expr (decl, node->loc);
15143                 else
15144                   {
15145                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15146                     varloc = NOTE_VAR_LOCATION (node->loc);
15147                     descr = dw_loc_list_1 (decl, varloc, want_address,
15148                                            initialized);
15149                   }
15150                 gcc_assert (descr);
15151                 /* The variable has a location between NODE->LABEL and
15152                    NODE->NEXT->LABEL.  */
15153                 if (node->next)
15154                   endname = node->next->label;
15155                 else
15156                   endname = current_fde ()->dw_fde_second_end;
15157                 *listp = new_loc_list (descr,
15158                                        current_fde ()->dw_fde_second_begin,
15159                                        endname, secname);
15160                 listp = &(*listp)->dw_loc_next;
15161               }
15162           }
15163       }
15164
15165   /* Try to avoid the overhead of a location list emitting a location
15166      expression instead, but only if we didn't have more than one
15167      location entry in the first place.  If some entries were not
15168      representable, we don't want to pretend a single entry that was
15169      applies to the entire scope in which the variable is
15170      available.  */
15171   if (list && loc_list->first->next)
15172     gen_llsym (list);
15173
15174   return list;
15175 }
15176
15177 /* Return if the loc_list has only single element and thus can be represented
15178    as location description.   */
15179
15180 static bool
15181 single_element_loc_list_p (dw_loc_list_ref list)
15182 {
15183   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15184   return !list->ll_symbol;
15185 }
15186
15187 /* To each location in list LIST add loc descr REF.  */
15188
15189 static void
15190 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15191 {
15192   dw_loc_descr_ref copy;
15193   add_loc_descr (&list->expr, ref);
15194   list = list->dw_loc_next;
15195   while (list)
15196     {
15197       copy = ggc_alloc_dw_loc_descr_node ();
15198       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15199       add_loc_descr (&list->expr, copy);
15200       while (copy->dw_loc_next)
15201         {
15202           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15203           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15204           copy->dw_loc_next = new_copy;
15205           copy = new_copy;
15206         }
15207       list = list->dw_loc_next;
15208     }
15209 }
15210
15211 /* Given two lists RET and LIST
15212    produce location list that is result of adding expression in LIST
15213    to expression in RET on each possition in program.
15214    Might be destructive on both RET and LIST.
15215
15216    TODO: We handle only simple cases of RET or LIST having at most one
15217    element. General case would inolve sorting the lists in program order
15218    and merging them that will need some additional work.
15219    Adding that will improve quality of debug info especially for SRA-ed
15220    structures.  */
15221
15222 static void
15223 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15224 {
15225   if (!list)
15226     return;
15227   if (!*ret)
15228     {
15229       *ret = list;
15230       return;
15231     }
15232   if (!list->dw_loc_next)
15233     {
15234       add_loc_descr_to_each (*ret, list->expr);
15235       return;
15236     }
15237   if (!(*ret)->dw_loc_next)
15238     {
15239       add_loc_descr_to_each (list, (*ret)->expr);
15240       *ret = list;
15241       return;
15242     }
15243   expansion_failed (NULL_TREE, NULL_RTX,
15244                     "Don't know how to merge two non-trivial"
15245                     " location lists.\n");
15246   *ret = NULL;
15247   return;
15248 }
15249
15250 /* LOC is constant expression.  Try a luck, look it up in constant
15251    pool and return its loc_descr of its address.  */
15252
15253 static dw_loc_descr_ref
15254 cst_pool_loc_descr (tree loc)
15255 {
15256   /* Get an RTL for this, if something has been emitted.  */
15257   rtx rtl = lookup_constant_def (loc);
15258   enum machine_mode mode;
15259
15260   if (!rtl || !MEM_P (rtl))
15261     {
15262       gcc_assert (!rtl);
15263       return 0;
15264     }
15265   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15266
15267   /* TODO: We might get more coverage if we was actually delaying expansion
15268      of all expressions till end of compilation when constant pools are fully
15269      populated.  */
15270   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15271     {
15272       expansion_failed (loc, NULL_RTX,
15273                         "CST value in contant pool but not marked.");
15274       return 0;
15275     }
15276   mode = GET_MODE (rtl);
15277   rtl = XEXP (rtl, 0);
15278   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15279 }
15280
15281 /* Return dw_loc_list representing address of addr_expr LOC
15282    by looking for innder INDIRECT_REF expression and turing it
15283    into simple arithmetics.  */
15284
15285 static dw_loc_list_ref
15286 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15287 {
15288   tree obj, offset;
15289   HOST_WIDE_INT bitsize, bitpos, bytepos;
15290   enum machine_mode mode;
15291   int volatilep;
15292   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15293   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15294
15295   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15296                              &bitsize, &bitpos, &offset, &mode,
15297                              &unsignedp, &volatilep, false);
15298   STRIP_NOPS (obj);
15299   if (bitpos % BITS_PER_UNIT)
15300     {
15301       expansion_failed (loc, NULL_RTX, "bitfield access");
15302       return 0;
15303     }
15304   if (!INDIRECT_REF_P (obj))
15305     {
15306       expansion_failed (obj,
15307                         NULL_RTX, "no indirect ref in inner refrence");
15308       return 0;
15309     }
15310   if (!offset && !bitpos)
15311     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15312   else if (toplev
15313            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15314            && (dwarf_version >= 4 || !dwarf_strict))
15315     {
15316       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15317       if (!list_ret)
15318         return 0;
15319       if (offset)
15320         {
15321           /* Variable offset.  */
15322           list_ret1 = loc_list_from_tree (offset, 0);
15323           if (list_ret1 == 0)
15324             return 0;
15325           add_loc_list (&list_ret, list_ret1);
15326           if (!list_ret)
15327             return 0;
15328           add_loc_descr_to_each (list_ret,
15329                                  new_loc_descr (DW_OP_plus, 0, 0));
15330         }
15331       bytepos = bitpos / BITS_PER_UNIT;
15332       if (bytepos > 0)
15333         add_loc_descr_to_each (list_ret,
15334                                new_loc_descr (DW_OP_plus_uconst,
15335                                               bytepos, 0));
15336       else if (bytepos < 0)
15337         loc_list_plus_const (list_ret, bytepos);
15338       add_loc_descr_to_each (list_ret,
15339                              new_loc_descr (DW_OP_stack_value, 0, 0));
15340     }
15341   return list_ret;
15342 }
15343
15344
15345 /* Generate Dwarf location list representing LOC.
15346    If WANT_ADDRESS is false, expression computing LOC will be computed
15347    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15348    if WANT_ADDRESS is 2, expression computing address useable in location
15349      will be returned (i.e. DW_OP_reg can be used
15350      to refer to register values).  */
15351
15352 static dw_loc_list_ref
15353 loc_list_from_tree (tree loc, int want_address)
15354 {
15355   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15356   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15357   int have_address = 0;
15358   enum dwarf_location_atom op;
15359
15360   /* ??? Most of the time we do not take proper care for sign/zero
15361      extending the values properly.  Hopefully this won't be a real
15362      problem...  */
15363
15364   switch (TREE_CODE (loc))
15365     {
15366     case ERROR_MARK:
15367       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15368       return 0;
15369
15370     case PLACEHOLDER_EXPR:
15371       /* This case involves extracting fields from an object to determine the
15372          position of other fields.  We don't try to encode this here.  The
15373          only user of this is Ada, which encodes the needed information using
15374          the names of types.  */
15375       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15376       return 0;
15377
15378     case CALL_EXPR:
15379       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15380       /* There are no opcodes for these operations.  */
15381       return 0;
15382
15383     case PREINCREMENT_EXPR:
15384     case PREDECREMENT_EXPR:
15385     case POSTINCREMENT_EXPR:
15386     case POSTDECREMENT_EXPR:
15387       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15388       /* There are no opcodes for these operations.  */
15389       return 0;
15390
15391     case ADDR_EXPR:
15392       /* If we already want an address, see if there is INDIRECT_REF inside
15393          e.g. for &this->field.  */
15394       if (want_address)
15395         {
15396           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15397                        (loc, want_address == 2);
15398           if (list_ret)
15399             have_address = 1;
15400           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15401                    && (ret = cst_pool_loc_descr (loc)))
15402             have_address = 1;
15403         }
15404         /* Otherwise, process the argument and look for the address.  */
15405       if (!list_ret && !ret)
15406         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15407       else
15408         {
15409           if (want_address)
15410             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15411           return NULL;
15412         }
15413       break;
15414
15415     case VAR_DECL:
15416       if (DECL_THREAD_LOCAL_P (loc))
15417         {
15418           rtx rtl;
15419           enum dwarf_location_atom first_op;
15420           enum dwarf_location_atom second_op;
15421           bool dtprel = false;
15422
15423           if (targetm.have_tls)
15424             {
15425               /* If this is not defined, we have no way to emit the
15426                  data.  */
15427               if (!targetm.asm_out.output_dwarf_dtprel)
15428                 return 0;
15429
15430                /* The way DW_OP_GNU_push_tls_address is specified, we
15431                   can only look up addresses of objects in the current
15432                   module.  We used DW_OP_addr as first op, but that's
15433                   wrong, because DW_OP_addr is relocated by the debug
15434                   info consumer, while DW_OP_GNU_push_tls_address
15435                   operand shouldn't be.  */
15436               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15437                 return 0;
15438               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15439               dtprel = true;
15440               second_op = DW_OP_GNU_push_tls_address;
15441             }
15442           else
15443             {
15444               if (!targetm.emutls.debug_form_tls_address
15445                   || !(dwarf_version >= 3 || !dwarf_strict))
15446                 return 0;
15447               /* We stuffed the control variable into the DECL_VALUE_EXPR
15448                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15449                  no longer appear in gimple code.  We used the control
15450                  variable in specific so that we could pick it up here.  */
15451               loc = DECL_VALUE_EXPR (loc);
15452               first_op = DW_OP_addr;
15453               second_op = DW_OP_form_tls_address;
15454             }
15455
15456           rtl = rtl_for_decl_location (loc);
15457           if (rtl == NULL_RTX)
15458             return 0;
15459
15460           if (!MEM_P (rtl))
15461             return 0;
15462           rtl = XEXP (rtl, 0);
15463           if (! CONSTANT_P (rtl))
15464             return 0;
15465
15466           ret = new_loc_descr (first_op, 0, 0);
15467           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15468           ret->dw_loc_oprnd1.v.val_addr = rtl;
15469           ret->dtprel = dtprel;
15470
15471           ret1 = new_loc_descr (second_op, 0, 0);
15472           add_loc_descr (&ret, ret1);
15473
15474           have_address = 1;
15475           break;
15476         }
15477       /* FALLTHRU */
15478
15479     case PARM_DECL:
15480     case RESULT_DECL:
15481       if (DECL_HAS_VALUE_EXPR_P (loc))
15482         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15483                                    want_address);
15484       /* FALLTHRU */
15485
15486     case FUNCTION_DECL:
15487       {
15488         rtx rtl;
15489         var_loc_list *loc_list = lookup_decl_loc (loc);
15490
15491         if (loc_list && loc_list->first)
15492           {
15493             list_ret = dw_loc_list (loc_list, loc, want_address);
15494             have_address = want_address != 0;
15495             break;
15496           }
15497         rtl = rtl_for_decl_location (loc);
15498         if (rtl == NULL_RTX)
15499           {
15500             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15501             return 0;
15502           }
15503         else if (CONST_INT_P (rtl))
15504           {
15505             HOST_WIDE_INT val = INTVAL (rtl);
15506             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15507               val &= GET_MODE_MASK (DECL_MODE (loc));
15508             ret = int_loc_descriptor (val);
15509           }
15510         else if (GET_CODE (rtl) == CONST_STRING)
15511           {
15512             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15513             return 0;
15514           }
15515         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15516           {
15517             ret = new_loc_descr (DW_OP_addr, 0, 0);
15518             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15519             ret->dw_loc_oprnd1.v.val_addr = rtl;
15520           }
15521         else
15522           {
15523             enum machine_mode mode;
15524
15525             /* Certain constructs can only be represented at top-level.  */
15526             if (want_address == 2)
15527               {
15528                 ret = loc_descriptor (rtl, VOIDmode,
15529                                       VAR_INIT_STATUS_INITIALIZED);
15530                 have_address = 1;
15531               }
15532             else
15533               {
15534                 mode = GET_MODE (rtl);
15535                 if (MEM_P (rtl))
15536                   {
15537                     rtl = XEXP (rtl, 0);
15538                     have_address = 1;
15539                   }
15540                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15541               }
15542             if (!ret)
15543               expansion_failed (loc, rtl,
15544                                 "failed to produce loc descriptor for rtl");
15545           }
15546       }
15547       break;
15548
15549     case MEM_REF:
15550       /* ??? FIXME.  */
15551       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15552         return 0;
15553       /* Fallthru.  */
15554     case INDIRECT_REF:
15555       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15556       have_address = 1;
15557       break;
15558
15559     case COMPOUND_EXPR:
15560       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15561
15562     CASE_CONVERT:
15563     case VIEW_CONVERT_EXPR:
15564     case SAVE_EXPR:
15565     case MODIFY_EXPR:
15566       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15567
15568     case COMPONENT_REF:
15569     case BIT_FIELD_REF:
15570     case ARRAY_REF:
15571     case ARRAY_RANGE_REF:
15572     case REALPART_EXPR:
15573     case IMAGPART_EXPR:
15574       {
15575         tree obj, offset;
15576         HOST_WIDE_INT bitsize, bitpos, bytepos;
15577         enum machine_mode mode;
15578         int volatilep;
15579         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15580
15581         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15582                                    &unsignedp, &volatilep, false);
15583
15584         gcc_assert (obj != loc);
15585
15586         list_ret = loc_list_from_tree (obj,
15587                                        want_address == 2
15588                                        && !bitpos && !offset ? 2 : 1);
15589         /* TODO: We can extract value of the small expression via shifting even
15590            for nonzero bitpos.  */
15591         if (list_ret == 0)
15592           return 0;
15593         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15594           {
15595             expansion_failed (loc, NULL_RTX,
15596                               "bitfield access");
15597             return 0;
15598           }
15599
15600         if (offset != NULL_TREE)
15601           {
15602             /* Variable offset.  */
15603             list_ret1 = loc_list_from_tree (offset, 0);
15604             if (list_ret1 == 0)
15605               return 0;
15606             add_loc_list (&list_ret, list_ret1);
15607             if (!list_ret)
15608               return 0;
15609             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15610           }
15611
15612         bytepos = bitpos / BITS_PER_UNIT;
15613         if (bytepos > 0)
15614           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15615         else if (bytepos < 0)
15616           loc_list_plus_const (list_ret, bytepos);
15617
15618         have_address = 1;
15619         break;
15620       }
15621
15622     case INTEGER_CST:
15623       if ((want_address || !host_integerp (loc, 0))
15624           && (ret = cst_pool_loc_descr (loc)))
15625         have_address = 1;
15626       else if (want_address == 2
15627                && host_integerp (loc, 0)
15628                && (ret = address_of_int_loc_descriptor
15629                            (int_size_in_bytes (TREE_TYPE (loc)),
15630                             tree_low_cst (loc, 0))))
15631         have_address = 1;
15632       else if (host_integerp (loc, 0))
15633         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15634       else
15635         {
15636           expansion_failed (loc, NULL_RTX,
15637                             "Integer operand is not host integer");
15638           return 0;
15639         }
15640       break;
15641
15642     case CONSTRUCTOR:
15643     case REAL_CST:
15644     case STRING_CST:
15645     case COMPLEX_CST:
15646       if ((ret = cst_pool_loc_descr (loc)))
15647         have_address = 1;
15648       else
15649       /* We can construct small constants here using int_loc_descriptor.  */
15650         expansion_failed (loc, NULL_RTX,
15651                           "constructor or constant not in constant pool");
15652       break;
15653
15654     case TRUTH_AND_EXPR:
15655     case TRUTH_ANDIF_EXPR:
15656     case BIT_AND_EXPR:
15657       op = DW_OP_and;
15658       goto do_binop;
15659
15660     case TRUTH_XOR_EXPR:
15661     case BIT_XOR_EXPR:
15662       op = DW_OP_xor;
15663       goto do_binop;
15664
15665     case TRUTH_OR_EXPR:
15666     case TRUTH_ORIF_EXPR:
15667     case BIT_IOR_EXPR:
15668       op = DW_OP_or;
15669       goto do_binop;
15670
15671     case FLOOR_DIV_EXPR:
15672     case CEIL_DIV_EXPR:
15673     case ROUND_DIV_EXPR:
15674     case TRUNC_DIV_EXPR:
15675       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15676         return 0;
15677       op = DW_OP_div;
15678       goto do_binop;
15679
15680     case MINUS_EXPR:
15681       op = DW_OP_minus;
15682       goto do_binop;
15683
15684     case FLOOR_MOD_EXPR:
15685     case CEIL_MOD_EXPR:
15686     case ROUND_MOD_EXPR:
15687     case TRUNC_MOD_EXPR:
15688       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15689         {
15690           op = DW_OP_mod;
15691           goto do_binop;
15692         }
15693       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15694       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15695       if (list_ret == 0 || list_ret1 == 0)
15696         return 0;
15697
15698       add_loc_list (&list_ret, list_ret1);
15699       if (list_ret == 0)
15700         return 0;
15701       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15702       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15703       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15704       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15705       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15706       break;
15707
15708     case MULT_EXPR:
15709       op = DW_OP_mul;
15710       goto do_binop;
15711
15712     case LSHIFT_EXPR:
15713       op = DW_OP_shl;
15714       goto do_binop;
15715
15716     case RSHIFT_EXPR:
15717       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15718       goto do_binop;
15719
15720     case POINTER_PLUS_EXPR:
15721     case PLUS_EXPR:
15722       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15723         {
15724           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15725           if (list_ret == 0)
15726             return 0;
15727
15728           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15729           break;
15730         }
15731
15732       op = DW_OP_plus;
15733       goto do_binop;
15734
15735     case LE_EXPR:
15736       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15737         return 0;
15738
15739       op = DW_OP_le;
15740       goto do_binop;
15741
15742     case GE_EXPR:
15743       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15744         return 0;
15745
15746       op = DW_OP_ge;
15747       goto do_binop;
15748
15749     case LT_EXPR:
15750       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15751         return 0;
15752
15753       op = DW_OP_lt;
15754       goto do_binop;
15755
15756     case GT_EXPR:
15757       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15758         return 0;
15759
15760       op = DW_OP_gt;
15761       goto do_binop;
15762
15763     case EQ_EXPR:
15764       op = DW_OP_eq;
15765       goto do_binop;
15766
15767     case NE_EXPR:
15768       op = DW_OP_ne;
15769       goto do_binop;
15770
15771     do_binop:
15772       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15773       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15774       if (list_ret == 0 || list_ret1 == 0)
15775         return 0;
15776
15777       add_loc_list (&list_ret, list_ret1);
15778       if (list_ret == 0)
15779         return 0;
15780       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15781       break;
15782
15783     case TRUTH_NOT_EXPR:
15784     case BIT_NOT_EXPR:
15785       op = DW_OP_not;
15786       goto do_unop;
15787
15788     case ABS_EXPR:
15789       op = DW_OP_abs;
15790       goto do_unop;
15791
15792     case NEGATE_EXPR:
15793       op = DW_OP_neg;
15794       goto do_unop;
15795
15796     do_unop:
15797       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15798       if (list_ret == 0)
15799         return 0;
15800
15801       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15802       break;
15803
15804     case MIN_EXPR:
15805     case MAX_EXPR:
15806       {
15807         const enum tree_code code =
15808           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15809
15810         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15811                       build2 (code, integer_type_node,
15812                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15813                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15814       }
15815
15816       /* ... fall through ...  */
15817
15818     case COND_EXPR:
15819       {
15820         dw_loc_descr_ref lhs
15821           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15822         dw_loc_list_ref rhs
15823           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15824         dw_loc_descr_ref bra_node, jump_node, tmp;
15825
15826         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15827         if (list_ret == 0 || lhs == 0 || rhs == 0)
15828           return 0;
15829
15830         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15831         add_loc_descr_to_each (list_ret, bra_node);
15832
15833         add_loc_list (&list_ret, rhs);
15834         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15835         add_loc_descr_to_each (list_ret, jump_node);
15836
15837         add_loc_descr_to_each (list_ret, lhs);
15838         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15839         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15840
15841         /* ??? Need a node to point the skip at.  Use a nop.  */
15842         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15843         add_loc_descr_to_each (list_ret, tmp);
15844         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15845         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15846       }
15847       break;
15848
15849     case FIX_TRUNC_EXPR:
15850       return 0;
15851
15852     default:
15853       /* Leave front-end specific codes as simply unknown.  This comes
15854          up, for instance, with the C STMT_EXPR.  */
15855       if ((unsigned int) TREE_CODE (loc)
15856           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15857         {
15858           expansion_failed (loc, NULL_RTX,
15859                             "language specific tree node");
15860           return 0;
15861         }
15862
15863 #ifdef ENABLE_CHECKING
15864       /* Otherwise this is a generic code; we should just lists all of
15865          these explicitly.  We forgot one.  */
15866       gcc_unreachable ();
15867 #else
15868       /* In a release build, we want to degrade gracefully: better to
15869          generate incomplete debugging information than to crash.  */
15870       return NULL;
15871 #endif
15872     }
15873
15874   if (!ret && !list_ret)
15875     return 0;
15876
15877   if (want_address == 2 && !have_address
15878       && (dwarf_version >= 4 || !dwarf_strict))
15879     {
15880       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15881         {
15882           expansion_failed (loc, NULL_RTX,
15883                             "DWARF address size mismatch");
15884           return 0;
15885         }
15886       if (ret)
15887         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15888       else
15889         add_loc_descr_to_each (list_ret,
15890                                new_loc_descr (DW_OP_stack_value, 0, 0));
15891       have_address = 1;
15892     }
15893   /* Show if we can't fill the request for an address.  */
15894   if (want_address && !have_address)
15895     {
15896       expansion_failed (loc, NULL_RTX,
15897                         "Want address and only have value");
15898       return 0;
15899     }
15900
15901   gcc_assert (!ret || !list_ret);
15902
15903   /* If we've got an address and don't want one, dereference.  */
15904   if (!want_address && have_address)
15905     {
15906       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15907
15908       if (size > DWARF2_ADDR_SIZE || size == -1)
15909         {
15910           expansion_failed (loc, NULL_RTX,
15911                             "DWARF address size mismatch");
15912           return 0;
15913         }
15914       else if (size == DWARF2_ADDR_SIZE)
15915         op = DW_OP_deref;
15916       else
15917         op = DW_OP_deref_size;
15918
15919       if (ret)
15920         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15921       else
15922         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15923     }
15924   if (ret)
15925     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15926
15927   return list_ret;
15928 }
15929
15930 /* Same as above but return only single location expression.  */
15931 static dw_loc_descr_ref
15932 loc_descriptor_from_tree (tree loc, int want_address)
15933 {
15934   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15935   if (!ret)
15936     return NULL;
15937   if (ret->dw_loc_next)
15938     {
15939       expansion_failed (loc, NULL_RTX,
15940                         "Location list where only loc descriptor needed");
15941       return NULL;
15942     }
15943   return ret->expr;
15944 }
15945
15946 /* Given a value, round it up to the lowest multiple of `boundary'
15947    which is not less than the value itself.  */
15948
15949 static inline HOST_WIDE_INT
15950 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15951 {
15952   return (((value + boundary - 1) / boundary) * boundary);
15953 }
15954
15955 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15956    pointer to the declared type for the relevant field variable, or return
15957    `integer_type_node' if the given node turns out to be an
15958    ERROR_MARK node.  */
15959
15960 static inline tree
15961 field_type (const_tree decl)
15962 {
15963   tree type;
15964
15965   if (TREE_CODE (decl) == ERROR_MARK)
15966     return integer_type_node;
15967
15968   type = DECL_BIT_FIELD_TYPE (decl);
15969   if (type == NULL_TREE)
15970     type = TREE_TYPE (decl);
15971
15972   return type;
15973 }
15974
15975 /* Given a pointer to a tree node, return the alignment in bits for
15976    it, or else return BITS_PER_WORD if the node actually turns out to
15977    be an ERROR_MARK node.  */
15978
15979 static inline unsigned
15980 simple_type_align_in_bits (const_tree type)
15981 {
15982   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15983 }
15984
15985 static inline unsigned
15986 simple_decl_align_in_bits (const_tree decl)
15987 {
15988   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15989 }
15990
15991 /* Return the result of rounding T up to ALIGN.  */
15992
15993 static inline double_int
15994 round_up_to_align (double_int t, unsigned int align)
15995 {
15996   double_int alignd = uhwi_to_double_int (align);
15997   t = double_int_add (t, alignd);
15998   t = double_int_add (t, double_int_minus_one);
15999   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
16000   t = double_int_mul (t, alignd);
16001   return t;
16002 }
16003
16004 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
16005    lowest addressed byte of the "containing object" for the given FIELD_DECL,
16006    or return 0 if we are unable to determine what that offset is, either
16007    because the argument turns out to be a pointer to an ERROR_MARK node, or
16008    because the offset is actually variable.  (We can't handle the latter case
16009    just yet).  */
16010
16011 static HOST_WIDE_INT
16012 field_byte_offset (const_tree decl)
16013 {
16014   double_int object_offset_in_bits;
16015   double_int object_offset_in_bytes;
16016   double_int bitpos_int;
16017
16018   if (TREE_CODE (decl) == ERROR_MARK)
16019     return 0;
16020
16021   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16022
16023   /* We cannot yet cope with fields whose positions are variable, so
16024      for now, when we see such things, we simply return 0.  Someday, we may
16025      be able to handle such cases, but it will be damn difficult.  */
16026   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16027     return 0;
16028
16029   bitpos_int = tree_to_double_int (bit_position (decl));
16030
16031 #ifdef PCC_BITFIELD_TYPE_MATTERS
16032   if (PCC_BITFIELD_TYPE_MATTERS)
16033     {
16034       tree type;
16035       tree field_size_tree;
16036       double_int deepest_bitpos;
16037       double_int field_size_in_bits;
16038       unsigned int type_align_in_bits;
16039       unsigned int decl_align_in_bits;
16040       double_int type_size_in_bits;
16041
16042       type = field_type (decl);
16043       type_size_in_bits = double_int_type_size_in_bits (type);
16044       type_align_in_bits = simple_type_align_in_bits (type);
16045
16046       field_size_tree = DECL_SIZE (decl);
16047
16048       /* The size could be unspecified if there was an error, or for
16049          a flexible array member.  */
16050       if (!field_size_tree)
16051         field_size_tree = bitsize_zero_node;
16052
16053       /* If the size of the field is not constant, use the type size.  */
16054       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16055         field_size_in_bits = tree_to_double_int (field_size_tree);
16056       else
16057         field_size_in_bits = type_size_in_bits;
16058
16059       decl_align_in_bits = simple_decl_align_in_bits (decl);
16060
16061       /* The GCC front-end doesn't make any attempt to keep track of the
16062          starting bit offset (relative to the start of the containing
16063          structure type) of the hypothetical "containing object" for a
16064          bit-field.  Thus, when computing the byte offset value for the
16065          start of the "containing object" of a bit-field, we must deduce
16066          this information on our own. This can be rather tricky to do in
16067          some cases.  For example, handling the following structure type
16068          definition when compiling for an i386/i486 target (which only
16069          aligns long long's to 32-bit boundaries) can be very tricky:
16070
16071          struct S { int field1; long long field2:31; };
16072
16073          Fortunately, there is a simple rule-of-thumb which can be used
16074          in such cases.  When compiling for an i386/i486, GCC will
16075          allocate 8 bytes for the structure shown above.  It decides to
16076          do this based upon one simple rule for bit-field allocation.
16077          GCC allocates each "containing object" for each bit-field at
16078          the first (i.e. lowest addressed) legitimate alignment boundary
16079          (based upon the required minimum alignment for the declared
16080          type of the field) which it can possibly use, subject to the
16081          condition that there is still enough available space remaining
16082          in the containing object (when allocated at the selected point)
16083          to fully accommodate all of the bits of the bit-field itself.
16084
16085          This simple rule makes it obvious why GCC allocates 8 bytes for
16086          each object of the structure type shown above.  When looking
16087          for a place to allocate the "containing object" for `field2',
16088          the compiler simply tries to allocate a 64-bit "containing
16089          object" at each successive 32-bit boundary (starting at zero)
16090          until it finds a place to allocate that 64- bit field such that
16091          at least 31 contiguous (and previously unallocated) bits remain
16092          within that selected 64 bit field.  (As it turns out, for the
16093          example above, the compiler finds it is OK to allocate the
16094          "containing object" 64-bit field at bit-offset zero within the
16095          structure type.)
16096
16097          Here we attempt to work backwards from the limited set of facts
16098          we're given, and we try to deduce from those facts, where GCC
16099          must have believed that the containing object started (within
16100          the structure type). The value we deduce is then used (by the
16101          callers of this routine) to generate DW_AT_location and
16102          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16103          the case of DW_AT_location, regular fields as well).  */
16104
16105       /* Figure out the bit-distance from the start of the structure to
16106          the "deepest" bit of the bit-field.  */
16107       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16108
16109       /* This is the tricky part.  Use some fancy footwork to deduce
16110          where the lowest addressed bit of the containing object must
16111          be.  */
16112       object_offset_in_bits
16113         = double_int_sub (deepest_bitpos, type_size_in_bits);
16114
16115       /* Round up to type_align by default.  This works best for
16116          bitfields.  */
16117       object_offset_in_bits
16118         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16119
16120       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16121         {
16122           object_offset_in_bits
16123             = double_int_sub (deepest_bitpos, type_size_in_bits);
16124
16125           /* Round up to decl_align instead.  */
16126           object_offset_in_bits
16127             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16128         }
16129     }
16130   else
16131 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16132     object_offset_in_bits = bitpos_int;
16133
16134   object_offset_in_bytes
16135     = double_int_div (object_offset_in_bits,
16136                       uhwi_to_double_int (BITS_PER_UNIT), true,
16137                       TRUNC_DIV_EXPR);
16138   return double_int_to_shwi (object_offset_in_bytes);
16139 }
16140 \f
16141 /* The following routines define various Dwarf attributes and any data
16142    associated with them.  */
16143
16144 /* Add a location description attribute value to a DIE.
16145
16146    This emits location attributes suitable for whole variables and
16147    whole parameters.  Note that the location attributes for struct fields are
16148    generated by the routine `data_member_location_attribute' below.  */
16149
16150 static inline void
16151 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16152                              dw_loc_list_ref descr)
16153 {
16154   if (descr == 0)
16155     return;
16156   if (single_element_loc_list_p (descr))
16157     add_AT_loc (die, attr_kind, descr->expr);
16158   else
16159     add_AT_loc_list (die, attr_kind, descr);
16160 }
16161
16162 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16163
16164 static void
16165 add_accessibility_attribute (dw_die_ref die, tree decl)
16166 {
16167   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16168      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16169      the default has always been DW_ACCESS_public.  */
16170   if (TREE_PROTECTED (decl))
16171     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16172   else if (TREE_PRIVATE (decl))
16173     {
16174       if (dwarf_version == 2
16175           || die->die_parent == NULL
16176           || die->die_parent->die_tag != DW_TAG_class_type)
16177         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16178     }
16179   else if (dwarf_version > 2
16180            && die->die_parent
16181            && die->die_parent->die_tag == DW_TAG_class_type)
16182     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16183 }
16184
16185 /* Attach the specialized form of location attribute used for data members of
16186    struct and union types.  In the special case of a FIELD_DECL node which
16187    represents a bit-field, the "offset" part of this special location
16188    descriptor must indicate the distance in bytes from the lowest-addressed
16189    byte of the containing struct or union type to the lowest-addressed byte of
16190    the "containing object" for the bit-field.  (See the `field_byte_offset'
16191    function above).
16192
16193    For any given bit-field, the "containing object" is a hypothetical object
16194    (of some integral or enum type) within which the given bit-field lives.  The
16195    type of this hypothetical "containing object" is always the same as the
16196    declared type of the individual bit-field itself (for GCC anyway... the
16197    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16198    bytes) of the hypothetical "containing object" which will be given in the
16199    DW_AT_byte_size attribute for this bit-field.  (See the
16200    `byte_size_attribute' function below.)  It is also used when calculating the
16201    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16202    function below.)  */
16203
16204 static void
16205 add_data_member_location_attribute (dw_die_ref die, tree decl)
16206 {
16207   HOST_WIDE_INT offset;
16208   dw_loc_descr_ref loc_descr = 0;
16209
16210   if (TREE_CODE (decl) == TREE_BINFO)
16211     {
16212       /* We're working on the TAG_inheritance for a base class.  */
16213       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16214         {
16215           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16216              aren't at a fixed offset from all (sub)objects of the same
16217              type.  We need to extract the appropriate offset from our
16218              vtable.  The following dwarf expression means
16219
16220                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16221
16222              This is specific to the V3 ABI, of course.  */
16223
16224           dw_loc_descr_ref tmp;
16225
16226           /* Make a copy of the object address.  */
16227           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16228           add_loc_descr (&loc_descr, tmp);
16229
16230           /* Extract the vtable address.  */
16231           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16232           add_loc_descr (&loc_descr, tmp);
16233
16234           /* Calculate the address of the offset.  */
16235           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16236           gcc_assert (offset < 0);
16237
16238           tmp = int_loc_descriptor (-offset);
16239           add_loc_descr (&loc_descr, tmp);
16240           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16241           add_loc_descr (&loc_descr, tmp);
16242
16243           /* Extract the offset.  */
16244           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16245           add_loc_descr (&loc_descr, tmp);
16246
16247           /* Add it to the object address.  */
16248           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16249           add_loc_descr (&loc_descr, tmp);
16250         }
16251       else
16252         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16253     }
16254   else
16255     offset = field_byte_offset (decl);
16256
16257   if (! loc_descr)
16258     {
16259       if (dwarf_version > 2)
16260         {
16261           /* Don't need to output a location expression, just the constant. */
16262           if (offset < 0)
16263             add_AT_int (die, DW_AT_data_member_location, offset);
16264           else
16265             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16266           return;
16267         }
16268       else
16269         {
16270           enum dwarf_location_atom op;
16271
16272           /* The DWARF2 standard says that we should assume that the structure
16273              address is already on the stack, so we can specify a structure
16274              field address by using DW_OP_plus_uconst.  */
16275
16276 #ifdef MIPS_DEBUGGING_INFO
16277           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16278              operator correctly.  It works only if we leave the offset on the
16279              stack.  */
16280           op = DW_OP_constu;
16281 #else
16282           op = DW_OP_plus_uconst;
16283 #endif
16284
16285           loc_descr = new_loc_descr (op, offset, 0);
16286         }
16287     }
16288
16289   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16290 }
16291
16292 /* Writes integer values to dw_vec_const array.  */
16293
16294 static void
16295 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16296 {
16297   while (size != 0)
16298     {
16299       *dest++ = val & 0xff;
16300       val >>= 8;
16301       --size;
16302     }
16303 }
16304
16305 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16306
16307 static HOST_WIDE_INT
16308 extract_int (const unsigned char *src, unsigned int size)
16309 {
16310   HOST_WIDE_INT val = 0;
16311
16312   src += size;
16313   while (size != 0)
16314     {
16315       val <<= 8;
16316       val |= *--src & 0xff;
16317       --size;
16318     }
16319   return val;
16320 }
16321
16322 /* Writes double_int values to dw_vec_const array.  */
16323
16324 static void
16325 insert_double (double_int val, unsigned char *dest)
16326 {
16327   unsigned char *p0 = dest;
16328   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16329
16330   if (WORDS_BIG_ENDIAN)
16331     {
16332       p0 = p1;
16333       p1 = dest;
16334     }
16335
16336   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16337   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16338 }
16339
16340 /* Writes floating point values to dw_vec_const array.  */
16341
16342 static void
16343 insert_float (const_rtx rtl, unsigned char *array)
16344 {
16345   REAL_VALUE_TYPE rv;
16346   long val[4];
16347   int i;
16348
16349   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16350   real_to_target (val, &rv, GET_MODE (rtl));
16351
16352   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16353   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16354     {
16355       insert_int (val[i], 4, array);
16356       array += 4;
16357     }
16358 }
16359
16360 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16361    does not have a "location" either in memory or in a register.  These
16362    things can arise in GNU C when a constant is passed as an actual parameter
16363    to an inlined function.  They can also arise in C++ where declared
16364    constants do not necessarily get memory "homes".  */
16365
16366 static bool
16367 add_const_value_attribute (dw_die_ref die, rtx rtl)
16368 {
16369   switch (GET_CODE (rtl))
16370     {
16371     case CONST_INT:
16372       {
16373         HOST_WIDE_INT val = INTVAL (rtl);
16374
16375         if (val < 0)
16376           add_AT_int (die, DW_AT_const_value, val);
16377         else
16378           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16379       }
16380       return true;
16381
16382     case CONST_DOUBLE:
16383       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16384          floating-point constant.  A CONST_DOUBLE is used whenever the
16385          constant requires more than one word in order to be adequately
16386          represented.  */
16387       {
16388         enum machine_mode mode = GET_MODE (rtl);
16389
16390         if (SCALAR_FLOAT_MODE_P (mode))
16391           {
16392             unsigned int length = GET_MODE_SIZE (mode);
16393             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16394
16395             insert_float (rtl, array);
16396             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16397           }
16398         else
16399           add_AT_double (die, DW_AT_const_value,
16400                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16401       }
16402       return true;
16403
16404     case CONST_VECTOR:
16405       {
16406         enum machine_mode mode = GET_MODE (rtl);
16407         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16408         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16409         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16410           (length * elt_size);
16411         unsigned int i;
16412         unsigned char *p;
16413
16414         switch (GET_MODE_CLASS (mode))
16415           {
16416           case MODE_VECTOR_INT:
16417             for (i = 0, p = array; i < length; i++, p += elt_size)
16418               {
16419                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16420                 double_int val = rtx_to_double_int (elt);
16421
16422                 if (elt_size <= sizeof (HOST_WIDE_INT))
16423                   insert_int (double_int_to_shwi (val), elt_size, p);
16424                 else
16425                   {
16426                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16427                     insert_double (val, p);
16428                   }
16429               }
16430             break;
16431
16432           case MODE_VECTOR_FLOAT:
16433             for (i = 0, p = array; i < length; i++, p += elt_size)
16434               {
16435                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16436                 insert_float (elt, p);
16437               }
16438             break;
16439
16440           default:
16441             gcc_unreachable ();
16442           }
16443
16444         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16445       }
16446       return true;
16447
16448     case CONST_STRING:
16449       if (dwarf_version >= 4 || !dwarf_strict)
16450         {
16451           dw_loc_descr_ref loc_result;
16452           resolve_one_addr (&rtl, NULL);
16453         rtl_addr:
16454           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16455           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16456           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16457           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16458           add_AT_loc (die, DW_AT_location, loc_result);
16459           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16460           return true;
16461         }
16462       return false;
16463
16464     case CONST:
16465       if (CONSTANT_P (XEXP (rtl, 0)))
16466         return add_const_value_attribute (die, XEXP (rtl, 0));
16467       /* FALLTHROUGH */
16468     case SYMBOL_REF:
16469       if (!const_ok_for_output (rtl))
16470         return false;
16471     case LABEL_REF:
16472       if (dwarf_version >= 4 || !dwarf_strict)
16473         goto rtl_addr;
16474       return false;
16475
16476     case PLUS:
16477       /* In cases where an inlined instance of an inline function is passed
16478          the address of an `auto' variable (which is local to the caller) we
16479          can get a situation where the DECL_RTL of the artificial local
16480          variable (for the inlining) which acts as a stand-in for the
16481          corresponding formal parameter (of the inline function) will look
16482          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16483          exactly a compile-time constant expression, but it isn't the address
16484          of the (artificial) local variable either.  Rather, it represents the
16485          *value* which the artificial local variable always has during its
16486          lifetime.  We currently have no way to represent such quasi-constant
16487          values in Dwarf, so for now we just punt and generate nothing.  */
16488       return false;
16489
16490     case HIGH:
16491     case CONST_FIXED:
16492       return false;
16493
16494     case MEM:
16495       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16496           && MEM_READONLY_P (rtl)
16497           && GET_MODE (rtl) == BLKmode)
16498         {
16499           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16500           return true;
16501         }
16502       return false;
16503
16504     default:
16505       /* No other kinds of rtx should be possible here.  */
16506       gcc_unreachable ();
16507     }
16508   return false;
16509 }
16510
16511 /* Determine whether the evaluation of EXPR references any variables
16512    or functions which aren't otherwise used (and therefore may not be
16513    output).  */
16514 static tree
16515 reference_to_unused (tree * tp, int * walk_subtrees,
16516                      void * data ATTRIBUTE_UNUSED)
16517 {
16518   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16519     *walk_subtrees = 0;
16520
16521   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16522       && ! TREE_ASM_WRITTEN (*tp))
16523     return *tp;
16524   /* ???  The C++ FE emits debug information for using decls, so
16525      putting gcc_unreachable here falls over.  See PR31899.  For now
16526      be conservative.  */
16527   else if (!cgraph_global_info_ready
16528            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16529     return *tp;
16530   else if (TREE_CODE (*tp) == VAR_DECL)
16531     {
16532       struct varpool_node *node = varpool_get_node (*tp);
16533       if (!node || !node->needed)
16534         return *tp;
16535     }
16536   else if (TREE_CODE (*tp) == FUNCTION_DECL
16537            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16538     {
16539       /* The call graph machinery must have finished analyzing,
16540          optimizing and gimplifying the CU by now.
16541          So if *TP has no call graph node associated
16542          to it, it means *TP will not be emitted.  */
16543       if (!cgraph_get_node (*tp))
16544         return *tp;
16545     }
16546   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16547     return *tp;
16548
16549   return NULL_TREE;
16550 }
16551
16552 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16553    for use in a later add_const_value_attribute call.  */
16554
16555 static rtx
16556 rtl_for_decl_init (tree init, tree type)
16557 {
16558   rtx rtl = NULL_RTX;
16559
16560   STRIP_NOPS (init);
16561
16562   /* If a variable is initialized with a string constant without embedded
16563      zeros, build CONST_STRING.  */
16564   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16565     {
16566       tree enttype = TREE_TYPE (type);
16567       tree domain = TYPE_DOMAIN (type);
16568       enum machine_mode mode = TYPE_MODE (enttype);
16569
16570       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16571           && domain
16572           && integer_zerop (TYPE_MIN_VALUE (domain))
16573           && compare_tree_int (TYPE_MAX_VALUE (domain),
16574                                TREE_STRING_LENGTH (init) - 1) == 0
16575           && ((size_t) TREE_STRING_LENGTH (init)
16576               == strlen (TREE_STRING_POINTER (init)) + 1))
16577         {
16578           rtl = gen_rtx_CONST_STRING (VOIDmode,
16579                                       ggc_strdup (TREE_STRING_POINTER (init)));
16580           rtl = gen_rtx_MEM (BLKmode, rtl);
16581           MEM_READONLY_P (rtl) = 1;
16582         }
16583     }
16584   /* Other aggregates, and complex values, could be represented using
16585      CONCAT: FIXME!  */
16586   else if (AGGREGATE_TYPE_P (type)
16587            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16588                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16589            || TREE_CODE (type) == COMPLEX_TYPE)
16590     ;
16591   /* Vectors only work if their mode is supported by the target.
16592      FIXME: generic vectors ought to work too.  */
16593   else if (TREE_CODE (type) == VECTOR_TYPE
16594            && !VECTOR_MODE_P (TYPE_MODE (type)))
16595     ;
16596   /* If the initializer is something that we know will expand into an
16597      immediate RTL constant, expand it now.  We must be careful not to
16598      reference variables which won't be output.  */
16599   else if (initializer_constant_valid_p (init, type)
16600            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16601     {
16602       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16603          possible.  */
16604       if (TREE_CODE (type) == VECTOR_TYPE)
16605         switch (TREE_CODE (init))
16606           {
16607           case VECTOR_CST:
16608             break;
16609           case CONSTRUCTOR:
16610             if (TREE_CONSTANT (init))
16611               {
16612                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16613                 bool constant_p = true;
16614                 tree value;
16615                 unsigned HOST_WIDE_INT ix;
16616
16617                 /* Even when ctor is constant, it might contain non-*_CST
16618                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16619                    belong into VECTOR_CST nodes.  */
16620                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16621                   if (!CONSTANT_CLASS_P (value))
16622                     {
16623                       constant_p = false;
16624                       break;
16625                     }
16626
16627                 if (constant_p)
16628                   {
16629                     init = build_vector_from_ctor (type, elts);
16630                     break;
16631                   }
16632               }
16633             /* FALLTHRU */
16634
16635           default:
16636             return NULL;
16637           }
16638
16639       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16640
16641       /* If expand_expr returns a MEM, it wasn't immediate.  */
16642       gcc_assert (!rtl || !MEM_P (rtl));
16643     }
16644
16645   return rtl;
16646 }
16647
16648 /* Generate RTL for the variable DECL to represent its location.  */
16649
16650 static rtx
16651 rtl_for_decl_location (tree decl)
16652 {
16653   rtx rtl;
16654
16655   /* Here we have to decide where we are going to say the parameter "lives"
16656      (as far as the debugger is concerned).  We only have a couple of
16657      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16658
16659      DECL_RTL normally indicates where the parameter lives during most of the
16660      activation of the function.  If optimization is enabled however, this
16661      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16662      that the parameter doesn't really live anywhere (as far as the code
16663      generation parts of GCC are concerned) during most of the function's
16664      activation.  That will happen (for example) if the parameter is never
16665      referenced within the function.
16666
16667      We could just generate a location descriptor here for all non-NULL
16668      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16669      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16670      where DECL_RTL is NULL or is a pseudo-reg.
16671
16672      Note however that we can only get away with using DECL_INCOMING_RTL as
16673      a backup substitute for DECL_RTL in certain limited cases.  In cases
16674      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16675      we can be sure that the parameter was passed using the same type as it is
16676      declared to have within the function, and that its DECL_INCOMING_RTL
16677      points us to a place where a value of that type is passed.
16678
16679      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16680      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16681      because in these cases DECL_INCOMING_RTL points us to a value of some
16682      type which is *different* from the type of the parameter itself.  Thus,
16683      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16684      such cases, the debugger would end up (for example) trying to fetch a
16685      `float' from a place which actually contains the first part of a
16686      `double'.  That would lead to really incorrect and confusing
16687      output at debug-time.
16688
16689      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16690      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16691      are a couple of exceptions however.  On little-endian machines we can
16692      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16693      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16694      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16695      when (on a little-endian machine) a non-prototyped function has a
16696      parameter declared to be of type `short' or `char'.  In such cases,
16697      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16698      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16699      passed `int' value.  If the debugger then uses that address to fetch
16700      a `short' or a `char' (on a little-endian machine) the result will be
16701      the correct data, so we allow for such exceptional cases below.
16702
16703      Note that our goal here is to describe the place where the given formal
16704      parameter lives during most of the function's activation (i.e. between the
16705      end of the prologue and the start of the epilogue).  We'll do that as best
16706      as we can. Note however that if the given formal parameter is modified
16707      sometime during the execution of the function, then a stack backtrace (at
16708      debug-time) will show the function as having been called with the *new*
16709      value rather than the value which was originally passed in.  This happens
16710      rarely enough that it is not a major problem, but it *is* a problem, and
16711      I'd like to fix it.
16712
16713      A future version of dwarf2out.c may generate two additional attributes for
16714      any given DW_TAG_formal_parameter DIE which will describe the "passed
16715      type" and the "passed location" for the given formal parameter in addition
16716      to the attributes we now generate to indicate the "declared type" and the
16717      "active location" for each parameter.  This additional set of attributes
16718      could be used by debuggers for stack backtraces. Separately, note that
16719      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16720      This happens (for example) for inlined-instances of inline function formal
16721      parameters which are never referenced.  This really shouldn't be
16722      happening.  All PARM_DECL nodes should get valid non-NULL
16723      DECL_INCOMING_RTL values.  FIXME.  */
16724
16725   /* Use DECL_RTL as the "location" unless we find something better.  */
16726   rtl = DECL_RTL_IF_SET (decl);
16727
16728   /* When generating abstract instances, ignore everything except
16729      constants, symbols living in memory, and symbols living in
16730      fixed registers.  */
16731   if (! reload_completed)
16732     {
16733       if (rtl
16734           && (CONSTANT_P (rtl)
16735               || (MEM_P (rtl)
16736                   && CONSTANT_P (XEXP (rtl, 0)))
16737               || (REG_P (rtl)
16738                   && TREE_CODE (decl) == VAR_DECL
16739                   && TREE_STATIC (decl))))
16740         {
16741           rtl = targetm.delegitimize_address (rtl);
16742           return rtl;
16743         }
16744       rtl = NULL_RTX;
16745     }
16746   else if (TREE_CODE (decl) == PARM_DECL)
16747     {
16748       if (rtl == NULL_RTX
16749           || is_pseudo_reg (rtl)
16750           || (MEM_P (rtl)
16751               && is_pseudo_reg (XEXP (rtl, 0))
16752               && DECL_INCOMING_RTL (decl)
16753               && MEM_P (DECL_INCOMING_RTL (decl))
16754               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
16755         {
16756           tree declared_type = TREE_TYPE (decl);
16757           tree passed_type = DECL_ARG_TYPE (decl);
16758           enum machine_mode dmode = TYPE_MODE (declared_type);
16759           enum machine_mode pmode = TYPE_MODE (passed_type);
16760
16761           /* This decl represents a formal parameter which was optimized out.
16762              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16763              all cases where (rtl == NULL_RTX) just below.  */
16764           if (dmode == pmode)
16765             rtl = DECL_INCOMING_RTL (decl);
16766           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
16767                    && SCALAR_INT_MODE_P (dmode)
16768                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16769                    && DECL_INCOMING_RTL (decl))
16770             {
16771               rtx inc = DECL_INCOMING_RTL (decl);
16772               if (REG_P (inc))
16773                 rtl = inc;
16774               else if (MEM_P (inc))
16775                 {
16776                   if (BYTES_BIG_ENDIAN)
16777                     rtl = adjust_address_nv (inc, dmode,
16778                                              GET_MODE_SIZE (pmode)
16779                                              - GET_MODE_SIZE (dmode));
16780                   else
16781                     rtl = inc;
16782                 }
16783             }
16784         }
16785
16786       /* If the parm was passed in registers, but lives on the stack, then
16787          make a big endian correction if the mode of the type of the
16788          parameter is not the same as the mode of the rtl.  */
16789       /* ??? This is the same series of checks that are made in dbxout.c before
16790          we reach the big endian correction code there.  It isn't clear if all
16791          of these checks are necessary here, but keeping them all is the safe
16792          thing to do.  */
16793       else if (MEM_P (rtl)
16794                && XEXP (rtl, 0) != const0_rtx
16795                && ! CONSTANT_P (XEXP (rtl, 0))
16796                /* Not passed in memory.  */
16797                && !MEM_P (DECL_INCOMING_RTL (decl))
16798                /* Not passed by invisible reference.  */
16799                && (!REG_P (XEXP (rtl, 0))
16800                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16801                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16802 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16803                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16804 #endif
16805                      )
16806                /* Big endian correction check.  */
16807                && BYTES_BIG_ENDIAN
16808                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16809                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16810                    < UNITS_PER_WORD))
16811         {
16812           int offset = (UNITS_PER_WORD
16813                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16814
16815           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16816                              plus_constant (XEXP (rtl, 0), offset));
16817         }
16818     }
16819   else if (TREE_CODE (decl) == VAR_DECL
16820            && rtl
16821            && MEM_P (rtl)
16822            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16823            && BYTES_BIG_ENDIAN)
16824     {
16825       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16826       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16827
16828       /* If a variable is declared "register" yet is smaller than
16829          a register, then if we store the variable to memory, it
16830          looks like we're storing a register-sized value, when in
16831          fact we are not.  We need to adjust the offset of the
16832          storage location to reflect the actual value's bytes,
16833          else gdb will not be able to display it.  */
16834       if (rsize > dsize)
16835         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16836                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16837     }
16838
16839   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16840      and will have been substituted directly into all expressions that use it.
16841      C does not have such a concept, but C++ and other languages do.  */
16842   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16843     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16844
16845   if (rtl)
16846     rtl = targetm.delegitimize_address (rtl);
16847
16848   /* If we don't look past the constant pool, we risk emitting a
16849      reference to a constant pool entry that isn't referenced from
16850      code, and thus is not emitted.  */
16851   if (rtl)
16852     rtl = avoid_constant_pool_reference (rtl);
16853
16854   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16855      in the current CU, resolve_addr will remove the expression referencing
16856      it.  */
16857   if (rtl == NULL_RTX
16858       && TREE_CODE (decl) == VAR_DECL
16859       && !DECL_EXTERNAL (decl)
16860       && TREE_STATIC (decl)
16861       && DECL_NAME (decl)
16862       && !DECL_HARD_REGISTER (decl)
16863       && DECL_MODE (decl) != VOIDmode)
16864     {
16865       rtl = make_decl_rtl_for_debug (decl);
16866       if (!MEM_P (rtl)
16867           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16868           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16869         rtl = NULL_RTX;
16870     }
16871
16872   return rtl;
16873 }
16874
16875 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16876    returned.  If so, the decl for the COMMON block is returned, and the
16877    value is the offset into the common block for the symbol.  */
16878
16879 static tree
16880 fortran_common (tree decl, HOST_WIDE_INT *value)
16881 {
16882   tree val_expr, cvar;
16883   enum machine_mode mode;
16884   HOST_WIDE_INT bitsize, bitpos;
16885   tree offset;
16886   int volatilep = 0, unsignedp = 0;
16887
16888   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16889      it does not have a value (the offset into the common area), or if it
16890      is thread local (as opposed to global) then it isn't common, and shouldn't
16891      be handled as such.  */
16892   if (TREE_CODE (decl) != VAR_DECL
16893       || !TREE_STATIC (decl)
16894       || !DECL_HAS_VALUE_EXPR_P (decl)
16895       || !is_fortran ())
16896     return NULL_TREE;
16897
16898   val_expr = DECL_VALUE_EXPR (decl);
16899   if (TREE_CODE (val_expr) != COMPONENT_REF)
16900     return NULL_TREE;
16901
16902   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16903                               &mode, &unsignedp, &volatilep, true);
16904
16905   if (cvar == NULL_TREE
16906       || TREE_CODE (cvar) != VAR_DECL
16907       || DECL_ARTIFICIAL (cvar)
16908       || !TREE_PUBLIC (cvar))
16909     return NULL_TREE;
16910
16911   *value = 0;
16912   if (offset != NULL)
16913     {
16914       if (!host_integerp (offset, 0))
16915         return NULL_TREE;
16916       *value = tree_low_cst (offset, 0);
16917     }
16918   if (bitpos != 0)
16919     *value += bitpos / BITS_PER_UNIT;
16920
16921   return cvar;
16922 }
16923
16924 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16925    data attribute for a variable or a parameter.  We generate the
16926    DW_AT_const_value attribute only in those cases where the given variable
16927    or parameter does not have a true "location" either in memory or in a
16928    register.  This can happen (for example) when a constant is passed as an
16929    actual argument in a call to an inline function.  (It's possible that
16930    these things can crop up in other ways also.)  Note that one type of
16931    constant value which can be passed into an inlined function is a constant
16932    pointer.  This can happen for example if an actual argument in an inlined
16933    function call evaluates to a compile-time constant address.
16934
16935    CACHE_P is true if it is worth caching the location list for DECL,
16936    so that future calls can reuse it rather than regenerate it from scratch.
16937    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
16938    since we will need to refer to them each time the function is inlined.  */
16939
16940 static bool
16941 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
16942                                        enum dwarf_attribute attr)
16943 {
16944   rtx rtl;
16945   dw_loc_list_ref list;
16946   var_loc_list *loc_list;
16947   cached_dw_loc_list *cache;
16948   void **slot;
16949
16950   if (TREE_CODE (decl) == ERROR_MARK)
16951     return false;
16952
16953   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16954               || TREE_CODE (decl) == RESULT_DECL);
16955
16956   /* Try to get some constant RTL for this decl, and use that as the value of
16957      the location.  */
16958
16959   rtl = rtl_for_decl_location (decl);
16960   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16961       && add_const_value_attribute (die, rtl))
16962     return true;
16963
16964   /* See if we have single element location list that is equivalent to
16965      a constant value.  That way we are better to use add_const_value_attribute
16966      rather than expanding constant value equivalent.  */
16967   loc_list = lookup_decl_loc (decl);
16968   if (loc_list
16969       && loc_list->first
16970       && loc_list->first->next == NULL
16971       && NOTE_P (loc_list->first->loc)
16972       && NOTE_VAR_LOCATION (loc_list->first->loc)
16973       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16974     {
16975       struct var_loc_node *node;
16976
16977       node = loc_list->first;
16978       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16979       if (GET_CODE (rtl) == EXPR_LIST)
16980         rtl = XEXP (rtl, 0);
16981       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16982           && add_const_value_attribute (die, rtl))
16983          return true;
16984     }
16985   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
16986      list several times.  See if we've already cached the contents.  */
16987   list = NULL;
16988   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
16989     cache_p = false;
16990   if (cache_p)
16991     {
16992       cache = (cached_dw_loc_list *)
16993         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
16994       if (cache)
16995         list = cache->loc_list;
16996     }
16997   if (list == NULL)
16998     {
16999       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
17000       /* It is usually worth caching this result if the decl is from
17001          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
17002       if (cache_p && list && list->dw_loc_next)
17003         {
17004           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
17005                                            DECL_UID (decl), INSERT);
17006           cache = ggc_alloc_cleared_cached_dw_loc_list ();
17007           cache->decl_id = DECL_UID (decl);
17008           cache->loc_list = list;
17009           *slot = cache;
17010         }
17011     }
17012   if (list)
17013     {
17014       add_AT_location_description (die, attr, list);
17015       return true;
17016     }
17017   /* None of that worked, so it must not really have a location;
17018      try adding a constant value attribute from the DECL_INITIAL.  */
17019   return tree_add_const_value_attribute_for_decl (die, decl);
17020 }
17021
17022 /* Add VARIABLE and DIE into deferred locations list.  */
17023
17024 static void
17025 defer_location (tree variable, dw_die_ref die)
17026 {
17027   deferred_locations entry;
17028   entry.variable = variable;
17029   entry.die = die;
17030   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
17031 }
17032
17033 /* Helper function for tree_add_const_value_attribute.  Natively encode
17034    initializer INIT into an array.  Return true if successful.  */
17035
17036 static bool
17037 native_encode_initializer (tree init, unsigned char *array, int size)
17038 {
17039   tree type;
17040
17041   if (init == NULL_TREE)
17042     return false;
17043
17044   STRIP_NOPS (init);
17045   switch (TREE_CODE (init))
17046     {
17047     case STRING_CST:
17048       type = TREE_TYPE (init);
17049       if (TREE_CODE (type) == ARRAY_TYPE)
17050         {
17051           tree enttype = TREE_TYPE (type);
17052           enum machine_mode mode = TYPE_MODE (enttype);
17053
17054           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17055             return false;
17056           if (int_size_in_bytes (type) != size)
17057             return false;
17058           if (size > TREE_STRING_LENGTH (init))
17059             {
17060               memcpy (array, TREE_STRING_POINTER (init),
17061                       TREE_STRING_LENGTH (init));
17062               memset (array + TREE_STRING_LENGTH (init),
17063                       '\0', size - TREE_STRING_LENGTH (init));
17064             }
17065           else
17066             memcpy (array, TREE_STRING_POINTER (init), size);
17067           return true;
17068         }
17069       return false;
17070     case CONSTRUCTOR:
17071       type = TREE_TYPE (init);
17072       if (int_size_in_bytes (type) != size)
17073         return false;
17074       if (TREE_CODE (type) == ARRAY_TYPE)
17075         {
17076           HOST_WIDE_INT min_index;
17077           unsigned HOST_WIDE_INT cnt;
17078           int curpos = 0, fieldsize;
17079           constructor_elt *ce;
17080
17081           if (TYPE_DOMAIN (type) == NULL_TREE
17082               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17083             return false;
17084
17085           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17086           if (fieldsize <= 0)
17087             return false;
17088
17089           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17090           memset (array, '\0', size);
17091           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17092             {
17093               tree val = ce->value;
17094               tree index = ce->index;
17095               int pos = curpos;
17096               if (index && TREE_CODE (index) == RANGE_EXPR)
17097                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17098                       * fieldsize;
17099               else if (index)
17100                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17101
17102               if (val)
17103                 {
17104                   STRIP_NOPS (val);
17105                   if (!native_encode_initializer (val, array + pos, fieldsize))
17106                     return false;
17107                 }
17108               curpos = pos + fieldsize;
17109               if (index && TREE_CODE (index) == RANGE_EXPR)
17110                 {
17111                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17112                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17113                   while (count > 0)
17114                     {
17115                       if (val)
17116                         memcpy (array + curpos, array + pos, fieldsize);
17117                       curpos += fieldsize;
17118                     }
17119                 }
17120               gcc_assert (curpos <= size);
17121             }
17122           return true;
17123         }
17124       else if (TREE_CODE (type) == RECORD_TYPE
17125                || TREE_CODE (type) == UNION_TYPE)
17126         {
17127           tree field = NULL_TREE;
17128           unsigned HOST_WIDE_INT cnt;
17129           constructor_elt *ce;
17130
17131           if (int_size_in_bytes (type) != size)
17132             return false;
17133
17134           if (TREE_CODE (type) == RECORD_TYPE)
17135             field = TYPE_FIELDS (type);
17136
17137           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17138             {
17139               tree val = ce->value;
17140               int pos, fieldsize;
17141
17142               if (ce->index != 0)
17143                 field = ce->index;
17144
17145               if (val)
17146                 STRIP_NOPS (val);
17147
17148               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17149                 return false;
17150
17151               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17152                   && TYPE_DOMAIN (TREE_TYPE (field))
17153                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17154                 return false;
17155               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17156                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17157                 return false;
17158               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17159               pos = int_byte_position (field);
17160               gcc_assert (pos + fieldsize <= size);
17161               if (val
17162                   && !native_encode_initializer (val, array + pos, fieldsize))
17163                 return false;
17164             }
17165           return true;
17166         }
17167       return false;
17168     case VIEW_CONVERT_EXPR:
17169     case NON_LVALUE_EXPR:
17170       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17171     default:
17172       return native_encode_expr (init, array, size) == size;
17173     }
17174 }
17175
17176 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17177    attribute is the const value T.  */
17178
17179 static bool
17180 tree_add_const_value_attribute (dw_die_ref die, tree t)
17181 {
17182   tree init;
17183   tree type = TREE_TYPE (t);
17184   rtx rtl;
17185
17186   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17187     return false;
17188
17189   init = t;
17190   gcc_assert (!DECL_P (init));
17191
17192   rtl = rtl_for_decl_init (init, type);
17193   if (rtl)
17194     return add_const_value_attribute (die, rtl);
17195   /* If the host and target are sane, try harder.  */
17196   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17197            && initializer_constant_valid_p (init, type))
17198     {
17199       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17200       if (size > 0 && (int) size == size)
17201         {
17202           unsigned char *array = (unsigned char *)
17203             ggc_alloc_cleared_atomic (size);
17204
17205           if (native_encode_initializer (init, array, size))
17206             {
17207               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17208               return true;
17209             }
17210         }
17211     }
17212   return false;
17213 }
17214
17215 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17216    attribute is the const value of T, where T is an integral constant
17217    variable with static storage duration
17218    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17219
17220 static bool
17221 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17222 {
17223
17224   if (!decl
17225       || (TREE_CODE (decl) != VAR_DECL
17226           && TREE_CODE (decl) != CONST_DECL))
17227     return false;
17228
17229     if (TREE_READONLY (decl)
17230         && ! TREE_THIS_VOLATILE (decl)
17231         && DECL_INITIAL (decl))
17232       /* OK */;
17233     else
17234       return false;
17235
17236   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17237   if (get_AT (var_die, DW_AT_const_value))
17238     return false;
17239
17240   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17241 }
17242
17243 /* Convert the CFI instructions for the current function into a
17244    location list.  This is used for DW_AT_frame_base when we targeting
17245    a dwarf2 consumer that does not support the dwarf3
17246    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17247    expressions.  */
17248
17249 static dw_loc_list_ref
17250 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17251 {
17252   dw_fde_ref fde;
17253   dw_loc_list_ref list, *list_tail;
17254   dw_cfi_ref cfi;
17255   dw_cfa_location last_cfa, next_cfa;
17256   const char *start_label, *last_label, *section;
17257   dw_cfa_location remember;
17258
17259   fde = current_fde ();
17260   gcc_assert (fde != NULL);
17261
17262   section = secname_for_decl (current_function_decl);
17263   list_tail = &list;
17264   list = NULL;
17265
17266   memset (&next_cfa, 0, sizeof (next_cfa));
17267   next_cfa.reg = INVALID_REGNUM;
17268   remember = next_cfa;
17269
17270   start_label = fde->dw_fde_begin;
17271
17272   /* ??? Bald assumption that the CIE opcode list does not contain
17273      advance opcodes.  */
17274   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17275     lookup_cfa_1 (cfi, &next_cfa, &remember);
17276
17277   last_cfa = next_cfa;
17278   last_label = start_label;
17279
17280   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi == NULL)
17281     {
17282       /* If the first partition contained no CFI adjustments, the
17283          CIE opcodes apply to the whole first partition.  */
17284       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17285                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17286       list_tail =&(*list_tail)->dw_loc_next;
17287       start_label = last_label = fde->dw_fde_second_begin;
17288     }
17289
17290   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17291     {
17292       switch (cfi->dw_cfi_opc)
17293         {
17294         case DW_CFA_set_loc:
17295         case DW_CFA_advance_loc1:
17296         case DW_CFA_advance_loc2:
17297         case DW_CFA_advance_loc4:
17298           if (!cfa_equal_p (&last_cfa, &next_cfa))
17299             {
17300               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17301                                          start_label, last_label, section);
17302
17303               list_tail = &(*list_tail)->dw_loc_next;
17304               last_cfa = next_cfa;
17305               start_label = last_label;
17306             }
17307           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17308           break;
17309
17310         case DW_CFA_advance_loc:
17311           /* The encoding is complex enough that we should never emit this.  */
17312           gcc_unreachable ();
17313
17314         default:
17315           lookup_cfa_1 (cfi, &next_cfa, &remember);
17316           break;
17317         }
17318       if (cfi == fde->dw_fde_switch_cfi)
17319         {
17320           if (!cfa_equal_p (&last_cfa, &next_cfa))
17321             {
17322               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17323                                          start_label, last_label, section);
17324
17325               list_tail = &(*list_tail)->dw_loc_next;
17326               last_cfa = next_cfa;
17327               start_label = last_label;
17328             }
17329           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17330                                      start_label, fde->dw_fde_end, section);
17331           list_tail = &(*list_tail)->dw_loc_next;
17332           start_label = last_label = fde->dw_fde_second_begin;
17333         }
17334     }
17335
17336   if (!cfa_equal_p (&last_cfa, &next_cfa))
17337     {
17338       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17339                                  start_label, last_label, section);
17340       list_tail = &(*list_tail)->dw_loc_next;
17341       start_label = last_label;
17342     }
17343
17344   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17345                              start_label,
17346                              fde->dw_fde_second_begin
17347                              ? fde->dw_fde_second_end : fde->dw_fde_end,
17348                              section);
17349
17350   if (list && list->dw_loc_next)
17351     gen_llsym (list);
17352
17353   return list;
17354 }
17355
17356 /* Compute a displacement from the "steady-state frame pointer" to the
17357    frame base (often the same as the CFA), and store it in
17358    frame_pointer_fb_offset.  OFFSET is added to the displacement
17359    before the latter is negated.  */
17360
17361 static void
17362 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17363 {
17364   rtx reg, elim;
17365
17366 #ifdef FRAME_POINTER_CFA_OFFSET
17367   reg = frame_pointer_rtx;
17368   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17369 #else
17370   reg = arg_pointer_rtx;
17371   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17372 #endif
17373
17374   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17375   if (GET_CODE (elim) == PLUS)
17376     {
17377       offset += INTVAL (XEXP (elim, 1));
17378       elim = XEXP (elim, 0);
17379     }
17380
17381   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17382                && (elim == hard_frame_pointer_rtx
17383                    || elim == stack_pointer_rtx))
17384               || elim == (frame_pointer_needed
17385                           ? hard_frame_pointer_rtx
17386                           : stack_pointer_rtx));
17387
17388   frame_pointer_fb_offset = -offset;
17389 }
17390
17391 /* Generate a DW_AT_name attribute given some string value to be included as
17392    the value of the attribute.  */
17393
17394 static void
17395 add_name_attribute (dw_die_ref die, const char *name_string)
17396 {
17397   if (name_string != NULL && *name_string != 0)
17398     {
17399       if (demangle_name_func)
17400         name_string = (*demangle_name_func) (name_string);
17401
17402       add_AT_string (die, DW_AT_name, name_string);
17403     }
17404 }
17405
17406 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17407
17408 static void
17409 add_comp_dir_attribute (dw_die_ref die)
17410 {
17411   const char *wd = get_src_pwd ();
17412   char *wd1;
17413
17414   if (wd == NULL)
17415     return;
17416
17417   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17418     {
17419       int wdlen;
17420
17421       wdlen = strlen (wd);
17422       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17423       strcpy (wd1, wd);
17424       wd1 [wdlen] = DIR_SEPARATOR;
17425       wd1 [wdlen + 1] = 0;
17426       wd = wd1;
17427     }
17428
17429     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17430 }
17431
17432 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17433    default.  */
17434
17435 static int
17436 lower_bound_default (void)
17437 {
17438   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17439     {
17440     case DW_LANG_C:
17441     case DW_LANG_C89:
17442     case DW_LANG_C99:
17443     case DW_LANG_C_plus_plus:
17444     case DW_LANG_ObjC:
17445     case DW_LANG_ObjC_plus_plus:
17446     case DW_LANG_Java:
17447       return 0;
17448     case DW_LANG_Fortran77:
17449     case DW_LANG_Fortran90:
17450     case DW_LANG_Fortran95:
17451       return 1;
17452     case DW_LANG_UPC:
17453     case DW_LANG_D:
17454     case DW_LANG_Python:
17455       return dwarf_version >= 4 ? 0 : -1;
17456     case DW_LANG_Ada95:
17457     case DW_LANG_Ada83:
17458     case DW_LANG_Cobol74:
17459     case DW_LANG_Cobol85:
17460     case DW_LANG_Pascal83:
17461     case DW_LANG_Modula2:
17462     case DW_LANG_PLI:
17463       return dwarf_version >= 4 ? 1 : -1;
17464     default:
17465       return -1;
17466     }
17467 }
17468
17469 /* Given a tree node describing an array bound (either lower or upper) output
17470    a representation for that bound.  */
17471
17472 static void
17473 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17474 {
17475   switch (TREE_CODE (bound))
17476     {
17477     case ERROR_MARK:
17478       return;
17479
17480     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17481     case INTEGER_CST:
17482       {
17483         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17484         int dflt;
17485
17486         /* Use the default if possible.  */
17487         if (bound_attr == DW_AT_lower_bound
17488             && host_integerp (bound, 0)
17489             && (dflt = lower_bound_default ()) != -1
17490             && tree_low_cst (bound, 0) == dflt)
17491           ;
17492
17493         /* Otherwise represent the bound as an unsigned value with the
17494            precision of its type.  The precision and signedness of the
17495            type will be necessary to re-interpret it unambiguously.  */
17496         else if (prec < HOST_BITS_PER_WIDE_INT)
17497           {
17498             unsigned HOST_WIDE_INT mask
17499               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17500             add_AT_unsigned (subrange_die, bound_attr,
17501                              TREE_INT_CST_LOW (bound) & mask);
17502           }
17503         else if (prec == HOST_BITS_PER_WIDE_INT
17504                  || TREE_INT_CST_HIGH (bound) == 0)
17505           add_AT_unsigned (subrange_die, bound_attr,
17506                            TREE_INT_CST_LOW (bound));
17507         else
17508           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17509                          TREE_INT_CST_LOW (bound));
17510       }
17511       break;
17512
17513     CASE_CONVERT:
17514     case VIEW_CONVERT_EXPR:
17515       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17516       break;
17517
17518     case SAVE_EXPR:
17519       break;
17520
17521     case VAR_DECL:
17522     case PARM_DECL:
17523     case RESULT_DECL:
17524       {
17525         dw_die_ref decl_die = lookup_decl_die (bound);
17526
17527         /* ??? Can this happen, or should the variable have been bound
17528            first?  Probably it can, since I imagine that we try to create
17529            the types of parameters in the order in which they exist in
17530            the list, and won't have created a forward reference to a
17531            later parameter.  */
17532         if (decl_die != NULL)
17533           {
17534             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17535             break;
17536           }
17537       }
17538       /* FALLTHRU */
17539
17540     default:
17541       {
17542         /* Otherwise try to create a stack operation procedure to
17543            evaluate the value of the array bound.  */
17544
17545         dw_die_ref ctx, decl_die;
17546         dw_loc_list_ref list;
17547
17548         list = loc_list_from_tree (bound, 2);
17549         if (list == NULL || single_element_loc_list_p (list))
17550           {
17551             /* If DW_AT_*bound is not a reference nor constant, it is
17552                a DWARF expression rather than location description.
17553                For that loc_list_from_tree (bound, 0) is needed.
17554                If that fails to give a single element list,
17555                fall back to outputting this as a reference anyway.  */
17556             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17557             if (list2 && single_element_loc_list_p (list2))
17558               {
17559                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17560                 break;
17561               }
17562           }
17563         if (list == NULL)
17564           break;
17565
17566         if (current_function_decl == 0)
17567           ctx = comp_unit_die ();
17568         else
17569           ctx = lookup_decl_die (current_function_decl);
17570
17571         decl_die = new_die (DW_TAG_variable, ctx, bound);
17572         add_AT_flag (decl_die, DW_AT_artificial, 1);
17573         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17574         add_AT_location_description (decl_die, DW_AT_location, list);
17575         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17576         break;
17577       }
17578     }
17579 }
17580
17581 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17582    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17583    Note that the block of subscript information for an array type also
17584    includes information about the element type of the given array type.  */
17585
17586 static void
17587 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17588 {
17589   unsigned dimension_number;
17590   tree lower, upper;
17591   dw_die_ref subrange_die;
17592
17593   for (dimension_number = 0;
17594        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17595        type = TREE_TYPE (type), dimension_number++)
17596     {
17597       tree domain = TYPE_DOMAIN (type);
17598
17599       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17600         break;
17601
17602       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17603          and (in GNU C only) variable bounds.  Handle all three forms
17604          here.  */
17605       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17606       if (domain)
17607         {
17608           /* We have an array type with specified bounds.  */
17609           lower = TYPE_MIN_VALUE (domain);
17610           upper = TYPE_MAX_VALUE (domain);
17611
17612           /* Define the index type.  */
17613           if (TREE_TYPE (domain))
17614             {
17615               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17616                  TREE_TYPE field.  We can't emit debug info for this
17617                  because it is an unnamed integral type.  */
17618               if (TREE_CODE (domain) == INTEGER_TYPE
17619                   && TYPE_NAME (domain) == NULL_TREE
17620                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17621                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17622                 ;
17623               else
17624                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17625                                     type_die);
17626             }
17627
17628           /* ??? If upper is NULL, the array has unspecified length,
17629              but it does have a lower bound.  This happens with Fortran
17630                dimension arr(N:*)
17631              Since the debugger is definitely going to need to know N
17632              to produce useful results, go ahead and output the lower
17633              bound solo, and hope the debugger can cope.  */
17634
17635           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17636           if (upper)
17637             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17638         }
17639
17640       /* Otherwise we have an array type with an unspecified length.  The
17641          DWARF-2 spec does not say how to handle this; let's just leave out the
17642          bounds.  */
17643     }
17644 }
17645
17646 static void
17647 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17648 {
17649   unsigned size;
17650
17651   switch (TREE_CODE (tree_node))
17652     {
17653     case ERROR_MARK:
17654       size = 0;
17655       break;
17656     case ENUMERAL_TYPE:
17657     case RECORD_TYPE:
17658     case UNION_TYPE:
17659     case QUAL_UNION_TYPE:
17660       size = int_size_in_bytes (tree_node);
17661       break;
17662     case FIELD_DECL:
17663       /* For a data member of a struct or union, the DW_AT_byte_size is
17664          generally given as the number of bytes normally allocated for an
17665          object of the *declared* type of the member itself.  This is true
17666          even for bit-fields.  */
17667       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17668       break;
17669     default:
17670       gcc_unreachable ();
17671     }
17672
17673   /* Note that `size' might be -1 when we get to this point.  If it is, that
17674      indicates that the byte size of the entity in question is variable.  We
17675      have no good way of expressing this fact in Dwarf at the present time,
17676      so just let the -1 pass on through.  */
17677   add_AT_unsigned (die, DW_AT_byte_size, size);
17678 }
17679
17680 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17681    which specifies the distance in bits from the highest order bit of the
17682    "containing object" for the bit-field to the highest order bit of the
17683    bit-field itself.
17684
17685    For any given bit-field, the "containing object" is a hypothetical object
17686    (of some integral or enum type) within which the given bit-field lives.  The
17687    type of this hypothetical "containing object" is always the same as the
17688    declared type of the individual bit-field itself.  The determination of the
17689    exact location of the "containing object" for a bit-field is rather
17690    complicated.  It's handled by the `field_byte_offset' function (above).
17691
17692    Note that it is the size (in bytes) of the hypothetical "containing object"
17693    which will be given in the DW_AT_byte_size attribute for this bit-field.
17694    (See `byte_size_attribute' above).  */
17695
17696 static inline void
17697 add_bit_offset_attribute (dw_die_ref die, tree decl)
17698 {
17699   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17700   tree type = DECL_BIT_FIELD_TYPE (decl);
17701   HOST_WIDE_INT bitpos_int;
17702   HOST_WIDE_INT highest_order_object_bit_offset;
17703   HOST_WIDE_INT highest_order_field_bit_offset;
17704   HOST_WIDE_INT bit_offset;
17705
17706   /* Must be a field and a bit field.  */
17707   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17708
17709   /* We can't yet handle bit-fields whose offsets are variable, so if we
17710      encounter such things, just return without generating any attribute
17711      whatsoever.  Likewise for variable or too large size.  */
17712   if (! host_integerp (bit_position (decl), 0)
17713       || ! host_integerp (DECL_SIZE (decl), 1))
17714     return;
17715
17716   bitpos_int = int_bit_position (decl);
17717
17718   /* Note that the bit offset is always the distance (in bits) from the
17719      highest-order bit of the "containing object" to the highest-order bit of
17720      the bit-field itself.  Since the "high-order end" of any object or field
17721      is different on big-endian and little-endian machines, the computation
17722      below must take account of these differences.  */
17723   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17724   highest_order_field_bit_offset = bitpos_int;
17725
17726   if (! BYTES_BIG_ENDIAN)
17727     {
17728       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17729       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17730     }
17731
17732   bit_offset
17733     = (! BYTES_BIG_ENDIAN
17734        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17735        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17736
17737   if (bit_offset < 0)
17738     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17739   else
17740     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17741 }
17742
17743 /* For a FIELD_DECL node which represents a bit field, output an attribute
17744    which specifies the length in bits of the given field.  */
17745
17746 static inline void
17747 add_bit_size_attribute (dw_die_ref die, tree decl)
17748 {
17749   /* Must be a field and a bit field.  */
17750   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17751               && DECL_BIT_FIELD_TYPE (decl));
17752
17753   if (host_integerp (DECL_SIZE (decl), 1))
17754     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17755 }
17756
17757 /* If the compiled language is ANSI C, then add a 'prototyped'
17758    attribute, if arg types are given for the parameters of a function.  */
17759
17760 static inline void
17761 add_prototyped_attribute (dw_die_ref die, tree func_type)
17762 {
17763   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17764       && prototype_p (func_type))
17765     add_AT_flag (die, DW_AT_prototyped, 1);
17766 }
17767
17768 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17769    by looking in either the type declaration or object declaration
17770    equate table.  */
17771
17772 static inline dw_die_ref
17773 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17774 {
17775   dw_die_ref origin_die = NULL;
17776
17777   if (TREE_CODE (origin) != FUNCTION_DECL)
17778     {
17779       /* We may have gotten separated from the block for the inlined
17780          function, if we're in an exception handler or some such; make
17781          sure that the abstract function has been written out.
17782
17783          Doing this for nested functions is wrong, however; functions are
17784          distinct units, and our context might not even be inline.  */
17785       tree fn = origin;
17786
17787       if (TYPE_P (fn))
17788         fn = TYPE_STUB_DECL (fn);
17789
17790       fn = decl_function_context (fn);
17791       if (fn)
17792         dwarf2out_abstract_function (fn);
17793     }
17794
17795   if (DECL_P (origin))
17796     origin_die = lookup_decl_die (origin);
17797   else if (TYPE_P (origin))
17798     origin_die = lookup_type_die (origin);
17799
17800   /* XXX: Functions that are never lowered don't always have correct block
17801      trees (in the case of java, they simply have no block tree, in some other
17802      languages).  For these functions, there is nothing we can really do to
17803      output correct debug info for inlined functions in all cases.  Rather
17804      than die, we'll just produce deficient debug info now, in that we will
17805      have variables without a proper abstract origin.  In the future, when all
17806      functions are lowered, we should re-add a gcc_assert (origin_die)
17807      here.  */
17808
17809   if (origin_die)
17810     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17811   return origin_die;
17812 }
17813
17814 /* We do not currently support the pure_virtual attribute.  */
17815
17816 static inline void
17817 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17818 {
17819   if (DECL_VINDEX (func_decl))
17820     {
17821       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17822
17823       if (host_integerp (DECL_VINDEX (func_decl), 0))
17824         add_AT_loc (die, DW_AT_vtable_elem_location,
17825                     new_loc_descr (DW_OP_constu,
17826                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17827                                    0));
17828
17829       /* GNU extension: Record what type this method came from originally.  */
17830       if (debug_info_level > DINFO_LEVEL_TERSE
17831           && DECL_CONTEXT (func_decl))
17832         add_AT_die_ref (die, DW_AT_containing_type,
17833                         lookup_type_die (DECL_CONTEXT (func_decl)));
17834     }
17835 }
17836 \f
17837 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17838    given decl.  This used to be a vendor extension until after DWARF 4
17839    standardized it.  */
17840
17841 static void
17842 add_linkage_attr (dw_die_ref die, tree decl)
17843 {
17844   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17845
17846   /* Mimic what assemble_name_raw does with a leading '*'.  */
17847   if (name[0] == '*')
17848     name = &name[1];
17849
17850   if (dwarf_version >= 4)
17851     add_AT_string (die, DW_AT_linkage_name, name);
17852   else
17853     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17854 }
17855
17856 /* Add source coordinate attributes for the given decl.  */
17857
17858 static void
17859 add_src_coords_attributes (dw_die_ref die, tree decl)
17860 {
17861   expanded_location s;
17862
17863   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17864     return;
17865   s = expand_location (DECL_SOURCE_LOCATION (decl));
17866   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17867   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17868 }
17869
17870 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17871
17872 static void
17873 add_linkage_name (dw_die_ref die, tree decl)
17874 {
17875   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17876        && TREE_PUBLIC (decl)
17877        && !DECL_ABSTRACT (decl)
17878        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17879        && die->die_tag != DW_TAG_member)
17880     {
17881       /* Defer until we have an assembler name set.  */
17882       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17883         {
17884           limbo_die_node *asm_name;
17885
17886           asm_name = ggc_alloc_cleared_limbo_die_node ();
17887           asm_name->die = die;
17888           asm_name->created_for = decl;
17889           asm_name->next = deferred_asm_name;
17890           deferred_asm_name = asm_name;
17891         }
17892       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17893         add_linkage_attr (die, decl);
17894     }
17895 }
17896
17897 /* Add a DW_AT_name attribute and source coordinate attribute for the
17898    given decl, but only if it actually has a name.  */
17899
17900 static void
17901 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17902 {
17903   tree decl_name;
17904
17905   decl_name = DECL_NAME (decl);
17906   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17907     {
17908       const char *name = dwarf2_name (decl, 0);
17909       if (name)
17910         add_name_attribute (die, name);
17911       if (! DECL_ARTIFICIAL (decl))
17912         add_src_coords_attributes (die, decl);
17913
17914       add_linkage_name (die, decl);
17915     }
17916
17917 #ifdef VMS_DEBUGGING_INFO
17918   /* Get the function's name, as described by its RTL.  This may be different
17919      from the DECL_NAME name used in the source file.  */
17920   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17921     {
17922       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17923                    XEXP (DECL_RTL (decl), 0));
17924       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17925     }
17926 #endif /* VMS_DEBUGGING_INFO */
17927 }
17928
17929 #ifdef VMS_DEBUGGING_INFO
17930 /* Output the debug main pointer die for VMS */
17931
17932 void
17933 dwarf2out_vms_debug_main_pointer (void)
17934 {
17935   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17936   dw_die_ref die;
17937
17938   /* Allocate the VMS debug main subprogram die.  */
17939   die = ggc_alloc_cleared_die_node ();
17940   die->die_tag = DW_TAG_subprogram;
17941   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17942   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17943                                current_function_funcdef_no);
17944   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17945
17946   /* Make it the first child of comp_unit_die ().  */
17947   die->die_parent = comp_unit_die ();
17948   if (comp_unit_die ()->die_child)
17949     {
17950       die->die_sib = comp_unit_die ()->die_child->die_sib;
17951       comp_unit_die ()->die_child->die_sib = die;
17952     }
17953   else
17954     {
17955       die->die_sib = die;
17956       comp_unit_die ()->die_child = die;
17957     }
17958 }
17959 #endif /* VMS_DEBUGGING_INFO */
17960
17961 /* Push a new declaration scope.  */
17962
17963 static void
17964 push_decl_scope (tree scope)
17965 {
17966   VEC_safe_push (tree, gc, decl_scope_table, scope);
17967 }
17968
17969 /* Pop a declaration scope.  */
17970
17971 static inline void
17972 pop_decl_scope (void)
17973 {
17974   VEC_pop (tree, decl_scope_table);
17975 }
17976
17977 /* Return the DIE for the scope that immediately contains this type.
17978    Non-named types get global scope.  Named types nested in other
17979    types get their containing scope if it's open, or global scope
17980    otherwise.  All other types (i.e. function-local named types) get
17981    the current active scope.  */
17982
17983 static dw_die_ref
17984 scope_die_for (tree t, dw_die_ref context_die)
17985 {
17986   dw_die_ref scope_die = NULL;
17987   tree containing_scope;
17988   int i;
17989
17990   /* Non-types always go in the current scope.  */
17991   gcc_assert (TYPE_P (t));
17992
17993   containing_scope = TYPE_CONTEXT (t);
17994
17995   /* Use the containing namespace if it was passed in (for a declaration).  */
17996   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17997     {
17998       if (context_die == lookup_decl_die (containing_scope))
17999         /* OK */;
18000       else
18001         containing_scope = NULL_TREE;
18002     }
18003
18004   /* Ignore function type "scopes" from the C frontend.  They mean that
18005      a tagged type is local to a parmlist of a function declarator, but
18006      that isn't useful to DWARF.  */
18007   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18008     containing_scope = NULL_TREE;
18009
18010   if (SCOPE_FILE_SCOPE_P (containing_scope))
18011     scope_die = comp_unit_die ();
18012   else if (TYPE_P (containing_scope))
18013     {
18014       /* For types, we can just look up the appropriate DIE.  But
18015          first we check to see if we're in the middle of emitting it
18016          so we know where the new DIE should go.  */
18017       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
18018         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
18019           break;
18020
18021       if (i < 0)
18022         {
18023           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
18024                       || TREE_ASM_WRITTEN (containing_scope));
18025           /*We are not in the middle of emitting the type
18026             CONTAINING_SCOPE. Let's see if it's emitted already.  */
18027           scope_die = lookup_type_die (containing_scope);
18028
18029           /* If none of the current dies are suitable, we get file scope.  */
18030           if (scope_die == NULL)
18031             scope_die = comp_unit_die ();
18032         }
18033       else
18034         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18035     }
18036   else
18037     scope_die = context_die;
18038
18039   return scope_die;
18040 }
18041
18042 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18043
18044 static inline int
18045 local_scope_p (dw_die_ref context_die)
18046 {
18047   for (; context_die; context_die = context_die->die_parent)
18048     if (context_die->die_tag == DW_TAG_inlined_subroutine
18049         || context_die->die_tag == DW_TAG_subprogram)
18050       return 1;
18051
18052   return 0;
18053 }
18054
18055 /* Returns nonzero if CONTEXT_DIE is a class.  */
18056
18057 static inline int
18058 class_scope_p (dw_die_ref context_die)
18059 {
18060   return (context_die
18061           && (context_die->die_tag == DW_TAG_structure_type
18062               || context_die->die_tag == DW_TAG_class_type
18063               || context_die->die_tag == DW_TAG_interface_type
18064               || context_die->die_tag == DW_TAG_union_type));
18065 }
18066
18067 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18068    whether or not to treat a DIE in this context as a declaration.  */
18069
18070 static inline int
18071 class_or_namespace_scope_p (dw_die_ref context_die)
18072 {
18073   return (class_scope_p (context_die)
18074           || (context_die && context_die->die_tag == DW_TAG_namespace));
18075 }
18076
18077 /* Many forms of DIEs require a "type description" attribute.  This
18078    routine locates the proper "type descriptor" die for the type given
18079    by 'type', and adds a DW_AT_type attribute below the given die.  */
18080
18081 static void
18082 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18083                     int decl_volatile, dw_die_ref context_die)
18084 {
18085   enum tree_code code  = TREE_CODE (type);
18086   dw_die_ref type_die  = NULL;
18087
18088   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18089      or fixed-point type, use the inner type.  This is because we have no
18090      support for unnamed types in base_type_die.  This can happen if this is
18091      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18092   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18093       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18094     type = TREE_TYPE (type), code = TREE_CODE (type);
18095
18096   if (code == ERROR_MARK
18097       /* Handle a special case.  For functions whose return type is void, we
18098          generate *no* type attribute.  (Note that no object may have type
18099          `void', so this only applies to function return types).  */
18100       || code == VOID_TYPE)
18101     return;
18102
18103   type_die = modified_type_die (type,
18104                                 decl_const || TYPE_READONLY (type),
18105                                 decl_volatile || TYPE_VOLATILE (type),
18106                                 context_die);
18107
18108   if (type_die != NULL)
18109     add_AT_die_ref (object_die, DW_AT_type, type_die);
18110 }
18111
18112 /* Given an object die, add the calling convention attribute for the
18113    function call type.  */
18114 static void
18115 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18116 {
18117   enum dwarf_calling_convention value = DW_CC_normal;
18118
18119   value = ((enum dwarf_calling_convention)
18120            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18121
18122   if (is_fortran ()
18123       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18124     {
18125       /* DWARF 2 doesn't provide a way to identify a program's source-level
18126         entry point.  DW_AT_calling_convention attributes are only meant
18127         to describe functions' calling conventions.  However, lacking a
18128         better way to signal the Fortran main program, we used this for 
18129         a long time, following existing custom.  Now, DWARF 4 has 
18130         DW_AT_main_subprogram, which we add below, but some tools still
18131         rely on the old way, which we thus keep.  */
18132       value = DW_CC_program;
18133
18134       if (dwarf_version >= 4 || !dwarf_strict)
18135         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18136     }
18137
18138   /* Only add the attribute if the backend requests it, and
18139      is not DW_CC_normal.  */
18140   if (value && (value != DW_CC_normal))
18141     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18142 }
18143
18144 /* Given a tree pointer to a struct, class, union, or enum type node, return
18145    a pointer to the (string) tag name for the given type, or zero if the type
18146    was declared without a tag.  */
18147
18148 static const char *
18149 type_tag (const_tree type)
18150 {
18151   const char *name = 0;
18152
18153   if (TYPE_NAME (type) != 0)
18154     {
18155       tree t = 0;
18156
18157       /* Find the IDENTIFIER_NODE for the type name.  */
18158       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18159           && !TYPE_NAMELESS (type))
18160         t = TYPE_NAME (type);
18161
18162       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18163          a TYPE_DECL node, regardless of whether or not a `typedef' was
18164          involved.  */
18165       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18166                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18167         {
18168           /* We want to be extra verbose.  Don't call dwarf_name if
18169              DECL_NAME isn't set.  The default hook for decl_printable_name
18170              doesn't like that, and in this context it's correct to return
18171              0, instead of "<anonymous>" or the like.  */
18172           if (DECL_NAME (TYPE_NAME (type))
18173               && !DECL_NAMELESS (TYPE_NAME (type)))
18174             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18175         }
18176
18177       /* Now get the name as a string, or invent one.  */
18178       if (!name && t != 0)
18179         name = IDENTIFIER_POINTER (t);
18180     }
18181
18182   return (name == 0 || *name == '\0') ? 0 : name;
18183 }
18184
18185 /* Return the type associated with a data member, make a special check
18186    for bit field types.  */
18187
18188 static inline tree
18189 member_declared_type (const_tree member)
18190 {
18191   return (DECL_BIT_FIELD_TYPE (member)
18192           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18193 }
18194
18195 /* Get the decl's label, as described by its RTL. This may be different
18196    from the DECL_NAME name used in the source file.  */
18197
18198 #if 0
18199 static const char *
18200 decl_start_label (tree decl)
18201 {
18202   rtx x;
18203   const char *fnname;
18204
18205   x = DECL_RTL (decl);
18206   gcc_assert (MEM_P (x));
18207
18208   x = XEXP (x, 0);
18209   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18210
18211   fnname = XSTR (x, 0);
18212   return fnname;
18213 }
18214 #endif
18215 \f
18216 /* These routines generate the internal representation of the DIE's for
18217    the compilation unit.  Debugging information is collected by walking
18218    the declaration trees passed in from dwarf2out_decl().  */
18219
18220 static void
18221 gen_array_type_die (tree type, dw_die_ref context_die)
18222 {
18223   dw_die_ref scope_die = scope_die_for (type, context_die);
18224   dw_die_ref array_die;
18225
18226   /* GNU compilers represent multidimensional array types as sequences of one
18227      dimensional array types whose element types are themselves array types.
18228      We sometimes squish that down to a single array_type DIE with multiple
18229      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18230      say that we are allowed to do this kind of compression in C, because
18231      there is no difference between an array of arrays and a multidimensional
18232      array.  We don't do this for Ada to remain as close as possible to the
18233      actual representation, which is especially important against the language
18234      flexibilty wrt arrays of variable size.  */
18235
18236   bool collapse_nested_arrays = !is_ada ();
18237   tree element_type;
18238
18239   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18240      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18241   if (TYPE_STRING_FLAG (type)
18242       && TREE_CODE (type) == ARRAY_TYPE
18243       && is_fortran ()
18244       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18245     {
18246       HOST_WIDE_INT size;
18247
18248       array_die = new_die (DW_TAG_string_type, scope_die, type);
18249       add_name_attribute (array_die, type_tag (type));
18250       equate_type_number_to_die (type, array_die);
18251       size = int_size_in_bytes (type);
18252       if (size >= 0)
18253         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18254       else if (TYPE_DOMAIN (type) != NULL_TREE
18255                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18256                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18257         {
18258           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18259           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18260
18261           size = int_size_in_bytes (TREE_TYPE (szdecl));
18262           if (loc && size > 0)
18263             {
18264               add_AT_location_description (array_die, DW_AT_string_length, loc);
18265               if (size != DWARF2_ADDR_SIZE)
18266                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18267             }
18268         }
18269       return;
18270     }
18271
18272   /* ??? The SGI dwarf reader fails for array of array of enum types
18273      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18274      array type comes before the outer array type.  We thus call gen_type_die
18275      before we new_die and must prevent nested array types collapsing for this
18276      target.  */
18277
18278 #ifdef MIPS_DEBUGGING_INFO
18279   gen_type_die (TREE_TYPE (type), context_die);
18280   collapse_nested_arrays = false;
18281 #endif
18282
18283   array_die = new_die (DW_TAG_array_type, scope_die, type);
18284   add_name_attribute (array_die, type_tag (type));
18285   equate_type_number_to_die (type, array_die);
18286
18287   if (TREE_CODE (type) == VECTOR_TYPE)
18288     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18289
18290   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18291   if (is_fortran ()
18292       && TREE_CODE (type) == ARRAY_TYPE
18293       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18294       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18295     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18296
18297 #if 0
18298   /* We default the array ordering.  SDB will probably do
18299      the right things even if DW_AT_ordering is not present.  It's not even
18300      an issue until we start to get into multidimensional arrays anyway.  If
18301      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18302      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18303      and when we find out that we need to put these in, we will only do so
18304      for multidimensional arrays.  */
18305   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18306 #endif
18307
18308 #ifdef MIPS_DEBUGGING_INFO
18309   /* The SGI compilers handle arrays of unknown bound by setting
18310      AT_declaration and not emitting any subrange DIEs.  */
18311   if (TREE_CODE (type) == ARRAY_TYPE
18312       && ! TYPE_DOMAIN (type))
18313     add_AT_flag (array_die, DW_AT_declaration, 1);
18314   else
18315 #endif
18316   if (TREE_CODE (type) == VECTOR_TYPE)
18317     {
18318       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18319       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18320       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18321       add_bound_info (subrange_die, DW_AT_upper_bound,
18322                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18323     }
18324   else
18325     add_subscript_info (array_die, type, collapse_nested_arrays);
18326
18327   /* Add representation of the type of the elements of this array type and
18328      emit the corresponding DIE if we haven't done it already.  */
18329   element_type = TREE_TYPE (type);
18330   if (collapse_nested_arrays)
18331     while (TREE_CODE (element_type) == ARRAY_TYPE)
18332       {
18333         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18334           break;
18335         element_type = TREE_TYPE (element_type);
18336       }
18337
18338 #ifndef MIPS_DEBUGGING_INFO
18339   gen_type_die (element_type, context_die);
18340 #endif
18341
18342   add_type_attribute (array_die, element_type, 0, 0, context_die);
18343
18344   if (get_AT (array_die, DW_AT_name))
18345     add_pubtype (type, array_die);
18346 }
18347
18348 static dw_loc_descr_ref
18349 descr_info_loc (tree val, tree base_decl)
18350 {
18351   HOST_WIDE_INT size;
18352   dw_loc_descr_ref loc, loc2;
18353   enum dwarf_location_atom op;
18354
18355   if (val == base_decl)
18356     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18357
18358   switch (TREE_CODE (val))
18359     {
18360     CASE_CONVERT:
18361       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18362     case VAR_DECL:
18363       return loc_descriptor_from_tree (val, 0);
18364     case INTEGER_CST:
18365       if (host_integerp (val, 0))
18366         return int_loc_descriptor (tree_low_cst (val, 0));
18367       break;
18368     case INDIRECT_REF:
18369       size = int_size_in_bytes (TREE_TYPE (val));
18370       if (size < 0)
18371         break;
18372       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18373       if (!loc)
18374         break;
18375       if (size == DWARF2_ADDR_SIZE)
18376         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18377       else
18378         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18379       return loc;
18380     case POINTER_PLUS_EXPR:
18381     case PLUS_EXPR:
18382       if (host_integerp (TREE_OPERAND (val, 1), 1)
18383           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18384              < 16384)
18385         {
18386           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18387           if (!loc)
18388             break;
18389           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18390         }
18391       else
18392         {
18393           op = DW_OP_plus;
18394         do_binop:
18395           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18396           if (!loc)
18397             break;
18398           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18399           if (!loc2)
18400             break;
18401           add_loc_descr (&loc, loc2);
18402           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18403         }
18404       return loc;
18405     case MINUS_EXPR:
18406       op = DW_OP_minus;
18407       goto do_binop;
18408     case MULT_EXPR:
18409       op = DW_OP_mul;
18410       goto do_binop;
18411     case EQ_EXPR:
18412       op = DW_OP_eq;
18413       goto do_binop;
18414     case NE_EXPR:
18415       op = DW_OP_ne;
18416       goto do_binop;
18417     default:
18418       break;
18419     }
18420   return NULL;
18421 }
18422
18423 static void
18424 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18425                       tree val, tree base_decl)
18426 {
18427   dw_loc_descr_ref loc;
18428
18429   if (host_integerp (val, 0))
18430     {
18431       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18432       return;
18433     }
18434
18435   loc = descr_info_loc (val, base_decl);
18436   if (!loc)
18437     return;
18438
18439   add_AT_loc (die, attr, loc);
18440 }
18441
18442 /* This routine generates DIE for array with hidden descriptor, details
18443    are filled into *info by a langhook.  */
18444
18445 static void
18446 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18447                           dw_die_ref context_die)
18448 {
18449   dw_die_ref scope_die = scope_die_for (type, context_die);
18450   dw_die_ref array_die;
18451   int dim;
18452
18453   array_die = new_die (DW_TAG_array_type, scope_die, type);
18454   add_name_attribute (array_die, type_tag (type));
18455   equate_type_number_to_die (type, array_die);
18456
18457   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18458   if (is_fortran ()
18459       && info->ndimensions >= 2)
18460     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18461
18462   if (info->data_location)
18463     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18464                           info->base_decl);
18465   if (info->associated)
18466     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18467                           info->base_decl);
18468   if (info->allocated)
18469     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18470                           info->base_decl);
18471
18472   for (dim = 0; dim < info->ndimensions; dim++)
18473     {
18474       dw_die_ref subrange_die
18475         = new_die (DW_TAG_subrange_type, array_die, NULL);
18476
18477       if (info->dimen[dim].lower_bound)
18478         {
18479           /* If it is the default value, omit it.  */
18480           int dflt;
18481
18482           if (host_integerp (info->dimen[dim].lower_bound, 0)
18483               && (dflt = lower_bound_default ()) != -1
18484               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18485             ;
18486           else
18487             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18488                                   info->dimen[dim].lower_bound,
18489                                   info->base_decl);
18490         }
18491       if (info->dimen[dim].upper_bound)
18492         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18493                               info->dimen[dim].upper_bound,
18494                               info->base_decl);
18495       if (info->dimen[dim].stride)
18496         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18497                               info->dimen[dim].stride,
18498                               info->base_decl);
18499     }
18500
18501   gen_type_die (info->element_type, context_die);
18502   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18503
18504   if (get_AT (array_die, DW_AT_name))
18505     add_pubtype (type, array_die);
18506 }
18507
18508 #if 0
18509 static void
18510 gen_entry_point_die (tree decl, dw_die_ref context_die)
18511 {
18512   tree origin = decl_ultimate_origin (decl);
18513   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18514
18515   if (origin != NULL)
18516     add_abstract_origin_attribute (decl_die, origin);
18517   else
18518     {
18519       add_name_and_src_coords_attributes (decl_die, decl);
18520       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18521                           0, 0, context_die);
18522     }
18523
18524   if (DECL_ABSTRACT (decl))
18525     equate_decl_number_to_die (decl, decl_die);
18526   else
18527     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18528 }
18529 #endif
18530
18531 /* Walk through the list of incomplete types again, trying once more to
18532    emit full debugging info for them.  */
18533
18534 static void
18535 retry_incomplete_types (void)
18536 {
18537   int i;
18538
18539   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18540     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18541                                   DINFO_USAGE_DIR_USE))
18542       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18543 }
18544
18545 /* Determine what tag to use for a record type.  */
18546
18547 static enum dwarf_tag
18548 record_type_tag (tree type)
18549 {
18550   if (! lang_hooks.types.classify_record)
18551     return DW_TAG_structure_type;
18552
18553   switch (lang_hooks.types.classify_record (type))
18554     {
18555     case RECORD_IS_STRUCT:
18556       return DW_TAG_structure_type;
18557
18558     case RECORD_IS_CLASS:
18559       return DW_TAG_class_type;
18560
18561     case RECORD_IS_INTERFACE:
18562       if (dwarf_version >= 3 || !dwarf_strict)
18563         return DW_TAG_interface_type;
18564       return DW_TAG_structure_type;
18565
18566     default:
18567       gcc_unreachable ();
18568     }
18569 }
18570
18571 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18572    include all of the information about the enumeration values also. Each
18573    enumerated type name/value is listed as a child of the enumerated type
18574    DIE.  */
18575
18576 static dw_die_ref
18577 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18578 {
18579   dw_die_ref type_die = lookup_type_die (type);
18580
18581   if (type_die == NULL)
18582     {
18583       type_die = new_die (DW_TAG_enumeration_type,
18584                           scope_die_for (type, context_die), type);
18585       equate_type_number_to_die (type, type_die);
18586       add_name_attribute (type_die, type_tag (type));
18587       if (dwarf_version >= 4 || !dwarf_strict)
18588         {
18589           if (ENUM_IS_SCOPED (type))
18590             add_AT_flag (type_die, DW_AT_enum_class, 1);
18591           if (ENUM_IS_OPAQUE (type))
18592             add_AT_flag (type_die, DW_AT_declaration, 1);
18593         }
18594     }
18595   else if (! TYPE_SIZE (type))
18596     return type_die;
18597   else
18598     remove_AT (type_die, DW_AT_declaration);
18599
18600   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18601      given enum type is incomplete, do not generate the DW_AT_byte_size
18602      attribute or the DW_AT_element_list attribute.  */
18603   if (TYPE_SIZE (type))
18604     {
18605       tree link;
18606
18607       TREE_ASM_WRITTEN (type) = 1;
18608       add_byte_size_attribute (type_die, type);
18609       if (TYPE_STUB_DECL (type) != NULL_TREE)
18610         {
18611           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18612           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18613         }
18614
18615       /* If the first reference to this type was as the return type of an
18616          inline function, then it may not have a parent.  Fix this now.  */
18617       if (type_die->die_parent == NULL)
18618         add_child_die (scope_die_for (type, context_die), type_die);
18619
18620       for (link = TYPE_VALUES (type);
18621            link != NULL; link = TREE_CHAIN (link))
18622         {
18623           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18624           tree value = TREE_VALUE (link);
18625
18626           add_name_attribute (enum_die,
18627                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18628
18629           if (TREE_CODE (value) == CONST_DECL)
18630             value = DECL_INITIAL (value);
18631
18632           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18633             /* DWARF2 does not provide a way of indicating whether or
18634                not enumeration constants are signed or unsigned.  GDB
18635                always assumes the values are signed, so we output all
18636                values as if they were signed.  That means that
18637                enumeration constants with very large unsigned values
18638                will appear to have negative values in the debugger.  */
18639             add_AT_int (enum_die, DW_AT_const_value,
18640                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18641         }
18642     }
18643   else
18644     add_AT_flag (type_die, DW_AT_declaration, 1);
18645
18646   if (get_AT (type_die, DW_AT_name))
18647     add_pubtype (type, type_die);
18648
18649   return type_die;
18650 }
18651
18652 /* Generate a DIE to represent either a real live formal parameter decl or to
18653    represent just the type of some formal parameter position in some function
18654    type.
18655
18656    Note that this routine is a bit unusual because its argument may be a
18657    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18658    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18659    node.  If it's the former then this function is being called to output a
18660    DIE to represent a formal parameter object (or some inlining thereof).  If
18661    it's the latter, then this function is only being called to output a
18662    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18663    argument type of some subprogram type.
18664    If EMIT_NAME_P is true, name and source coordinate attributes
18665    are emitted.  */
18666
18667 static dw_die_ref
18668 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18669                           dw_die_ref context_die)
18670 {
18671   tree node_or_origin = node ? node : origin;
18672   tree ultimate_origin;
18673   dw_die_ref parm_die
18674     = new_die (DW_TAG_formal_parameter, context_die, node);
18675
18676   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18677     {
18678     case tcc_declaration:
18679       ultimate_origin = decl_ultimate_origin (node_or_origin);
18680       if (node || ultimate_origin)
18681         origin = ultimate_origin;
18682       if (origin != NULL)
18683         add_abstract_origin_attribute (parm_die, origin);
18684       else if (emit_name_p)
18685         add_name_and_src_coords_attributes (parm_die, node);
18686       if (origin == NULL
18687           || (! DECL_ABSTRACT (node_or_origin)
18688               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18689                                            decl_function_context
18690                                                             (node_or_origin))))
18691         {
18692           tree type = TREE_TYPE (node_or_origin);
18693           if (decl_by_reference_p (node_or_origin))
18694             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18695                                 context_die);
18696           else
18697             add_type_attribute (parm_die, type,
18698                                 TREE_READONLY (node_or_origin),
18699                                 TREE_THIS_VOLATILE (node_or_origin),
18700                                 context_die);
18701         }
18702       if (origin == NULL && DECL_ARTIFICIAL (node))
18703         add_AT_flag (parm_die, DW_AT_artificial, 1);
18704
18705       if (node && node != origin)
18706         equate_decl_number_to_die (node, parm_die);
18707       if (! DECL_ABSTRACT (node_or_origin))
18708         add_location_or_const_value_attribute (parm_die, node_or_origin,
18709                                                node == NULL, DW_AT_location);
18710
18711       break;
18712
18713     case tcc_type:
18714       /* We were called with some kind of a ..._TYPE node.  */
18715       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18716       break;
18717
18718     default:
18719       gcc_unreachable ();
18720     }
18721
18722   return parm_die;
18723 }
18724
18725 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18726    children DW_TAG_formal_parameter DIEs representing the arguments of the
18727    parameter pack.
18728
18729    PARM_PACK must be a function parameter pack.
18730    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18731    must point to the subsequent arguments of the function PACK_ARG belongs to.
18732    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18733    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18734    following the last one for which a DIE was generated.  */
18735
18736 static dw_die_ref
18737 gen_formal_parameter_pack_die  (tree parm_pack,
18738                                 tree pack_arg,
18739                                 dw_die_ref subr_die,
18740                                 tree *next_arg)
18741 {
18742   tree arg;
18743   dw_die_ref parm_pack_die;
18744
18745   gcc_assert (parm_pack
18746               && lang_hooks.function_parameter_pack_p (parm_pack)
18747               && subr_die);
18748
18749   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18750   add_src_coords_attributes (parm_pack_die, parm_pack);
18751
18752   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18753     {
18754       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18755                                                                  parm_pack))
18756         break;
18757       gen_formal_parameter_die (arg, NULL,
18758                                 false /* Don't emit name attribute.  */,
18759                                 parm_pack_die);
18760     }
18761   if (next_arg)
18762     *next_arg = arg;
18763   return parm_pack_die;
18764 }
18765
18766 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18767    at the end of an (ANSI prototyped) formal parameters list.  */
18768
18769 static void
18770 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18771 {
18772   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18773 }
18774
18775 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18776    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18777    parameters as specified in some function type specification (except for
18778    those which appear as part of a function *definition*).  */
18779
18780 static void
18781 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18782 {
18783   tree link;
18784   tree formal_type = NULL;
18785   tree first_parm_type;
18786   tree arg;
18787
18788   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18789     {
18790       arg = DECL_ARGUMENTS (function_or_method_type);
18791       function_or_method_type = TREE_TYPE (function_or_method_type);
18792     }
18793   else
18794     arg = NULL_TREE;
18795
18796   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18797
18798   /* Make our first pass over the list of formal parameter types and output a
18799      DW_TAG_formal_parameter DIE for each one.  */
18800   for (link = first_parm_type; link; )
18801     {
18802       dw_die_ref parm_die;
18803
18804       formal_type = TREE_VALUE (link);
18805       if (formal_type == void_type_node)
18806         break;
18807
18808       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18809       parm_die = gen_formal_parameter_die (formal_type, NULL,
18810                                            true /* Emit name attribute.  */,
18811                                            context_die);
18812       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18813           && link == first_parm_type)
18814         {
18815           add_AT_flag (parm_die, DW_AT_artificial, 1);
18816           if (dwarf_version >= 3 || !dwarf_strict)
18817             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18818         }
18819       else if (arg && DECL_ARTIFICIAL (arg))
18820         add_AT_flag (parm_die, DW_AT_artificial, 1);
18821
18822       link = TREE_CHAIN (link);
18823       if (arg)
18824         arg = DECL_CHAIN (arg);
18825     }
18826
18827   /* If this function type has an ellipsis, add a
18828      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18829   if (formal_type != void_type_node)
18830     gen_unspecified_parameters_die (function_or_method_type, context_die);
18831
18832   /* Make our second (and final) pass over the list of formal parameter types
18833      and output DIEs to represent those types (as necessary).  */
18834   for (link = TYPE_ARG_TYPES (function_or_method_type);
18835        link && TREE_VALUE (link);
18836        link = TREE_CHAIN (link))
18837     gen_type_die (TREE_VALUE (link), context_die);
18838 }
18839
18840 /* We want to generate the DIE for TYPE so that we can generate the
18841    die for MEMBER, which has been defined; we will need to refer back
18842    to the member declaration nested within TYPE.  If we're trying to
18843    generate minimal debug info for TYPE, processing TYPE won't do the
18844    trick; we need to attach the member declaration by hand.  */
18845
18846 static void
18847 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18848 {
18849   gen_type_die (type, context_die);
18850
18851   /* If we're trying to avoid duplicate debug info, we may not have
18852      emitted the member decl for this function.  Emit it now.  */
18853   if (TYPE_STUB_DECL (type)
18854       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18855       && ! lookup_decl_die (member))
18856     {
18857       dw_die_ref type_die;
18858       gcc_assert (!decl_ultimate_origin (member));
18859
18860       push_decl_scope (type);
18861       type_die = lookup_type_die_strip_naming_typedef (type);
18862       if (TREE_CODE (member) == FUNCTION_DECL)
18863         gen_subprogram_die (member, type_die);
18864       else if (TREE_CODE (member) == FIELD_DECL)
18865         {
18866           /* Ignore the nameless fields that are used to skip bits but handle
18867              C++ anonymous unions and structs.  */
18868           if (DECL_NAME (member) != NULL_TREE
18869               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18870               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18871             {
18872               gen_type_die (member_declared_type (member), type_die);
18873               gen_field_die (member, type_die);
18874             }
18875         }
18876       else
18877         gen_variable_die (member, NULL_TREE, type_die);
18878
18879       pop_decl_scope ();
18880     }
18881 }
18882
18883 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18884    may later generate inlined and/or out-of-line instances of.  */
18885
18886 static void
18887 dwarf2out_abstract_function (tree decl)
18888 {
18889   dw_die_ref old_die;
18890   tree save_fn;
18891   tree context;
18892   int was_abstract;
18893   htab_t old_decl_loc_table;
18894   htab_t old_cached_dw_loc_list_table;
18895   int old_call_site_count, old_tail_call_site_count;
18896   struct call_arg_loc_node *old_call_arg_locations;
18897
18898   /* Make sure we have the actual abstract inline, not a clone.  */
18899   decl = DECL_ORIGIN (decl);
18900
18901   old_die = lookup_decl_die (decl);
18902   if (old_die && get_AT (old_die, DW_AT_inline))
18903     /* We've already generated the abstract instance.  */
18904     return;
18905
18906   /* We can be called while recursively when seeing block defining inlined subroutine
18907      DIE.  Be sure to not clobber the outer location table nor use it or we would
18908      get locations in abstract instantces.  */
18909   old_decl_loc_table = decl_loc_table;
18910   decl_loc_table = NULL;
18911   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
18912   cached_dw_loc_list_table = NULL;
18913   old_call_arg_locations = call_arg_locations;
18914   call_arg_locations = NULL;
18915   old_call_site_count = call_site_count;
18916   call_site_count = -1;
18917   old_tail_call_site_count = tail_call_site_count;
18918   tail_call_site_count = -1;
18919
18920   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18921      we don't get confused by DECL_ABSTRACT.  */
18922   if (debug_info_level > DINFO_LEVEL_TERSE)
18923     {
18924       context = decl_class_context (decl);
18925       if (context)
18926         gen_type_die_for_member
18927           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18928     }
18929
18930   /* Pretend we've just finished compiling this function.  */
18931   save_fn = current_function_decl;
18932   current_function_decl = decl;
18933   push_cfun (DECL_STRUCT_FUNCTION (decl));
18934
18935   was_abstract = DECL_ABSTRACT (decl);
18936   set_decl_abstract_flags (decl, 1);
18937   dwarf2out_decl (decl);
18938   if (! was_abstract)
18939     set_decl_abstract_flags (decl, 0);
18940
18941   current_function_decl = save_fn;
18942   decl_loc_table = old_decl_loc_table;
18943   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
18944   call_arg_locations = old_call_arg_locations;
18945   call_site_count = old_call_site_count;
18946   tail_call_site_count = old_tail_call_site_count;
18947   pop_cfun ();
18948 }
18949
18950 /* Helper function of premark_used_types() which gets called through
18951    htab_traverse.
18952
18953    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18954    marked as unused by prune_unused_types.  */
18955
18956 static int
18957 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18958 {
18959   tree type;
18960   dw_die_ref die;
18961
18962   type = (tree) *slot;
18963   die = lookup_type_die (type);
18964   if (die != NULL)
18965     die->die_perennial_p = 1;
18966   return 1;
18967 }
18968
18969 /* Helper function of premark_types_used_by_global_vars which gets called
18970    through htab_traverse.
18971
18972    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18973    marked as unused by prune_unused_types. The DIE of the type is marked
18974    only if the global variable using the type will actually be emitted.  */
18975
18976 static int
18977 premark_types_used_by_global_vars_helper (void **slot,
18978                                           void *data ATTRIBUTE_UNUSED)
18979 {
18980   struct types_used_by_vars_entry *entry;
18981   dw_die_ref die;
18982
18983   entry = (struct types_used_by_vars_entry *) *slot;
18984   gcc_assert (entry->type != NULL
18985               && entry->var_decl != NULL);
18986   die = lookup_type_die (entry->type);
18987   if (die)
18988     {
18989       /* Ask cgraph if the global variable really is to be emitted.
18990          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18991       struct varpool_node *node = varpool_get_node (entry->var_decl);
18992       if (node && node->needed)
18993         {
18994           die->die_perennial_p = 1;
18995           /* Keep the parent DIEs as well.  */
18996           while ((die = die->die_parent) && die->die_perennial_p == 0)
18997             die->die_perennial_p = 1;
18998         }
18999     }
19000   return 1;
19001 }
19002
19003 /* Mark all members of used_types_hash as perennial.  */
19004
19005 static void
19006 premark_used_types (void)
19007 {
19008   if (cfun && cfun->used_types_hash)
19009     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
19010 }
19011
19012 /* Mark all members of types_used_by_vars_entry as perennial.  */
19013
19014 static void
19015 premark_types_used_by_global_vars (void)
19016 {
19017   if (types_used_by_vars_hash)
19018     htab_traverse (types_used_by_vars_hash,
19019                    premark_types_used_by_global_vars_helper, NULL);
19020 }
19021
19022 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
19023    for CA_LOC call arg loc node.  */
19024
19025 static dw_die_ref
19026 gen_call_site_die (tree decl, dw_die_ref subr_die,
19027                    struct call_arg_loc_node *ca_loc)
19028 {
19029   dw_die_ref stmt_die = NULL, die;
19030   tree block = ca_loc->block;
19031
19032   while (block
19033          && block != DECL_INITIAL (decl)
19034          && TREE_CODE (block) == BLOCK)
19035     {
19036       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
19037         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
19038       if (stmt_die)
19039         break;
19040       block = BLOCK_SUPERCONTEXT (block);
19041     }
19042   if (stmt_die == NULL)
19043     stmt_die = subr_die;
19044   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
19045   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
19046   if (ca_loc->tail_call_p)
19047     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
19048   if (ca_loc->symbol_ref)
19049     {
19050       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
19051       if (tdie)
19052         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
19053       else
19054         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
19055     }
19056   return die;
19057 }
19058
19059 /* Generate a DIE to represent a declared function (either file-scope or
19060    block-local).  */
19061
19062 static void
19063 gen_subprogram_die (tree decl, dw_die_ref context_die)
19064 {
19065   tree origin = decl_ultimate_origin (decl);
19066   dw_die_ref subr_die;
19067   tree outer_scope;
19068   dw_die_ref old_die = lookup_decl_die (decl);
19069   int declaration = (current_function_decl != decl
19070                      || class_or_namespace_scope_p (context_die));
19071
19072   premark_used_types ();
19073
19074   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19075      started to generate the abstract instance of an inline, decided to output
19076      its containing class, and proceeded to emit the declaration of the inline
19077      from the member list for the class.  If so, DECLARATION takes priority;
19078      we'll get back to the abstract instance when done with the class.  */
19079
19080   /* The class-scope declaration DIE must be the primary DIE.  */
19081   if (origin && declaration && class_or_namespace_scope_p (context_die))
19082     {
19083       origin = NULL;
19084       gcc_assert (!old_die);
19085     }
19086
19087   /* Now that the C++ front end lazily declares artificial member fns, we
19088      might need to retrofit the declaration into its class.  */
19089   if (!declaration && !origin && !old_die
19090       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19091       && !class_or_namespace_scope_p (context_die)
19092       && debug_info_level > DINFO_LEVEL_TERSE)
19093     old_die = force_decl_die (decl);
19094
19095   if (origin != NULL)
19096     {
19097       gcc_assert (!declaration || local_scope_p (context_die));
19098
19099       /* Fixup die_parent for the abstract instance of a nested
19100          inline function.  */
19101       if (old_die && old_die->die_parent == NULL)
19102         add_child_die (context_die, old_die);
19103
19104       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19105       add_abstract_origin_attribute (subr_die, origin);
19106     }
19107   else if (old_die)
19108     {
19109       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19110       struct dwarf_file_data * file_index = lookup_filename (s.file);
19111
19112       if (!get_AT_flag (old_die, DW_AT_declaration)
19113           /* We can have a normal definition following an inline one in the
19114              case of redefinition of GNU C extern inlines.
19115              It seems reasonable to use AT_specification in this case.  */
19116           && !get_AT (old_die, DW_AT_inline))
19117         {
19118           /* Detect and ignore this case, where we are trying to output
19119              something we have already output.  */
19120           return;
19121         }
19122
19123       /* If the definition comes from the same place as the declaration,
19124          maybe use the old DIE.  We always want the DIE for this function
19125          that has the *_pc attributes to be under comp_unit_die so the
19126          debugger can find it.  We also need to do this for abstract
19127          instances of inlines, since the spec requires the out-of-line copy
19128          to have the same parent.  For local class methods, this doesn't
19129          apply; we just use the old DIE.  */
19130       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19131           && (DECL_ARTIFICIAL (decl)
19132               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19133                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19134                       == (unsigned) s.line))))
19135         {
19136           subr_die = old_die;
19137
19138           /* Clear out the declaration attribute and the formal parameters.
19139              Do not remove all children, because it is possible that this
19140              declaration die was forced using force_decl_die(). In such
19141              cases die that forced declaration die (e.g. TAG_imported_module)
19142              is one of the children that we do not want to remove.  */
19143           remove_AT (subr_die, DW_AT_declaration);
19144           remove_AT (subr_die, DW_AT_object_pointer);
19145           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19146         }
19147       else
19148         {
19149           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19150           add_AT_specification (subr_die, old_die);
19151           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19152             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19153           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19154             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19155         }
19156     }
19157   else
19158     {
19159       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19160
19161       if (TREE_PUBLIC (decl))
19162         add_AT_flag (subr_die, DW_AT_external, 1);
19163
19164       add_name_and_src_coords_attributes (subr_die, decl);
19165       if (debug_info_level > DINFO_LEVEL_TERSE)
19166         {
19167           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19168           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19169                               0, 0, context_die);
19170         }
19171
19172       add_pure_or_virtual_attribute (subr_die, decl);
19173       if (DECL_ARTIFICIAL (decl))
19174         add_AT_flag (subr_die, DW_AT_artificial, 1);
19175
19176       add_accessibility_attribute (subr_die, decl);
19177     }
19178
19179   if (declaration)
19180     {
19181       if (!old_die || !get_AT (old_die, DW_AT_inline))
19182         {
19183           add_AT_flag (subr_die, DW_AT_declaration, 1);
19184
19185           /* If this is an explicit function declaration then generate
19186              a DW_AT_explicit attribute.  */
19187           if (lang_hooks.decls.function_decl_explicit_p (decl)
19188               && (dwarf_version >= 3 || !dwarf_strict))
19189             add_AT_flag (subr_die, DW_AT_explicit, 1);
19190
19191           /* The first time we see a member function, it is in the context of
19192              the class to which it belongs.  We make sure of this by emitting
19193              the class first.  The next time is the definition, which is
19194              handled above.  The two may come from the same source text.
19195
19196              Note that force_decl_die() forces function declaration die. It is
19197              later reused to represent definition.  */
19198           equate_decl_number_to_die (decl, subr_die);
19199         }
19200     }
19201   else if (DECL_ABSTRACT (decl))
19202     {
19203       if (DECL_DECLARED_INLINE_P (decl))
19204         {
19205           if (cgraph_function_possibly_inlined_p (decl))
19206             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19207           else
19208             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19209         }
19210       else
19211         {
19212           if (cgraph_function_possibly_inlined_p (decl))
19213             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19214           else
19215             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19216         }
19217
19218       if (DECL_DECLARED_INLINE_P (decl)
19219           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19220         add_AT_flag (subr_die, DW_AT_artificial, 1);
19221
19222       equate_decl_number_to_die (decl, subr_die);
19223     }
19224   else if (!DECL_EXTERNAL (decl))
19225     {
19226       HOST_WIDE_INT cfa_fb_offset;
19227
19228       if (!old_die || !get_AT (old_die, DW_AT_inline))
19229         equate_decl_number_to_die (decl, subr_die);
19230
19231       if (!flag_reorder_blocks_and_partition)
19232         {
19233           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19234           if (fde->dw_fde_begin)
19235             {
19236               /* We have already generated the labels.  */
19237               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19238               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19239             }
19240           else
19241             {
19242               /* Create start/end labels and add the range.  */
19243               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19244               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19245                                            current_function_funcdef_no);
19246               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19247               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19248                                            current_function_funcdef_no);
19249               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19250             }
19251
19252 #if VMS_DEBUGGING_INFO
19253       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19254          Section 2.3 Prologue and Epilogue Attributes:
19255          When a breakpoint is set on entry to a function, it is generally
19256          desirable for execution to be suspended, not on the very first
19257          instruction of the function, but rather at a point after the
19258          function's frame has been set up, after any language defined local
19259          declaration processing has been completed, and before execution of
19260          the first statement of the function begins. Debuggers generally
19261          cannot properly determine where this point is.  Similarly for a
19262          breakpoint set on exit from a function. The prologue and epilogue
19263          attributes allow a compiler to communicate the location(s) to use.  */
19264
19265       {
19266         if (fde->dw_fde_vms_end_prologue)
19267           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19268             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19269
19270         if (fde->dw_fde_vms_begin_epilogue)
19271           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19272             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19273       }
19274 #endif
19275
19276           add_pubname (decl, subr_die);
19277         }
19278       else
19279         {  /* Generate pubnames entries for the split function code
19280               ranges.  */
19281           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19282
19283           if (fde->dw_fde_second_begin)
19284             {
19285               if (dwarf_version >= 3 || !dwarf_strict)
19286                 {
19287                   /* We should use ranges for non-contiguous code section 
19288                      addresses.  Use the actual code range for the initial
19289                      section, since the HOT/COLD labels might precede an 
19290                      alignment offset.  */
19291                   bool range_list_added = false;
19292                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19293                                         fde->dw_fde_end, &range_list_added);
19294                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19295                                         fde->dw_fde_second_end,
19296                                         &range_list_added);
19297                   add_pubname (decl, subr_die);
19298                   if (range_list_added)
19299                     add_ranges (NULL);
19300                 }
19301               else
19302                 {
19303                   /* There is no real support in DW2 for this .. so we make
19304                      a work-around.  First, emit the pub name for the segment
19305                      containing the function label.  Then make and emit a
19306                      simplified subprogram DIE for the second segment with the
19307                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19308                      linkage name for the second die so that gdb will find both
19309                      sections when given "b foo".  */
19310                   const char *name = NULL;
19311                   tree decl_name = DECL_NAME (decl);
19312                   dw_die_ref seg_die;
19313
19314                   /* Do the 'primary' section.   */
19315                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19316                                  fde->dw_fde_begin);
19317                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19318                                  fde->dw_fde_end);
19319                   /* Add it.   */
19320                   add_pubname (decl, subr_die);
19321
19322                   /* Build a minimal DIE for the secondary section.  */
19323                   seg_die = new_die (DW_TAG_subprogram,
19324                                      subr_die->die_parent, decl);
19325
19326                   if (TREE_PUBLIC (decl))
19327                     add_AT_flag (seg_die, DW_AT_external, 1);
19328
19329                   if (decl_name != NULL 
19330                       && IDENTIFIER_POINTER (decl_name) != NULL)
19331                     {
19332                       name = dwarf2_name (decl, 1);
19333                       if (! DECL_ARTIFICIAL (decl))
19334                         add_src_coords_attributes (seg_die, decl);
19335
19336                       add_linkage_name (seg_die, decl);
19337                     }
19338                   gcc_assert (name != NULL);
19339                   add_pure_or_virtual_attribute (seg_die, decl);
19340                   if (DECL_ARTIFICIAL (decl))
19341                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19342
19343                   name = concat ("__second_sect_of_", name, NULL); 
19344                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
19345                                  fde->dw_fde_second_begin);
19346                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
19347                                  fde->dw_fde_second_end);
19348                   add_name_attribute (seg_die, name);
19349                   add_pubname_string (name, seg_die);
19350                 }
19351             }
19352           else
19353             {
19354               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19355               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19356               add_pubname (decl, subr_die);
19357             }
19358         }
19359
19360 #ifdef MIPS_DEBUGGING_INFO
19361       /* Add a reference to the FDE for this routine.  */
19362       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19363 #endif
19364
19365       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19366
19367       /* We define the "frame base" as the function's CFA.  This is more
19368          convenient for several reasons: (1) It's stable across the prologue
19369          and epilogue, which makes it better than just a frame pointer,
19370          (2) With dwarf3, there exists a one-byte encoding that allows us
19371          to reference the .debug_frame data by proxy, but failing that,
19372          (3) We can at least reuse the code inspection and interpretation
19373          code that determines the CFA position at various points in the
19374          function.  */
19375       if (dwarf_version >= 3)
19376         {
19377           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19378           add_AT_loc (subr_die, DW_AT_frame_base, op);
19379         }
19380       else
19381         {
19382           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19383           if (list->dw_loc_next)
19384             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19385           else
19386             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19387         }
19388
19389       /* Compute a displacement from the "steady-state frame pointer" to
19390          the CFA.  The former is what all stack slots and argument slots
19391          will reference in the rtl; the later is what we've told the
19392          debugger about.  We'll need to adjust all frame_base references
19393          by this displacement.  */
19394       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19395
19396       if (cfun->static_chain_decl)
19397         add_AT_location_description (subr_die, DW_AT_static_link,
19398                  loc_list_from_tree (cfun->static_chain_decl, 2));
19399     }
19400
19401   /* Generate child dies for template paramaters.  */
19402   if (debug_info_level > DINFO_LEVEL_TERSE)
19403     gen_generic_params_dies (decl);
19404
19405   /* Now output descriptions of the arguments for this function. This gets
19406      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19407      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19408      `...' at the end of the formal parameter list.  In order to find out if
19409      there was a trailing ellipsis or not, we must instead look at the type
19410      associated with the FUNCTION_DECL.  This will be a node of type
19411      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19412      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19413      an ellipsis at the end.  */
19414
19415   /* In the case where we are describing a mere function declaration, all we
19416      need to do here (and all we *can* do here) is to describe the *types* of
19417      its formal parameters.  */
19418   if (debug_info_level <= DINFO_LEVEL_TERSE)
19419     ;
19420   else if (declaration)
19421     gen_formal_types_die (decl, subr_die);
19422   else
19423     {
19424       /* Generate DIEs to represent all known formal parameters.  */
19425       tree parm = DECL_ARGUMENTS (decl);
19426       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19427       tree generic_decl_parm = generic_decl
19428                                 ? DECL_ARGUMENTS (generic_decl)
19429                                 : NULL;
19430
19431       /* Now we want to walk the list of parameters of the function and
19432          emit their relevant DIEs.
19433
19434          We consider the case of DECL being an instance of a generic function
19435          as well as it being a normal function.
19436
19437          If DECL is an instance of a generic function we walk the
19438          parameters of the generic function declaration _and_ the parameters of
19439          DECL itself. This is useful because we want to emit specific DIEs for
19440          function parameter packs and those are declared as part of the
19441          generic function declaration. In that particular case,
19442          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19443          That DIE has children DIEs representing the set of arguments
19444          of the pack. Note that the set of pack arguments can be empty.
19445          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19446          children DIE.
19447
19448          Otherwise, we just consider the parameters of DECL.  */
19449       while (generic_decl_parm || parm)
19450         {
19451           if (generic_decl_parm
19452               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19453             gen_formal_parameter_pack_die (generic_decl_parm,
19454                                            parm, subr_die,
19455                                            &parm);
19456           else if (parm)
19457             {
19458               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19459
19460               if (parm == DECL_ARGUMENTS (decl)
19461                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19462                   && parm_die
19463                   && (dwarf_version >= 3 || !dwarf_strict))
19464                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19465
19466               parm = DECL_CHAIN (parm);
19467             }
19468
19469           if (generic_decl_parm)
19470             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19471         }
19472
19473       /* Decide whether we need an unspecified_parameters DIE at the end.
19474          There are 2 more cases to do this for: 1) the ansi ... declaration -
19475          this is detectable when the end of the arg list is not a
19476          void_type_node 2) an unprototyped function declaration (not a
19477          definition).  This just means that we have no info about the
19478          parameters at all.  */
19479       if (prototype_p (TREE_TYPE (decl)))
19480         {
19481           /* This is the prototyped case, check for....  */
19482           if (stdarg_p (TREE_TYPE (decl)))
19483             gen_unspecified_parameters_die (decl, subr_die);
19484         }
19485       else if (DECL_INITIAL (decl) == NULL_TREE)
19486         gen_unspecified_parameters_die (decl, subr_die);
19487     }
19488
19489   /* Output Dwarf info for all of the stuff within the body of the function
19490      (if it has one - it may be just a declaration).  */
19491   outer_scope = DECL_INITIAL (decl);
19492
19493   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19494      a function.  This BLOCK actually represents the outermost binding contour
19495      for the function, i.e. the contour in which the function's formal
19496      parameters and labels get declared. Curiously, it appears that the front
19497      end doesn't actually put the PARM_DECL nodes for the current function onto
19498      the BLOCK_VARS list for this outer scope, but are strung off of the
19499      DECL_ARGUMENTS list for the function instead.
19500
19501      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19502      the LABEL_DECL nodes for the function however, and we output DWARF info
19503      for those in decls_for_scope.  Just within the `outer_scope' there will be
19504      a BLOCK node representing the function's outermost pair of curly braces,
19505      and any blocks used for the base and member initializers of a C++
19506      constructor function.  */
19507   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19508     {
19509       int call_site_note_count = 0;
19510       int tail_call_site_note_count = 0;
19511
19512       /* Emit a DW_TAG_variable DIE for a named return value.  */
19513       if (DECL_NAME (DECL_RESULT (decl)))
19514         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19515
19516       current_function_has_inlines = 0;
19517       decls_for_scope (outer_scope, subr_die, 0);
19518
19519       if (call_arg_locations && !dwarf_strict)
19520         {
19521           struct call_arg_loc_node *ca_loc;
19522           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19523             {
19524               dw_die_ref die = NULL;
19525               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
19526               rtx arg, next_arg;
19527
19528               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19529                    arg; arg = next_arg)
19530                 {
19531                   dw_loc_descr_ref reg, val;
19532                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19533                   dw_die_ref cdie;
19534
19535                   next_arg = XEXP (arg, 1);
19536                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19537                       && next_arg
19538                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19539                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19540                       && REGNO (XEXP (XEXP (arg, 0), 0))
19541                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19542                     next_arg = XEXP (next_arg, 1);
19543                   if (mode == VOIDmode)
19544                     {
19545                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19546                       if (mode == VOIDmode)
19547                         mode = GET_MODE (XEXP (arg, 0));
19548                     }
19549                   if (GET_MODE_CLASS (mode) != MODE_INT
19550                       || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19551                     continue;
19552                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19553                     {
19554                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19555                       tloc = XEXP (XEXP (arg, 0), 1);
19556                       continue;
19557                     }
19558                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
19559                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
19560                     {
19561                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19562                       tlocc = XEXP (XEXP (arg, 0), 1);
19563                       continue;
19564                     }
19565                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19566                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19567                                               VAR_INIT_STATUS_INITIALIZED);
19568                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19569                     reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19570                                                           0), 0), mode,
19571                                               VAR_INIT_STATUS_INITIALIZED);
19572                   else
19573                     continue;
19574                   if (reg == NULL)
19575                     continue;
19576                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19577                                             VAR_INIT_STATUS_INITIALIZED);
19578                   if (val == NULL)
19579                     continue;
19580                   if (die == NULL)
19581                     die = gen_call_site_die (decl, subr_die, ca_loc);
19582                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19583                                   NULL_TREE);           
19584                   add_AT_loc (cdie, DW_AT_location, reg);
19585                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19586                   if (next_arg != XEXP (arg, 1))
19587                     {
19588                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19589                                                             0), 1), VOIDmode,
19590                                                 VAR_INIT_STATUS_INITIALIZED);
19591                       if (val != NULL)
19592                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19593                     }
19594                 }
19595               if (die == NULL
19596                   && (ca_loc->symbol_ref || tloc))
19597                 die = gen_call_site_die (decl, subr_die, ca_loc);
19598               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
19599                 {
19600                   dw_loc_descr_ref tval = NULL;
19601
19602                   if (tloc != NULL_RTX)
19603                     tval = mem_loc_descriptor (tloc, VOIDmode,
19604                                                VAR_INIT_STATUS_INITIALIZED);
19605                   if (tval)
19606                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19607                   else if (tlocc != NULL_RTX)
19608                     {
19609                       tval = mem_loc_descriptor (tlocc, VOIDmode,
19610                                                  VAR_INIT_STATUS_INITIALIZED);
19611                       if (tval)
19612                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
19613                                     tval);
19614                     }
19615                 }
19616               if (die != NULL)
19617                 {
19618                   call_site_note_count++;
19619                   if (ca_loc->tail_call_p)
19620                     tail_call_site_note_count++;
19621                 }
19622             }
19623         }
19624       call_arg_locations = NULL;
19625       call_arg_loc_last = NULL;
19626       if (tail_call_site_count >= 0
19627           && tail_call_site_count == tail_call_site_note_count
19628           && !dwarf_strict)
19629         {
19630           if (call_site_count >= 0
19631               && call_site_count == call_site_note_count)
19632             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19633           else
19634             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19635         }
19636       call_site_count = -1;
19637       tail_call_site_count = -1;
19638     }
19639   /* Add the calling convention attribute if requested.  */
19640   add_calling_convention_attribute (subr_die, decl);
19641
19642 }
19643
19644 /* Returns a hash value for X (which really is a die_struct).  */
19645
19646 static hashval_t
19647 common_block_die_table_hash (const void *x)
19648 {
19649   const_dw_die_ref d = (const_dw_die_ref) x;
19650   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19651 }
19652
19653 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19654    as decl_id and die_parent of die_struct Y.  */
19655
19656 static int
19657 common_block_die_table_eq (const void *x, const void *y)
19658 {
19659   const_dw_die_ref d = (const_dw_die_ref) x;
19660   const_dw_die_ref e = (const_dw_die_ref) y;
19661   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19662 }
19663
19664 /* Generate a DIE to represent a declared data object.
19665    Either DECL or ORIGIN must be non-null.  */
19666
19667 static void
19668 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19669 {
19670   HOST_WIDE_INT off;
19671   tree com_decl;
19672   tree decl_or_origin = decl ? decl : origin;
19673   tree ultimate_origin;
19674   dw_die_ref var_die;
19675   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19676   dw_die_ref origin_die;
19677   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19678                       || class_or_namespace_scope_p (context_die));
19679   bool specialization_p = false;
19680
19681   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19682   if (decl || ultimate_origin)
19683     origin = ultimate_origin;
19684   com_decl = fortran_common (decl_or_origin, &off);
19685
19686   /* Symbol in common gets emitted as a child of the common block, in the form
19687      of a data member.  */
19688   if (com_decl)
19689     {
19690       dw_die_ref com_die;
19691       dw_loc_list_ref loc;
19692       die_node com_die_arg;
19693
19694       var_die = lookup_decl_die (decl_or_origin);
19695       if (var_die)
19696         {
19697           if (get_AT (var_die, DW_AT_location) == NULL)
19698             {
19699               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19700               if (loc)
19701                 {
19702                   if (off)
19703                     {
19704                       /* Optimize the common case.  */
19705                       if (single_element_loc_list_p (loc)
19706                           && loc->expr->dw_loc_opc == DW_OP_addr
19707                           && loc->expr->dw_loc_next == NULL
19708                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19709                              == SYMBOL_REF)
19710                         loc->expr->dw_loc_oprnd1.v.val_addr
19711                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19712                         else
19713                           loc_list_plus_const (loc, off);
19714                     }
19715                   add_AT_location_description (var_die, DW_AT_location, loc);
19716                   remove_AT (var_die, DW_AT_declaration);
19717                 }
19718             }
19719           return;
19720         }
19721
19722       if (common_block_die_table == NULL)
19723         common_block_die_table
19724           = htab_create_ggc (10, common_block_die_table_hash,
19725                              common_block_die_table_eq, NULL);
19726
19727       com_die_arg.decl_id = DECL_UID (com_decl);
19728       com_die_arg.die_parent = context_die;
19729       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19730       loc = loc_list_from_tree (com_decl, 2);
19731       if (com_die == NULL)
19732         {
19733           const char *cnam
19734             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19735           void **slot;
19736
19737           com_die = new_die (DW_TAG_common_block, context_die, decl);
19738           add_name_and_src_coords_attributes (com_die, com_decl);
19739           if (loc)
19740             {
19741               add_AT_location_description (com_die, DW_AT_location, loc);
19742               /* Avoid sharing the same loc descriptor between
19743                  DW_TAG_common_block and DW_TAG_variable.  */
19744               loc = loc_list_from_tree (com_decl, 2);
19745             }
19746           else if (DECL_EXTERNAL (decl))
19747             add_AT_flag (com_die, DW_AT_declaration, 1);
19748           add_pubname_string (cnam, com_die); /* ??? needed? */
19749           com_die->decl_id = DECL_UID (com_decl);
19750           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19751           *slot = (void *) com_die;
19752         }
19753       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19754         {
19755           add_AT_location_description (com_die, DW_AT_location, loc);
19756           loc = loc_list_from_tree (com_decl, 2);
19757           remove_AT (com_die, DW_AT_declaration);
19758         }
19759       var_die = new_die (DW_TAG_variable, com_die, decl);
19760       add_name_and_src_coords_attributes (var_die, decl);
19761       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19762                           TREE_THIS_VOLATILE (decl), context_die);
19763       add_AT_flag (var_die, DW_AT_external, 1);
19764       if (loc)
19765         {
19766           if (off)
19767             {
19768               /* Optimize the common case.  */
19769               if (single_element_loc_list_p (loc)
19770                   && loc->expr->dw_loc_opc == DW_OP_addr
19771                   && loc->expr->dw_loc_next == NULL
19772                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19773                 loc->expr->dw_loc_oprnd1.v.val_addr
19774                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19775               else
19776                 loc_list_plus_const (loc, off);
19777             }
19778           add_AT_location_description (var_die, DW_AT_location, loc);
19779         }
19780       else if (DECL_EXTERNAL (decl))
19781         add_AT_flag (var_die, DW_AT_declaration, 1);
19782       equate_decl_number_to_die (decl, var_die);
19783       return;
19784     }
19785
19786   /* If the compiler emitted a definition for the DECL declaration
19787      and if we already emitted a DIE for it, don't emit a second
19788      DIE for it again. Allow re-declarations of DECLs that are
19789      inside functions, though.  */
19790   if (old_die && declaration && !local_scope_p (context_die))
19791     return;
19792
19793   /* For static data members, the declaration in the class is supposed
19794      to have DW_TAG_member tag; the specification should still be
19795      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19796   if (declaration && class_scope_p (context_die))
19797     var_die = new_die (DW_TAG_member, context_die, decl);
19798   else
19799     var_die = new_die (DW_TAG_variable, context_die, decl);
19800
19801   origin_die = NULL;
19802   if (origin != NULL)
19803     origin_die = add_abstract_origin_attribute (var_die, origin);
19804
19805   /* Loop unrolling can create multiple blocks that refer to the same
19806      static variable, so we must test for the DW_AT_declaration flag.
19807
19808      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19809      copy decls and set the DECL_ABSTRACT flag on them instead of
19810      sharing them.
19811
19812      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19813
19814      ??? The declare_in_namespace support causes us to get two DIEs for one
19815      variable, both of which are declarations.  We want to avoid considering
19816      one to be a specification, so we must test that this DIE is not a
19817      declaration.  */
19818   else if (old_die && TREE_STATIC (decl) && ! declaration
19819            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19820     {
19821       /* This is a definition of a C++ class level static.  */
19822       add_AT_specification (var_die, old_die);
19823       specialization_p = true;
19824       if (DECL_NAME (decl))
19825         {
19826           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19827           struct dwarf_file_data * file_index = lookup_filename (s.file);
19828
19829           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19830             add_AT_file (var_die, DW_AT_decl_file, file_index);
19831
19832           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19833             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19834
19835           if (old_die->die_tag == DW_TAG_member)
19836             add_linkage_name (var_die, decl);
19837         }
19838     }
19839   else
19840     add_name_and_src_coords_attributes (var_die, decl);
19841
19842   if ((origin == NULL && !specialization_p)
19843       || (origin != NULL
19844           && !DECL_ABSTRACT (decl_or_origin)
19845           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19846                                        decl_function_context
19847                                                         (decl_or_origin))))
19848     {
19849       tree type = TREE_TYPE (decl_or_origin);
19850
19851       if (decl_by_reference_p (decl_or_origin))
19852         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19853       else
19854         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19855                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19856     }
19857
19858   if (origin == NULL && !specialization_p)
19859     {
19860       if (TREE_PUBLIC (decl))
19861         add_AT_flag (var_die, DW_AT_external, 1);
19862
19863       if (DECL_ARTIFICIAL (decl))
19864         add_AT_flag (var_die, DW_AT_artificial, 1);
19865
19866       add_accessibility_attribute (var_die, decl);
19867     }
19868
19869   if (declaration)
19870     add_AT_flag (var_die, DW_AT_declaration, 1);
19871
19872   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19873     equate_decl_number_to_die (decl, var_die);
19874
19875   if (! declaration
19876       && (! DECL_ABSTRACT (decl_or_origin)
19877           /* Local static vars are shared between all clones/inlines,
19878              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19879              already set.  */
19880           || (TREE_CODE (decl_or_origin) == VAR_DECL
19881               && TREE_STATIC (decl_or_origin)
19882               && DECL_RTL_SET_P (decl_or_origin)))
19883       /* When abstract origin already has DW_AT_location attribute, no need
19884          to add it again.  */
19885       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19886     {
19887       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19888           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19889         defer_location (decl_or_origin, var_die);
19890       else
19891         add_location_or_const_value_attribute (var_die, decl_or_origin,
19892                                                decl == NULL, DW_AT_location);
19893       add_pubname (decl_or_origin, var_die);
19894     }
19895   else
19896     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19897 }
19898
19899 /* Generate a DIE to represent a named constant.  */
19900
19901 static void
19902 gen_const_die (tree decl, dw_die_ref context_die)
19903 {
19904   dw_die_ref const_die;
19905   tree type = TREE_TYPE (decl);
19906
19907   const_die = new_die (DW_TAG_constant, context_die, decl);
19908   add_name_and_src_coords_attributes (const_die, decl);
19909   add_type_attribute (const_die, type, 1, 0, context_die);
19910   if (TREE_PUBLIC (decl))
19911     add_AT_flag (const_die, DW_AT_external, 1);
19912   if (DECL_ARTIFICIAL (decl))
19913     add_AT_flag (const_die, DW_AT_artificial, 1);
19914   tree_add_const_value_attribute_for_decl (const_die, decl);
19915 }
19916
19917 /* Generate a DIE to represent a label identifier.  */
19918
19919 static void
19920 gen_label_die (tree decl, dw_die_ref context_die)
19921 {
19922   tree origin = decl_ultimate_origin (decl);
19923   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19924   rtx insn;
19925   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19926
19927   if (origin != NULL)
19928     add_abstract_origin_attribute (lbl_die, origin);
19929   else
19930     add_name_and_src_coords_attributes (lbl_die, decl);
19931
19932   if (DECL_ABSTRACT (decl))
19933     equate_decl_number_to_die (decl, lbl_die);
19934   else
19935     {
19936       insn = DECL_RTL_IF_SET (decl);
19937
19938       /* Deleted labels are programmer specified labels which have been
19939          eliminated because of various optimizations.  We still emit them
19940          here so that it is possible to put breakpoints on them.  */
19941       if (insn
19942           && (LABEL_P (insn)
19943               || ((NOTE_P (insn)
19944                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19945         {
19946           /* When optimization is enabled (via -O) some parts of the compiler
19947              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19948              represent source-level labels which were explicitly declared by
19949              the user.  This really shouldn't be happening though, so catch
19950              it if it ever does happen.  */
19951           gcc_assert (!INSN_DELETED_P (insn));
19952
19953           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19954           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19955         }
19956     }
19957 }
19958
19959 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19960    attributes to the DIE for a block STMT, to describe where the inlined
19961    function was called from.  This is similar to add_src_coords_attributes.  */
19962
19963 static inline void
19964 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19965 {
19966   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19967
19968   if (dwarf_version >= 3 || !dwarf_strict)
19969     {
19970       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19971       add_AT_unsigned (die, DW_AT_call_line, s.line);
19972     }
19973 }
19974
19975
19976 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19977    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19978
19979 static inline void
19980 add_high_low_attributes (tree stmt, dw_die_ref die)
19981 {
19982   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19983
19984   if (BLOCK_FRAGMENT_CHAIN (stmt)
19985       && (dwarf_version >= 3 || !dwarf_strict))
19986     {
19987       tree chain;
19988
19989       if (inlined_function_outer_scope_p (stmt))
19990         {
19991           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19992                                        BLOCK_NUMBER (stmt));
19993           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19994         }
19995
19996       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19997
19998       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19999       do
20000         {
20001           add_ranges (chain);
20002           chain = BLOCK_FRAGMENT_CHAIN (chain);
20003         }
20004       while (chain);
20005       add_ranges (NULL);
20006     }
20007   else
20008     {
20009       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20010                                    BLOCK_NUMBER (stmt));
20011       add_AT_lbl_id (die, DW_AT_low_pc, label);
20012       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
20013                                    BLOCK_NUMBER (stmt));
20014       add_AT_lbl_id (die, DW_AT_high_pc, label);
20015     }
20016 }
20017
20018 /* Generate a DIE for a lexical block.  */
20019
20020 static void
20021 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
20022 {
20023   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
20024
20025   if (call_arg_locations)
20026     {
20027       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20028         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20029                                BLOCK_NUMBER (stmt) + 1);
20030       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
20031     }
20032
20033   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
20034     add_high_low_attributes (stmt, stmt_die);
20035
20036   decls_for_scope (stmt, stmt_die, depth);
20037 }
20038
20039 /* Generate a DIE for an inlined subprogram.  */
20040
20041 static void
20042 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
20043 {
20044   tree decl;
20045
20046   /* The instance of function that is effectively being inlined shall not
20047      be abstract.  */
20048   gcc_assert (! BLOCK_ABSTRACT (stmt));
20049
20050   decl = block_ultimate_origin (stmt);
20051
20052   /* Emit info for the abstract instance first, if we haven't yet.  We
20053      must emit this even if the block is abstract, otherwise when we
20054      emit the block below (or elsewhere), we may end up trying to emit
20055      a die whose origin die hasn't been emitted, and crashing.  */
20056   dwarf2out_abstract_function (decl);
20057
20058   if (! BLOCK_ABSTRACT (stmt))
20059     {
20060       dw_die_ref subr_die
20061         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20062
20063       if (call_arg_locations)
20064         {
20065           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20066             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20067                                    BLOCK_NUMBER (stmt) + 1);
20068           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20069         }
20070       add_abstract_origin_attribute (subr_die, decl);
20071       if (TREE_ASM_WRITTEN (stmt))
20072         add_high_low_attributes (stmt, subr_die);
20073       add_call_src_coords_attributes (stmt, subr_die);
20074
20075       decls_for_scope (stmt, subr_die, depth);
20076       current_function_has_inlines = 1;
20077     }
20078 }
20079
20080 /* Generate a DIE for a field in a record, or structure.  */
20081
20082 static void
20083 gen_field_die (tree decl, dw_die_ref context_die)
20084 {
20085   dw_die_ref decl_die;
20086
20087   if (TREE_TYPE (decl) == error_mark_node)
20088     return;
20089
20090   decl_die = new_die (DW_TAG_member, context_die, decl);
20091   add_name_and_src_coords_attributes (decl_die, decl);
20092   add_type_attribute (decl_die, member_declared_type (decl),
20093                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20094                       context_die);
20095
20096   if (DECL_BIT_FIELD_TYPE (decl))
20097     {
20098       add_byte_size_attribute (decl_die, decl);
20099       add_bit_size_attribute (decl_die, decl);
20100       add_bit_offset_attribute (decl_die, decl);
20101     }
20102
20103   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20104     add_data_member_location_attribute (decl_die, decl);
20105
20106   if (DECL_ARTIFICIAL (decl))
20107     add_AT_flag (decl_die, DW_AT_artificial, 1);
20108
20109   add_accessibility_attribute (decl_die, decl);
20110
20111   /* Equate decl number to die, so that we can look up this decl later on.  */
20112   equate_decl_number_to_die (decl, decl_die);
20113 }
20114
20115 #if 0
20116 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20117    Use modified_type_die instead.
20118    We keep this code here just in case these types of DIEs may be needed to
20119    represent certain things in other languages (e.g. Pascal) someday.  */
20120
20121 static void
20122 gen_pointer_type_die (tree type, dw_die_ref context_die)
20123 {
20124   dw_die_ref ptr_die
20125     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20126
20127   equate_type_number_to_die (type, ptr_die);
20128   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20129   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20130 }
20131
20132 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20133    Use modified_type_die instead.
20134    We keep this code here just in case these types of DIEs may be needed to
20135    represent certain things in other languages (e.g. Pascal) someday.  */
20136
20137 static void
20138 gen_reference_type_die (tree type, dw_die_ref context_die)
20139 {
20140   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20141
20142   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20143     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20144   else
20145     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20146
20147   equate_type_number_to_die (type, ref_die);
20148   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20149   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20150 }
20151 #endif
20152
20153 /* Generate a DIE for a pointer to a member type.  */
20154
20155 static void
20156 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20157 {
20158   dw_die_ref ptr_die
20159     = new_die (DW_TAG_ptr_to_member_type,
20160                scope_die_for (type, context_die), type);
20161
20162   equate_type_number_to_die (type, ptr_die);
20163   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20164                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20165   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20166 }
20167
20168 /* Generate the DIE for the compilation unit.  */
20169
20170 static dw_die_ref
20171 gen_compile_unit_die (const char *filename)
20172 {
20173   dw_die_ref die;
20174   char producer[250];
20175   const char *language_string = lang_hooks.name;
20176   int language;
20177
20178   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20179
20180   if (filename)
20181     {
20182       add_name_attribute (die, filename);
20183       /* Don't add cwd for <built-in>.  */
20184       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20185         add_comp_dir_attribute (die);
20186     }
20187
20188   sprintf (producer, "%s %s", language_string, version_string);
20189
20190 #ifdef MIPS_DEBUGGING_INFO
20191   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20192      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20193      not appear in the producer string, the debugger reaches the conclusion
20194      that the object file is stripped and has no debugging information.
20195      To get the MIPS/SGI debugger to believe that there is debugging
20196      information in the object file, we add a -g to the producer string.  */
20197   if (debug_info_level > DINFO_LEVEL_TERSE)
20198     strcat (producer, " -g");
20199 #endif
20200
20201   add_AT_string (die, DW_AT_producer, producer);
20202
20203   /* If our producer is LTO try to figure out a common language to use
20204      from the global list of translation units.  */
20205   if (strcmp (language_string, "GNU GIMPLE") == 0)
20206     {
20207       unsigned i;
20208       tree t;
20209       const char *common_lang = NULL;
20210
20211       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20212         {
20213           if (!TRANSLATION_UNIT_LANGUAGE (t))
20214             continue;
20215           if (!common_lang)
20216             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20217           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20218             ;
20219           else if (strncmp (common_lang, "GNU C", 5) == 0
20220                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20221             /* Mixing C and C++ is ok, use C++ in that case.  */
20222             common_lang = "GNU C++";
20223           else
20224             {
20225               /* Fall back to C.  */
20226               common_lang = NULL;
20227               break;
20228             }
20229         }
20230
20231       if (common_lang)
20232         language_string = common_lang;
20233     }
20234
20235   language = DW_LANG_C89;
20236   if (strcmp (language_string, "GNU C++") == 0)
20237     language = DW_LANG_C_plus_plus;
20238   else if (strcmp (language_string, "GNU F77") == 0)
20239     language = DW_LANG_Fortran77;
20240   else if (strcmp (language_string, "GNU Pascal") == 0)
20241     language = DW_LANG_Pascal83;
20242   else if (dwarf_version >= 3 || !dwarf_strict)
20243     {
20244       if (strcmp (language_string, "GNU Ada") == 0)
20245         language = DW_LANG_Ada95;
20246       else if (strcmp (language_string, "GNU Fortran") == 0)
20247         language = DW_LANG_Fortran95;
20248       else if (strcmp (language_string, "GNU Java") == 0)
20249         language = DW_LANG_Java;
20250       else if (strcmp (language_string, "GNU Objective-C") == 0)
20251         language = DW_LANG_ObjC;
20252       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20253         language = DW_LANG_ObjC_plus_plus;
20254     }
20255
20256   add_AT_unsigned (die, DW_AT_language, language);
20257
20258   switch (language)
20259     {
20260     case DW_LANG_Fortran77:
20261     case DW_LANG_Fortran90:
20262     case DW_LANG_Fortran95:
20263       /* Fortran has case insensitive identifiers and the front-end
20264          lowercases everything.  */
20265       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20266       break;
20267     default:
20268       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20269       break;
20270     }
20271   return die;
20272 }
20273
20274 /* Generate the DIE for a base class.  */
20275
20276 static void
20277 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20278 {
20279   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20280
20281   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20282   add_data_member_location_attribute (die, binfo);
20283
20284   if (BINFO_VIRTUAL_P (binfo))
20285     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20286
20287   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20288      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20289      the default has always been DW_ACCESS_private.  */
20290   if (access == access_public_node)
20291     {
20292       if (dwarf_version == 2
20293           || context_die->die_tag == DW_TAG_class_type)
20294       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20295     }
20296   else if (access == access_protected_node)
20297     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20298   else if (dwarf_version > 2
20299            && context_die->die_tag != DW_TAG_class_type)
20300     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20301 }
20302
20303 /* Generate a DIE for a class member.  */
20304
20305 static void
20306 gen_member_die (tree type, dw_die_ref context_die)
20307 {
20308   tree member;
20309   tree binfo = TYPE_BINFO (type);
20310   dw_die_ref child;
20311
20312   /* If this is not an incomplete type, output descriptions of each of its
20313      members. Note that as we output the DIEs necessary to represent the
20314      members of this record or union type, we will also be trying to output
20315      DIEs to represent the *types* of those members. However the `type'
20316      function (above) will specifically avoid generating type DIEs for member
20317      types *within* the list of member DIEs for this (containing) type except
20318      for those types (of members) which are explicitly marked as also being
20319      members of this (containing) type themselves.  The g++ front- end can
20320      force any given type to be treated as a member of some other (containing)
20321      type by setting the TYPE_CONTEXT of the given (member) type to point to
20322      the TREE node representing the appropriate (containing) type.  */
20323
20324   /* First output info about the base classes.  */
20325   if (binfo)
20326     {
20327       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20328       int i;
20329       tree base;
20330
20331       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20332         gen_inheritance_die (base,
20333                              (accesses ? VEC_index (tree, accesses, i)
20334                               : access_public_node), context_die);
20335     }
20336
20337   /* Now output info about the data members and type members.  */
20338   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20339     {
20340       /* If we thought we were generating minimal debug info for TYPE
20341          and then changed our minds, some of the member declarations
20342          may have already been defined.  Don't define them again, but
20343          do put them in the right order.  */
20344
20345       child = lookup_decl_die (member);
20346       if (child)
20347         splice_child_die (context_die, child);
20348       else
20349         gen_decl_die (member, NULL, context_die);
20350     }
20351
20352   /* Now output info about the function members (if any).  */
20353   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20354     {
20355       /* Don't include clones in the member list.  */
20356       if (DECL_ABSTRACT_ORIGIN (member))
20357         continue;
20358
20359       child = lookup_decl_die (member);
20360       if (child)
20361         splice_child_die (context_die, child);
20362       else
20363         gen_decl_die (member, NULL, context_die);
20364     }
20365 }
20366
20367 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20368    is set, we pretend that the type was never defined, so we only get the
20369    member DIEs needed by later specification DIEs.  */
20370
20371 static void
20372 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20373                                 enum debug_info_usage usage)
20374 {
20375   dw_die_ref type_die = lookup_type_die (type);
20376   dw_die_ref scope_die = 0;
20377   int nested = 0;
20378   int complete = (TYPE_SIZE (type)
20379                   && (! TYPE_STUB_DECL (type)
20380                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20381   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20382   complete = complete && should_emit_struct_debug (type, usage);
20383
20384   if (type_die && ! complete)
20385     return;
20386
20387   if (TYPE_CONTEXT (type) != NULL_TREE
20388       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20389           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20390     nested = 1;
20391
20392   scope_die = scope_die_for (type, context_die);
20393
20394   if (! type_die || (nested && is_cu_die (scope_die)))
20395     /* First occurrence of type or toplevel definition of nested class.  */
20396     {
20397       dw_die_ref old_die = type_die;
20398
20399       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20400                           ? record_type_tag (type) : DW_TAG_union_type,
20401                           scope_die, type);
20402       equate_type_number_to_die (type, type_die);
20403       if (old_die)
20404         add_AT_specification (type_die, old_die);
20405       else
20406         add_name_attribute (type_die, type_tag (type));
20407     }
20408   else
20409     remove_AT (type_die, DW_AT_declaration);
20410
20411   /* Generate child dies for template paramaters.  */
20412   if (debug_info_level > DINFO_LEVEL_TERSE
20413       && COMPLETE_TYPE_P (type))
20414     schedule_generic_params_dies_gen (type);
20415
20416   /* If this type has been completed, then give it a byte_size attribute and
20417      then give a list of members.  */
20418   if (complete && !ns_decl)
20419     {
20420       /* Prevent infinite recursion in cases where the type of some member of
20421          this type is expressed in terms of this type itself.  */
20422       TREE_ASM_WRITTEN (type) = 1;
20423       add_byte_size_attribute (type_die, type);
20424       if (TYPE_STUB_DECL (type) != NULL_TREE)
20425         {
20426           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20427           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20428         }
20429
20430       /* If the first reference to this type was as the return type of an
20431          inline function, then it may not have a parent.  Fix this now.  */
20432       if (type_die->die_parent == NULL)
20433         add_child_die (scope_die, type_die);
20434
20435       push_decl_scope (type);
20436       gen_member_die (type, type_die);
20437       pop_decl_scope ();
20438
20439       /* GNU extension: Record what type our vtable lives in.  */
20440       if (TYPE_VFIELD (type))
20441         {
20442           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20443
20444           gen_type_die (vtype, context_die);
20445           add_AT_die_ref (type_die, DW_AT_containing_type,
20446                           lookup_type_die (vtype));
20447         }
20448     }
20449   else
20450     {
20451       add_AT_flag (type_die, DW_AT_declaration, 1);
20452
20453       /* We don't need to do this for function-local types.  */
20454       if (TYPE_STUB_DECL (type)
20455           && ! decl_function_context (TYPE_STUB_DECL (type)))
20456         VEC_safe_push (tree, gc, incomplete_types, type);
20457     }
20458
20459   if (get_AT (type_die, DW_AT_name))
20460     add_pubtype (type, type_die);
20461 }
20462
20463 /* Generate a DIE for a subroutine _type_.  */
20464
20465 static void
20466 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20467 {
20468   tree return_type = TREE_TYPE (type);
20469   dw_die_ref subr_die
20470     = new_die (DW_TAG_subroutine_type,
20471                scope_die_for (type, context_die), type);
20472
20473   equate_type_number_to_die (type, subr_die);
20474   add_prototyped_attribute (subr_die, type);
20475   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20476   gen_formal_types_die (type, subr_die);
20477
20478   if (get_AT (subr_die, DW_AT_name))
20479     add_pubtype (type, subr_die);
20480 }
20481
20482 /* Generate a DIE for a type definition.  */
20483
20484 static void
20485 gen_typedef_die (tree decl, dw_die_ref context_die)
20486 {
20487   dw_die_ref type_die;
20488   tree origin;
20489
20490   if (TREE_ASM_WRITTEN (decl))
20491     return;
20492
20493   TREE_ASM_WRITTEN (decl) = 1;
20494   type_die = new_die (DW_TAG_typedef, context_die, decl);
20495   origin = decl_ultimate_origin (decl);
20496   if (origin != NULL)
20497     add_abstract_origin_attribute (type_die, origin);
20498   else
20499     {
20500       tree type;
20501
20502       add_name_and_src_coords_attributes (type_die, decl);
20503       if (DECL_ORIGINAL_TYPE (decl))
20504         {
20505           type = DECL_ORIGINAL_TYPE (decl);
20506
20507           gcc_assert (type != TREE_TYPE (decl));
20508           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20509         }
20510       else
20511         {
20512           type = TREE_TYPE (decl);
20513
20514           if (is_naming_typedef_decl (TYPE_NAME (type)))
20515             {
20516               /* Here, we are in the case of decl being a typedef naming
20517                  an anonymous type, e.g:
20518                      typedef struct {...} foo;
20519                  In that case TREE_TYPE (decl) is not a typedef variant
20520                  type and TYPE_NAME of the anonymous type is set to the
20521                  TYPE_DECL of the typedef. This construct is emitted by
20522                  the C++ FE.
20523
20524                  TYPE is the anonymous struct named by the typedef
20525                  DECL. As we need the DW_AT_type attribute of the
20526                  DW_TAG_typedef to point to the DIE of TYPE, let's
20527                  generate that DIE right away. add_type_attribute
20528                  called below will then pick (via lookup_type_die) that
20529                  anonymous struct DIE.  */
20530               if (!TREE_ASM_WRITTEN (type))
20531                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20532
20533               /* This is a GNU Extension.  We are adding a
20534                  DW_AT_linkage_name attribute to the DIE of the
20535                  anonymous struct TYPE.  The value of that attribute
20536                  is the name of the typedef decl naming the anonymous
20537                  struct.  This greatly eases the work of consumers of
20538                  this debug info.  */
20539               add_linkage_attr (lookup_type_die (type), decl);
20540             }
20541         }
20542
20543       add_type_attribute (type_die, type, TREE_READONLY (decl),
20544                           TREE_THIS_VOLATILE (decl), context_die);
20545
20546       if (is_naming_typedef_decl (decl))
20547         /* We want that all subsequent calls to lookup_type_die with
20548            TYPE in argument yield the DW_TAG_typedef we have just
20549            created.  */
20550         equate_type_number_to_die (type, type_die);
20551
20552       add_accessibility_attribute (type_die, decl);
20553     }
20554
20555   if (DECL_ABSTRACT (decl))
20556     equate_decl_number_to_die (decl, type_die);
20557
20558   if (get_AT (type_die, DW_AT_name))
20559     add_pubtype (decl, type_die);
20560 }
20561
20562 /* Generate a DIE for a struct, class, enum or union type.  */
20563
20564 static void
20565 gen_tagged_type_die (tree type,
20566                      dw_die_ref context_die,
20567                      enum debug_info_usage usage)
20568 {
20569   int need_pop;
20570
20571   if (type == NULL_TREE
20572       || !is_tagged_type (type))
20573     return;
20574
20575   /* If this is a nested type whose containing class hasn't been written
20576      out yet, writing it out will cover this one, too.  This does not apply
20577      to instantiations of member class templates; they need to be added to
20578      the containing class as they are generated.  FIXME: This hurts the
20579      idea of combining type decls from multiple TUs, since we can't predict
20580      what set of template instantiations we'll get.  */
20581   if (TYPE_CONTEXT (type)
20582       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20583       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20584     {
20585       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20586
20587       if (TREE_ASM_WRITTEN (type))
20588         return;
20589
20590       /* If that failed, attach ourselves to the stub.  */
20591       push_decl_scope (TYPE_CONTEXT (type));
20592       context_die = lookup_type_die (TYPE_CONTEXT (type));
20593       need_pop = 1;
20594     }
20595   else if (TYPE_CONTEXT (type) != NULL_TREE
20596            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20597     {
20598       /* If this type is local to a function that hasn't been written
20599          out yet, use a NULL context for now; it will be fixed up in
20600          decls_for_scope.  */
20601       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20602       /* A declaration DIE doesn't count; nested types need to go in the
20603          specification.  */
20604       if (context_die && is_declaration_die (context_die))
20605         context_die = NULL;
20606       need_pop = 0;
20607     }
20608   else
20609     {
20610       context_die = declare_in_namespace (type, context_die);
20611       need_pop = 0;
20612     }
20613
20614   if (TREE_CODE (type) == ENUMERAL_TYPE)
20615     {
20616       /* This might have been written out by the call to
20617          declare_in_namespace.  */
20618       if (!TREE_ASM_WRITTEN (type))
20619         gen_enumeration_type_die (type, context_die);
20620     }
20621   else
20622     gen_struct_or_union_type_die (type, context_die, usage);
20623
20624   if (need_pop)
20625     pop_decl_scope ();
20626
20627   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20628      it up if it is ever completed.  gen_*_type_die will set it for us
20629      when appropriate.  */
20630 }
20631
20632 /* Generate a type description DIE.  */
20633
20634 static void
20635 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20636                          enum debug_info_usage usage)
20637 {
20638   struct array_descr_info info;
20639
20640   if (type == NULL_TREE || type == error_mark_node)
20641     return;
20642
20643   if (TYPE_NAME (type) != NULL_TREE
20644       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20645       && is_redundant_typedef (TYPE_NAME (type))
20646       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20647     /* The DECL of this type is a typedef we don't want to emit debug
20648        info for but we want debug info for its underlying typedef.
20649        This can happen for e.g, the injected-class-name of a C++
20650        type.  */
20651     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20652
20653   /* If TYPE is a typedef type variant, let's generate debug info
20654      for the parent typedef which TYPE is a type of.  */
20655   if (typedef_variant_p (type))
20656     {
20657       if (TREE_ASM_WRITTEN (type))
20658         return;
20659
20660       /* Prevent broken recursion; we can't hand off to the same type.  */
20661       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20662
20663       /* Use the DIE of the containing namespace as the parent DIE of
20664          the type description DIE we want to generate.  */
20665       if (DECL_CONTEXT (TYPE_NAME (type))
20666           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20667         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20668
20669       TREE_ASM_WRITTEN (type) = 1;
20670
20671       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20672       return;
20673     }
20674
20675   /* If type is an anonymous tagged type named by a typedef, let's
20676      generate debug info for the typedef.  */
20677   if (is_naming_typedef_decl (TYPE_NAME (type)))
20678     {
20679       /* Use the DIE of the containing namespace as the parent DIE of
20680          the type description DIE we want to generate.  */
20681       if (DECL_CONTEXT (TYPE_NAME (type))
20682           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20683         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20684       
20685       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20686       return;
20687     }
20688
20689   /* If this is an array type with hidden descriptor, handle it first.  */
20690   if (!TREE_ASM_WRITTEN (type)
20691       && lang_hooks.types.get_array_descr_info
20692       && lang_hooks.types.get_array_descr_info (type, &info)
20693       && (dwarf_version >= 3 || !dwarf_strict))
20694     {
20695       gen_descr_array_type_die (type, &info, context_die);
20696       TREE_ASM_WRITTEN (type) = 1;
20697       return;
20698     }
20699
20700   /* We are going to output a DIE to represent the unqualified version
20701      of this type (i.e. without any const or volatile qualifiers) so
20702      get the main variant (i.e. the unqualified version) of this type
20703      now.  (Vectors are special because the debugging info is in the
20704      cloned type itself).  */
20705   if (TREE_CODE (type) != VECTOR_TYPE)
20706     type = type_main_variant (type);
20707
20708   if (TREE_ASM_WRITTEN (type))
20709     return;
20710
20711   switch (TREE_CODE (type))
20712     {
20713     case ERROR_MARK:
20714       break;
20715
20716     case POINTER_TYPE:
20717     case REFERENCE_TYPE:
20718       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20719          ensures that the gen_type_die recursion will terminate even if the
20720          type is recursive.  Recursive types are possible in Ada.  */
20721       /* ??? We could perhaps do this for all types before the switch
20722          statement.  */
20723       TREE_ASM_WRITTEN (type) = 1;
20724
20725       /* For these types, all that is required is that we output a DIE (or a
20726          set of DIEs) to represent the "basis" type.  */
20727       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20728                                 DINFO_USAGE_IND_USE);
20729       break;
20730
20731     case OFFSET_TYPE:
20732       /* This code is used for C++ pointer-to-data-member types.
20733          Output a description of the relevant class type.  */
20734       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20735                                         DINFO_USAGE_IND_USE);
20736
20737       /* Output a description of the type of the object pointed to.  */
20738       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20739                                         DINFO_USAGE_IND_USE);
20740
20741       /* Now output a DIE to represent this pointer-to-data-member type
20742          itself.  */
20743       gen_ptr_to_mbr_type_die (type, context_die);
20744       break;
20745
20746     case FUNCTION_TYPE:
20747       /* Force out return type (in case it wasn't forced out already).  */
20748       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20749                                         DINFO_USAGE_DIR_USE);
20750       gen_subroutine_type_die (type, context_die);
20751       break;
20752
20753     case METHOD_TYPE:
20754       /* Force out return type (in case it wasn't forced out already).  */
20755       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20756                                         DINFO_USAGE_DIR_USE);
20757       gen_subroutine_type_die (type, context_die);
20758       break;
20759
20760     case ARRAY_TYPE:
20761       gen_array_type_die (type, context_die);
20762       break;
20763
20764     case VECTOR_TYPE:
20765       gen_array_type_die (type, context_die);
20766       break;
20767
20768     case ENUMERAL_TYPE:
20769     case RECORD_TYPE:
20770     case UNION_TYPE:
20771     case QUAL_UNION_TYPE:
20772       gen_tagged_type_die (type, context_die, usage);
20773       return;
20774
20775     case VOID_TYPE:
20776     case INTEGER_TYPE:
20777     case REAL_TYPE:
20778     case FIXED_POINT_TYPE:
20779     case COMPLEX_TYPE:
20780     case BOOLEAN_TYPE:
20781       /* No DIEs needed for fundamental types.  */
20782       break;
20783
20784     case NULLPTR_TYPE:
20785     case LANG_TYPE:
20786       /* Just use DW_TAG_unspecified_type.  */
20787       {
20788         dw_die_ref type_die = lookup_type_die (type);
20789         if (type_die == NULL)
20790           {
20791             tree name = TYPE_NAME (type);
20792             if (TREE_CODE (name) == TYPE_DECL)
20793               name = DECL_NAME (name);
20794             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20795             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20796             equate_type_number_to_die (type, type_die);
20797           }
20798       }
20799       break;
20800
20801     default:
20802       gcc_unreachable ();
20803     }
20804
20805   TREE_ASM_WRITTEN (type) = 1;
20806 }
20807
20808 static void
20809 gen_type_die (tree type, dw_die_ref context_die)
20810 {
20811   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20812 }
20813
20814 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20815    things which are local to the given block.  */
20816
20817 static void
20818 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20819 {
20820   int must_output_die = 0;
20821   bool inlined_func;
20822
20823   /* Ignore blocks that are NULL.  */
20824   if (stmt == NULL_TREE)
20825     return;
20826
20827   inlined_func = inlined_function_outer_scope_p (stmt);
20828
20829   /* If the block is one fragment of a non-contiguous block, do not
20830      process the variables, since they will have been done by the
20831      origin block.  Do process subblocks.  */
20832   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20833     {
20834       tree sub;
20835
20836       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20837         gen_block_die (sub, context_die, depth + 1);
20838
20839       return;
20840     }
20841
20842   /* Determine if we need to output any Dwarf DIEs at all to represent this
20843      block.  */
20844   if (inlined_func)
20845     /* The outer scopes for inlinings *must* always be represented.  We
20846        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20847     must_output_die = 1;
20848   else
20849     {
20850       /* Determine if this block directly contains any "significant"
20851          local declarations which we will need to output DIEs for.  */
20852       if (debug_info_level > DINFO_LEVEL_TERSE)
20853         /* We are not in terse mode so *any* local declaration counts
20854            as being a "significant" one.  */
20855         must_output_die = ((BLOCK_VARS (stmt) != NULL
20856                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20857                            && (TREE_USED (stmt)
20858                                || TREE_ASM_WRITTEN (stmt)
20859                                || BLOCK_ABSTRACT (stmt)));
20860       else if ((TREE_USED (stmt)
20861                 || TREE_ASM_WRITTEN (stmt)
20862                 || BLOCK_ABSTRACT (stmt))
20863                && !dwarf2out_ignore_block (stmt))
20864         must_output_die = 1;
20865     }
20866
20867   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20868      DIE for any block which contains no significant local declarations at
20869      all.  Rather, in such cases we just call `decls_for_scope' so that any
20870      needed Dwarf info for any sub-blocks will get properly generated. Note
20871      that in terse mode, our definition of what constitutes a "significant"
20872      local declaration gets restricted to include only inlined function
20873      instances and local (nested) function definitions.  */
20874   if (must_output_die)
20875     {
20876       if (inlined_func)
20877         {
20878           /* If STMT block is abstract, that means we have been called
20879              indirectly from dwarf2out_abstract_function.
20880              That function rightfully marks the descendent blocks (of
20881              the abstract function it is dealing with) as being abstract,
20882              precisely to prevent us from emitting any
20883              DW_TAG_inlined_subroutine DIE as a descendent
20884              of an abstract function instance. So in that case, we should
20885              not call gen_inlined_subroutine_die.
20886
20887              Later though, when cgraph asks dwarf2out to emit info
20888              for the concrete instance of the function decl into which
20889              the concrete instance of STMT got inlined, the later will lead
20890              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20891           if (! BLOCK_ABSTRACT (stmt))
20892             gen_inlined_subroutine_die (stmt, context_die, depth);
20893         }
20894       else
20895         gen_lexical_block_die (stmt, context_die, depth);
20896     }
20897   else
20898     decls_for_scope (stmt, context_die, depth);
20899 }
20900
20901 /* Process variable DECL (or variable with origin ORIGIN) within
20902    block STMT and add it to CONTEXT_DIE.  */
20903 static void
20904 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20905 {
20906   dw_die_ref die;
20907   tree decl_or_origin = decl ? decl : origin;
20908
20909   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20910     die = lookup_decl_die (decl_or_origin);
20911   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20912            && TYPE_DECL_IS_STUB (decl_or_origin))
20913     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20914   else
20915     die = NULL;
20916
20917   if (die != NULL && die->die_parent == NULL)
20918     add_child_die (context_die, die);
20919   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20920     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20921                                          stmt, context_die);
20922   else
20923     gen_decl_die (decl, origin, context_die);
20924 }
20925
20926 /* Generate all of the decls declared within a given scope and (recursively)
20927    all of its sub-blocks.  */
20928
20929 static void
20930 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20931 {
20932   tree decl;
20933   unsigned int i;
20934   tree subblocks;
20935
20936   /* Ignore NULL blocks.  */
20937   if (stmt == NULL_TREE)
20938     return;
20939
20940   /* Output the DIEs to represent all of the data objects and typedefs
20941      declared directly within this block but not within any nested
20942      sub-blocks.  Also, nested function and tag DIEs have been
20943      generated with a parent of NULL; fix that up now.  */
20944   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20945     process_scope_var (stmt, decl, NULL_TREE, context_die);
20946   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20947     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20948                        context_die);
20949
20950   /* If we're at -g1, we're not interested in subblocks.  */
20951   if (debug_info_level <= DINFO_LEVEL_TERSE)
20952     return;
20953
20954   /* Output the DIEs to represent all sub-blocks (and the items declared
20955      therein) of this block.  */
20956   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20957        subblocks != NULL;
20958        subblocks = BLOCK_CHAIN (subblocks))
20959     gen_block_die (subblocks, context_die, depth + 1);
20960 }
20961
20962 /* Is this a typedef we can avoid emitting?  */
20963
20964 static inline int
20965 is_redundant_typedef (const_tree decl)
20966 {
20967   if (TYPE_DECL_IS_STUB (decl))
20968     return 1;
20969
20970   if (DECL_ARTIFICIAL (decl)
20971       && DECL_CONTEXT (decl)
20972       && is_tagged_type (DECL_CONTEXT (decl))
20973       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20974       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20975     /* Also ignore the artificial member typedef for the class name.  */
20976     return 1;
20977
20978   return 0;
20979 }
20980
20981 /* Return TRUE if TYPE is a typedef that names a type for linkage
20982    purposes. This kind of typedefs is produced by the C++ FE for
20983    constructs like:
20984
20985    typedef struct {...} foo;
20986
20987    In that case, there is no typedef variant type produced for foo.
20988    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20989    struct type.  */
20990
20991 static bool
20992 is_naming_typedef_decl (const_tree decl)
20993 {
20994   if (decl == NULL_TREE
20995       || TREE_CODE (decl) != TYPE_DECL
20996       || !is_tagged_type (TREE_TYPE (decl))
20997       || DECL_IS_BUILTIN (decl)
20998       || is_redundant_typedef (decl)
20999       /* It looks like Ada produces TYPE_DECLs that are very similar
21000          to C++ naming typedefs but that have different
21001          semantics. Let's be specific to c++ for now.  */
21002       || !is_cxx ())
21003     return FALSE;
21004
21005   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21006           && TYPE_NAME (TREE_TYPE (decl)) == decl
21007           && (TYPE_STUB_DECL (TREE_TYPE (decl))
21008               != TYPE_NAME (TREE_TYPE (decl))));
21009 }
21010
21011 /* Returns the DIE for a context.  */
21012
21013 static inline dw_die_ref
21014 get_context_die (tree context)
21015 {
21016   if (context)
21017     {
21018       /* Find die that represents this context.  */
21019       if (TYPE_P (context))
21020         {
21021           context = TYPE_MAIN_VARIANT (context);
21022           return strip_naming_typedef (context, force_type_die (context));
21023         }
21024       else
21025         return force_decl_die (context);
21026     }
21027   return comp_unit_die ();
21028 }
21029
21030 /* Returns the DIE for decl.  A DIE will always be returned.  */
21031
21032 static dw_die_ref
21033 force_decl_die (tree decl)
21034 {
21035   dw_die_ref decl_die;
21036   unsigned saved_external_flag;
21037   tree save_fn = NULL_TREE;
21038   decl_die = lookup_decl_die (decl);
21039   if (!decl_die)
21040     {
21041       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21042
21043       decl_die = lookup_decl_die (decl);
21044       if (decl_die)
21045         return decl_die;
21046
21047       switch (TREE_CODE (decl))
21048         {
21049         case FUNCTION_DECL:
21050           /* Clear current_function_decl, so that gen_subprogram_die thinks
21051              that this is a declaration. At this point, we just want to force
21052              declaration die.  */
21053           save_fn = current_function_decl;
21054           current_function_decl = NULL_TREE;
21055           gen_subprogram_die (decl, context_die);
21056           current_function_decl = save_fn;
21057           break;
21058
21059         case VAR_DECL:
21060           /* Set external flag to force declaration die. Restore it after
21061            gen_decl_die() call.  */
21062           saved_external_flag = DECL_EXTERNAL (decl);
21063           DECL_EXTERNAL (decl) = 1;
21064           gen_decl_die (decl, NULL, context_die);
21065           DECL_EXTERNAL (decl) = saved_external_flag;
21066           break;
21067
21068         case NAMESPACE_DECL:
21069           if (dwarf_version >= 3 || !dwarf_strict)
21070             dwarf2out_decl (decl);
21071           else
21072             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21073             decl_die = comp_unit_die ();
21074           break;
21075
21076         case TRANSLATION_UNIT_DECL:
21077           decl_die = comp_unit_die ();
21078           break;
21079
21080         default:
21081           gcc_unreachable ();
21082         }
21083
21084       /* We should be able to find the DIE now.  */
21085       if (!decl_die)
21086         decl_die = lookup_decl_die (decl);
21087       gcc_assert (decl_die);
21088     }
21089
21090   return decl_die;
21091 }
21092
21093 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21094    always returned.  */
21095
21096 static dw_die_ref
21097 force_type_die (tree type)
21098 {
21099   dw_die_ref type_die;
21100
21101   type_die = lookup_type_die (type);
21102   if (!type_die)
21103     {
21104       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21105
21106       type_die = modified_type_die (type, TYPE_READONLY (type),
21107                                     TYPE_VOLATILE (type), context_die);
21108       gcc_assert (type_die);
21109     }
21110   return type_die;
21111 }
21112
21113 /* Force out any required namespaces to be able to output DECL,
21114    and return the new context_die for it, if it's changed.  */
21115
21116 static dw_die_ref
21117 setup_namespace_context (tree thing, dw_die_ref context_die)
21118 {
21119   tree context = (DECL_P (thing)
21120                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21121   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21122     /* Force out the namespace.  */
21123     context_die = force_decl_die (context);
21124
21125   return context_die;
21126 }
21127
21128 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21129    type) within its namespace, if appropriate.
21130
21131    For compatibility with older debuggers, namespace DIEs only contain
21132    declarations; all definitions are emitted at CU scope.  */
21133
21134 static dw_die_ref
21135 declare_in_namespace (tree thing, dw_die_ref context_die)
21136 {
21137   dw_die_ref ns_context;
21138
21139   if (debug_info_level <= DINFO_LEVEL_TERSE)
21140     return context_die;
21141
21142   /* If this decl is from an inlined function, then don't try to emit it in its
21143      namespace, as we will get confused.  It would have already been emitted
21144      when the abstract instance of the inline function was emitted anyways.  */
21145   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21146     return context_die;
21147
21148   ns_context = setup_namespace_context (thing, context_die);
21149
21150   if (ns_context != context_die)
21151     {
21152       if (is_fortran ())
21153         return ns_context;
21154       if (DECL_P (thing))
21155         gen_decl_die (thing, NULL, ns_context);
21156       else
21157         gen_type_die (thing, ns_context);
21158     }
21159   return context_die;
21160 }
21161
21162 /* Generate a DIE for a namespace or namespace alias.  */
21163
21164 static void
21165 gen_namespace_die (tree decl, dw_die_ref context_die)
21166 {
21167   dw_die_ref namespace_die;
21168
21169   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21170      they are an alias of.  */
21171   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21172     {
21173       /* Output a real namespace or module.  */
21174       context_die = setup_namespace_context (decl, comp_unit_die ());
21175       namespace_die = new_die (is_fortran ()
21176                                ? DW_TAG_module : DW_TAG_namespace,
21177                                context_die, decl);
21178       /* For Fortran modules defined in different CU don't add src coords.  */
21179       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21180         {
21181           const char *name = dwarf2_name (decl, 0);
21182           if (name)
21183             add_name_attribute (namespace_die, name);
21184         }
21185       else
21186         add_name_and_src_coords_attributes (namespace_die, decl);
21187       if (DECL_EXTERNAL (decl))
21188         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21189       equate_decl_number_to_die (decl, namespace_die);
21190     }
21191   else
21192     {
21193       /* Output a namespace alias.  */
21194
21195       /* Force out the namespace we are an alias of, if necessary.  */
21196       dw_die_ref origin_die
21197         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21198
21199       if (DECL_FILE_SCOPE_P (decl)
21200           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21201         context_die = setup_namespace_context (decl, comp_unit_die ());
21202       /* Now create the namespace alias DIE.  */
21203       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21204       add_name_and_src_coords_attributes (namespace_die, decl);
21205       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21206       equate_decl_number_to_die (decl, namespace_die);
21207     }
21208 }
21209
21210 /* Generate Dwarf debug information for a decl described by DECL.
21211    The return value is currently only meaningful for PARM_DECLs,
21212    for all other decls it returns NULL.  */
21213
21214 static dw_die_ref
21215 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21216 {
21217   tree decl_or_origin = decl ? decl : origin;
21218   tree class_origin = NULL, ultimate_origin;
21219
21220   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21221     return NULL;
21222
21223   switch (TREE_CODE (decl_or_origin))
21224     {
21225     case ERROR_MARK:
21226       break;
21227
21228     case CONST_DECL:
21229       if (!is_fortran () && !is_ada ())
21230         {
21231           /* The individual enumerators of an enum type get output when we output
21232              the Dwarf representation of the relevant enum type itself.  */
21233           break;
21234         }
21235
21236       /* Emit its type.  */
21237       gen_type_die (TREE_TYPE (decl), context_die);
21238
21239       /* And its containing namespace.  */
21240       context_die = declare_in_namespace (decl, context_die);
21241
21242       gen_const_die (decl, context_die);
21243       break;
21244
21245     case FUNCTION_DECL:
21246       /* Don't output any DIEs to represent mere function declarations,
21247          unless they are class members or explicit block externs.  */
21248       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21249           && DECL_FILE_SCOPE_P (decl_or_origin)
21250           && (current_function_decl == NULL_TREE
21251               || DECL_ARTIFICIAL (decl_or_origin)))
21252         break;
21253
21254 #if 0
21255       /* FIXME */
21256       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21257          on local redeclarations of global functions.  That seems broken.  */
21258       if (current_function_decl != decl)
21259         /* This is only a declaration.  */;
21260 #endif
21261
21262       /* If we're emitting a clone, emit info for the abstract instance.  */
21263       if (origin || DECL_ORIGIN (decl) != decl)
21264         dwarf2out_abstract_function (origin
21265                                      ? DECL_ORIGIN (origin)
21266                                      : DECL_ABSTRACT_ORIGIN (decl));
21267
21268       /* If we're emitting an out-of-line copy of an inline function,
21269          emit info for the abstract instance and set up to refer to it.  */
21270       else if (cgraph_function_possibly_inlined_p (decl)
21271                && ! DECL_ABSTRACT (decl)
21272                && ! class_or_namespace_scope_p (context_die)
21273                /* dwarf2out_abstract_function won't emit a die if this is just
21274                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21275                   that case, because that works only if we have a die.  */
21276                && DECL_INITIAL (decl) != NULL_TREE)
21277         {
21278           dwarf2out_abstract_function (decl);
21279           set_decl_origin_self (decl);
21280         }
21281
21282       /* Otherwise we're emitting the primary DIE for this decl.  */
21283       else if (debug_info_level > DINFO_LEVEL_TERSE)
21284         {
21285           /* Before we describe the FUNCTION_DECL itself, make sure that we
21286              have its containing type.  */
21287           if (!origin)
21288             origin = decl_class_context (decl);
21289           if (origin != NULL_TREE)
21290             gen_type_die (origin, context_die);
21291
21292           /* And its return type.  */
21293           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21294
21295           /* And its virtual context.  */
21296           if (DECL_VINDEX (decl) != NULL_TREE)
21297             gen_type_die (DECL_CONTEXT (decl), context_die);
21298
21299           /* Make sure we have a member DIE for decl.  */
21300           if (origin != NULL_TREE)
21301             gen_type_die_for_member (origin, decl, context_die);
21302
21303           /* And its containing namespace.  */
21304           context_die = declare_in_namespace (decl, context_die);
21305         }
21306
21307       /* Now output a DIE to represent the function itself.  */
21308       if (decl)
21309         gen_subprogram_die (decl, context_die);
21310       break;
21311
21312     case TYPE_DECL:
21313       /* If we are in terse mode, don't generate any DIEs to represent any
21314          actual typedefs.  */
21315       if (debug_info_level <= DINFO_LEVEL_TERSE)
21316         break;
21317
21318       /* In the special case of a TYPE_DECL node representing the declaration
21319          of some type tag, if the given TYPE_DECL is marked as having been
21320          instantiated from some other (original) TYPE_DECL node (e.g. one which
21321          was generated within the original definition of an inline function) we
21322          used to generate a special (abbreviated) DW_TAG_structure_type,
21323          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21324          should be actually referencing those DIEs, as variable DIEs with that
21325          type would be emitted already in the abstract origin, so it was always
21326          removed during unused type prunning.  Don't add anything in this
21327          case.  */
21328       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21329         break;
21330
21331       if (is_redundant_typedef (decl))
21332         gen_type_die (TREE_TYPE (decl), context_die);
21333       else
21334         /* Output a DIE to represent the typedef itself.  */
21335         gen_typedef_die (decl, context_die);
21336       break;
21337
21338     case LABEL_DECL:
21339       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21340         gen_label_die (decl, context_die);
21341       break;
21342
21343     case VAR_DECL:
21344     case RESULT_DECL:
21345       /* If we are in terse mode, don't generate any DIEs to represent any
21346          variable declarations or definitions.  */
21347       if (debug_info_level <= DINFO_LEVEL_TERSE)
21348         break;
21349
21350       /* Output any DIEs that are needed to specify the type of this data
21351          object.  */
21352       if (decl_by_reference_p (decl_or_origin))
21353         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21354       else
21355         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21356
21357       /* And its containing type.  */
21358       class_origin = decl_class_context (decl_or_origin);
21359       if (class_origin != NULL_TREE)
21360         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21361
21362       /* And its containing namespace.  */
21363       context_die = declare_in_namespace (decl_or_origin, context_die);
21364
21365       /* Now output the DIE to represent the data object itself.  This gets
21366          complicated because of the possibility that the VAR_DECL really
21367          represents an inlined instance of a formal parameter for an inline
21368          function.  */
21369       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21370       if (ultimate_origin != NULL_TREE
21371           && TREE_CODE (ultimate_origin) == PARM_DECL)
21372         gen_formal_parameter_die (decl, origin,
21373                                   true /* Emit name attribute.  */,
21374                                   context_die);
21375       else
21376         gen_variable_die (decl, origin, context_die);
21377       break;
21378
21379     case FIELD_DECL:
21380       /* Ignore the nameless fields that are used to skip bits but handle C++
21381          anonymous unions and structs.  */
21382       if (DECL_NAME (decl) != NULL_TREE
21383           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21384           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21385         {
21386           gen_type_die (member_declared_type (decl), context_die);
21387           gen_field_die (decl, context_die);
21388         }
21389       break;
21390
21391     case PARM_DECL:
21392       if (DECL_BY_REFERENCE (decl_or_origin))
21393         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21394       else
21395         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21396       return gen_formal_parameter_die (decl, origin,
21397                                        true /* Emit name attribute.  */,
21398                                        context_die);
21399
21400     case NAMESPACE_DECL:
21401     case IMPORTED_DECL:
21402       if (dwarf_version >= 3 || !dwarf_strict)
21403         gen_namespace_die (decl, context_die);
21404       break;
21405
21406     default:
21407       /* Probably some frontend-internal decl.  Assume we don't care.  */
21408       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21409       break;
21410     }
21411
21412   return NULL;
21413 }
21414 \f
21415 /* Output debug information for global decl DECL.  Called from toplev.c after
21416    compilation proper has finished.  */
21417
21418 static void
21419 dwarf2out_global_decl (tree decl)
21420 {
21421   /* Output DWARF2 information for file-scope tentative data object
21422      declarations, file-scope (extern) function declarations (which
21423      had no corresponding body) and file-scope tagged type declarations
21424      and definitions which have not yet been forced out.  */
21425   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21426     dwarf2out_decl (decl);
21427 }
21428
21429 /* Output debug information for type decl DECL.  Called from toplev.c
21430    and from language front ends (to record built-in types).  */
21431 static void
21432 dwarf2out_type_decl (tree decl, int local)
21433 {
21434   if (!local)
21435     dwarf2out_decl (decl);
21436 }
21437
21438 /* Output debug information for imported module or decl DECL.
21439    NAME is non-NULL name in the lexical block if the decl has been renamed.
21440    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21441    that DECL belongs to.
21442    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21443 static void
21444 dwarf2out_imported_module_or_decl_1 (tree decl,
21445                                      tree name,
21446                                      tree lexical_block,
21447                                      dw_die_ref lexical_block_die)
21448 {
21449   expanded_location xloc;
21450   dw_die_ref imported_die = NULL;
21451   dw_die_ref at_import_die;
21452
21453   if (TREE_CODE (decl) == IMPORTED_DECL)
21454     {
21455       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21456       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21457       gcc_assert (decl);
21458     }
21459   else
21460     xloc = expand_location (input_location);
21461
21462   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21463     {
21464       at_import_die = force_type_die (TREE_TYPE (decl));
21465       /* For namespace N { typedef void T; } using N::T; base_type_die
21466          returns NULL, but DW_TAG_imported_declaration requires
21467          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21468       if (!at_import_die)
21469         {
21470           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21471           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21472           at_import_die = lookup_type_die (TREE_TYPE (decl));
21473           gcc_assert (at_import_die);
21474         }
21475     }
21476   else
21477     {
21478       at_import_die = lookup_decl_die (decl);
21479       if (!at_import_die)
21480         {
21481           /* If we're trying to avoid duplicate debug info, we may not have
21482              emitted the member decl for this field.  Emit it now.  */
21483           if (TREE_CODE (decl) == FIELD_DECL)
21484             {
21485               tree type = DECL_CONTEXT (decl);
21486
21487               if (TYPE_CONTEXT (type)
21488                   && TYPE_P (TYPE_CONTEXT (type))
21489                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21490                                                 DINFO_USAGE_DIR_USE))
21491                 return;
21492               gen_type_die_for_member (type, decl,
21493                                        get_context_die (TYPE_CONTEXT (type)));
21494             }
21495           at_import_die = force_decl_die (decl);
21496         }
21497     }
21498
21499   if (TREE_CODE (decl) == NAMESPACE_DECL)
21500     {
21501       if (dwarf_version >= 3 || !dwarf_strict)
21502         imported_die = new_die (DW_TAG_imported_module,
21503                                 lexical_block_die,
21504                                 lexical_block);
21505       else
21506         return;
21507     }
21508   else
21509     imported_die = new_die (DW_TAG_imported_declaration,
21510                             lexical_block_die,
21511                             lexical_block);
21512
21513   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21514   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21515   if (name)
21516     add_AT_string (imported_die, DW_AT_name,
21517                    IDENTIFIER_POINTER (name));
21518   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21519 }
21520
21521 /* Output debug information for imported module or decl DECL.
21522    NAME is non-NULL name in context if the decl has been renamed.
21523    CHILD is true if decl is one of the renamed decls as part of
21524    importing whole module.  */
21525
21526 static void
21527 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21528                                    bool child)
21529 {
21530   /* dw_die_ref at_import_die;  */
21531   dw_die_ref scope_die;
21532
21533   if (debug_info_level <= DINFO_LEVEL_TERSE)
21534     return;
21535
21536   gcc_assert (decl);
21537
21538   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21539      We need decl DIE for reference and scope die. First, get DIE for the decl
21540      itself.  */
21541
21542   /* Get the scope die for decl context. Use comp_unit_die for global module
21543      or decl. If die is not found for non globals, force new die.  */
21544   if (context
21545       && TYPE_P (context)
21546       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21547     return;
21548
21549   if (!(dwarf_version >= 3 || !dwarf_strict))
21550     return;
21551
21552   scope_die = get_context_die (context);
21553
21554   if (child)
21555     {
21556       gcc_assert (scope_die->die_child);
21557       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21558       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21559       scope_die = scope_die->die_child;
21560     }
21561
21562   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21563   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21564
21565 }
21566
21567 /* Write the debugging output for DECL.  */
21568
21569 void
21570 dwarf2out_decl (tree decl)
21571 {
21572   dw_die_ref context_die = comp_unit_die ();
21573
21574   switch (TREE_CODE (decl))
21575     {
21576     case ERROR_MARK:
21577       return;
21578
21579     case FUNCTION_DECL:
21580       /* What we would really like to do here is to filter out all mere
21581          file-scope declarations of file-scope functions which are never
21582          referenced later within this translation unit (and keep all of ones
21583          that *are* referenced later on) but we aren't clairvoyant, so we have
21584          no idea which functions will be referenced in the future (i.e. later
21585          on within the current translation unit). So here we just ignore all
21586          file-scope function declarations which are not also definitions.  If
21587          and when the debugger needs to know something about these functions,
21588          it will have to hunt around and find the DWARF information associated
21589          with the definition of the function.
21590
21591          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21592          nodes represent definitions and which ones represent mere
21593          declarations.  We have to check DECL_INITIAL instead. That's because
21594          the C front-end supports some weird semantics for "extern inline"
21595          function definitions.  These can get inlined within the current
21596          translation unit (and thus, we need to generate Dwarf info for their
21597          abstract instances so that the Dwarf info for the concrete inlined
21598          instances can have something to refer to) but the compiler never
21599          generates any out-of-lines instances of such things (despite the fact
21600          that they *are* definitions).
21601
21602          The important point is that the C front-end marks these "extern
21603          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21604          them anyway. Note that the C++ front-end also plays some similar games
21605          for inline function definitions appearing within include files which
21606          also contain `#pragma interface' pragmas.  */
21607       if (DECL_INITIAL (decl) == NULL_TREE)
21608         return;
21609
21610       /* If we're a nested function, initially use a parent of NULL; if we're
21611          a plain function, this will be fixed up in decls_for_scope.  If
21612          we're a method, it will be ignored, since we already have a DIE.  */
21613       if (decl_function_context (decl)
21614           /* But if we're in terse mode, we don't care about scope.  */
21615           && debug_info_level > DINFO_LEVEL_TERSE)
21616         context_die = NULL;
21617       break;
21618
21619     case VAR_DECL:
21620       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21621          declaration and if the declaration was never even referenced from
21622          within this entire compilation unit.  We suppress these DIEs in
21623          order to save space in the .debug section (by eliminating entries
21624          which are probably useless).  Note that we must not suppress
21625          block-local extern declarations (whether used or not) because that
21626          would screw-up the debugger's name lookup mechanism and cause it to
21627          miss things which really ought to be in scope at a given point.  */
21628       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21629         return;
21630
21631       /* For local statics lookup proper context die.  */
21632       if (TREE_STATIC (decl) && decl_function_context (decl))
21633         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21634
21635       /* If we are in terse mode, don't generate any DIEs to represent any
21636          variable declarations or definitions.  */
21637       if (debug_info_level <= DINFO_LEVEL_TERSE)
21638         return;
21639       break;
21640
21641     case CONST_DECL:
21642       if (debug_info_level <= DINFO_LEVEL_TERSE)
21643         return;
21644       if (!is_fortran () && !is_ada ())
21645         return;
21646       if (TREE_STATIC (decl) && decl_function_context (decl))
21647         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21648       break;
21649
21650     case NAMESPACE_DECL:
21651     case IMPORTED_DECL:
21652       if (debug_info_level <= DINFO_LEVEL_TERSE)
21653         return;
21654       if (lookup_decl_die (decl) != NULL)
21655         return;
21656       break;
21657
21658     case TYPE_DECL:
21659       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21660       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21661         return;
21662
21663       /* Don't bother trying to generate any DIEs to represent any of the
21664          normal built-in types for the language we are compiling.  */
21665       if (DECL_IS_BUILTIN (decl))
21666         return;
21667
21668       /* If we are in terse mode, don't generate any DIEs for types.  */
21669       if (debug_info_level <= DINFO_LEVEL_TERSE)
21670         return;
21671
21672       /* If we're a function-scope tag, initially use a parent of NULL;
21673          this will be fixed up in decls_for_scope.  */
21674       if (decl_function_context (decl))
21675         context_die = NULL;
21676
21677       break;
21678
21679     default:
21680       return;
21681     }
21682
21683   gen_decl_die (decl, NULL, context_die);
21684 }
21685
21686 /* Write the debugging output for DECL.  */
21687
21688 static void
21689 dwarf2out_function_decl (tree decl)
21690 {
21691   dwarf2out_decl (decl);
21692   call_arg_locations = NULL;
21693   call_arg_loc_last = NULL;
21694   call_site_count = -1;
21695   tail_call_site_count = -1;
21696   VEC_free (dw_die_ref, heap, block_map);
21697   htab_empty (decl_loc_table);
21698   htab_empty (cached_dw_loc_list_table);
21699 }
21700
21701 /* Output a marker (i.e. a label) for the beginning of the generated code for
21702    a lexical block.  */
21703
21704 static void
21705 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21706                        unsigned int blocknum)
21707 {
21708   switch_to_section (current_function_section ());
21709   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21710 }
21711
21712 /* Output a marker (i.e. a label) for the end of the generated code for a
21713    lexical block.  */
21714
21715 static void
21716 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21717 {
21718   switch_to_section (current_function_section ());
21719   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21720 }
21721
21722 /* Returns nonzero if it is appropriate not to emit any debugging
21723    information for BLOCK, because it doesn't contain any instructions.
21724
21725    Don't allow this for blocks with nested functions or local classes
21726    as we would end up with orphans, and in the presence of scheduling
21727    we may end up calling them anyway.  */
21728
21729 static bool
21730 dwarf2out_ignore_block (const_tree block)
21731 {
21732   tree decl;
21733   unsigned int i;
21734
21735   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21736     if (TREE_CODE (decl) == FUNCTION_DECL
21737         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21738       return 0;
21739   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21740     {
21741       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21742       if (TREE_CODE (decl) == FUNCTION_DECL
21743           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21744       return 0;
21745     }
21746
21747   return 1;
21748 }
21749
21750 /* Hash table routines for file_hash.  */
21751
21752 static int
21753 file_table_eq (const void *p1_p, const void *p2_p)
21754 {
21755   const struct dwarf_file_data *const p1 =
21756     (const struct dwarf_file_data *) p1_p;
21757   const char *const p2 = (const char *) p2_p;
21758   return filename_cmp (p1->filename, p2) == 0;
21759 }
21760
21761 static hashval_t
21762 file_table_hash (const void *p_p)
21763 {
21764   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21765   return htab_hash_string (p->filename);
21766 }
21767
21768 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21769    dwarf2out.c) and return its "index".  The index of each (known) filename is
21770    just a unique number which is associated with only that one filename.  We
21771    need such numbers for the sake of generating labels (in the .debug_sfnames
21772    section) and references to those files numbers (in the .debug_srcinfo
21773    and.debug_macinfo sections).  If the filename given as an argument is not
21774    found in our current list, add it to the list and assign it the next
21775    available unique index number.  In order to speed up searches, we remember
21776    the index of the filename was looked up last.  This handles the majority of
21777    all searches.  */
21778
21779 static struct dwarf_file_data *
21780 lookup_filename (const char *file_name)
21781 {
21782   void ** slot;
21783   struct dwarf_file_data * created;
21784
21785   /* Check to see if the file name that was searched on the previous
21786      call matches this file name.  If so, return the index.  */
21787   if (file_table_last_lookup
21788       && (file_name == file_table_last_lookup->filename
21789           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21790     return file_table_last_lookup;
21791
21792   /* Didn't match the previous lookup, search the table.  */
21793   slot = htab_find_slot_with_hash (file_table, file_name,
21794                                    htab_hash_string (file_name), INSERT);
21795   if (*slot)
21796     return (struct dwarf_file_data *) *slot;
21797
21798   created = ggc_alloc_dwarf_file_data ();
21799   created->filename = file_name;
21800   created->emitted_number = 0;
21801   *slot = created;
21802   return created;
21803 }
21804
21805 /* If the assembler will construct the file table, then translate the compiler
21806    internal file table number into the assembler file table number, and emit
21807    a .file directive if we haven't already emitted one yet.  The file table
21808    numbers are different because we prune debug info for unused variables and
21809    types, which may include filenames.  */
21810
21811 static int
21812 maybe_emit_file (struct dwarf_file_data * fd)
21813 {
21814   if (! fd->emitted_number)
21815     {
21816       if (last_emitted_file)
21817         fd->emitted_number = last_emitted_file->emitted_number + 1;
21818       else
21819         fd->emitted_number = 1;
21820       last_emitted_file = fd;
21821
21822       if (DWARF2_ASM_LINE_DEBUG_INFO)
21823         {
21824           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21825           output_quoted_string (asm_out_file,
21826                                 remap_debug_filename (fd->filename));
21827           fputc ('\n', asm_out_file);
21828         }
21829     }
21830
21831   return fd->emitted_number;
21832 }
21833
21834 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21835    That generation should happen after function debug info has been
21836    generated. The value of the attribute is the constant value of ARG.  */
21837
21838 static void
21839 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21840 {
21841   die_arg_entry entry;
21842
21843   if (!die || !arg)
21844     return;
21845
21846   if (!tmpl_value_parm_die_table)
21847     tmpl_value_parm_die_table
21848       = VEC_alloc (die_arg_entry, gc, 32);
21849
21850   entry.die = die;
21851   entry.arg = arg;
21852   VEC_safe_push (die_arg_entry, gc,
21853                  tmpl_value_parm_die_table,
21854                  &entry);
21855 }
21856
21857 /* Return TRUE if T is an instance of generic type, FALSE
21858    otherwise.  */
21859
21860 static bool
21861 generic_type_p (tree t)
21862 {
21863   if (t == NULL_TREE || !TYPE_P (t))
21864     return false;
21865   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21866 }
21867
21868 /* Schedule the generation of the generic parameter dies for the
21869   instance of generic type T. The proper generation itself is later
21870   done by gen_scheduled_generic_parms_dies. */
21871
21872 static void
21873 schedule_generic_params_dies_gen (tree t)
21874 {
21875   if (!generic_type_p (t))
21876     return;
21877
21878   if (generic_type_instances == NULL)
21879     generic_type_instances = VEC_alloc (tree, gc, 256);
21880
21881   VEC_safe_push (tree, gc, generic_type_instances, t);
21882 }
21883
21884 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21885    by append_entry_to_tmpl_value_parm_die_table. This function must
21886    be called after function DIEs have been generated.  */
21887
21888 static void
21889 gen_remaining_tmpl_value_param_die_attribute (void)
21890 {
21891   if (tmpl_value_parm_die_table)
21892     {
21893       unsigned i;
21894       die_arg_entry *e;
21895
21896       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21897         tree_add_const_value_attribute (e->die, e->arg);
21898     }
21899 }
21900
21901 /* Generate generic parameters DIEs for instances of generic types
21902    that have been previously scheduled by
21903    schedule_generic_params_dies_gen. This function must be called
21904    after all the types of the CU have been laid out.  */
21905
21906 static void
21907 gen_scheduled_generic_parms_dies (void)
21908 {
21909   unsigned i;
21910   tree t;
21911
21912   if (generic_type_instances == NULL)
21913     return;
21914   
21915   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21916     gen_generic_params_dies (t);
21917 }
21918
21919
21920 /* Replace DW_AT_name for the decl with name.  */
21921
21922 static void
21923 dwarf2out_set_name (tree decl, tree name)
21924 {
21925   dw_die_ref die;
21926   dw_attr_ref attr;
21927   const char *dname;
21928
21929   die = TYPE_SYMTAB_DIE (decl);
21930   if (!die)
21931     return;
21932
21933   dname = dwarf2_name (name, 0);
21934   if (!dname)
21935     return;
21936
21937   attr = get_AT (die, DW_AT_name);
21938   if (attr)
21939     {
21940       struct indirect_string_node *node;
21941
21942       node = find_AT_string (dname);
21943       /* replace the string.  */
21944       attr->dw_attr_val.v.val_str = node;
21945     }
21946
21947   else
21948     add_name_attribute (die, dname);
21949 }
21950
21951 /* Called by the final INSN scan whenever we see a var location.  We
21952    use it to drop labels in the right places, and throw the location in
21953    our lookup table.  */
21954
21955 static void
21956 dwarf2out_var_location (rtx loc_note)
21957 {
21958   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21959   struct var_loc_node *newloc;
21960   rtx next_real;
21961   static const char *last_label;
21962   static const char *last_postcall_label;
21963   static bool last_in_cold_section_p;
21964   tree decl;
21965   bool var_loc_p;
21966
21967   if (!NOTE_P (loc_note))
21968     {
21969       if (CALL_P (loc_note))
21970         {
21971           call_site_count++;
21972           if (SIBLING_CALL_P (loc_note))
21973             tail_call_site_count++;
21974         }
21975       return;
21976     }
21977
21978   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
21979   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21980     return;
21981
21982   next_real = next_real_insn (loc_note);
21983
21984   /* If there are no instructions which would be affected by this note,
21985      don't do anything.  */
21986   if (var_loc_p
21987       && next_real == NULL_RTX
21988       && !NOTE_DURING_CALL_P (loc_note))
21989     return;
21990
21991   if (next_real == NULL_RTX)
21992     next_real = get_last_insn ();
21993
21994   /* If there were any real insns between note we processed last time
21995      and this note (or if it is the first note), clear
21996      last_{,postcall_}label so that they are not reused this time.  */
21997   if (last_var_location_insn == NULL_RTX
21998       || last_var_location_insn != next_real
21999       || last_in_cold_section_p != in_cold_section_p)
22000     {
22001       last_label = NULL;
22002       last_postcall_label = NULL;
22003     }
22004
22005   if (var_loc_p)
22006     {
22007       decl = NOTE_VAR_LOCATION_DECL (loc_note);
22008       newloc = add_var_loc_to_decl (decl, loc_note,
22009                                     NOTE_DURING_CALL_P (loc_note)
22010                                     ? last_postcall_label : last_label);
22011       if (newloc == NULL)
22012         return;
22013     }
22014   else
22015     {
22016       decl = NULL_TREE;
22017       newloc = NULL;
22018     }
22019
22020   /* If there were no real insns between note we processed last time
22021      and this note, use the label we emitted last time.  Otherwise
22022      create a new label and emit it.  */
22023   if (last_label == NULL)
22024     {
22025       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22026       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22027       loclabel_num++;
22028       last_label = ggc_strdup (loclabel);
22029     }
22030
22031   if (!var_loc_p)
22032     {
22033       struct call_arg_loc_node *ca_loc
22034         = ggc_alloc_cleared_call_arg_loc_node ();
22035       rtx prev = prev_real_insn (loc_note), x;
22036       ca_loc->call_arg_loc_note = loc_note;
22037       ca_loc->next = NULL;
22038       ca_loc->label = last_label;
22039       gcc_assert (prev
22040                   && (CALL_P (prev)
22041                       || (NONJUMP_INSN_P (prev)
22042                           && GET_CODE (PATTERN (prev)) == SEQUENCE
22043                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22044       if (!CALL_P (prev))
22045         prev = XVECEXP (PATTERN (prev), 0, 0);
22046       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22047       x = PATTERN (prev);
22048       if (GET_CODE (x) == PARALLEL)
22049         x = XVECEXP (x, 0, 0);
22050       if (GET_CODE (x) == SET)
22051         x = SET_SRC (x);
22052       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22053         {
22054           x = XEXP (XEXP (x, 0), 0);
22055           if (GET_CODE (x) == SYMBOL_REF
22056               && SYMBOL_REF_DECL (x)
22057               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22058             ca_loc->symbol_ref = x;
22059         }
22060       ca_loc->block = insn_scope (prev);
22061       if (call_arg_locations)
22062         call_arg_loc_last->next = ca_loc;
22063       else
22064         call_arg_locations = ca_loc;
22065       call_arg_loc_last = ca_loc;
22066     }
22067   else if (!NOTE_DURING_CALL_P (loc_note))
22068     newloc->label = last_label;
22069   else
22070     {
22071       if (!last_postcall_label)
22072         {
22073           sprintf (loclabel, "%s-1", last_label);
22074           last_postcall_label = ggc_strdup (loclabel);
22075         }
22076       newloc->label = last_postcall_label;
22077     }
22078
22079   last_var_location_insn = next_real;
22080   last_in_cold_section_p = in_cold_section_p;
22081 }
22082
22083 /* Note in one location list that text section has changed.  */
22084
22085 static int
22086 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
22087 {
22088   var_loc_list *list = (var_loc_list *) *slot;
22089   if (list->first)
22090     list->last_before_switch
22091       = list->last->next ? list->last->next : list->last;
22092   return 1;
22093 }
22094
22095 /* Note in all location lists that text section has changed.  */
22096
22097 static void
22098 var_location_switch_text_section (void)
22099 {
22100   if (decl_loc_table == NULL)
22101     return;
22102
22103   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
22104 }
22105
22106 /* We need to reset the locations at the beginning of each
22107    function. We can't do this in the end_function hook, because the
22108    declarations that use the locations won't have been output when
22109    that hook is called.  Also compute have_multiple_function_sections here.  */
22110
22111 static void
22112 dwarf2out_begin_function (tree fun)
22113 {
22114   if (function_section (fun) != text_section)
22115     have_multiple_function_sections = true;
22116   if (flag_reorder_blocks_and_partition && !cold_text_section)
22117     {
22118       gcc_assert (current_function_decl == fun);
22119       cold_text_section = unlikely_text_section ();
22120       switch_to_section (cold_text_section);
22121       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22122       switch_to_section (current_function_section ());
22123     }
22124
22125   dwarf2out_note_section_used ();
22126   call_site_count = 0;
22127   tail_call_site_count = 0;
22128 }
22129
22130 /* Output a label to mark the beginning of a source code line entry
22131    and record information relating to this source line, in
22132    'line_info_table' for later output of the .debug_line section.  */
22133
22134 static void
22135 dwarf2out_source_line (unsigned int line, const char *filename,
22136                        int discriminator, bool is_stmt)
22137 {
22138   static bool last_is_stmt = true;
22139
22140   if (debug_info_level >= DINFO_LEVEL_NORMAL
22141       && line != 0)
22142     {
22143       int file_num = maybe_emit_file (lookup_filename (filename));
22144
22145       switch_to_section (current_function_section ());
22146
22147       /* If requested, emit something human-readable.  */
22148       if (flag_debug_asm)
22149         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
22150                  filename, line);
22151
22152       if (DWARF2_ASM_LINE_DEBUG_INFO)
22153         {
22154           /* Emit the .loc directive understood by GNU as.  */
22155           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22156           if (is_stmt != last_is_stmt)
22157             {
22158               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22159               last_is_stmt = is_stmt;
22160             }
22161           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22162             fprintf (asm_out_file, " discriminator %d", discriminator);
22163           fputc ('\n', asm_out_file);
22164
22165           /* Indicate that line number info exists.  */
22166           line_info_table_in_use++;
22167         }
22168       else if (function_section (current_function_decl) != text_section)
22169         {
22170           dw_separate_line_info_ref line_info;
22171           targetm.asm_out.internal_label (asm_out_file,
22172                                           SEPARATE_LINE_CODE_LABEL,
22173                                           separate_line_info_table_in_use);
22174
22175           /* Expand the line info table if necessary.  */
22176           if (separate_line_info_table_in_use
22177               == separate_line_info_table_allocated)
22178             {
22179               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22180               separate_line_info_table
22181                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
22182                                  separate_line_info_table,
22183                                  separate_line_info_table_allocated);
22184               memset (separate_line_info_table
22185                        + separate_line_info_table_in_use,
22186                       0,
22187                       (LINE_INFO_TABLE_INCREMENT
22188                        * sizeof (dw_separate_line_info_entry)));
22189             }
22190
22191           /* Add the new entry at the end of the line_info_table.  */
22192           line_info
22193             = &separate_line_info_table[separate_line_info_table_in_use++];
22194           line_info->dw_file_num = file_num;
22195           line_info->dw_line_num = line;
22196           line_info->function = current_function_funcdef_no;
22197         }
22198       else
22199         {
22200           dw_line_info_ref line_info;
22201
22202           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
22203                                      line_info_table_in_use);
22204
22205           /* Expand the line info table if necessary.  */
22206           if (line_info_table_in_use == line_info_table_allocated)
22207             {
22208               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22209               line_info_table
22210                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
22211                                  line_info_table_allocated);
22212               memset (line_info_table + line_info_table_in_use, 0,
22213                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
22214             }
22215
22216           /* Add the new entry at the end of the line_info_table.  */
22217           line_info = &line_info_table[line_info_table_in_use++];
22218           line_info->dw_file_num = file_num;
22219           line_info->dw_line_num = line;
22220         }
22221     }
22222 }
22223
22224 /* Record the beginning of a new source file.  */
22225
22226 static void
22227 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22228 {
22229   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22230     {
22231       /* Record the beginning of the file for break_out_includes.  */
22232       dw_die_ref bincl_die;
22233
22234       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22235       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22236     }
22237
22238   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22239     {
22240       macinfo_entry e;
22241       e.code = DW_MACINFO_start_file;
22242       e.lineno = lineno;
22243       e.info = xstrdup (filename);
22244       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22245     }
22246 }
22247
22248 /* Record the end of a source file.  */
22249
22250 static void
22251 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22252 {
22253   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22254     /* Record the end of the file for break_out_includes.  */
22255     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22256
22257   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22258     {
22259       macinfo_entry e;
22260       e.code = DW_MACINFO_end_file;
22261       e.lineno = lineno;
22262       e.info = NULL;
22263       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22264     }
22265 }
22266
22267 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22268    the tail part of the directive line, i.e. the part which is past the
22269    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22270
22271 static void
22272 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22273                   const char *buffer ATTRIBUTE_UNUSED)
22274 {
22275   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22276     {
22277       macinfo_entry e;
22278       e.code = DW_MACINFO_define;
22279       e.lineno = lineno;
22280       e.info = xstrdup (buffer);;
22281       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22282     }
22283 }
22284
22285 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22286    the tail part of the directive line, i.e. the part which is past the
22287    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22288
22289 static void
22290 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22291                  const char *buffer ATTRIBUTE_UNUSED)
22292 {
22293   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22294     {
22295       macinfo_entry e;
22296       e.code = DW_MACINFO_undef;
22297       e.lineno = lineno;
22298       e.info = xstrdup (buffer);;
22299       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22300     }
22301 }
22302
22303 static void
22304 output_macinfo (void)
22305 {
22306   unsigned i;
22307   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22308   macinfo_entry *ref;
22309
22310   if (! length)
22311     return;
22312
22313   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22314     {
22315       switch (ref->code)
22316         {
22317           case DW_MACINFO_start_file:
22318             {
22319               int file_num = maybe_emit_file (lookup_filename (ref->info));
22320               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22321               dw2_asm_output_data_uleb128 
22322                         (ref->lineno, "Included from line number %lu", 
22323                                                 (unsigned long)ref->lineno);
22324               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22325             }
22326             break;
22327           case DW_MACINFO_end_file:
22328             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22329             break;
22330           case DW_MACINFO_define:
22331             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22332             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22333                                                 (unsigned long)ref->lineno);
22334             dw2_asm_output_nstring (ref->info, -1, "The macro");
22335             break;
22336           case DW_MACINFO_undef:
22337             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22338             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22339                                                 (unsigned long)ref->lineno);
22340             dw2_asm_output_nstring (ref->info, -1, "The macro");
22341             break;
22342           default:
22343            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22344              ASM_COMMENT_START, (unsigned long)ref->code);
22345           break;
22346         }
22347     }
22348 }
22349
22350 /* Set up for Dwarf output at the start of compilation.  */
22351
22352 static void
22353 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22354 {
22355   /* Allocate the file_table.  */
22356   file_table = htab_create_ggc (50, file_table_hash,
22357                                 file_table_eq, NULL);
22358
22359   /* Allocate the decl_die_table.  */
22360   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22361                                     decl_die_table_eq, NULL);
22362
22363   /* Allocate the decl_loc_table.  */
22364   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22365                                     decl_loc_table_eq, NULL);
22366
22367   /* Allocate the cached_dw_loc_list_table.  */
22368   cached_dw_loc_list_table
22369     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22370                        cached_dw_loc_list_table_eq, NULL);
22371
22372   /* Allocate the initial hunk of the decl_scope_table.  */
22373   decl_scope_table = VEC_alloc (tree, gc, 256);
22374
22375   /* Allocate the initial hunk of the abbrev_die_table.  */
22376   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22377     (ABBREV_DIE_TABLE_INCREMENT);
22378   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22379   /* Zero-th entry is allocated, but unused.  */
22380   abbrev_die_table_in_use = 1;
22381
22382   /* Allocate the initial hunk of the line_info_table.  */
22383   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22384     (LINE_INFO_TABLE_INCREMENT);
22385   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22386
22387   /* Zero-th entry is allocated, but unused.  */
22388   line_info_table_in_use = 1;
22389
22390   /* Allocate the pubtypes and pubnames vectors.  */
22391   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22392   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22393
22394   incomplete_types = VEC_alloc (tree, gc, 64);
22395
22396   used_rtx_array = VEC_alloc (rtx, gc, 32);
22397
22398   debug_info_section = get_section (DEBUG_INFO_SECTION,
22399                                     SECTION_DEBUG, NULL);
22400   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22401                                       SECTION_DEBUG, NULL);
22402   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22403                                        SECTION_DEBUG, NULL);
22404   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22405                                        SECTION_DEBUG, NULL);
22406   debug_line_section = get_section (DEBUG_LINE_SECTION,
22407                                     SECTION_DEBUG, NULL);
22408   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22409                                    SECTION_DEBUG, NULL);
22410   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22411                                         SECTION_DEBUG, NULL);
22412   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22413                                         SECTION_DEBUG, NULL);
22414   debug_str_section = get_section (DEBUG_STR_SECTION,
22415                                    DEBUG_STR_SECTION_FLAGS, NULL);
22416   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22417                                       SECTION_DEBUG, NULL);
22418   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22419                                      SECTION_DEBUG, NULL);
22420
22421   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22422   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22423                                DEBUG_ABBREV_SECTION_LABEL, 0);
22424   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22425   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22426                                COLD_TEXT_SECTION_LABEL, 0);
22427   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22428
22429   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22430                                DEBUG_INFO_SECTION_LABEL, 0);
22431   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22432                                DEBUG_LINE_SECTION_LABEL, 0);
22433   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22434                                DEBUG_RANGES_SECTION_LABEL, 0);
22435   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22436                                DEBUG_MACINFO_SECTION_LABEL, 0);
22437
22438   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22439     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22440
22441   switch_to_section (text_section);
22442   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22443 }
22444
22445 /* Called before cgraph_optimize starts outputtting functions, variables
22446    and toplevel asms into assembly.  */
22447
22448 static void
22449 dwarf2out_assembly_start (void)
22450 {
22451   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22452       && dwarf2out_do_cfi_asm ()
22453       && (!(flag_unwind_tables || flag_exceptions)
22454           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22455     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22456 }
22457
22458 /* A helper function for dwarf2out_finish called through
22459    htab_traverse.  Emit one queued .debug_str string.  */
22460
22461 static int
22462 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22463 {
22464   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22465
22466   if (node->label && node->refcount)
22467     {
22468       switch_to_section (debug_str_section);
22469       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22470       assemble_string (node->str, strlen (node->str) + 1);
22471     }
22472
22473   return 1;
22474 }
22475
22476 #if ENABLE_ASSERT_CHECKING
22477 /* Verify that all marks are clear.  */
22478
22479 static void
22480 verify_marks_clear (dw_die_ref die)
22481 {
22482   dw_die_ref c;
22483
22484   gcc_assert (! die->die_mark);
22485   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22486 }
22487 #endif /* ENABLE_ASSERT_CHECKING */
22488
22489 /* Clear the marks for a die and its children.
22490    Be cool if the mark isn't set.  */
22491
22492 static void
22493 prune_unmark_dies (dw_die_ref die)
22494 {
22495   dw_die_ref c;
22496
22497   if (die->die_mark)
22498     die->die_mark = 0;
22499   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22500 }
22501
22502 /* Given DIE that we're marking as used, find any other dies
22503    it references as attributes and mark them as used.  */
22504
22505 static void
22506 prune_unused_types_walk_attribs (dw_die_ref die)
22507 {
22508   dw_attr_ref a;
22509   unsigned ix;
22510
22511   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22512     {
22513       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22514         {
22515           /* A reference to another DIE.
22516              Make sure that it will get emitted.
22517              If it was broken out into a comdat group, don't follow it.  */
22518           if (dwarf_version < 4
22519               || a->dw_attr == DW_AT_specification
22520               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22521             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22522         }
22523       /* Set the string's refcount to 0 so that prune_unused_types_mark
22524          accounts properly for it.  */
22525       if (AT_class (a) == dw_val_class_str)
22526         a->dw_attr_val.v.val_str->refcount = 0;
22527     }
22528 }
22529
22530 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22531
22532 static void
22533 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22534 {
22535   dw_die_ref c;
22536
22537   if (die == NULL || die->die_child == NULL)
22538     return;
22539   c = die->die_child;
22540   do
22541     {
22542       switch (c->die_tag)
22543         {
22544         case DW_TAG_template_type_param:
22545         case DW_TAG_template_value_param:
22546         case DW_TAG_GNU_template_template_param:
22547         case DW_TAG_GNU_template_parameter_pack:
22548           prune_unused_types_mark (c, 1);
22549           break;
22550         default:
22551           break;
22552         }
22553       c = c->die_sib;
22554     } while (c && c != die->die_child);
22555 }
22556
22557 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22558    to DIE's children.  */
22559
22560 static void
22561 prune_unused_types_mark (dw_die_ref die, int dokids)
22562 {
22563   dw_die_ref c;
22564
22565   if (die->die_mark == 0)
22566     {
22567       /* We haven't done this node yet.  Mark it as used.  */
22568       die->die_mark = 1;
22569       /* If this is the DIE of a generic type instantiation,
22570          mark the children DIEs that describe its generic parms and
22571          args.  */
22572       prune_unused_types_mark_generic_parms_dies (die);
22573
22574       /* We also have to mark its parents as used.
22575          (But we don't want to mark our parents' kids due to this.)  */
22576       if (die->die_parent)
22577         prune_unused_types_mark (die->die_parent, 0);
22578
22579       /* Mark any referenced nodes.  */
22580       prune_unused_types_walk_attribs (die);
22581
22582       /* If this node is a specification,
22583          also mark the definition, if it exists.  */
22584       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22585         prune_unused_types_mark (die->die_definition, 1);
22586     }
22587
22588   if (dokids && die->die_mark != 2)
22589     {
22590       /* We need to walk the children, but haven't done so yet.
22591          Remember that we've walked the kids.  */
22592       die->die_mark = 2;
22593
22594       /* If this is an array type, we need to make sure our
22595          kids get marked, even if they're types.  If we're
22596          breaking out types into comdat sections, do this
22597          for all type definitions.  */
22598       if (die->die_tag == DW_TAG_array_type
22599           || (dwarf_version >= 4
22600               && is_type_die (die) && ! is_declaration_die (die)))
22601         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22602       else
22603         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22604     }
22605 }
22606
22607 /* For local classes, look if any static member functions were emitted
22608    and if so, mark them.  */
22609
22610 static void
22611 prune_unused_types_walk_local_classes (dw_die_ref die)
22612 {
22613   dw_die_ref c;
22614
22615   if (die->die_mark == 2)
22616     return;
22617
22618   switch (die->die_tag)
22619     {
22620     case DW_TAG_structure_type:
22621     case DW_TAG_union_type:
22622     case DW_TAG_class_type:
22623       break;
22624
22625     case DW_TAG_subprogram:
22626       if (!get_AT_flag (die, DW_AT_declaration)
22627           || die->die_definition != NULL)
22628         prune_unused_types_mark (die, 1);
22629       return;
22630
22631     default:
22632       return;
22633     }
22634
22635   /* Mark children.  */
22636   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22637 }
22638
22639 /* Walk the tree DIE and mark types that we actually use.  */
22640
22641 static void
22642 prune_unused_types_walk (dw_die_ref die)
22643 {
22644   dw_die_ref c;
22645
22646   /* Don't do anything if this node is already marked and
22647      children have been marked as well.  */
22648   if (die->die_mark == 2)
22649     return;
22650
22651   switch (die->die_tag)
22652     {
22653     case DW_TAG_structure_type:
22654     case DW_TAG_union_type:
22655     case DW_TAG_class_type:
22656       if (die->die_perennial_p)
22657         break;
22658
22659       for (c = die->die_parent; c; c = c->die_parent)
22660         if (c->die_tag == DW_TAG_subprogram)
22661           break;
22662
22663       /* Finding used static member functions inside of classes
22664          is needed just for local classes, because for other classes
22665          static member function DIEs with DW_AT_specification
22666          are emitted outside of the DW_TAG_*_type.  If we ever change
22667          it, we'd need to call this even for non-local classes.  */
22668       if (c)
22669         prune_unused_types_walk_local_classes (die);
22670
22671       /* It's a type node --- don't mark it.  */
22672       return;
22673
22674     case DW_TAG_const_type:
22675     case DW_TAG_packed_type:
22676     case DW_TAG_pointer_type:
22677     case DW_TAG_reference_type:
22678     case DW_TAG_rvalue_reference_type:
22679     case DW_TAG_volatile_type:
22680     case DW_TAG_typedef:
22681     case DW_TAG_array_type:
22682     case DW_TAG_interface_type:
22683     case DW_TAG_friend:
22684     case DW_TAG_variant_part:
22685     case DW_TAG_enumeration_type:
22686     case DW_TAG_subroutine_type:
22687     case DW_TAG_string_type:
22688     case DW_TAG_set_type:
22689     case DW_TAG_subrange_type:
22690     case DW_TAG_ptr_to_member_type:
22691     case DW_TAG_file_type:
22692       if (die->die_perennial_p)
22693         break;
22694
22695       /* It's a type node --- don't mark it.  */
22696       return;
22697
22698     default:
22699       /* Mark everything else.  */
22700       break;
22701   }
22702
22703   if (die->die_mark == 0)
22704     {
22705       die->die_mark = 1;
22706
22707       /* Now, mark any dies referenced from here.  */
22708       prune_unused_types_walk_attribs (die);
22709     }
22710
22711   die->die_mark = 2;
22712
22713   /* Mark children.  */
22714   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22715 }
22716
22717 /* Increment the string counts on strings referred to from DIE's
22718    attributes.  */
22719
22720 static void
22721 prune_unused_types_update_strings (dw_die_ref die)
22722 {
22723   dw_attr_ref a;
22724   unsigned ix;
22725
22726   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22727     if (AT_class (a) == dw_val_class_str)
22728       {
22729         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22730         s->refcount++;
22731         /* Avoid unnecessarily putting strings that are used less than
22732            twice in the hash table.  */
22733         if (s->refcount
22734             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22735           {
22736             void ** slot;
22737             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22738                                              htab_hash_string (s->str),
22739                                              INSERT);
22740             gcc_assert (*slot == NULL);
22741             *slot = s;
22742           }
22743       }
22744 }
22745
22746 /* Remove from the tree DIE any dies that aren't marked.  */
22747
22748 static void
22749 prune_unused_types_prune (dw_die_ref die)
22750 {
22751   dw_die_ref c;
22752
22753   gcc_assert (die->die_mark);
22754   prune_unused_types_update_strings (die);
22755
22756   if (! die->die_child)
22757     return;
22758
22759   c = die->die_child;
22760   do {
22761     dw_die_ref prev = c;
22762     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22763       if (c == die->die_child)
22764         {
22765           /* No marked children between 'prev' and the end of the list.  */
22766           if (prev == c)
22767             /* No marked children at all.  */
22768             die->die_child = NULL;
22769           else
22770             {
22771               prev->die_sib = c->die_sib;
22772               die->die_child = prev;
22773             }
22774           return;
22775         }
22776
22777     if (c != prev->die_sib)
22778       prev->die_sib = c;
22779     prune_unused_types_prune (c);
22780   } while (c != die->die_child);
22781 }
22782
22783 /* A helper function for dwarf2out_finish called through
22784    htab_traverse.  Clear .debug_str strings that we haven't already
22785    decided to emit.  */
22786
22787 static int
22788 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22789 {
22790   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22791
22792   if (!node->label || !node->refcount)
22793     htab_clear_slot (debug_str_hash, h);
22794
22795   return 1;
22796 }
22797
22798 /* Remove dies representing declarations that we never use.  */
22799
22800 static void
22801 prune_unused_types (void)
22802 {
22803   unsigned int i;
22804   limbo_die_node *node;
22805   comdat_type_node *ctnode;
22806   pubname_ref pub;
22807
22808 #if ENABLE_ASSERT_CHECKING
22809   /* All the marks should already be clear.  */
22810   verify_marks_clear (comp_unit_die ());
22811   for (node = limbo_die_list; node; node = node->next)
22812     verify_marks_clear (node->die);
22813   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22814     verify_marks_clear (ctnode->root_die);
22815 #endif /* ENABLE_ASSERT_CHECKING */
22816
22817   /* Mark types that are used in global variables.  */
22818   premark_types_used_by_global_vars ();
22819
22820   /* Set the mark on nodes that are actually used.  */
22821   prune_unused_types_walk (comp_unit_die ());
22822   for (node = limbo_die_list; node; node = node->next)
22823     prune_unused_types_walk (node->die);
22824   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22825     {
22826       prune_unused_types_walk (ctnode->root_die);
22827       prune_unused_types_mark (ctnode->type_die, 1);
22828     }
22829
22830   /* Also set the mark on nodes referenced from the
22831      pubname_table.  */
22832   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
22833     prune_unused_types_mark (pub->die, 1);
22834
22835   /* Get rid of nodes that aren't marked; and update the string counts.  */
22836   if (debug_str_hash && debug_str_hash_forced)
22837     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22838   else if (debug_str_hash)
22839     htab_empty (debug_str_hash);
22840   prune_unused_types_prune (comp_unit_die ());
22841   for (node = limbo_die_list; node; node = node->next)
22842     prune_unused_types_prune (node->die);
22843   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22844     prune_unused_types_prune (ctnode->root_die);
22845
22846   /* Leave the marks clear.  */
22847   prune_unmark_dies (comp_unit_die ());
22848   for (node = limbo_die_list; node; node = node->next)
22849     prune_unmark_dies (node->die);
22850   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22851     prune_unmark_dies (ctnode->root_die);
22852 }
22853
22854 /* Set the parameter to true if there are any relative pathnames in
22855    the file table.  */
22856 static int
22857 file_table_relative_p (void ** slot, void *param)
22858 {
22859   bool *p = (bool *) param;
22860   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22861   if (!IS_ABSOLUTE_PATH (d->filename))
22862     {
22863       *p = true;
22864       return 0;
22865     }
22866   return 1;
22867 }
22868
22869 /* Routines to manipulate hash table of comdat type units.  */
22870
22871 static hashval_t
22872 htab_ct_hash (const void *of)
22873 {
22874   hashval_t h;
22875   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22876
22877   memcpy (&h, type_node->signature, sizeof (h));
22878   return h;
22879 }
22880
22881 static int
22882 htab_ct_eq (const void *of1, const void *of2)
22883 {
22884   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22885   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22886
22887   return (! memcmp (type_node_1->signature, type_node_2->signature,
22888                     DWARF_TYPE_SIGNATURE_SIZE));
22889 }
22890
22891 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22892    to the location it would have been added, should we know its
22893    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22894    probably improve compactness of debug info, removing equivalent
22895    abbrevs, and hide any differences caused by deferring the
22896    computation of the assembler name, triggered by e.g. PCH.  */
22897
22898 static inline void
22899 move_linkage_attr (dw_die_ref die)
22900 {
22901   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22902   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22903
22904   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22905               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22906
22907   while (--ix > 0)
22908     {
22909       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22910
22911       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22912         break;
22913     }
22914
22915   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22916     {
22917       VEC_pop (dw_attr_node, die->die_attr);
22918       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22919     }
22920 }
22921
22922 /* Helper function for resolve_addr, attempt to resolve
22923    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22924    SYMBOL_REFs refer to variables emitted in the current CU.  */
22925
22926 static int
22927 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22928 {
22929   rtx rtl = *addr;
22930
22931   if (GET_CODE (rtl) == CONST_STRING)
22932     {
22933       size_t len = strlen (XSTR (rtl, 0)) + 1;
22934       tree t = build_string (len, XSTR (rtl, 0));
22935       tree tlen = build_int_cst (NULL_TREE, len - 1);
22936       TREE_TYPE (t)
22937         = build_array_type (char_type_node, build_index_type (tlen));
22938       rtl = lookup_constant_def (t);
22939       if (!rtl || !MEM_P (rtl))
22940         return 1;
22941       rtl = XEXP (rtl, 0);
22942       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22943       *addr = rtl;
22944       return 0;
22945     }
22946
22947   if (GET_CODE (rtl) == SYMBOL_REF
22948       && SYMBOL_REF_DECL (rtl))
22949     {
22950       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22951         {
22952           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22953             return 1;
22954         }
22955       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22956         return 1;
22957     }
22958
22959   if (GET_CODE (rtl) == CONST
22960       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22961     return 1;
22962
22963   return 0;
22964 }
22965
22966 /* Helper function for resolve_addr, handle one location
22967    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22968    the location list couldn't be resolved.  */
22969
22970 static bool
22971 resolve_addr_in_expr (dw_loc_descr_ref loc)
22972 {
22973   for (; loc; loc = loc->dw_loc_next)
22974     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22975          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22976         || (loc->dw_loc_opc == DW_OP_implicit_value
22977             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22978             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22979       return false;
22980     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
22981              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22982       {
22983         dw_die_ref ref
22984           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22985         if (ref == NULL)
22986           return false;
22987         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22988         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22989         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22990       }
22991   return true;
22992 }
22993
22994 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22995    an address in .rodata section if the string literal is emitted there,
22996    or remove the containing location list or replace DW_AT_const_value
22997    with DW_AT_location and empty location expression, if it isn't found
22998    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22999    to something that has been emitted in the current CU.  */
23000
23001 static void
23002 resolve_addr (dw_die_ref die)
23003 {
23004   dw_die_ref c;
23005   dw_attr_ref a;
23006   dw_loc_list_ref *curr, *start, loc;
23007   unsigned ix;
23008
23009   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23010     switch (AT_class (a))
23011       {
23012       case dw_val_class_loc_list:
23013         start = curr = AT_loc_list_ptr (a);
23014         loc = *curr;
23015         gcc_assert (loc);
23016         /* The same list can be referenced more than once.  See if we have
23017            already recorded the result from a previous pass.  */
23018         if (loc->replaced)
23019           *curr = loc->dw_loc_next;
23020         else if (!loc->resolved_addr)
23021           {
23022             /* As things stand, we do not expect or allow one die to
23023                reference a suffix of another die's location list chain.
23024                References must be identical or completely separate.
23025                There is therefore no need to cache the result of this
23026                pass on any list other than the first; doing so
23027                would lead to unnecessary writes.  */
23028             while (*curr)
23029               {
23030                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23031                 if (!resolve_addr_in_expr ((*curr)->expr))
23032                   {
23033                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23034                     if (next && (*curr)->ll_symbol)
23035                       {
23036                         gcc_assert (!next->ll_symbol);
23037                         next->ll_symbol = (*curr)->ll_symbol;
23038                       }
23039                     *curr = next;
23040                   }
23041                 else
23042                   curr = &(*curr)->dw_loc_next;
23043               }
23044             if (loc == *start)
23045               loc->resolved_addr = 1;
23046             else
23047               {
23048                 loc->replaced = 1;
23049                 loc->dw_loc_next = *start;
23050               }
23051           }
23052         if (!*start)
23053           {
23054             remove_AT (die, a->dw_attr);
23055             ix--;
23056           }
23057         break;
23058       case dw_val_class_loc:
23059         if (!resolve_addr_in_expr (AT_loc (a)))
23060           {
23061             remove_AT (die, a->dw_attr);
23062             ix--;
23063           }
23064         break;
23065       case dw_val_class_addr:
23066         if (a->dw_attr == DW_AT_const_value
23067             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23068           {
23069             remove_AT (die, a->dw_attr);
23070             ix--;
23071           }
23072         if (die->die_tag == DW_TAG_GNU_call_site
23073             && a->dw_attr == DW_AT_abstract_origin)
23074           {
23075             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23076             dw_die_ref tdie = lookup_decl_die (tdecl);
23077             if (tdie == NULL && DECL_EXTERNAL (tdecl))
23078               {
23079                 force_decl_die (tdecl);
23080                 tdie = lookup_decl_die (tdecl);
23081               }
23082             if (tdie)
23083               {
23084                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23085                 a->dw_attr_val.v.val_die_ref.die = tdie;
23086                 a->dw_attr_val.v.val_die_ref.external = 0;
23087               }
23088             else
23089               {
23090                 remove_AT (die, a->dw_attr);
23091                 ix--;
23092               }
23093           }
23094         break;
23095       default:
23096         break;
23097       }
23098
23099   FOR_EACH_CHILD (die, c, resolve_addr (c));
23100 }
23101 \f
23102 /* Helper routines for optimize_location_lists.
23103    This pass tries to share identical local lists in .debug_loc
23104    section.  */
23105
23106 /* Iteratively hash operands of LOC opcode.  */
23107
23108 static inline hashval_t
23109 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23110 {
23111   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23112   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23113
23114   switch (loc->dw_loc_opc)
23115     {
23116     case DW_OP_const4u:
23117     case DW_OP_const8u:
23118       if (loc->dtprel)
23119         goto hash_addr;
23120       /* FALLTHRU */
23121     case DW_OP_const1u:
23122     case DW_OP_const1s:
23123     case DW_OP_const2u:
23124     case DW_OP_const2s:
23125     case DW_OP_const4s:
23126     case DW_OP_const8s:
23127     case DW_OP_constu:
23128     case DW_OP_consts:
23129     case DW_OP_pick:
23130     case DW_OP_plus_uconst:
23131     case DW_OP_breg0:
23132     case DW_OP_breg1:
23133     case DW_OP_breg2:
23134     case DW_OP_breg3:
23135     case DW_OP_breg4:
23136     case DW_OP_breg5:
23137     case DW_OP_breg6:
23138     case DW_OP_breg7:
23139     case DW_OP_breg8:
23140     case DW_OP_breg9:
23141     case DW_OP_breg10:
23142     case DW_OP_breg11:
23143     case DW_OP_breg12:
23144     case DW_OP_breg13:
23145     case DW_OP_breg14:
23146     case DW_OP_breg15:
23147     case DW_OP_breg16:
23148     case DW_OP_breg17:
23149     case DW_OP_breg18:
23150     case DW_OP_breg19:
23151     case DW_OP_breg20:
23152     case DW_OP_breg21:
23153     case DW_OP_breg22:
23154     case DW_OP_breg23:
23155     case DW_OP_breg24:
23156     case DW_OP_breg25:
23157     case DW_OP_breg26:
23158     case DW_OP_breg27:
23159     case DW_OP_breg28:
23160     case DW_OP_breg29:
23161     case DW_OP_breg30:
23162     case DW_OP_breg31:
23163     case DW_OP_regx:
23164     case DW_OP_fbreg:
23165     case DW_OP_piece:
23166     case DW_OP_deref_size:
23167     case DW_OP_xderef_size:
23168       hash = iterative_hash_object (val1->v.val_int, hash);
23169       break;
23170     case DW_OP_skip:
23171     case DW_OP_bra:
23172       {
23173         int offset;
23174
23175         gcc_assert (val1->val_class == dw_val_class_loc);
23176         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23177         hash = iterative_hash_object (offset, hash);
23178       }
23179       break;
23180     case DW_OP_implicit_value:
23181       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23182       switch (val2->val_class)
23183         {
23184         case dw_val_class_const:
23185           hash = iterative_hash_object (val2->v.val_int, hash);
23186           break;
23187         case dw_val_class_vec:
23188           {
23189             unsigned int elt_size = val2->v.val_vec.elt_size;
23190             unsigned int len = val2->v.val_vec.length;
23191
23192             hash = iterative_hash_object (elt_size, hash);
23193             hash = iterative_hash_object (len, hash);
23194             hash = iterative_hash (val2->v.val_vec.array,
23195                                    len * elt_size, hash);
23196           }
23197           break;
23198         case dw_val_class_const_double:
23199           hash = iterative_hash_object (val2->v.val_double.low, hash);
23200           hash = iterative_hash_object (val2->v.val_double.high, hash);
23201           break;
23202         case dw_val_class_addr:
23203           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23204           break;
23205         default:
23206           gcc_unreachable ();
23207         }
23208       break;
23209     case DW_OP_bregx:
23210     case DW_OP_bit_piece:
23211       hash = iterative_hash_object (val1->v.val_int, hash);
23212       hash = iterative_hash_object (val2->v.val_int, hash);
23213       break;
23214     case DW_OP_addr:
23215     hash_addr:
23216       if (loc->dtprel)
23217         {
23218           unsigned char dtprel = 0xd1;
23219           hash = iterative_hash_object (dtprel, hash);
23220         }
23221       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23222       break;
23223     case DW_OP_GNU_implicit_pointer:
23224       hash = iterative_hash_object (val2->v.val_int, hash);
23225       break;
23226     case DW_OP_GNU_entry_value:
23227       hash = hash_loc_operands (val1->v.val_loc, hash);
23228       break;
23229
23230     default:
23231       /* Other codes have no operands.  */
23232       break;
23233     }
23234   return hash;
23235 }
23236
23237 /* Iteratively hash the whole DWARF location expression LOC.  */
23238
23239 static inline hashval_t
23240 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23241 {
23242   dw_loc_descr_ref l;
23243   bool sizes_computed = false;
23244   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23245   size_of_locs (loc);
23246
23247   for (l = loc; l != NULL; l = l->dw_loc_next)
23248     {
23249       enum dwarf_location_atom opc = l->dw_loc_opc;
23250       hash = iterative_hash_object (opc, hash);
23251       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23252         {
23253           size_of_locs (loc);
23254           sizes_computed = true;
23255         }
23256       hash = hash_loc_operands (l, hash);
23257     }
23258   return hash;
23259 }
23260
23261 /* Compute hash of the whole location list LIST_HEAD.  */
23262
23263 static inline void
23264 hash_loc_list (dw_loc_list_ref list_head)
23265 {
23266   dw_loc_list_ref curr = list_head;
23267   hashval_t hash = 0;
23268
23269   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23270     {
23271       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23272       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23273       if (curr->section)
23274         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23275                                hash);
23276       hash = hash_locs (curr->expr, hash);
23277     }
23278   list_head->hash = hash;
23279 }
23280
23281 /* Return true if X and Y opcodes have the same operands.  */
23282
23283 static inline bool
23284 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23285 {
23286   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23287   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23288   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23289   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23290
23291   switch (x->dw_loc_opc)
23292     {
23293     case DW_OP_const4u:
23294     case DW_OP_const8u:
23295       if (x->dtprel)
23296         goto hash_addr;
23297       /* FALLTHRU */
23298     case DW_OP_const1u:
23299     case DW_OP_const1s:
23300     case DW_OP_const2u:
23301     case DW_OP_const2s:
23302     case DW_OP_const4s:
23303     case DW_OP_const8s:
23304     case DW_OP_constu:
23305     case DW_OP_consts:
23306     case DW_OP_pick:
23307     case DW_OP_plus_uconst:
23308     case DW_OP_breg0:
23309     case DW_OP_breg1:
23310     case DW_OP_breg2:
23311     case DW_OP_breg3:
23312     case DW_OP_breg4:
23313     case DW_OP_breg5:
23314     case DW_OP_breg6:
23315     case DW_OP_breg7:
23316     case DW_OP_breg8:
23317     case DW_OP_breg9:
23318     case DW_OP_breg10:
23319     case DW_OP_breg11:
23320     case DW_OP_breg12:
23321     case DW_OP_breg13:
23322     case DW_OP_breg14:
23323     case DW_OP_breg15:
23324     case DW_OP_breg16:
23325     case DW_OP_breg17:
23326     case DW_OP_breg18:
23327     case DW_OP_breg19:
23328     case DW_OP_breg20:
23329     case DW_OP_breg21:
23330     case DW_OP_breg22:
23331     case DW_OP_breg23:
23332     case DW_OP_breg24:
23333     case DW_OP_breg25:
23334     case DW_OP_breg26:
23335     case DW_OP_breg27:
23336     case DW_OP_breg28:
23337     case DW_OP_breg29:
23338     case DW_OP_breg30:
23339     case DW_OP_breg31:
23340     case DW_OP_regx:
23341     case DW_OP_fbreg:
23342     case DW_OP_piece:
23343     case DW_OP_deref_size:
23344     case DW_OP_xderef_size:
23345       return valx1->v.val_int == valy1->v.val_int;
23346     case DW_OP_skip:
23347     case DW_OP_bra:
23348       gcc_assert (valx1->val_class == dw_val_class_loc
23349                   && valy1->val_class == dw_val_class_loc
23350                   && x->dw_loc_addr == y->dw_loc_addr);
23351       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23352     case DW_OP_implicit_value:
23353       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23354           || valx2->val_class != valy2->val_class)
23355         return false;
23356       switch (valx2->val_class)
23357         {
23358         case dw_val_class_const:
23359           return valx2->v.val_int == valy2->v.val_int;
23360         case dw_val_class_vec:
23361           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23362                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23363                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23364                             valx2->v.val_vec.elt_size
23365                             * valx2->v.val_vec.length) == 0;
23366         case dw_val_class_const_double:
23367           return valx2->v.val_double.low == valy2->v.val_double.low
23368                  && valx2->v.val_double.high == valy2->v.val_double.high;
23369         case dw_val_class_addr:
23370           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23371         default:
23372           gcc_unreachable ();
23373         }
23374     case DW_OP_bregx:
23375     case DW_OP_bit_piece:
23376       return valx1->v.val_int == valy1->v.val_int
23377              && valx2->v.val_int == valy2->v.val_int;
23378     case DW_OP_addr:
23379     hash_addr:
23380       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23381     case DW_OP_GNU_implicit_pointer:
23382       return valx1->val_class == dw_val_class_die_ref
23383              && valx1->val_class == valy1->val_class
23384              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23385              && valx2->v.val_int == valy2->v.val_int;
23386     case DW_OP_GNU_entry_value:
23387       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23388     default:
23389       /* Other codes have no operands.  */
23390       return true;
23391     }
23392 }
23393
23394 /* Return true if DWARF location expressions X and Y are the same.  */
23395
23396 static inline bool
23397 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23398 {
23399   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23400     if (x->dw_loc_opc != y->dw_loc_opc
23401         || x->dtprel != y->dtprel
23402         || !compare_loc_operands (x, y))
23403       break;
23404   return x == NULL && y == NULL;
23405 }
23406
23407 /* Return precomputed hash of location list X.  */
23408
23409 static hashval_t
23410 loc_list_hash (const void *x)
23411 {
23412   return ((const struct dw_loc_list_struct *) x)->hash;
23413 }
23414
23415 /* Return 1 if location lists X and Y are the same.  */
23416
23417 static int
23418 loc_list_eq (const void *x, const void *y)
23419 {
23420   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23421   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23422   if (a == b)
23423     return 1;
23424   if (a->hash != b->hash)
23425     return 0;
23426   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23427     if (strcmp (a->begin, b->begin) != 0
23428         || strcmp (a->end, b->end) != 0
23429         || (a->section == NULL) != (b->section == NULL)
23430         || (a->section && strcmp (a->section, b->section) != 0)
23431         || !compare_locs (a->expr, b->expr))
23432       break;
23433   return a == NULL && b == NULL;
23434 }
23435
23436 /* Recursively optimize location lists referenced from DIE
23437    children and share them whenever possible.  */
23438
23439 static void
23440 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23441 {
23442   dw_die_ref c;
23443   dw_attr_ref a;
23444   unsigned ix;
23445   void **slot;
23446
23447   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23448     if (AT_class (a) == dw_val_class_loc_list)
23449       {
23450         dw_loc_list_ref list = AT_loc_list (a);
23451         /* TODO: perform some optimizations here, before hashing
23452            it and storing into the hash table.  */
23453         hash_loc_list (list);
23454         slot = htab_find_slot_with_hash (htab, list, list->hash,
23455                                          INSERT);
23456         if (*slot == NULL)
23457           *slot = (void *) list;
23458         else
23459           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23460       }
23461
23462   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23463 }
23464
23465 /* Optimize location lists referenced from DIE
23466    children and share them whenever possible.  */
23467
23468 static void
23469 optimize_location_lists (dw_die_ref die)
23470 {
23471   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23472   optimize_location_lists_1 (die, htab);
23473   htab_delete (htab);
23474 }
23475 \f
23476 /* Output stuff that dwarf requires at the end of every file,
23477    and generate the DWARF-2 debugging info.  */
23478
23479 static void
23480 dwarf2out_finish (const char *filename)
23481 {
23482   limbo_die_node *node, *next_node;
23483   comdat_type_node *ctnode;
23484   htab_t comdat_type_table;
23485   unsigned int i;
23486
23487   gen_scheduled_generic_parms_dies ();
23488   gen_remaining_tmpl_value_param_die_attribute ();
23489
23490   /* Add the name for the main input file now.  We delayed this from
23491      dwarf2out_init to avoid complications with PCH.  */
23492   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23493   if (!IS_ABSOLUTE_PATH (filename))
23494     add_comp_dir_attribute (comp_unit_die ());
23495   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23496     {
23497       bool p = false;
23498       htab_traverse (file_table, file_table_relative_p, &p);
23499       if (p)
23500         add_comp_dir_attribute (comp_unit_die ());
23501     }
23502
23503   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23504     {
23505       add_location_or_const_value_attribute (
23506         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23507         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23508         false,
23509         DW_AT_location);
23510     }
23511
23512   /* Traverse the limbo die list, and add parent/child links.  The only
23513      dies without parents that should be here are concrete instances of
23514      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23515      For concrete instances, we can get the parent die from the abstract
23516      instance.  */
23517   for (node = limbo_die_list; node; node = next_node)
23518     {
23519       dw_die_ref die = node->die;
23520       next_node = node->next;
23521
23522       if (die->die_parent == NULL)
23523         {
23524           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23525
23526           if (origin)
23527             add_child_die (origin->die_parent, die);
23528           else if (is_cu_die (die))
23529             ;
23530           else if (seen_error ())
23531             /* It's OK to be confused by errors in the input.  */
23532             add_child_die (comp_unit_die (), die);
23533           else
23534             {
23535               /* In certain situations, the lexical block containing a
23536                  nested function can be optimized away, which results
23537                  in the nested function die being orphaned.  Likewise
23538                  with the return type of that nested function.  Force
23539                  this to be a child of the containing function.
23540
23541                  It may happen that even the containing function got fully
23542                  inlined and optimized out.  In that case we are lost and
23543                  assign the empty child.  This should not be big issue as
23544                  the function is likely unreachable too.  */
23545               tree context = NULL_TREE;
23546
23547               gcc_assert (node->created_for);
23548
23549               if (DECL_P (node->created_for))
23550                 context = DECL_CONTEXT (node->created_for);
23551               else if (TYPE_P (node->created_for))
23552                 context = TYPE_CONTEXT (node->created_for);
23553
23554               gcc_assert (context
23555                           && (TREE_CODE (context) == FUNCTION_DECL
23556                               || TREE_CODE (context) == NAMESPACE_DECL));
23557
23558               origin = lookup_decl_die (context);
23559               if (origin)
23560                 add_child_die (origin, die);
23561               else
23562                 add_child_die (comp_unit_die (), die);
23563             }
23564         }
23565     }
23566
23567   limbo_die_list = NULL;
23568
23569   resolve_addr (comp_unit_die ());
23570
23571   for (node = deferred_asm_name; node; node = node->next)
23572     {
23573       tree decl = node->created_for;
23574       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23575         {
23576           add_linkage_attr (node->die, decl);
23577           move_linkage_attr (node->die);
23578         }
23579     }
23580
23581   deferred_asm_name = NULL;
23582
23583   /* Walk through the list of incomplete types again, trying once more to
23584      emit full debugging info for them.  */
23585   retry_incomplete_types ();
23586
23587   if (flag_eliminate_unused_debug_types)
23588     prune_unused_types ();
23589
23590   /* Generate separate CUs for each of the include files we've seen.
23591      They will go into limbo_die_list.  */
23592   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23593     break_out_includes (comp_unit_die ());
23594
23595   /* Generate separate COMDAT sections for type DIEs. */
23596   if (dwarf_version >= 4)
23597     {
23598       break_out_comdat_types (comp_unit_die ());
23599
23600       /* Each new type_unit DIE was added to the limbo die list when created.
23601          Since these have all been added to comdat_type_list, clear the
23602          limbo die list.  */
23603       limbo_die_list = NULL;
23604
23605       /* For each new comdat type unit, copy declarations for incomplete
23606          types to make the new unit self-contained (i.e., no direct
23607          references to the main compile unit).  */
23608       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23609         copy_decls_for_unworthy_types (ctnode->root_die);
23610       copy_decls_for_unworthy_types (comp_unit_die ());
23611
23612       /* In the process of copying declarations from one unit to another,
23613          we may have left some declarations behind that are no longer
23614          referenced.  Prune them.  */
23615       prune_unused_types ();
23616     }
23617
23618   /* Traverse the DIE's and add add sibling attributes to those DIE's
23619      that have children.  */
23620   add_sibling_attributes (comp_unit_die ());
23621   for (node = limbo_die_list; node; node = node->next)
23622     add_sibling_attributes (node->die);
23623   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23624     add_sibling_attributes (ctnode->root_die);
23625
23626   /* Output a terminator label for the .text section.  */
23627   switch_to_section (text_section);
23628   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23629   if (cold_text_section)
23630     {
23631       switch_to_section (cold_text_section);
23632       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23633     }
23634
23635   /* We can only use the low/high_pc attributes if all of the code was
23636      in .text.  */
23637   if (!have_multiple_function_sections 
23638       || (dwarf_version < 3 && dwarf_strict))
23639     {
23640       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23641       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23642     }
23643
23644   else
23645     {
23646       unsigned fde_idx = 0;
23647       bool range_list_added = false;
23648
23649       /* We need to give .debug_loc and .debug_ranges an appropriate
23650          "base address".  Use zero so that these addresses become
23651          absolute.  Historically, we've emitted the unexpected
23652          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23653          Emit both to give time for other tools to adapt.  */
23654       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23655       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23656
23657       if (text_section_used)
23658         add_ranges_by_labels (comp_unit_die (), text_section_label,
23659                               text_end_label, &range_list_added);
23660       if (cold_text_section_used)
23661         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23662                               cold_end_label, &range_list_added);
23663
23664       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23665         {
23666           dw_fde_ref fde = &fde_table[fde_idx];
23667
23668           if (!fde->in_std_section)
23669             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23670                                   fde->dw_fde_end, &range_list_added);
23671           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23672             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
23673                                   fde->dw_fde_second_end, &range_list_added);
23674         }
23675
23676       if (range_list_added)
23677         add_ranges (NULL);
23678     }
23679
23680   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23681     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23682                     debug_line_section_label);
23683
23684   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23685     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23686
23687   if (have_location_lists)
23688     optimize_location_lists (comp_unit_die ());
23689
23690   /* Output all of the compilation units.  We put the main one last so that
23691      the offsets are available to output_pubnames.  */
23692   for (node = limbo_die_list; node; node = node->next)
23693     output_comp_unit (node->die, 0);
23694
23695   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23696   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23697     {
23698       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23699
23700       /* Don't output duplicate types.  */
23701       if (*slot != HTAB_EMPTY_ENTRY)
23702         continue;
23703
23704       /* Add a pointer to the line table for the main compilation unit
23705          so that the debugger can make sense of DW_AT_decl_file
23706          attributes.  */
23707       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23708         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23709                         debug_line_section_label);
23710
23711       output_comdat_type_unit (ctnode);
23712       *slot = ctnode;
23713     }
23714   htab_delete (comdat_type_table);
23715
23716   /* Output the main compilation unit if non-empty or if .debug_macinfo
23717      will be emitted.  */
23718   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23719
23720   /* Output the abbreviation table.  */
23721   switch_to_section (debug_abbrev_section);
23722   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23723   output_abbrev_section ();
23724
23725   /* Output location list section if necessary.  */
23726   if (have_location_lists)
23727     {
23728       /* Output the location lists info.  */
23729       switch_to_section (debug_loc_section);
23730       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23731                                    DEBUG_LOC_SECTION_LABEL, 0);
23732       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23733       output_location_lists (comp_unit_die ());
23734     }
23735
23736   /* Output public names table if necessary.  */
23737   if (!VEC_empty (pubname_entry, pubname_table))
23738     {
23739       gcc_assert (info_section_emitted);
23740       switch_to_section (debug_pubnames_section);
23741       output_pubnames (pubname_table);
23742     }
23743
23744   /* Output public types table if necessary.  */
23745   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23746      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23747      simply won't look for the section.  */
23748   if (!VEC_empty (pubname_entry, pubtype_table))
23749     {
23750       bool empty = false;
23751       
23752       if (flag_eliminate_unused_debug_types)
23753         {
23754           /* The pubtypes table might be emptied by pruning unused items.  */
23755           unsigned i;
23756           pubname_ref p;
23757           empty = true;
23758           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23759             if (p->die->die_offset != 0)
23760               {
23761                 empty = false;
23762                 break;
23763               }
23764         }
23765       if (!empty)
23766         {
23767           gcc_assert (info_section_emitted);
23768           switch_to_section (debug_pubtypes_section);
23769           output_pubnames (pubtype_table);
23770         }
23771     }
23772
23773   /* Output the address range information.  We only put functions in the
23774      arange table, so don't write it out if we don't have any.  */
23775   if (info_section_emitted)
23776     {
23777       unsigned long aranges_length = size_of_aranges ();
23778
23779       /* Empty .debug_aranges would contain just header and
23780          terminating 0,0.  */
23781       if (aranges_length
23782           != (unsigned long) (DWARF_ARANGES_HEADER_SIZE
23783                               + 2 * DWARF2_ADDR_SIZE))
23784         {
23785           switch_to_section (debug_aranges_section);
23786           output_aranges (aranges_length);
23787         }
23788     }
23789
23790   /* Output ranges section if necessary.  */
23791   if (ranges_table_in_use)
23792     {
23793       switch_to_section (debug_ranges_section);
23794       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23795       output_ranges ();
23796     }
23797
23798   /* Output the source line correspondence table.  We must do this
23799      even if there is no line information.  Otherwise, on an empty
23800      translation unit, we will generate a present, but empty,
23801      .debug_info section.  IRIX 6.5 `nm' will then complain when
23802      examining the file.  This is done late so that any filenames
23803      used by the debug_info section are marked as 'used'.  */
23804   switch_to_section (debug_line_section);
23805   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23806   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23807     output_line_info ();
23808
23809   /* Have to end the macro section.  */
23810   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23811     {
23812       switch_to_section (debug_macinfo_section);
23813       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23814       if (!VEC_empty (macinfo_entry, macinfo_table))
23815         output_macinfo ();
23816       dw2_asm_output_data (1, 0, "End compilation unit");
23817     }
23818
23819   /* If we emitted any DW_FORM_strp form attribute, output the string
23820      table too.  */
23821   if (debug_str_hash)
23822     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23823 }
23824
23825 #include "gt-dwarf2out.h"