OSDN Git Service

* final.c (final_scan_insn): Handle NOTE_INSN_CALL_ARG_LOCATION.
[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_dcall_section;
232 static GTY(()) section *debug_vcall_section;
233 static GTY(()) section *debug_str_section;
234 static GTY(()) section *debug_ranges_section;
235 static GTY(()) section *debug_frame_section;
236
237 /* Personality decl of current unit.  Used only when assembler does not support
238    personality CFI.  */
239 static GTY(()) rtx current_unit_personality;
240
241 /* How to start an assembler comment.  */
242 #ifndef ASM_COMMENT_START
243 #define ASM_COMMENT_START ";#"
244 #endif
245
246 typedef struct dw_cfi_struct *dw_cfi_ref;
247 typedef struct dw_fde_struct *dw_fde_ref;
248 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
249
250 /* Call frames are described using a sequence of Call Frame
251    Information instructions.  The register number, offset
252    and address fields are provided as possible operands;
253    their use is selected by the opcode field.  */
254
255 enum dw_cfi_oprnd_type {
256   dw_cfi_oprnd_unused,
257   dw_cfi_oprnd_reg_num,
258   dw_cfi_oprnd_offset,
259   dw_cfi_oprnd_addr,
260   dw_cfi_oprnd_loc
261 };
262
263 typedef union GTY(()) dw_cfi_oprnd_struct {
264   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
265   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
266   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
267   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
268 }
269 dw_cfi_oprnd;
270
271 typedef struct GTY(()) dw_cfi_struct {
272   dw_cfi_ref dw_cfi_next;
273   enum dwarf_call_frame_info dw_cfi_opc;
274   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
275     dw_cfi_oprnd1;
276   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
277     dw_cfi_oprnd2;
278 }
279 dw_cfi_node;
280
281 /* This is how we define the location of the CFA. We use to handle it
282    as REG + OFFSET all the time,  but now it can be more complex.
283    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
284    Instead of passing around REG and OFFSET, we pass a copy
285    of this structure.  */
286 typedef struct cfa_loc {
287   HOST_WIDE_INT offset;
288   HOST_WIDE_INT base_offset;
289   unsigned int reg;
290   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
291   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
292 } dw_cfa_location;
293
294 /* All call frame descriptions (FDE's) in the GCC generated DWARF
295    refer to a single Common Information Entry (CIE), defined at
296    the beginning of the .debug_frame section.  This use of a single
297    CIE obviates the need to keep track of multiple CIE's
298    in the DWARF generation routines below.  */
299
300 typedef struct GTY(()) dw_fde_struct {
301   tree decl;
302   const char *dw_fde_begin;
303   const char *dw_fde_current_label;
304   const char *dw_fde_end;
305   const char *dw_fde_vms_end_prologue;
306   const char *dw_fde_vms_begin_epilogue;
307   const char *dw_fde_hot_section_label;
308   const char *dw_fde_hot_section_end_label;
309   const char *dw_fde_unlikely_section_label;
310   const char *dw_fde_unlikely_section_end_label;
311   dw_cfi_ref dw_fde_cfi;
312   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
313   HOST_WIDE_INT stack_realignment;
314   unsigned funcdef_number;
315   /* Dynamic realign argument pointer register.  */
316   unsigned int drap_reg;
317   /* Virtual dynamic realign argument pointer register.  */
318   unsigned int vdrap_reg;
319   /* These 3 flags are copied from rtl_data in function.h.  */
320   unsigned all_throwers_are_sibcalls : 1;
321   unsigned uses_eh_lsda : 1;
322   unsigned nothrow : 1;
323   /* Whether we did stack realign in this call frame.  */
324   unsigned stack_realign : 1;
325   /* Whether dynamic realign argument pointer register has been saved.  */
326   unsigned drap_reg_saved: 1;
327   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
328   unsigned in_std_section : 1;
329   /* True iff dw_fde_unlikely_section_label is in text_section or
330      cold_text_section.  */
331   unsigned cold_in_std_section : 1;
332   /* True iff switched sections.  */
333   unsigned dw_fde_switched_sections : 1;
334   /* True iff switching from cold to hot section.  */
335   unsigned dw_fde_switched_cold_to_hot : 1;
336 }
337 dw_fde_node;
338
339 /* Maximum size (in bytes) of an artificially generated label.  */
340 #define MAX_ARTIFICIAL_LABEL_BYTES      30
341
342 /* The size of addresses as they appear in the Dwarf 2 data.
343    Some architectures use word addresses to refer to code locations,
344    but Dwarf 2 info always uses byte addresses.  On such machines,
345    Dwarf 2 addresses need to be larger than the architecture's
346    pointers.  */
347 #ifndef DWARF2_ADDR_SIZE
348 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
349 #endif
350
351 /* The size in bytes of a DWARF field indicating an offset or length
352    relative to a debug info section, specified to be 4 bytes in the
353    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
354    as PTR_SIZE.  */
355
356 #ifndef DWARF_OFFSET_SIZE
357 #define DWARF_OFFSET_SIZE 4
358 #endif
359
360 /* The size in bytes of a DWARF 4 type signature.  */
361
362 #ifndef DWARF_TYPE_SIGNATURE_SIZE
363 #define DWARF_TYPE_SIGNATURE_SIZE 8
364 #endif
365
366 /* According to the (draft) DWARF 3 specification, the initial length
367    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
368    bytes are 0xffffffff, followed by the length stored in the next 8
369    bytes.
370
371    However, the SGI/MIPS ABI uses an initial length which is equal to
372    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
373
374 #ifndef DWARF_INITIAL_LENGTH_SIZE
375 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
376 #endif
377
378 /* Round SIZE up to the nearest BOUNDARY.  */
379 #define DWARF_ROUND(SIZE,BOUNDARY) \
380   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
381
382 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
383 #ifndef DWARF_CIE_DATA_ALIGNMENT
384 #ifdef STACK_GROWS_DOWNWARD
385 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
386 #else
387 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
388 #endif
389 #endif
390
391 /* CIE identifier.  */
392 #if HOST_BITS_PER_WIDE_INT >= 64
393 #define DWARF_CIE_ID \
394   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
395 #else
396 #define DWARF_CIE_ID DW_CIE_ID
397 #endif
398
399 /* A pointer to the base of a table that contains frame description
400    information for each routine.  */
401 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
402
403 /* Number of elements currently allocated for fde_table.  */
404 static GTY(()) unsigned fde_table_allocated;
405
406 /* Number of elements in fde_table currently in use.  */
407 static GTY(()) unsigned fde_table_in_use;
408
409 /* Size (in elements) of increments by which we may expand the
410    fde_table.  */
411 #define FDE_TABLE_INCREMENT 256
412
413 /* Get the current fde_table entry we should use.  */
414
415 static inline dw_fde_ref
416 current_fde (void)
417 {
418   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
419 }
420
421 /* A list of call frame insns for the CIE.  */
422 static GTY(()) dw_cfi_ref cie_cfi_head;
423
424 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
425    attribute that accelerates the lookup of the FDE associated
426    with the subprogram.  This variable holds the table index of the FDE
427    associated with the current function (body) definition.  */
428 static unsigned current_funcdef_fde;
429
430 struct GTY(()) indirect_string_node {
431   const char *str;
432   unsigned int refcount;
433   enum dwarf_form form;
434   char *label;
435 };
436
437 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
438
439 /* True if the compilation unit has location entries that reference
440    debug strings.  */
441 static GTY(()) bool debug_str_hash_forced = false;
442
443 static GTY(()) int dw2_string_counter;
444 static GTY(()) unsigned long dwarf2out_cfi_label_num;
445
446 /* True if the compilation unit places functions in more than one section.  */
447 static GTY(()) bool have_multiple_function_sections = false;
448
449 /* Whether the default text and cold text sections have been used at all.  */
450
451 static GTY(()) bool text_section_used = false;
452 static GTY(()) bool cold_text_section_used = false;
453
454 /* The default cold text section.  */
455 static GTY(()) section *cold_text_section;
456
457 /* Forward declarations for functions defined in this file.  */
458
459 static char *stripattributes (const char *);
460 static const char *dwarf_cfi_name (unsigned);
461 static dw_cfi_ref new_cfi (void);
462 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
463 static void add_fde_cfi (const char *, dw_cfi_ref);
464 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
465 static void lookup_cfa (dw_cfa_location *);
466 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
467 static void initial_return_save (rtx);
468 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
469                                           HOST_WIDE_INT);
470 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
471 static void output_cfi_directive (dw_cfi_ref);
472 static void output_call_frame_info (int);
473 static void dwarf2out_note_section_used (void);
474 static bool clobbers_queued_reg_save (const_rtx);
475 static void dwarf2out_frame_debug_expr (rtx, const char *);
476
477 /* Support for complex CFA locations.  */
478 static void output_cfa_loc (dw_cfi_ref, int);
479 static void output_cfa_loc_raw (dw_cfi_ref);
480 static void get_cfa_from_loc_descr (dw_cfa_location *,
481                                     struct dw_loc_descr_struct *);
482 static struct dw_loc_descr_struct *build_cfa_loc
483   (dw_cfa_location *, HOST_WIDE_INT);
484 static struct dw_loc_descr_struct *build_cfa_aligned_loc
485   (HOST_WIDE_INT, HOST_WIDE_INT);
486 static void def_cfa_1 (const char *, dw_cfa_location *);
487 static struct dw_loc_descr_struct *mem_loc_descriptor
488   (rtx, enum machine_mode mode, enum var_init_status);
489
490 /* How to start an assembler comment.  */
491 #ifndef ASM_COMMENT_START
492 #define ASM_COMMENT_START ";#"
493 #endif
494
495 /* Data and reference forms for relocatable data.  */
496 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
497 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
498
499 #ifndef DEBUG_FRAME_SECTION
500 #define DEBUG_FRAME_SECTION     ".debug_frame"
501 #endif
502
503 #ifndef FUNC_BEGIN_LABEL
504 #define FUNC_BEGIN_LABEL        "LFB"
505 #endif
506
507 #ifndef FUNC_END_LABEL
508 #define FUNC_END_LABEL          "LFE"
509 #endif
510
511 #ifndef PROLOGUE_END_LABEL
512 #define PROLOGUE_END_LABEL      "LPE"
513 #endif
514
515 #ifndef EPILOGUE_BEGIN_LABEL
516 #define EPILOGUE_BEGIN_LABEL    "LEB"
517 #endif
518
519 #ifndef FRAME_BEGIN_LABEL
520 #define FRAME_BEGIN_LABEL       "Lframe"
521 #endif
522 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
523 #define CIE_END_LABEL           "LECIE"
524 #define FDE_LABEL               "LSFDE"
525 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
526 #define FDE_END_LABEL           "LEFDE"
527 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
528 #define LINE_NUMBER_END_LABEL   "LELT"
529 #define LN_PROLOG_AS_LABEL      "LASLTP"
530 #define LN_PROLOG_END_LABEL     "LELTP"
531 #define DIE_LABEL_PREFIX        "DW"
532
533 /* The DWARF 2 CFA column which tracks the return address.  Normally this
534    is the column for PC, or the first column after all of the hard
535    registers.  */
536 #ifndef DWARF_FRAME_RETURN_COLUMN
537 #ifdef PC_REGNUM
538 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
539 #else
540 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
541 #endif
542 #endif
543
544 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
545    default, we just provide columns for all registers.  */
546 #ifndef DWARF_FRAME_REGNUM
547 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
548 #endif
549 \f
550 /* Match the base name of a file to the base name of a compilation unit. */
551
552 static int
553 matches_main_base (const char *path)
554 {
555   /* Cache the last query. */
556   static const char *last_path = NULL;
557   static int last_match = 0;
558   if (path != last_path)
559     {
560       const char *base;
561       int length = base_of_path (path, &base);
562       last_path = path;
563       last_match = (length == main_input_baselength
564                     && memcmp (base, main_input_basename, length) == 0);
565     }
566   return last_match;
567 }
568
569 #ifdef DEBUG_DEBUG_STRUCT
570
571 static int
572 dump_struct_debug (tree type, enum debug_info_usage usage,
573                    enum debug_struct_file criterion, int generic,
574                    int matches, int result)
575 {
576   /* Find the type name. */
577   tree type_decl = TYPE_STUB_DECL (type);
578   tree t = type_decl;
579   const char *name = 0;
580   if (TREE_CODE (t) == TYPE_DECL)
581     t = DECL_NAME (t);
582   if (t)
583     name = IDENTIFIER_POINTER (t);
584
585   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
586            criterion,
587            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
588            matches ? "bas" : "hdr",
589            generic ? "gen" : "ord",
590            usage == DINFO_USAGE_DFN ? ";" :
591              usage == DINFO_USAGE_DIR_USE ? "." : "*",
592            result,
593            (void*) type_decl, name);
594   return result;
595 }
596 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
597   dump_struct_debug (type, usage, criterion, generic, matches, result)
598
599 #else
600
601 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
602   (result)
603
604 #endif
605
606 static bool
607 should_emit_struct_debug (tree type, enum debug_info_usage usage)
608 {
609   enum debug_struct_file criterion;
610   tree type_decl;
611   bool generic = lang_hooks.types.generic_p (type);
612
613   if (generic)
614     criterion = debug_struct_generic[usage];
615   else
616     criterion = debug_struct_ordinary[usage];
617
618   if (criterion == DINFO_STRUCT_FILE_NONE)
619     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
620   if (criterion == DINFO_STRUCT_FILE_ANY)
621     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
622
623   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
624
625   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
626     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
627
628   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
629     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
630   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
631 }
632 \f
633 /* Hook used by __throw.  */
634
635 rtx
636 expand_builtin_dwarf_sp_column (void)
637 {
638   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
639   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
640 }
641
642 /* Return a pointer to a copy of the section string name S with all
643    attributes stripped off, and an asterisk prepended (for assemble_name).  */
644
645 static inline char *
646 stripattributes (const char *s)
647 {
648   char *stripped = XNEWVEC (char, strlen (s) + 2);
649   char *p = stripped;
650
651   *p++ = '*';
652
653   while (*s && *s != ',')
654     *p++ = *s++;
655
656   *p = '\0';
657   return stripped;
658 }
659
660 /* MEM is a memory reference for the register size table, each element of
661    which has mode MODE.  Initialize column C as a return address column.  */
662
663 static void
664 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
665 {
666   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
667   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
668   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
669 }
670
671 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
672
673 static inline HOST_WIDE_INT
674 div_data_align (HOST_WIDE_INT off)
675 {
676   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
677   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
678   return r;
679 }
680
681 /* Return true if we need a signed version of a given opcode
682    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
683
684 static inline bool
685 need_data_align_sf_opcode (HOST_WIDE_INT off)
686 {
687   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
688 }
689
690 /* Generate code to initialize the register size table.  */
691
692 void
693 expand_builtin_init_dwarf_reg_sizes (tree address)
694 {
695   unsigned int i;
696   enum machine_mode mode = TYPE_MODE (char_type_node);
697   rtx addr = expand_normal (address);
698   rtx mem = gen_rtx_MEM (BLKmode, addr);
699   bool wrote_return_column = false;
700
701   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
702     {
703       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
704
705       if (rnum < DWARF_FRAME_REGISTERS)
706         {
707           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
708           enum machine_mode save_mode = reg_raw_mode[i];
709           HOST_WIDE_INT size;
710
711           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
712             save_mode = choose_hard_reg_mode (i, 1, true);
713           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
714             {
715               if (save_mode == VOIDmode)
716                 continue;
717               wrote_return_column = true;
718             }
719           size = GET_MODE_SIZE (save_mode);
720           if (offset < 0)
721             continue;
722
723           emit_move_insn (adjust_address (mem, mode, offset),
724                           gen_int_mode (size, mode));
725         }
726     }
727
728   if (!wrote_return_column)
729     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
730
731 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
732   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
733 #endif
734
735   targetm.init_dwarf_reg_sizes_extra (address);
736 }
737
738 /* Convert a DWARF call frame info. operation to its string name */
739
740 static const char *
741 dwarf_cfi_name (unsigned int cfi_opc)
742 {
743   switch (cfi_opc)
744     {
745     case DW_CFA_advance_loc:
746       return "DW_CFA_advance_loc";
747     case DW_CFA_offset:
748       return "DW_CFA_offset";
749     case DW_CFA_restore:
750       return "DW_CFA_restore";
751     case DW_CFA_nop:
752       return "DW_CFA_nop";
753     case DW_CFA_set_loc:
754       return "DW_CFA_set_loc";
755     case DW_CFA_advance_loc1:
756       return "DW_CFA_advance_loc1";
757     case DW_CFA_advance_loc2:
758       return "DW_CFA_advance_loc2";
759     case DW_CFA_advance_loc4:
760       return "DW_CFA_advance_loc4";
761     case DW_CFA_offset_extended:
762       return "DW_CFA_offset_extended";
763     case DW_CFA_restore_extended:
764       return "DW_CFA_restore_extended";
765     case DW_CFA_undefined:
766       return "DW_CFA_undefined";
767     case DW_CFA_same_value:
768       return "DW_CFA_same_value";
769     case DW_CFA_register:
770       return "DW_CFA_register";
771     case DW_CFA_remember_state:
772       return "DW_CFA_remember_state";
773     case DW_CFA_restore_state:
774       return "DW_CFA_restore_state";
775     case DW_CFA_def_cfa:
776       return "DW_CFA_def_cfa";
777     case DW_CFA_def_cfa_register:
778       return "DW_CFA_def_cfa_register";
779     case DW_CFA_def_cfa_offset:
780       return "DW_CFA_def_cfa_offset";
781
782     /* DWARF 3 */
783     case DW_CFA_def_cfa_expression:
784       return "DW_CFA_def_cfa_expression";
785     case DW_CFA_expression:
786       return "DW_CFA_expression";
787     case DW_CFA_offset_extended_sf:
788       return "DW_CFA_offset_extended_sf";
789     case DW_CFA_def_cfa_sf:
790       return "DW_CFA_def_cfa_sf";
791     case DW_CFA_def_cfa_offset_sf:
792       return "DW_CFA_def_cfa_offset_sf";
793
794     /* SGI/MIPS specific */
795     case DW_CFA_MIPS_advance_loc8:
796       return "DW_CFA_MIPS_advance_loc8";
797
798     /* GNU extensions */
799     case DW_CFA_GNU_window_save:
800       return "DW_CFA_GNU_window_save";
801     case DW_CFA_GNU_args_size:
802       return "DW_CFA_GNU_args_size";
803     case DW_CFA_GNU_negative_offset_extended:
804       return "DW_CFA_GNU_negative_offset_extended";
805
806     default:
807       return "DW_CFA_<unknown>";
808     }
809 }
810
811 /* Return a pointer to a newly allocated Call Frame Instruction.  */
812
813 static inline dw_cfi_ref
814 new_cfi (void)
815 {
816   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
817
818   cfi->dw_cfi_next = NULL;
819   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
820   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
821
822   return cfi;
823 }
824
825 /* Add a Call Frame Instruction to list of instructions.  */
826
827 static inline void
828 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
829 {
830   dw_cfi_ref *p;
831   dw_fde_ref fde = current_fde ();
832
833   /* When DRAP is used, CFA is defined with an expression.  Redefine
834      CFA may lead to a different CFA value.   */
835   /* ??? Of course, this heuristic fails when we're annotating epilogues,
836      because of course we'll always want to redefine the CFA back to the
837      stack pointer on the way out.  Where should we move this check?  */
838   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
839     switch (cfi->dw_cfi_opc)
840       {
841         case DW_CFA_def_cfa_register:
842         case DW_CFA_def_cfa_offset:
843         case DW_CFA_def_cfa_offset_sf:
844         case DW_CFA_def_cfa:
845         case DW_CFA_def_cfa_sf:
846           gcc_unreachable ();
847
848         default:
849           break;
850       }
851
852   /* Find the end of the chain.  */
853   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
854     ;
855
856   *p = cfi;
857 }
858
859 /* Generate a new label for the CFI info to refer to.  FORCE is true
860    if a label needs to be output even when using .cfi_* directives.  */
861
862 char *
863 dwarf2out_cfi_label (bool force)
864 {
865   static char label[20];
866
867   if (!force && dwarf2out_do_cfi_asm ())
868     {
869       /* In this case, we will be emitting the asm directive instead of
870          the label, so just return a placeholder to keep the rest of the
871          interfaces happy.  */
872       strcpy (label, "<do not output>");
873     }
874   else
875     {
876       int num = dwarf2out_cfi_label_num++;
877       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
878       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
879     }
880
881   return label;
882 }
883
884 /* True if remember_state should be emitted before following CFI directive.  */
885 static bool emit_cfa_remember;
886
887 /* True if any CFI directives were emitted at the current insn.  */
888 static bool any_cfis_emitted;
889
890 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
891    or to the CIE if LABEL is NULL.  */
892
893 static void
894 add_fde_cfi (const char *label, dw_cfi_ref cfi)
895 {
896   dw_cfi_ref *list_head;
897
898   if (emit_cfa_remember)
899     {
900       dw_cfi_ref cfi_remember;
901
902       /* Emit the state save.  */
903       emit_cfa_remember = false;
904       cfi_remember = new_cfi ();
905       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
906       add_fde_cfi (label, cfi_remember);
907     }
908
909   list_head = &cie_cfi_head;
910
911   if (dwarf2out_do_cfi_asm ())
912     {
913       if (label)
914         {
915           dw_fde_ref fde = current_fde ();
916
917           gcc_assert (fde != NULL);
918
919           /* We still have to add the cfi to the list so that lookup_cfa
920              works later on.  When -g2 and above we even need to force
921              emitting of CFI labels and add to list a DW_CFA_set_loc for
922              convert_cfa_to_fb_loc_list purposes.  If we're generating
923              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
924              convert_cfa_to_fb_loc_list.  */
925           if (dwarf_version == 2
926               && debug_info_level > DINFO_LEVEL_TERSE
927               && (write_symbols == DWARF2_DEBUG
928                   || write_symbols == VMS_AND_DWARF2_DEBUG))
929             {
930               switch (cfi->dw_cfi_opc)
931                 {
932                 case DW_CFA_def_cfa_offset:
933                 case DW_CFA_def_cfa_offset_sf:
934                 case DW_CFA_def_cfa_register:
935                 case DW_CFA_def_cfa:
936                 case DW_CFA_def_cfa_sf:
937                 case DW_CFA_def_cfa_expression:
938                 case DW_CFA_restore_state:
939                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
940                     label = dwarf2out_cfi_label (true);
941
942                   if (fde->dw_fde_current_label == NULL
943                       || strcmp (label, fde->dw_fde_current_label) != 0)
944                     {
945                       dw_cfi_ref xcfi;
946
947                       label = xstrdup (label);
948
949                       /* Set the location counter to the new label.  */
950                       xcfi = new_cfi ();
951                       /* It doesn't metter whether DW_CFA_set_loc
952                          or DW_CFA_advance_loc4 is added here, those aren't
953                          emitted into assembly, only looked up by
954                          convert_cfa_to_fb_loc_list.  */
955                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
956                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
957                       add_cfi (&fde->dw_fde_cfi, xcfi);
958                       fde->dw_fde_current_label = label;
959                     }
960                   break;
961                 default:
962                   break;
963                 }
964             }
965
966           output_cfi_directive (cfi);
967
968           list_head = &fde->dw_fde_cfi;
969           any_cfis_emitted = true;
970         }
971       /* ??? If this is a CFI for the CIE, we don't emit.  This
972          assumes that the standard CIE contents that the assembler
973          uses matches the standard CIE contents that the compiler
974          uses.  This is probably a bad assumption.  I'm not quite
975          sure how to address this for now.  */
976     }
977   else if (label)
978     {
979       dw_fde_ref fde = current_fde ();
980
981       gcc_assert (fde != NULL);
982
983       if (*label == 0)
984         label = dwarf2out_cfi_label (false);
985
986       if (fde->dw_fde_current_label == NULL
987           || strcmp (label, fde->dw_fde_current_label) != 0)
988         {
989           dw_cfi_ref xcfi;
990
991           label = xstrdup (label);
992
993           /* Set the location counter to the new label.  */
994           xcfi = new_cfi ();
995           /* If we have a current label, advance from there, otherwise
996              set the location directly using set_loc.  */
997           xcfi->dw_cfi_opc = fde->dw_fde_current_label
998                              ? DW_CFA_advance_loc4
999                              : DW_CFA_set_loc;
1000           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
1001           add_cfi (&fde->dw_fde_cfi, xcfi);
1002
1003           fde->dw_fde_current_label = label;
1004         }
1005
1006       list_head = &fde->dw_fde_cfi;
1007       any_cfis_emitted = true;
1008     }
1009
1010   add_cfi (list_head, cfi);
1011 }
1012
1013 /* Subroutine of lookup_cfa.  */
1014
1015 static void
1016 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1017 {
1018   switch (cfi->dw_cfi_opc)
1019     {
1020     case DW_CFA_def_cfa_offset:
1021     case DW_CFA_def_cfa_offset_sf:
1022       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1023       break;
1024     case DW_CFA_def_cfa_register:
1025       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1026       break;
1027     case DW_CFA_def_cfa:
1028     case DW_CFA_def_cfa_sf:
1029       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1030       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1031       break;
1032     case DW_CFA_def_cfa_expression:
1033       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1034       break;
1035
1036     case DW_CFA_remember_state:
1037       gcc_assert (!remember->in_use);
1038       *remember = *loc;
1039       remember->in_use = 1;
1040       break;
1041     case DW_CFA_restore_state:
1042       gcc_assert (remember->in_use);
1043       *loc = *remember;
1044       remember->in_use = 0;
1045       break;
1046
1047     default:
1048       break;
1049     }
1050 }
1051
1052 /* Find the previous value for the CFA.  */
1053
1054 static void
1055 lookup_cfa (dw_cfa_location *loc)
1056 {
1057   dw_cfi_ref cfi;
1058   dw_fde_ref fde;
1059   dw_cfa_location remember;
1060
1061   memset (loc, 0, sizeof (*loc));
1062   loc->reg = INVALID_REGNUM;
1063   remember = *loc;
1064
1065   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
1066     lookup_cfa_1 (cfi, loc, &remember);
1067
1068   fde = current_fde ();
1069   if (fde)
1070     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
1071       lookup_cfa_1 (cfi, loc, &remember);
1072 }
1073
1074 /* The current rule for calculating the DWARF2 canonical frame address.  */
1075 static dw_cfa_location cfa;
1076
1077 /* The register used for saving registers to the stack, and its offset
1078    from the CFA.  */
1079 static dw_cfa_location cfa_store;
1080
1081 /* The current save location around an epilogue.  */
1082 static dw_cfa_location cfa_remember;
1083
1084 /* The running total of the size of arguments pushed onto the stack.  */
1085 static HOST_WIDE_INT args_size;
1086
1087 /* The last args_size we actually output.  */
1088 static HOST_WIDE_INT old_args_size;
1089
1090 /* Entry point to update the canonical frame address (CFA).
1091    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1092    calculated from REG+OFFSET.  */
1093
1094 void
1095 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1096 {
1097   dw_cfa_location loc;
1098   loc.indirect = 0;
1099   loc.base_offset = 0;
1100   loc.reg = reg;
1101   loc.offset = offset;
1102   def_cfa_1 (label, &loc);
1103 }
1104
1105 /* Determine if two dw_cfa_location structures define the same data.  */
1106
1107 static bool
1108 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1109 {
1110   return (loc1->reg == loc2->reg
1111           && loc1->offset == loc2->offset
1112           && loc1->indirect == loc2->indirect
1113           && (loc1->indirect == 0
1114               || loc1->base_offset == loc2->base_offset));
1115 }
1116
1117 /* This routine does the actual work.  The CFA is now calculated from
1118    the dw_cfa_location structure.  */
1119
1120 static void
1121 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1122 {
1123   dw_cfi_ref cfi;
1124   dw_cfa_location old_cfa, loc;
1125
1126   cfa = *loc_p;
1127   loc = *loc_p;
1128
1129   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1130     cfa_store.offset = loc.offset;
1131
1132   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1133   lookup_cfa (&old_cfa);
1134
1135   /* If nothing changed, no need to issue any call frame instructions.  */
1136   if (cfa_equal_p (&loc, &old_cfa))
1137     return;
1138
1139   cfi = new_cfi ();
1140
1141   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1142     {
1143       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1144          the CFA register did not change but the offset did.  The data
1145          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1146          in the assembler via the .cfi_def_cfa_offset directive.  */
1147       if (loc.offset < 0)
1148         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1149       else
1150         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1151       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1152     }
1153
1154 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1155   else if (loc.offset == old_cfa.offset
1156            && old_cfa.reg != INVALID_REGNUM
1157            && !loc.indirect
1158            && !old_cfa.indirect)
1159     {
1160       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1161          indicating the CFA register has changed to <register> but the
1162          offset has not changed.  */
1163       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1164       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1165     }
1166 #endif
1167
1168   else if (loc.indirect == 0)
1169     {
1170       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1171          indicating the CFA register has changed to <register> with
1172          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1173          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1174          directive.  */
1175       if (loc.offset < 0)
1176         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1177       else
1178         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1179       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1180       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1181     }
1182   else
1183     {
1184       /* Construct a DW_CFA_def_cfa_expression instruction to
1185          calculate the CFA using a full location expression since no
1186          register-offset pair is available.  */
1187       struct dw_loc_descr_struct *loc_list;
1188
1189       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1190       loc_list = build_cfa_loc (&loc, 0);
1191       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1192     }
1193
1194   add_fde_cfi (label, cfi);
1195 }
1196
1197 /* Add the CFI for saving a register.  REG is the CFA column number.
1198    LABEL is passed to add_fde_cfi.
1199    If SREG is -1, the register is saved at OFFSET from the CFA;
1200    otherwise it is saved in SREG.  */
1201
1202 static void
1203 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1204 {
1205   dw_cfi_ref cfi = new_cfi ();
1206   dw_fde_ref fde = current_fde ();
1207
1208   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1209
1210   /* When stack is aligned, store REG using DW_CFA_expression with
1211      FP.  */
1212   if (fde
1213       && fde->stack_realign
1214       && sreg == INVALID_REGNUM)
1215     {
1216       cfi->dw_cfi_opc = DW_CFA_expression;
1217       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1218       cfi->dw_cfi_oprnd2.dw_cfi_loc
1219         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1220     }
1221   else if (sreg == INVALID_REGNUM)
1222     {
1223       if (need_data_align_sf_opcode (offset))
1224         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1225       else if (reg & ~0x3f)
1226         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1227       else
1228         cfi->dw_cfi_opc = DW_CFA_offset;
1229       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1230     }
1231   else if (sreg == reg)
1232     cfi->dw_cfi_opc = DW_CFA_same_value;
1233   else
1234     {
1235       cfi->dw_cfi_opc = DW_CFA_register;
1236       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1237     }
1238
1239   add_fde_cfi (label, cfi);
1240 }
1241
1242 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1243    This CFI tells the unwinder that it needs to restore the window registers
1244    from the previous frame's window save area.
1245
1246    ??? Perhaps we should note in the CIE where windows are saved (instead of
1247    assuming 0(cfa)) and what registers are in the window.  */
1248
1249 void
1250 dwarf2out_window_save (const char *label)
1251 {
1252   dw_cfi_ref cfi = new_cfi ();
1253
1254   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1255   add_fde_cfi (label, cfi);
1256 }
1257
1258 /* Entry point for saving a register to the stack.  REG is the GCC register
1259    number.  LABEL and OFFSET are passed to reg_save.  */
1260
1261 void
1262 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1263 {
1264   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1265 }
1266
1267 /* Entry point for saving the return address in the stack.
1268    LABEL and OFFSET are passed to reg_save.  */
1269
1270 void
1271 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1272 {
1273   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1274 }
1275
1276 /* Entry point for saving the return address in a register.
1277    LABEL and SREG are passed to reg_save.  */
1278
1279 void
1280 dwarf2out_return_reg (const char *label, unsigned int sreg)
1281 {
1282   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1283 }
1284
1285 /* Record the initial position of the return address.  RTL is
1286    INCOMING_RETURN_ADDR_RTX.  */
1287
1288 static void
1289 initial_return_save (rtx rtl)
1290 {
1291   unsigned int reg = INVALID_REGNUM;
1292   HOST_WIDE_INT offset = 0;
1293
1294   switch (GET_CODE (rtl))
1295     {
1296     case REG:
1297       /* RA is in a register.  */
1298       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1299       break;
1300
1301     case MEM:
1302       /* RA is on the stack.  */
1303       rtl = XEXP (rtl, 0);
1304       switch (GET_CODE (rtl))
1305         {
1306         case REG:
1307           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1308           offset = 0;
1309           break;
1310
1311         case PLUS:
1312           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1313           offset = INTVAL (XEXP (rtl, 1));
1314           break;
1315
1316         case MINUS:
1317           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1318           offset = -INTVAL (XEXP (rtl, 1));
1319           break;
1320
1321         default:
1322           gcc_unreachable ();
1323         }
1324
1325       break;
1326
1327     case PLUS:
1328       /* The return address is at some offset from any value we can
1329          actually load.  For instance, on the SPARC it is in %i7+8. Just
1330          ignore the offset for now; it doesn't matter for unwinding frames.  */
1331       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1332       initial_return_save (XEXP (rtl, 0));
1333       return;
1334
1335     default:
1336       gcc_unreachable ();
1337     }
1338
1339   if (reg != DWARF_FRAME_RETURN_COLUMN)
1340     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1341 }
1342
1343 /* Given a SET, calculate the amount of stack adjustment it
1344    contains.  */
1345
1346 static HOST_WIDE_INT
1347 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1348                      HOST_WIDE_INT cur_offset)
1349 {
1350   const_rtx src = SET_SRC (pattern);
1351   const_rtx dest = SET_DEST (pattern);
1352   HOST_WIDE_INT offset = 0;
1353   enum rtx_code code;
1354
1355   if (dest == stack_pointer_rtx)
1356     {
1357       code = GET_CODE (src);
1358
1359       /* Assume (set (reg sp) (reg whatever)) sets args_size
1360          level to 0.  */
1361       if (code == REG && src != stack_pointer_rtx)
1362         {
1363           offset = -cur_args_size;
1364 #ifndef STACK_GROWS_DOWNWARD
1365           offset = -offset;
1366 #endif
1367           return offset - cur_offset;
1368         }
1369
1370       if (! (code == PLUS || code == MINUS)
1371           || XEXP (src, 0) != stack_pointer_rtx
1372           || !CONST_INT_P (XEXP (src, 1)))
1373         return 0;
1374
1375       /* (set (reg sp) (plus (reg sp) (const_int))) */
1376       offset = INTVAL (XEXP (src, 1));
1377       if (code == PLUS)
1378         offset = -offset;
1379       return offset;
1380     }
1381
1382   if (MEM_P (src) && !MEM_P (dest))
1383     dest = src;
1384   if (MEM_P (dest))
1385     {
1386       /* (set (mem (pre_dec (reg sp))) (foo)) */
1387       src = XEXP (dest, 0);
1388       code = GET_CODE (src);
1389
1390       switch (code)
1391         {
1392         case PRE_MODIFY:
1393         case POST_MODIFY:
1394           if (XEXP (src, 0) == stack_pointer_rtx)
1395             {
1396               rtx val = XEXP (XEXP (src, 1), 1);
1397               /* We handle only adjustments by constant amount.  */
1398               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1399                           && CONST_INT_P (val));
1400               offset = -INTVAL (val);
1401               break;
1402             }
1403           return 0;
1404
1405         case PRE_DEC:
1406         case POST_DEC:
1407           if (XEXP (src, 0) == stack_pointer_rtx)
1408             {
1409               offset = GET_MODE_SIZE (GET_MODE (dest));
1410               break;
1411             }
1412           return 0;
1413
1414         case PRE_INC:
1415         case POST_INC:
1416           if (XEXP (src, 0) == stack_pointer_rtx)
1417             {
1418               offset = -GET_MODE_SIZE (GET_MODE (dest));
1419               break;
1420             }
1421           return 0;
1422
1423         default:
1424           return 0;
1425         }
1426     }
1427   else
1428     return 0;
1429
1430   return offset;
1431 }
1432
1433 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1434    indexed by INSN_UID.  */
1435
1436 static HOST_WIDE_INT *barrier_args_size;
1437
1438 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1439
1440 static HOST_WIDE_INT
1441 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1442                              VEC (rtx, heap) **next)
1443 {
1444   HOST_WIDE_INT offset = 0;
1445   int i;
1446
1447   if (! RTX_FRAME_RELATED_P (insn))
1448     {
1449       if (prologue_epilogue_contains (insn))
1450         /* Nothing */;
1451       else if (GET_CODE (PATTERN (insn)) == SET)
1452         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1453       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1454                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1455         {
1456           /* There may be stack adjustments inside compound insns.  Search
1457              for them.  */
1458           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1459             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1460               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1461                                              cur_args_size, offset);
1462         }
1463     }
1464   else
1465     {
1466       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1467
1468       if (expr)
1469         {
1470           expr = XEXP (expr, 0);
1471           if (GET_CODE (expr) == PARALLEL
1472               || GET_CODE (expr) == SEQUENCE)
1473             for (i = 1; i < XVECLEN (expr, 0); i++)
1474               {
1475                 rtx elem = XVECEXP (expr, 0, i);
1476
1477                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1478                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1479               }
1480         }
1481     }
1482
1483 #ifndef STACK_GROWS_DOWNWARD
1484   offset = -offset;
1485 #endif
1486
1487   cur_args_size += offset;
1488   if (cur_args_size < 0)
1489     cur_args_size = 0;
1490
1491   if (JUMP_P (insn))
1492     {
1493       rtx dest = JUMP_LABEL (insn);
1494
1495       if (dest)
1496         {
1497           if (barrier_args_size [INSN_UID (dest)] < 0)
1498             {
1499               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1500               VEC_safe_push (rtx, heap, *next, dest);
1501             }
1502         }
1503     }
1504
1505   return cur_args_size;
1506 }
1507
1508 /* Walk the whole function and compute args_size on BARRIERs.  */
1509
1510 static void
1511 compute_barrier_args_size (void)
1512 {
1513   int max_uid = get_max_uid (), i;
1514   rtx insn;
1515   VEC (rtx, heap) *worklist, *next, *tmp;
1516
1517   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1518   for (i = 0; i < max_uid; i++)
1519     barrier_args_size[i] = -1;
1520
1521   worklist = VEC_alloc (rtx, heap, 20);
1522   next = VEC_alloc (rtx, heap, 20);
1523   insn = get_insns ();
1524   barrier_args_size[INSN_UID (insn)] = 0;
1525   VEC_quick_push (rtx, worklist, insn);
1526   for (;;)
1527     {
1528       while (!VEC_empty (rtx, worklist))
1529         {
1530           rtx prev, body, first_insn;
1531           HOST_WIDE_INT cur_args_size;
1532
1533           first_insn = insn = VEC_pop (rtx, worklist);
1534           cur_args_size = barrier_args_size[INSN_UID (insn)];
1535           prev = prev_nonnote_insn (insn);
1536           if (prev && BARRIER_P (prev))
1537             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1538
1539           for (; insn; insn = NEXT_INSN (insn))
1540             {
1541               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1542                 continue;
1543               if (BARRIER_P (insn))
1544                 break;
1545
1546               if (LABEL_P (insn))
1547                 {
1548                   if (insn == first_insn)
1549                     continue;
1550                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1551                     {
1552                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1553                       continue;
1554                     }
1555                   else
1556                     {
1557                       /* The insns starting with this label have been
1558                          already scanned or are in the worklist.  */
1559                       break;
1560                     }
1561                 }
1562
1563               body = PATTERN (insn);
1564               if (GET_CODE (body) == SEQUENCE)
1565                 {
1566                   HOST_WIDE_INT dest_args_size = cur_args_size;
1567                   for (i = 1; i < XVECLEN (body, 0); i++)
1568                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1569                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1570                       dest_args_size
1571                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1572                                                        dest_args_size, &next);
1573                     else
1574                       cur_args_size
1575                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1576                                                        cur_args_size, &next);
1577
1578                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1579                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1580                                                  dest_args_size, &next);
1581                   else
1582                     cur_args_size
1583                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1584                                                      cur_args_size, &next);
1585                 }
1586               else
1587                 cur_args_size
1588                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1589             }
1590         }
1591
1592       if (VEC_empty (rtx, next))
1593         break;
1594
1595       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1596       tmp = next;
1597       next = worklist;
1598       worklist = tmp;
1599       VEC_truncate (rtx, next, 0);
1600     }
1601
1602   VEC_free (rtx, heap, worklist);
1603   VEC_free (rtx, heap, next);
1604 }
1605
1606 /* Add a CFI to update the running total of the size of arguments
1607    pushed onto the stack.  */
1608
1609 static void
1610 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1611 {
1612   dw_cfi_ref cfi;
1613
1614   if (size == old_args_size)
1615     return;
1616
1617   old_args_size = size;
1618
1619   cfi = new_cfi ();
1620   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1621   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1622   add_fde_cfi (label, cfi);
1623 }
1624
1625 /* Record a stack adjustment of OFFSET bytes.  */
1626
1627 static void
1628 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1629 {
1630   if (cfa.reg == STACK_POINTER_REGNUM)
1631     cfa.offset += offset;
1632
1633   if (cfa_store.reg == STACK_POINTER_REGNUM)
1634     cfa_store.offset += offset;
1635
1636   if (ACCUMULATE_OUTGOING_ARGS)
1637     return;
1638
1639 #ifndef STACK_GROWS_DOWNWARD
1640   offset = -offset;
1641 #endif
1642
1643   args_size += offset;
1644   if (args_size < 0)
1645     args_size = 0;
1646
1647   def_cfa_1 (label, &cfa);
1648   if (flag_asynchronous_unwind_tables)
1649     dwarf2out_args_size (label, args_size);
1650 }
1651
1652 /* Check INSN to see if it looks like a push or a stack adjustment, and
1653    make a note of it if it does.  EH uses this information to find out
1654    how much extra space it needs to pop off the stack.  */
1655
1656 static void
1657 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1658 {
1659   HOST_WIDE_INT offset;
1660   const char *label;
1661   int i;
1662
1663   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1664      with this function.  Proper support would require all frame-related
1665      insns to be marked, and to be able to handle saving state around
1666      epilogues textually in the middle of the function.  */
1667   if (prologue_epilogue_contains (insn))
1668     return;
1669
1670   /* If INSN is an instruction from target of an annulled branch, the
1671      effects are for the target only and so current argument size
1672      shouldn't change at all.  */
1673   if (final_sequence
1674       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1675       && INSN_FROM_TARGET_P (insn))
1676     return;
1677
1678   /* If only calls can throw, and we have a frame pointer,
1679      save up adjustments until we see the CALL_INSN.  */
1680   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1681     {
1682       if (CALL_P (insn) && !after_p)
1683         {
1684           /* Extract the size of the args from the CALL rtx itself.  */
1685           insn = PATTERN (insn);
1686           if (GET_CODE (insn) == PARALLEL)
1687             insn = XVECEXP (insn, 0, 0);
1688           if (GET_CODE (insn) == SET)
1689             insn = SET_SRC (insn);
1690           gcc_assert (GET_CODE (insn) == CALL);
1691           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1692         }
1693       return;
1694     }
1695
1696   if (CALL_P (insn) && !after_p)
1697     {
1698       if (!flag_asynchronous_unwind_tables)
1699         dwarf2out_args_size ("", args_size);
1700       return;
1701     }
1702   else if (BARRIER_P (insn))
1703     {
1704       /* Don't call compute_barrier_args_size () if the only
1705          BARRIER is at the end of function.  */
1706       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1707         compute_barrier_args_size ();
1708       if (barrier_args_size == NULL)
1709         offset = 0;
1710       else
1711         {
1712           offset = barrier_args_size[INSN_UID (insn)];
1713           if (offset < 0)
1714             offset = 0;
1715         }
1716
1717       offset -= args_size;
1718 #ifndef STACK_GROWS_DOWNWARD
1719       offset = -offset;
1720 #endif
1721     }
1722   else if (GET_CODE (PATTERN (insn)) == SET)
1723     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1724   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1725            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1726     {
1727       /* There may be stack adjustments inside compound insns.  Search
1728          for them.  */
1729       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1730         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1731           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1732                                          args_size, offset);
1733     }
1734   else
1735     return;
1736
1737   if (offset == 0)
1738     return;
1739
1740   label = dwarf2out_cfi_label (false);
1741   dwarf2out_stack_adjust (offset, label);
1742 }
1743
1744 /* We delay emitting a register save until either (a) we reach the end
1745    of the prologue or (b) the register is clobbered.  This clusters
1746    register saves so that there are fewer pc advances.  */
1747
1748 struct GTY(()) queued_reg_save {
1749   struct queued_reg_save *next;
1750   rtx reg;
1751   HOST_WIDE_INT cfa_offset;
1752   rtx saved_reg;
1753 };
1754
1755 static GTY(()) struct queued_reg_save *queued_reg_saves;
1756
1757 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1758 struct GTY(()) reg_saved_in_data {
1759   rtx orig_reg;
1760   rtx saved_in_reg;
1761 };
1762
1763 /* A list of registers saved in other registers.
1764    The list intentionally has a small maximum capacity of 4; if your
1765    port needs more than that, you might consider implementing a
1766    more efficient data structure.  */
1767 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1768 static GTY(()) size_t num_regs_saved_in_regs;
1769
1770 static const char *last_reg_save_label;
1771
1772 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1773    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1774
1775 static void
1776 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1777 {
1778   struct queued_reg_save *q;
1779
1780   /* Duplicates waste space, but it's also necessary to remove them
1781      for correctness, since the queue gets output in reverse
1782      order.  */
1783   for (q = queued_reg_saves; q != NULL; q = q->next)
1784     if (REGNO (q->reg) == REGNO (reg))
1785       break;
1786
1787   if (q == NULL)
1788     {
1789       q = ggc_alloc_queued_reg_save ();
1790       q->next = queued_reg_saves;
1791       queued_reg_saves = q;
1792     }
1793
1794   q->reg = reg;
1795   q->cfa_offset = offset;
1796   q->saved_reg = sreg;
1797
1798   last_reg_save_label = label;
1799 }
1800
1801 /* Output all the entries in QUEUED_REG_SAVES.  */
1802
1803 void
1804 dwarf2out_flush_queued_reg_saves (void)
1805 {
1806   struct queued_reg_save *q;
1807
1808   for (q = queued_reg_saves; q; q = q->next)
1809     {
1810       size_t i;
1811       unsigned int reg, sreg;
1812
1813       for (i = 0; i < num_regs_saved_in_regs; i++)
1814         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1815           break;
1816       if (q->saved_reg && i == num_regs_saved_in_regs)
1817         {
1818           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1819           num_regs_saved_in_regs++;
1820         }
1821       if (i != num_regs_saved_in_regs)
1822         {
1823           regs_saved_in_regs[i].orig_reg = q->reg;
1824           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1825         }
1826
1827       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1828       if (q->saved_reg)
1829         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1830       else
1831         sreg = INVALID_REGNUM;
1832       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1833     }
1834
1835   queued_reg_saves = NULL;
1836   last_reg_save_label = NULL;
1837 }
1838
1839 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1840    location for?  Or, does it clobber a register which we've previously
1841    said that some other register is saved in, and for which we now
1842    have a new location for?  */
1843
1844 static bool
1845 clobbers_queued_reg_save (const_rtx insn)
1846 {
1847   struct queued_reg_save *q;
1848
1849   for (q = queued_reg_saves; q; q = q->next)
1850     {
1851       size_t i;
1852       if (modified_in_p (q->reg, insn))
1853         return true;
1854       for (i = 0; i < num_regs_saved_in_regs; i++)
1855         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1856             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1857           return true;
1858     }
1859
1860   return false;
1861 }
1862
1863 /* Entry point for saving the first register into the second.  */
1864
1865 void
1866 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1867 {
1868   size_t i;
1869   unsigned int regno, sregno;
1870
1871   for (i = 0; i < num_regs_saved_in_regs; i++)
1872     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1873       break;
1874   if (i == num_regs_saved_in_regs)
1875     {
1876       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1877       num_regs_saved_in_regs++;
1878     }
1879   regs_saved_in_regs[i].orig_reg = reg;
1880   regs_saved_in_regs[i].saved_in_reg = sreg;
1881
1882   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1883   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1884   reg_save (label, regno, sregno, 0);
1885 }
1886
1887 /* What register, if any, is currently saved in REG?  */
1888
1889 static rtx
1890 reg_saved_in (rtx reg)
1891 {
1892   unsigned int regn = REGNO (reg);
1893   size_t i;
1894   struct queued_reg_save *q;
1895
1896   for (q = queued_reg_saves; q; q = q->next)
1897     if (q->saved_reg && regn == REGNO (q->saved_reg))
1898       return q->reg;
1899
1900   for (i = 0; i < num_regs_saved_in_regs; i++)
1901     if (regs_saved_in_regs[i].saved_in_reg
1902         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1903       return regs_saved_in_regs[i].orig_reg;
1904
1905   return NULL_RTX;
1906 }
1907
1908
1909 /* A temporary register holding an integral value used in adjusting SP
1910    or setting up the store_reg.  The "offset" field holds the integer
1911    value, not an offset.  */
1912 static dw_cfa_location cfa_temp;
1913
1914 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1915
1916 static void
1917 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1918 {
1919   memset (&cfa, 0, sizeof (cfa));
1920
1921   switch (GET_CODE (pat))
1922     {
1923     case PLUS:
1924       cfa.reg = REGNO (XEXP (pat, 0));
1925       cfa.offset = INTVAL (XEXP (pat, 1));
1926       break;
1927
1928     case REG:
1929       cfa.reg = REGNO (pat);
1930       break;
1931
1932     case MEM:
1933       cfa.indirect = 1;
1934       pat = XEXP (pat, 0);
1935       if (GET_CODE (pat) == PLUS)
1936         {
1937           cfa.base_offset = INTVAL (XEXP (pat, 1));
1938           pat = XEXP (pat, 0);
1939         }
1940       cfa.reg = REGNO (pat);
1941       break;
1942
1943     default:
1944       /* Recurse and define an expression.  */
1945       gcc_unreachable ();
1946     }
1947
1948   def_cfa_1 (label, &cfa);
1949 }
1950
1951 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1952
1953 static void
1954 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1955 {
1956   rtx src, dest;
1957
1958   gcc_assert (GET_CODE (pat) == SET);
1959   dest = XEXP (pat, 0);
1960   src = XEXP (pat, 1);
1961
1962   switch (GET_CODE (src))
1963     {
1964     case PLUS:
1965       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1966       cfa.offset -= INTVAL (XEXP (src, 1));
1967       break;
1968
1969     case REG:
1970         break;
1971
1972     default:
1973         gcc_unreachable ();
1974     }
1975
1976   cfa.reg = REGNO (dest);
1977   gcc_assert (cfa.indirect == 0);
1978
1979   def_cfa_1 (label, &cfa);
1980 }
1981
1982 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1983
1984 static void
1985 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1986 {
1987   HOST_WIDE_INT offset;
1988   rtx src, addr, span;
1989
1990   src = XEXP (set, 1);
1991   addr = XEXP (set, 0);
1992   gcc_assert (MEM_P (addr));
1993   addr = XEXP (addr, 0);
1994
1995   /* As documented, only consider extremely simple addresses.  */
1996   switch (GET_CODE (addr))
1997     {
1998     case REG:
1999       gcc_assert (REGNO (addr) == cfa.reg);
2000       offset = -cfa.offset;
2001       break;
2002     case PLUS:
2003       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2004       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2005       break;
2006     default:
2007       gcc_unreachable ();
2008     }
2009
2010   span = targetm.dwarf_register_span (src);
2011
2012   /* ??? We'd like to use queue_reg_save, but we need to come up with
2013      a different flushing heuristic for epilogues.  */
2014   if (!span)
2015     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2016   else
2017     {
2018       /* We have a PARALLEL describing where the contents of SRC live.
2019          Queue register saves for each piece of the PARALLEL.  */
2020       int par_index;
2021       int limit;
2022       HOST_WIDE_INT span_offset = offset;
2023
2024       gcc_assert (GET_CODE (span) == PARALLEL);
2025
2026       limit = XVECLEN (span, 0);
2027       for (par_index = 0; par_index < limit; par_index++)
2028         {
2029           rtx elem = XVECEXP (span, 0, par_index);
2030
2031           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2032                     INVALID_REGNUM, span_offset);
2033           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2034         }
2035     }
2036 }
2037
2038 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2039
2040 static void
2041 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2042 {
2043   rtx src, dest;
2044   unsigned sregno, dregno;
2045
2046   src = XEXP (set, 1);
2047   dest = XEXP (set, 0);
2048
2049   if (src == pc_rtx)
2050     sregno = DWARF_FRAME_RETURN_COLUMN;
2051   else
2052     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2053
2054   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2055
2056   /* ??? We'd like to use queue_reg_save, but we need to come up with
2057      a different flushing heuristic for epilogues.  */
2058   reg_save (label, sregno, dregno, 0);
2059 }
2060
2061 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2062
2063 static void
2064 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2065 {
2066   rtx src, dest, span;
2067   dw_cfi_ref cfi = new_cfi ();
2068
2069   dest = SET_DEST (set);
2070   src = SET_SRC (set);
2071
2072   gcc_assert (REG_P (src));
2073   gcc_assert (MEM_P (dest));
2074
2075   span = targetm.dwarf_register_span (src);
2076   gcc_assert (!span);
2077
2078   cfi->dw_cfi_opc = DW_CFA_expression;
2079   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2080   cfi->dw_cfi_oprnd2.dw_cfi_loc
2081     = mem_loc_descriptor (XEXP (dest, 0), GET_MODE (dest),
2082                           VAR_INIT_STATUS_INITIALIZED);
2083
2084   /* ??? We'd like to use queue_reg_save, were the interface different,
2085      and, as above, we could manage flushing for epilogues.  */
2086   add_fde_cfi (label, cfi);
2087 }
2088
2089 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2090
2091 static void
2092 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2093 {
2094   dw_cfi_ref cfi = new_cfi ();
2095   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2096
2097   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2098   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2099
2100   add_fde_cfi (label, cfi);
2101 }
2102
2103 /* Record call frame debugging information for an expression EXPR,
2104    which either sets SP or FP (adjusting how we calculate the frame
2105    address) or saves a register to the stack or another register.
2106    LABEL indicates the address of EXPR.
2107
2108    This function encodes a state machine mapping rtxes to actions on
2109    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2110    users need not read the source code.
2111
2112   The High-Level Picture
2113
2114   Changes in the register we use to calculate the CFA: Currently we
2115   assume that if you copy the CFA register into another register, we
2116   should take the other one as the new CFA register; this seems to
2117   work pretty well.  If it's wrong for some target, it's simple
2118   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2119
2120   Changes in the register we use for saving registers to the stack:
2121   This is usually SP, but not always.  Again, we deduce that if you
2122   copy SP into another register (and SP is not the CFA register),
2123   then the new register is the one we will be using for register
2124   saves.  This also seems to work.
2125
2126   Register saves: There's not much guesswork about this one; if
2127   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2128   register save, and the register used to calculate the destination
2129   had better be the one we think we're using for this purpose.
2130   It's also assumed that a copy from a call-saved register to another
2131   register is saving that register if RTX_FRAME_RELATED_P is set on
2132   that instruction.  If the copy is from a call-saved register to
2133   the *same* register, that means that the register is now the same
2134   value as in the caller.
2135
2136   Except: If the register being saved is the CFA register, and the
2137   offset is nonzero, we are saving the CFA, so we assume we have to
2138   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2139   the intent is to save the value of SP from the previous frame.
2140
2141   In addition, if a register has previously been saved to a different
2142   register,
2143
2144   Invariants / Summaries of Rules
2145
2146   cfa          current rule for calculating the CFA.  It usually
2147                consists of a register and an offset.
2148   cfa_store    register used by prologue code to save things to the stack
2149                cfa_store.offset is the offset from the value of
2150                cfa_store.reg to the actual CFA
2151   cfa_temp     register holding an integral value.  cfa_temp.offset
2152                stores the value, which will be used to adjust the
2153                stack pointer.  cfa_temp is also used like cfa_store,
2154                to track stores to the stack via fp or a temp reg.
2155
2156   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2157                with cfa.reg as the first operand changes the cfa.reg and its
2158                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2159                cfa_temp.offset.
2160
2161   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2162                expression yielding a constant.  This sets cfa_temp.reg
2163                and cfa_temp.offset.
2164
2165   Rule 5:      Create a new register cfa_store used to save items to the
2166                stack.
2167
2168   Rules 10-14: Save a register to the stack.  Define offset as the
2169                difference of the original location and cfa_store's
2170                location (or cfa_temp's location if cfa_temp is used).
2171
2172   Rules 16-20: If AND operation happens on sp in prologue, we assume
2173                stack is realigned.  We will use a group of DW_OP_XXX
2174                expressions to represent the location of the stored
2175                register instead of CFA+offset.
2176
2177   The Rules
2178
2179   "{a,b}" indicates a choice of a xor b.
2180   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2181
2182   Rule 1:
2183   (set <reg1> <reg2>:cfa.reg)
2184   effects: cfa.reg = <reg1>
2185            cfa.offset unchanged
2186            cfa_temp.reg = <reg1>
2187            cfa_temp.offset = cfa.offset
2188
2189   Rule 2:
2190   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2191                               {<const_int>,<reg>:cfa_temp.reg}))
2192   effects: cfa.reg = sp if fp used
2193            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2194            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2195              if cfa_store.reg==sp
2196
2197   Rule 3:
2198   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2199   effects: cfa.reg = fp
2200            cfa_offset += +/- <const_int>
2201
2202   Rule 4:
2203   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2204   constraints: <reg1> != fp
2205                <reg1> != sp
2206   effects: cfa.reg = <reg1>
2207            cfa_temp.reg = <reg1>
2208            cfa_temp.offset = cfa.offset
2209
2210   Rule 5:
2211   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2212   constraints: <reg1> != fp
2213                <reg1> != sp
2214   effects: cfa_store.reg = <reg1>
2215            cfa_store.offset = cfa.offset - cfa_temp.offset
2216
2217   Rule 6:
2218   (set <reg> <const_int>)
2219   effects: cfa_temp.reg = <reg>
2220            cfa_temp.offset = <const_int>
2221
2222   Rule 7:
2223   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2224   effects: cfa_temp.reg = <reg1>
2225            cfa_temp.offset |= <const_int>
2226
2227   Rule 8:
2228   (set <reg> (high <exp>))
2229   effects: none
2230
2231   Rule 9:
2232   (set <reg> (lo_sum <exp> <const_int>))
2233   effects: cfa_temp.reg = <reg>
2234            cfa_temp.offset = <const_int>
2235
2236   Rule 10:
2237   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2238   effects: cfa_store.offset -= <const_int>
2239            cfa.offset = cfa_store.offset if cfa.reg == sp
2240            cfa.reg = sp
2241            cfa.base_offset = -cfa_store.offset
2242
2243   Rule 11:
2244   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2245   effects: cfa_store.offset += -/+ mode_size(mem)
2246            cfa.offset = cfa_store.offset if cfa.reg == sp
2247            cfa.reg = sp
2248            cfa.base_offset = -cfa_store.offset
2249
2250   Rule 12:
2251   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2252
2253        <reg2>)
2254   effects: cfa.reg = <reg1>
2255            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2256
2257   Rule 13:
2258   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2259   effects: cfa.reg = <reg1>
2260            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2261
2262   Rule 14:
2263   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2264   effects: cfa.reg = <reg1>
2265            cfa.base_offset = -cfa_temp.offset
2266            cfa_temp.offset -= mode_size(mem)
2267
2268   Rule 15:
2269   (set <reg> {unspec, unspec_volatile})
2270   effects: target-dependent
2271
2272   Rule 16:
2273   (set sp (and: sp <const_int>))
2274   constraints: cfa_store.reg == sp
2275   effects: current_fde.stack_realign = 1
2276            cfa_store.offset = 0
2277            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2278
2279   Rule 17:
2280   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2281   effects: cfa_store.offset += -/+ mode_size(mem)
2282
2283   Rule 18:
2284   (set (mem ({pre_inc, pre_dec} sp)) fp)
2285   constraints: fde->stack_realign == 1
2286   effects: cfa_store.offset = 0
2287            cfa.reg != HARD_FRAME_POINTER_REGNUM
2288
2289   Rule 19:
2290   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2291   constraints: fde->stack_realign == 1
2292                && cfa.offset == 0
2293                && cfa.indirect == 0
2294                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2295   effects: Use DW_CFA_def_cfa_expression to define cfa
2296            cfa.reg == fde->drap_reg  */
2297
2298 static void
2299 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2300 {
2301   rtx src, dest, span;
2302   HOST_WIDE_INT offset;
2303   dw_fde_ref fde;
2304
2305   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2306      the PARALLEL independently. The first element is always processed if
2307      it is a SET. This is for backward compatibility.   Other elements
2308      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2309      flag is set in them.  */
2310   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2311     {
2312       int par_index;
2313       int limit = XVECLEN (expr, 0);
2314       rtx elem;
2315
2316       /* PARALLELs have strict read-modify-write semantics, so we
2317          ought to evaluate every rvalue before changing any lvalue.
2318          It's cumbersome to do that in general, but there's an
2319          easy approximation that is enough for all current users:
2320          handle register saves before register assignments.  */
2321       if (GET_CODE (expr) == PARALLEL)
2322         for (par_index = 0; par_index < limit; par_index++)
2323           {
2324             elem = XVECEXP (expr, 0, par_index);
2325             if (GET_CODE (elem) == SET
2326                 && MEM_P (SET_DEST (elem))
2327                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2328               dwarf2out_frame_debug_expr (elem, label);
2329           }
2330
2331       for (par_index = 0; par_index < limit; par_index++)
2332         {
2333           elem = XVECEXP (expr, 0, par_index);
2334           if (GET_CODE (elem) == SET
2335               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2336               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2337             dwarf2out_frame_debug_expr (elem, label);
2338           else if (GET_CODE (elem) == SET
2339                    && par_index != 0
2340                    && !RTX_FRAME_RELATED_P (elem))
2341             {
2342               /* Stack adjustment combining might combine some post-prologue
2343                  stack adjustment into a prologue stack adjustment.  */
2344               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2345
2346               if (offset != 0)
2347                 dwarf2out_stack_adjust (offset, label);
2348             }
2349         }
2350       return;
2351     }
2352
2353   gcc_assert (GET_CODE (expr) == SET);
2354
2355   src = SET_SRC (expr);
2356   dest = SET_DEST (expr);
2357
2358   if (REG_P (src))
2359     {
2360       rtx rsi = reg_saved_in (src);
2361       if (rsi)
2362         src = rsi;
2363     }
2364
2365   fde = current_fde ();
2366
2367   switch (GET_CODE (dest))
2368     {
2369     case REG:
2370       switch (GET_CODE (src))
2371         {
2372           /* Setting FP from SP.  */
2373         case REG:
2374           if (cfa.reg == (unsigned) REGNO (src))
2375             {
2376               /* Rule 1 */
2377               /* Update the CFA rule wrt SP or FP.  Make sure src is
2378                  relative to the current CFA register.
2379
2380                  We used to require that dest be either SP or FP, but the
2381                  ARM copies SP to a temporary register, and from there to
2382                  FP.  So we just rely on the backends to only set
2383                  RTX_FRAME_RELATED_P on appropriate insns.  */
2384               cfa.reg = REGNO (dest);
2385               cfa_temp.reg = cfa.reg;
2386               cfa_temp.offset = cfa.offset;
2387             }
2388           else
2389             {
2390               /* Saving a register in a register.  */
2391               gcc_assert (!fixed_regs [REGNO (dest)]
2392                           /* For the SPARC and its register window.  */
2393                           || (DWARF_FRAME_REGNUM (REGNO (src))
2394                               == DWARF_FRAME_RETURN_COLUMN));
2395
2396               /* After stack is aligned, we can only save SP in FP
2397                  if drap register is used.  In this case, we have
2398                  to restore stack pointer with the CFA value and we
2399                  don't generate this DWARF information.  */
2400               if (fde
2401                   && fde->stack_realign
2402                   && REGNO (src) == STACK_POINTER_REGNUM)
2403                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2404                             && fde->drap_reg != INVALID_REGNUM
2405                             && cfa.reg != REGNO (src));
2406               else
2407                 queue_reg_save (label, src, dest, 0);
2408             }
2409           break;
2410
2411         case PLUS:
2412         case MINUS:
2413         case LO_SUM:
2414           if (dest == stack_pointer_rtx)
2415             {
2416               /* Rule 2 */
2417               /* Adjusting SP.  */
2418               switch (GET_CODE (XEXP (src, 1)))
2419                 {
2420                 case CONST_INT:
2421                   offset = INTVAL (XEXP (src, 1));
2422                   break;
2423                 case REG:
2424                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2425                               == cfa_temp.reg);
2426                   offset = cfa_temp.offset;
2427                   break;
2428                 default:
2429                   gcc_unreachable ();
2430                 }
2431
2432               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2433                 {
2434                   /* Restoring SP from FP in the epilogue.  */
2435                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2436                   cfa.reg = STACK_POINTER_REGNUM;
2437                 }
2438               else if (GET_CODE (src) == LO_SUM)
2439                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2440                 ;
2441               else
2442                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2443
2444               if (GET_CODE (src) != MINUS)
2445                 offset = -offset;
2446               if (cfa.reg == STACK_POINTER_REGNUM)
2447                 cfa.offset += offset;
2448               if (cfa_store.reg == STACK_POINTER_REGNUM)
2449                 cfa_store.offset += offset;
2450             }
2451           else if (dest == hard_frame_pointer_rtx)
2452             {
2453               /* Rule 3 */
2454               /* Either setting the FP from an offset of the SP,
2455                  or adjusting the FP */
2456               gcc_assert (frame_pointer_needed);
2457
2458               gcc_assert (REG_P (XEXP (src, 0))
2459                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2460                           && CONST_INT_P (XEXP (src, 1)));
2461               offset = INTVAL (XEXP (src, 1));
2462               if (GET_CODE (src) != MINUS)
2463                 offset = -offset;
2464               cfa.offset += offset;
2465               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2466             }
2467           else
2468             {
2469               gcc_assert (GET_CODE (src) != MINUS);
2470
2471               /* Rule 4 */
2472               if (REG_P (XEXP (src, 0))
2473                   && REGNO (XEXP (src, 0)) == cfa.reg
2474                   && CONST_INT_P (XEXP (src, 1)))
2475                 {
2476                   /* Setting a temporary CFA register that will be copied
2477                      into the FP later on.  */
2478                   offset = - INTVAL (XEXP (src, 1));
2479                   cfa.offset += offset;
2480                   cfa.reg = REGNO (dest);
2481                   /* Or used to save regs to the stack.  */
2482                   cfa_temp.reg = cfa.reg;
2483                   cfa_temp.offset = cfa.offset;
2484                 }
2485
2486               /* Rule 5 */
2487               else if (REG_P (XEXP (src, 0))
2488                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2489                        && XEXP (src, 1) == stack_pointer_rtx)
2490                 {
2491                   /* Setting a scratch register that we will use instead
2492                      of SP for saving registers to the stack.  */
2493                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2494                   cfa_store.reg = REGNO (dest);
2495                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2496                 }
2497
2498               /* Rule 9 */
2499               else if (GET_CODE (src) == LO_SUM
2500                        && CONST_INT_P (XEXP (src, 1)))
2501                 {
2502                   cfa_temp.reg = REGNO (dest);
2503                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2504                 }
2505               else
2506                 gcc_unreachable ();
2507             }
2508           break;
2509
2510           /* Rule 6 */
2511         case CONST_INT:
2512           cfa_temp.reg = REGNO (dest);
2513           cfa_temp.offset = INTVAL (src);
2514           break;
2515
2516           /* Rule 7 */
2517         case IOR:
2518           gcc_assert (REG_P (XEXP (src, 0))
2519                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2520                       && CONST_INT_P (XEXP (src, 1)));
2521
2522           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2523             cfa_temp.reg = REGNO (dest);
2524           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2525           break;
2526
2527           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2528              which will fill in all of the bits.  */
2529           /* Rule 8 */
2530         case HIGH:
2531           break;
2532
2533           /* Rule 15 */
2534         case UNSPEC:
2535         case UNSPEC_VOLATILE:
2536           gcc_assert (targetm.dwarf_handle_frame_unspec);
2537           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2538           return;
2539
2540           /* Rule 16 */
2541         case AND:
2542           /* If this AND operation happens on stack pointer in prologue,
2543              we assume the stack is realigned and we extract the
2544              alignment.  */
2545           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2546             {
2547               /* We interpret reg_save differently with stack_realign set.
2548                  Thus we must flush whatever we have queued first.  */
2549               dwarf2out_flush_queued_reg_saves ();
2550
2551               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2552               fde->stack_realign = 1;
2553               fde->stack_realignment = INTVAL (XEXP (src, 1));
2554               cfa_store.offset = 0;
2555
2556               if (cfa.reg != STACK_POINTER_REGNUM
2557                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2558                 fde->drap_reg = cfa.reg;
2559             }
2560           return;
2561
2562         default:
2563           gcc_unreachable ();
2564         }
2565
2566       def_cfa_1 (label, &cfa);
2567       break;
2568
2569     case MEM:
2570
2571       /* Saving a register to the stack.  Make sure dest is relative to the
2572          CFA register.  */
2573       switch (GET_CODE (XEXP (dest, 0)))
2574         {
2575           /* Rule 10 */
2576           /* With a push.  */
2577         case PRE_MODIFY:
2578           /* We can't handle variable size modifications.  */
2579           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2580                       == CONST_INT);
2581           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2582
2583           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2584                       && cfa_store.reg == STACK_POINTER_REGNUM);
2585
2586           cfa_store.offset += offset;
2587           if (cfa.reg == STACK_POINTER_REGNUM)
2588             cfa.offset = cfa_store.offset;
2589
2590           offset = -cfa_store.offset;
2591           break;
2592
2593           /* Rule 11 */
2594         case PRE_INC:
2595         case PRE_DEC:
2596           offset = GET_MODE_SIZE (GET_MODE (dest));
2597           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2598             offset = -offset;
2599
2600           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2601                        == STACK_POINTER_REGNUM)
2602                       && cfa_store.reg == STACK_POINTER_REGNUM);
2603
2604           cfa_store.offset += offset;
2605
2606           /* Rule 18: If stack is aligned, we will use FP as a
2607              reference to represent the address of the stored
2608              regiser.  */
2609           if (fde
2610               && fde->stack_realign
2611               && src == hard_frame_pointer_rtx)
2612             {
2613               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2614               cfa_store.offset = 0;
2615             }
2616
2617           if (cfa.reg == STACK_POINTER_REGNUM)
2618             cfa.offset = cfa_store.offset;
2619
2620           offset = -cfa_store.offset;
2621           break;
2622
2623           /* Rule 12 */
2624           /* With an offset.  */
2625         case PLUS:
2626         case MINUS:
2627         case LO_SUM:
2628           {
2629             int regno;
2630
2631             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2632                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2633             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2634             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2635               offset = -offset;
2636
2637             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2638
2639             if (cfa.reg == (unsigned) regno)
2640               offset -= cfa.offset;
2641             else if (cfa_store.reg == (unsigned) regno)
2642               offset -= cfa_store.offset;
2643             else
2644               {
2645                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2646                 offset -= cfa_temp.offset;
2647               }
2648           }
2649           break;
2650
2651           /* Rule 13 */
2652           /* Without an offset.  */
2653         case REG:
2654           {
2655             int regno = REGNO (XEXP (dest, 0));
2656
2657             if (cfa.reg == (unsigned) regno)
2658               offset = -cfa.offset;
2659             else if (cfa_store.reg == (unsigned) regno)
2660               offset = -cfa_store.offset;
2661             else
2662               {
2663                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2664                 offset = -cfa_temp.offset;
2665               }
2666           }
2667           break;
2668
2669           /* Rule 14 */
2670         case POST_INC:
2671           gcc_assert (cfa_temp.reg
2672                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2673           offset = -cfa_temp.offset;
2674           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2675           break;
2676
2677         default:
2678           gcc_unreachable ();
2679         }
2680
2681         /* Rule 17 */
2682         /* If the source operand of this MEM operation is not a
2683            register, basically the source is return address.  Here
2684            we only care how much stack grew and we don't save it.  */
2685       if (!REG_P (src))
2686         break;
2687
2688       if (REGNO (src) != STACK_POINTER_REGNUM
2689           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2690           && (unsigned) REGNO (src) == cfa.reg)
2691         {
2692           /* We're storing the current CFA reg into the stack.  */
2693
2694           if (cfa.offset == 0)
2695             {
2696               /* Rule 19 */
2697               /* If stack is aligned, putting CFA reg into stack means
2698                  we can no longer use reg + offset to represent CFA.
2699                  Here we use DW_CFA_def_cfa_expression instead.  The
2700                  result of this expression equals to the original CFA
2701                  value.  */
2702               if (fde
2703                   && fde->stack_realign
2704                   && cfa.indirect == 0
2705                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2706                 {
2707                   dw_cfa_location cfa_exp;
2708
2709                   gcc_assert (fde->drap_reg == cfa.reg);
2710
2711                   cfa_exp.indirect = 1;
2712                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2713                   cfa_exp.base_offset = offset;
2714                   cfa_exp.offset = 0;
2715
2716                   fde->drap_reg_saved = 1;
2717
2718                   def_cfa_1 (label, &cfa_exp);
2719                   break;
2720                 }
2721
2722               /* If the source register is exactly the CFA, assume
2723                  we're saving SP like any other register; this happens
2724                  on the ARM.  */
2725               def_cfa_1 (label, &cfa);
2726               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2727               break;
2728             }
2729           else
2730             {
2731               /* Otherwise, we'll need to look in the stack to
2732                  calculate the CFA.  */
2733               rtx x = XEXP (dest, 0);
2734
2735               if (!REG_P (x))
2736                 x = XEXP (x, 0);
2737               gcc_assert (REG_P (x));
2738
2739               cfa.reg = REGNO (x);
2740               cfa.base_offset = offset;
2741               cfa.indirect = 1;
2742               def_cfa_1 (label, &cfa);
2743               break;
2744             }
2745         }
2746
2747       def_cfa_1 (label, &cfa);
2748       {
2749         span = targetm.dwarf_register_span (src);
2750
2751         if (!span)
2752           queue_reg_save (label, src, NULL_RTX, offset);
2753         else
2754           {
2755             /* We have a PARALLEL describing where the contents of SRC
2756                live.  Queue register saves for each piece of the
2757                PARALLEL.  */
2758             int par_index;
2759             int limit;
2760             HOST_WIDE_INT span_offset = offset;
2761
2762             gcc_assert (GET_CODE (span) == PARALLEL);
2763
2764             limit = XVECLEN (span, 0);
2765             for (par_index = 0; par_index < limit; par_index++)
2766               {
2767                 rtx elem = XVECEXP (span, 0, par_index);
2768
2769                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2770                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2771               }
2772           }
2773       }
2774       break;
2775
2776     default:
2777       gcc_unreachable ();
2778     }
2779 }
2780
2781 /* Record call frame debugging information for INSN, which either
2782    sets SP or FP (adjusting how we calculate the frame address) or saves a
2783    register to the stack.  If INSN is NULL_RTX, initialize our state.
2784
2785    If AFTER_P is false, we're being called before the insn is emitted,
2786    otherwise after.  Call instructions get invoked twice.  */
2787
2788 void
2789 dwarf2out_frame_debug (rtx insn, bool after_p)
2790 {
2791   const char *label;
2792   rtx note, n;
2793   bool handled_one = false;
2794
2795   if (insn == NULL_RTX)
2796     {
2797       size_t i;
2798
2799       /* Flush any queued register saves.  */
2800       dwarf2out_flush_queued_reg_saves ();
2801
2802       /* Set up state for generating call frame debug info.  */
2803       lookup_cfa (&cfa);
2804       gcc_assert (cfa.reg
2805                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2806
2807       cfa.reg = STACK_POINTER_REGNUM;
2808       cfa_store = cfa;
2809       cfa_temp.reg = -1;
2810       cfa_temp.offset = 0;
2811
2812       for (i = 0; i < num_regs_saved_in_regs; i++)
2813         {
2814           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2815           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2816         }
2817       num_regs_saved_in_regs = 0;
2818
2819       if (barrier_args_size)
2820         {
2821           XDELETEVEC (barrier_args_size);
2822           barrier_args_size = NULL;
2823         }
2824       return;
2825     }
2826
2827   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2828     dwarf2out_flush_queued_reg_saves ();
2829
2830   if (!RTX_FRAME_RELATED_P (insn))
2831     {
2832       /* ??? This should be done unconditionally since stack adjustments
2833          matter if the stack pointer is not the CFA register anymore but
2834          is still used to save registers.  */
2835       if (!ACCUMULATE_OUTGOING_ARGS)
2836         dwarf2out_notice_stack_adjust (insn, after_p);
2837       return;
2838     }
2839
2840   label = dwarf2out_cfi_label (false);
2841   any_cfis_emitted = false;
2842
2843   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2844     switch (REG_NOTE_KIND (note))
2845       {
2846       case REG_FRAME_RELATED_EXPR:
2847         insn = XEXP (note, 0);
2848         goto found;
2849
2850       case REG_CFA_DEF_CFA:
2851         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2852         handled_one = true;
2853         break;
2854
2855       case REG_CFA_ADJUST_CFA:
2856         n = XEXP (note, 0);
2857         if (n == NULL)
2858           {
2859             n = PATTERN (insn);
2860             if (GET_CODE (n) == PARALLEL)
2861               n = XVECEXP (n, 0, 0);
2862           }
2863         dwarf2out_frame_debug_adjust_cfa (n, label);
2864         handled_one = true;
2865         break;
2866
2867       case REG_CFA_OFFSET:
2868         n = XEXP (note, 0);
2869         if (n == NULL)
2870           n = single_set (insn);
2871         dwarf2out_frame_debug_cfa_offset (n, label);
2872         handled_one = true;
2873         break;
2874
2875       case REG_CFA_REGISTER:
2876         n = XEXP (note, 0);
2877         if (n == NULL)
2878           {
2879             n = PATTERN (insn);
2880             if (GET_CODE (n) == PARALLEL)
2881               n = XVECEXP (n, 0, 0);
2882           }
2883         dwarf2out_frame_debug_cfa_register (n, label);
2884         handled_one = true;
2885         break;
2886
2887       case REG_CFA_EXPRESSION:
2888         n = XEXP (note, 0);
2889         if (n == NULL)
2890           n = single_set (insn);
2891         dwarf2out_frame_debug_cfa_expression (n, label);
2892         handled_one = true;
2893         break;
2894
2895       case REG_CFA_RESTORE:
2896         n = XEXP (note, 0);
2897         if (n == NULL)
2898           {
2899             n = PATTERN (insn);
2900             if (GET_CODE (n) == PARALLEL)
2901               n = XVECEXP (n, 0, 0);
2902             n = XEXP (n, 0);
2903           }
2904         dwarf2out_frame_debug_cfa_restore (n, label);
2905         handled_one = true;
2906         break;
2907
2908       case REG_CFA_SET_VDRAP:
2909         n = XEXP (note, 0);
2910         if (REG_P (n))
2911           {
2912             dw_fde_ref fde = current_fde ();
2913             if (fde)
2914               {
2915                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2916                 if (REG_P (n))
2917                   fde->vdrap_reg = REGNO (n);
2918               }
2919           }
2920         handled_one = true;
2921         break;
2922
2923       default:
2924         break;
2925       }
2926   if (handled_one)
2927     {
2928       if (any_cfis_emitted)
2929         dwarf2out_flush_queued_reg_saves ();
2930       return;
2931     }
2932
2933   insn = PATTERN (insn);
2934  found:
2935   dwarf2out_frame_debug_expr (insn, label);
2936
2937   /* Check again.  A parallel can save and update the same register.
2938      We could probably check just once, here, but this is safer than
2939      removing the check above.  */
2940   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2941     dwarf2out_flush_queued_reg_saves ();
2942 }
2943
2944 /* Determine if we need to save and restore CFI information around this
2945    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2946    we do need to save/restore, then emit the save now, and insert a
2947    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2948
2949 void
2950 dwarf2out_cfi_begin_epilogue (rtx insn)
2951 {
2952   bool saw_frp = false;
2953   rtx i;
2954
2955   /* Scan forward to the return insn, noticing if there are possible
2956      frame related insns.  */
2957   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2958     {
2959       if (!INSN_P (i))
2960         continue;
2961
2962       /* Look for both regular and sibcalls to end the block.  */
2963       if (returnjump_p (i))
2964         break;
2965       if (CALL_P (i) && SIBLING_CALL_P (i))
2966         break;
2967
2968       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2969         {
2970           int idx;
2971           rtx seq = PATTERN (i);
2972
2973           if (returnjump_p (XVECEXP (seq, 0, 0)))
2974             break;
2975           if (CALL_P (XVECEXP (seq, 0, 0))
2976               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2977             break;
2978
2979           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2980             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2981               saw_frp = true;
2982         }
2983
2984       if (RTX_FRAME_RELATED_P (i))
2985         saw_frp = true;
2986     }
2987
2988   /* If the port doesn't emit epilogue unwind info, we don't need a
2989      save/restore pair.  */
2990   if (!saw_frp)
2991     return;
2992
2993   /* Otherwise, search forward to see if the return insn was the last
2994      basic block of the function.  If so, we don't need save/restore.  */
2995   gcc_assert (i != NULL);
2996   i = next_real_insn (i);
2997   if (i == NULL)
2998     return;
2999
3000   /* Insert the restore before that next real insn in the stream, and before
3001      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3002      properly nested.  This should be after any label or alignment.  This
3003      will be pushed into the CFI stream by the function below.  */
3004   while (1)
3005     {
3006       rtx p = PREV_INSN (i);
3007       if (!NOTE_P (p))
3008         break;
3009       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3010         break;
3011       i = p;
3012     }
3013   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3014
3015   emit_cfa_remember = true;
3016
3017   /* And emulate the state save.  */
3018   gcc_assert (!cfa_remember.in_use);
3019   cfa_remember = cfa;
3020   cfa_remember.in_use = 1;
3021 }
3022
3023 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3024    required.  */
3025
3026 void
3027 dwarf2out_frame_debug_restore_state (void)
3028 {
3029   dw_cfi_ref cfi = new_cfi ();
3030   const char *label = dwarf2out_cfi_label (false);
3031
3032   cfi->dw_cfi_opc = DW_CFA_restore_state;
3033   add_fde_cfi (label, cfi);
3034
3035   gcc_assert (cfa_remember.in_use);
3036   cfa = cfa_remember;
3037   cfa_remember.in_use = 0;
3038 }
3039
3040 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3041 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3042  (enum dwarf_call_frame_info cfi);
3043
3044 static enum dw_cfi_oprnd_type
3045 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3046 {
3047   switch (cfi)
3048     {
3049     case DW_CFA_nop:
3050     case DW_CFA_GNU_window_save:
3051     case DW_CFA_remember_state:
3052     case DW_CFA_restore_state:
3053       return dw_cfi_oprnd_unused;
3054
3055     case DW_CFA_set_loc:
3056     case DW_CFA_advance_loc1:
3057     case DW_CFA_advance_loc2:
3058     case DW_CFA_advance_loc4:
3059     case DW_CFA_MIPS_advance_loc8:
3060       return dw_cfi_oprnd_addr;
3061
3062     case DW_CFA_offset:
3063     case DW_CFA_offset_extended:
3064     case DW_CFA_def_cfa:
3065     case DW_CFA_offset_extended_sf:
3066     case DW_CFA_def_cfa_sf:
3067     case DW_CFA_restore:
3068     case DW_CFA_restore_extended:
3069     case DW_CFA_undefined:
3070     case DW_CFA_same_value:
3071     case DW_CFA_def_cfa_register:
3072     case DW_CFA_register:
3073     case DW_CFA_expression:
3074       return dw_cfi_oprnd_reg_num;
3075
3076     case DW_CFA_def_cfa_offset:
3077     case DW_CFA_GNU_args_size:
3078     case DW_CFA_def_cfa_offset_sf:
3079       return dw_cfi_oprnd_offset;
3080
3081     case DW_CFA_def_cfa_expression:
3082       return dw_cfi_oprnd_loc;
3083
3084     default:
3085       gcc_unreachable ();
3086     }
3087 }
3088
3089 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3090 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3091  (enum dwarf_call_frame_info cfi);
3092
3093 static enum dw_cfi_oprnd_type
3094 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3095 {
3096   switch (cfi)
3097     {
3098     case DW_CFA_def_cfa:
3099     case DW_CFA_def_cfa_sf:
3100     case DW_CFA_offset:
3101     case DW_CFA_offset_extended_sf:
3102     case DW_CFA_offset_extended:
3103       return dw_cfi_oprnd_offset;
3104
3105     case DW_CFA_register:
3106       return dw_cfi_oprnd_reg_num;
3107
3108     case DW_CFA_expression:
3109       return dw_cfi_oprnd_loc;
3110
3111     default:
3112       return dw_cfi_oprnd_unused;
3113     }
3114 }
3115
3116 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3117    switch to the data section instead, and write out a synthetic start label
3118    for collect2 the first time around.  */
3119
3120 static void
3121 switch_to_eh_frame_section (bool back)
3122 {
3123   tree label;
3124
3125 #ifdef EH_FRAME_SECTION_NAME
3126   if (eh_frame_section == 0)
3127     {
3128       int flags;
3129
3130       if (EH_TABLES_CAN_BE_READ_ONLY)
3131         {
3132           int fde_encoding;
3133           int per_encoding;
3134           int lsda_encoding;
3135
3136           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3137                                                        /*global=*/0);
3138           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3139                                                        /*global=*/1);
3140           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3141                                                         /*global=*/0);
3142           flags = ((! flag_pic
3143                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3144                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3145                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3146                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3147                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3148                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3149                    ? 0 : SECTION_WRITE);
3150         }
3151       else
3152         flags = SECTION_WRITE;
3153       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3154     }
3155 #endif /* EH_FRAME_SECTION_NAME */
3156
3157   if (eh_frame_section)
3158     switch_to_section (eh_frame_section);
3159   else
3160     {
3161       /* We have no special eh_frame section.  Put the information in
3162          the data section and emit special labels to guide collect2.  */
3163       switch_to_section (data_section);
3164
3165       if (!back)
3166         {
3167           label = get_file_function_name ("F");
3168           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3169           targetm.asm_out.globalize_label (asm_out_file,
3170                                            IDENTIFIER_POINTER (label));
3171           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3172         }
3173     }
3174 }
3175
3176 /* Switch [BACK] to the eh or debug frame table section, depending on
3177    FOR_EH.  */
3178
3179 static void
3180 switch_to_frame_table_section (int for_eh, bool back)
3181 {
3182   if (for_eh)
3183     switch_to_eh_frame_section (back);
3184   else
3185     {
3186       if (!debug_frame_section)
3187         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3188                                            SECTION_DEBUG, NULL);
3189       switch_to_section (debug_frame_section);
3190     }
3191 }
3192
3193 /* Output a Call Frame Information opcode and its operand(s).  */
3194
3195 static void
3196 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3197 {
3198   unsigned long r;
3199   HOST_WIDE_INT off;
3200
3201   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3202     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3203                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3204                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3205                          ((unsigned HOST_WIDE_INT)
3206                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3207   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3208     {
3209       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3210       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3211                            "DW_CFA_offset, column %#lx", r);
3212       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3213       dw2_asm_output_data_uleb128 (off, NULL);
3214     }
3215   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3216     {
3217       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3218       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3219                            "DW_CFA_restore, column %#lx", r);
3220     }
3221   else
3222     {
3223       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3224                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3225
3226       switch (cfi->dw_cfi_opc)
3227         {
3228         case DW_CFA_set_loc:
3229           if (for_eh)
3230             dw2_asm_output_encoded_addr_rtx (
3231                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3232                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3233                 false, NULL);
3234           else
3235             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3236                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3237           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3238           break;
3239
3240         case DW_CFA_advance_loc1:
3241           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3242                                 fde->dw_fde_current_label, NULL);
3243           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3244           break;
3245
3246         case DW_CFA_advance_loc2:
3247           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3248                                 fde->dw_fde_current_label, NULL);
3249           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3250           break;
3251
3252         case DW_CFA_advance_loc4:
3253           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3254                                 fde->dw_fde_current_label, NULL);
3255           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3256           break;
3257
3258         case DW_CFA_MIPS_advance_loc8:
3259           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3260                                 fde->dw_fde_current_label, NULL);
3261           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3262           break;
3263
3264         case DW_CFA_offset_extended:
3265           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3266           dw2_asm_output_data_uleb128 (r, NULL);
3267           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3268           dw2_asm_output_data_uleb128 (off, NULL);
3269           break;
3270
3271         case DW_CFA_def_cfa:
3272           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3273           dw2_asm_output_data_uleb128 (r, NULL);
3274           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3275           break;
3276
3277         case DW_CFA_offset_extended_sf:
3278           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3279           dw2_asm_output_data_uleb128 (r, NULL);
3280           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3281           dw2_asm_output_data_sleb128 (off, NULL);
3282           break;
3283
3284         case DW_CFA_def_cfa_sf:
3285           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3286           dw2_asm_output_data_uleb128 (r, NULL);
3287           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3288           dw2_asm_output_data_sleb128 (off, NULL);
3289           break;
3290
3291         case DW_CFA_restore_extended:
3292         case DW_CFA_undefined:
3293         case DW_CFA_same_value:
3294         case DW_CFA_def_cfa_register:
3295           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3296           dw2_asm_output_data_uleb128 (r, NULL);
3297           break;
3298
3299         case DW_CFA_register:
3300           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3301           dw2_asm_output_data_uleb128 (r, NULL);
3302           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3303           dw2_asm_output_data_uleb128 (r, NULL);
3304           break;
3305
3306         case DW_CFA_def_cfa_offset:
3307         case DW_CFA_GNU_args_size:
3308           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3309           break;
3310
3311         case DW_CFA_def_cfa_offset_sf:
3312           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3313           dw2_asm_output_data_sleb128 (off, NULL);
3314           break;
3315
3316         case DW_CFA_GNU_window_save:
3317           break;
3318
3319         case DW_CFA_def_cfa_expression:
3320         case DW_CFA_expression:
3321           output_cfa_loc (cfi, for_eh);
3322           break;
3323
3324         case DW_CFA_GNU_negative_offset_extended:
3325           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3326           gcc_unreachable ();
3327
3328         default:
3329           break;
3330         }
3331     }
3332 }
3333
3334 /* Similar, but do it via assembler directives instead.  */
3335
3336 static void
3337 output_cfi_directive (dw_cfi_ref cfi)
3338 {
3339   unsigned long r, r2;
3340
3341   switch (cfi->dw_cfi_opc)
3342     {
3343     case DW_CFA_advance_loc:
3344     case DW_CFA_advance_loc1:
3345     case DW_CFA_advance_loc2:
3346     case DW_CFA_advance_loc4:
3347     case DW_CFA_MIPS_advance_loc8:
3348     case DW_CFA_set_loc:
3349       /* Should only be created by add_fde_cfi in a code path not
3350          followed when emitting via directives.  The assembler is
3351          going to take care of this for us.  */
3352       gcc_unreachable ();
3353
3354     case DW_CFA_offset:
3355     case DW_CFA_offset_extended:
3356     case DW_CFA_offset_extended_sf:
3357       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3358       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3359                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3360       break;
3361
3362     case DW_CFA_restore:
3363     case DW_CFA_restore_extended:
3364       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3365       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3366       break;
3367
3368     case DW_CFA_undefined:
3369       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3370       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3371       break;
3372
3373     case DW_CFA_same_value:
3374       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3375       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3376       break;
3377
3378     case DW_CFA_def_cfa:
3379     case DW_CFA_def_cfa_sf:
3380       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3381       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3382                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3383       break;
3384
3385     case DW_CFA_def_cfa_register:
3386       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3387       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3388       break;
3389
3390     case DW_CFA_register:
3391       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3392       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3393       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3394       break;
3395
3396     case DW_CFA_def_cfa_offset:
3397     case DW_CFA_def_cfa_offset_sf:
3398       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3399                HOST_WIDE_INT_PRINT_DEC"\n",
3400                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3401       break;
3402
3403     case DW_CFA_remember_state:
3404       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3405       break;
3406     case DW_CFA_restore_state:
3407       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3408       break;
3409
3410     case DW_CFA_GNU_args_size:
3411       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3412       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3413       if (flag_debug_asm)
3414         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3415                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3416       fputc ('\n', asm_out_file);
3417       break;
3418
3419     case DW_CFA_GNU_window_save:
3420       fprintf (asm_out_file, "\t.cfi_window_save\n");
3421       break;
3422
3423     case DW_CFA_def_cfa_expression:
3424     case DW_CFA_expression:
3425       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3426       output_cfa_loc_raw (cfi);
3427       fputc ('\n', asm_out_file);
3428       break;
3429
3430     default:
3431       gcc_unreachable ();
3432     }
3433 }
3434
3435 DEF_VEC_P (dw_cfi_ref);
3436 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3437
3438 /* Output CFIs to bring current FDE to the same state as after executing
3439    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3440    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3441    other arguments to pass to output_cfi.  */
3442
3443 static void
3444 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3445 {
3446   struct dw_cfi_struct cfi_buf;
3447   dw_cfi_ref cfi2;
3448   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3449   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3450   unsigned int len, idx;
3451
3452   for (;; cfi = cfi->dw_cfi_next)
3453     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3454       {
3455       case DW_CFA_advance_loc:
3456       case DW_CFA_advance_loc1:
3457       case DW_CFA_advance_loc2:
3458       case DW_CFA_advance_loc4:
3459       case DW_CFA_MIPS_advance_loc8:
3460       case DW_CFA_set_loc:
3461         /* All advances should be ignored.  */
3462         break;
3463       case DW_CFA_remember_state:
3464         {
3465           dw_cfi_ref args_size = cfi_args_size;
3466
3467           /* Skip everything between .cfi_remember_state and
3468              .cfi_restore_state.  */
3469           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3470             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3471               break;
3472             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3473               args_size = cfi2;
3474             else
3475               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3476
3477           if (cfi2 == NULL)
3478             goto flush_all;
3479           else
3480             {
3481               cfi = cfi2;
3482               cfi_args_size = args_size;
3483             }
3484           break;
3485         }
3486       case DW_CFA_GNU_args_size:
3487         cfi_args_size = cfi;
3488         break;
3489       case DW_CFA_GNU_window_save:
3490         goto flush_all;
3491       case DW_CFA_offset:
3492       case DW_CFA_offset_extended:
3493       case DW_CFA_offset_extended_sf:
3494       case DW_CFA_restore:
3495       case DW_CFA_restore_extended:
3496       case DW_CFA_undefined:
3497       case DW_CFA_same_value:
3498       case DW_CFA_register:
3499       case DW_CFA_val_offset:
3500       case DW_CFA_val_offset_sf:
3501       case DW_CFA_expression:
3502       case DW_CFA_val_expression:
3503       case DW_CFA_GNU_negative_offset_extended:
3504         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3505           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3506                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3507         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3508         break;
3509       case DW_CFA_def_cfa:
3510       case DW_CFA_def_cfa_sf:
3511       case DW_CFA_def_cfa_expression:
3512         cfi_cfa = cfi;
3513         cfi_cfa_offset = cfi;
3514         break;
3515       case DW_CFA_def_cfa_register:
3516         cfi_cfa = cfi;
3517         break;
3518       case DW_CFA_def_cfa_offset:
3519       case DW_CFA_def_cfa_offset_sf:
3520         cfi_cfa_offset = cfi;
3521         break;
3522       case DW_CFA_nop:
3523         gcc_assert (cfi == NULL);
3524       flush_all:
3525         len = VEC_length (dw_cfi_ref, regs);
3526         for (idx = 0; idx < len; idx++)
3527           {
3528             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3529             if (cfi2 != NULL
3530                 && cfi2->dw_cfi_opc != DW_CFA_restore
3531                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3532               {
3533                 if (do_cfi_asm)
3534                   output_cfi_directive (cfi2);
3535                 else
3536                   output_cfi (cfi2, fde, for_eh);
3537               }
3538           }
3539         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3540           {
3541             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3542             cfi_buf = *cfi_cfa;
3543             switch (cfi_cfa_offset->dw_cfi_opc)
3544               {
3545               case DW_CFA_def_cfa_offset:
3546                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3547                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3548                 break;
3549               case DW_CFA_def_cfa_offset_sf:
3550                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3551                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3552                 break;
3553               case DW_CFA_def_cfa:
3554               case DW_CFA_def_cfa_sf:
3555                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3556                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3557                 break;
3558               default:
3559                 gcc_unreachable ();
3560               }
3561             cfi_cfa = &cfi_buf;
3562           }
3563         else if (cfi_cfa_offset)
3564           cfi_cfa = cfi_cfa_offset;
3565         if (cfi_cfa)
3566           {
3567             if (do_cfi_asm)
3568               output_cfi_directive (cfi_cfa);
3569             else
3570               output_cfi (cfi_cfa, fde, for_eh);
3571           }
3572         cfi_cfa = NULL;
3573         cfi_cfa_offset = NULL;
3574         if (cfi_args_size
3575             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3576           {
3577             if (do_cfi_asm)
3578               output_cfi_directive (cfi_args_size);
3579             else
3580               output_cfi (cfi_args_size, fde, for_eh);
3581           }
3582         cfi_args_size = NULL;
3583         if (cfi == NULL)
3584           {
3585             VEC_free (dw_cfi_ref, heap, regs);
3586             return;
3587           }
3588         else if (do_cfi_asm)
3589           output_cfi_directive (cfi);
3590         else
3591           output_cfi (cfi, fde, for_eh);
3592         break;
3593       default:
3594         gcc_unreachable ();
3595     }
3596 }
3597
3598 /* Output one FDE.  */
3599
3600 static void
3601 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3602             char *section_start_label, int fde_encoding, char *augmentation,
3603             bool any_lsda_needed, int lsda_encoding)
3604 {
3605   const char *begin, *end;
3606   static unsigned int j;
3607   char l1[20], l2[20];
3608   dw_cfi_ref cfi;
3609
3610   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3611                                      /* empty */ 0);
3612   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3613                                   for_eh + j);
3614   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3615   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3616   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3617     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3618                          " indicating 64-bit DWARF extension");
3619   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3620                         "FDE Length");
3621   ASM_OUTPUT_LABEL (asm_out_file, l1);
3622
3623   if (for_eh)
3624     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3625   else
3626     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3627                            debug_frame_section, "FDE CIE offset");
3628
3629   if (!fde->dw_fde_switched_sections)
3630     {
3631       begin = fde->dw_fde_begin;
3632       end = fde->dw_fde_end;
3633     }
3634   else
3635     {
3636       /* For the first section, prefer dw_fde_begin over
3637          dw_fde_{hot,cold}_section_label, as the latter
3638          might be separated from the real start of the
3639          function by alignment padding.  */
3640       if (!second)
3641         begin = fde->dw_fde_begin;
3642       else if (fde->dw_fde_switched_cold_to_hot)
3643         begin = fde->dw_fde_hot_section_label;
3644       else
3645         begin = fde->dw_fde_unlikely_section_label;
3646       if (second ^ fde->dw_fde_switched_cold_to_hot)
3647         end = fde->dw_fde_unlikely_section_end_label;
3648       else
3649         end = fde->dw_fde_hot_section_end_label;
3650     }
3651
3652   if (for_eh)
3653     {
3654       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3655       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3656       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3657                                        "FDE initial location");
3658       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3659                             end, begin, "FDE address range");
3660     }
3661   else
3662     {
3663       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3664       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3665     }
3666
3667   if (augmentation[0])
3668     {
3669       if (any_lsda_needed)
3670         {
3671           int size = size_of_encoded_value (lsda_encoding);
3672
3673           if (lsda_encoding == DW_EH_PE_aligned)
3674             {
3675               int offset = (  4         /* Length */
3676                             + 4         /* CIE offset */
3677                             + 2 * size_of_encoded_value (fde_encoding)
3678                             + 1         /* Augmentation size */ );
3679               int pad = -offset & (PTR_SIZE - 1);
3680
3681               size += pad;
3682               gcc_assert (size_of_uleb128 (size) == 1);
3683             }
3684
3685           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3686
3687           if (fde->uses_eh_lsda)
3688             {
3689               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3690                                            fde->funcdef_number);
3691               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3692                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3693                                                false,
3694                                                "Language Specific Data Area");
3695             }
3696           else
3697             {
3698               if (lsda_encoding == DW_EH_PE_aligned)
3699                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3700               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3701                                    "Language Specific Data Area (none)");
3702             }
3703         }
3704       else
3705         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3706     }
3707
3708   /* Loop through the Call Frame Instructions associated with
3709      this FDE.  */
3710   fde->dw_fde_current_label = begin;
3711   if (!fde->dw_fde_switched_sections)
3712     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3713       output_cfi (cfi, fde, for_eh);
3714   else if (!second)
3715     {
3716       if (fde->dw_fde_switch_cfi)
3717         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3718           {
3719             output_cfi (cfi, fde, for_eh);
3720             if (cfi == fde->dw_fde_switch_cfi)
3721               break;
3722           }
3723     }
3724   else
3725     {
3726       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3727
3728       if (fde->dw_fde_switch_cfi)
3729         {
3730           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3731           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3732           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3733           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3734         }
3735       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3736         output_cfi (cfi, fde, for_eh);
3737     }
3738
3739   /* If we are to emit a ref/link from function bodies to their frame tables,
3740      do it now.  This is typically performed to make sure that tables
3741      associated with functions are dragged with them and not discarded in
3742      garbage collecting links. We need to do this on a per function basis to
3743      cope with -ffunction-sections.  */
3744
3745 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3746   /* Switch to the function section, emit the ref to the tables, and
3747      switch *back* into the table section.  */
3748   switch_to_section (function_section (fde->decl));
3749   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3750   switch_to_frame_table_section (for_eh, true);
3751 #endif
3752
3753   /* Pad the FDE out to an address sized boundary.  */
3754   ASM_OUTPUT_ALIGN (asm_out_file,
3755                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3756   ASM_OUTPUT_LABEL (asm_out_file, l2);
3757
3758   j += 2;
3759 }
3760
3761 /* Return true if frame description entry FDE is needed for EH.  */
3762
3763 static bool
3764 fde_needed_for_eh_p (dw_fde_ref fde)
3765 {
3766   if (flag_asynchronous_unwind_tables)
3767     return true;
3768
3769   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3770     return true;
3771
3772   if (fde->uses_eh_lsda)
3773     return true;
3774
3775   /* If exceptions are enabled, we have collected nothrow info.  */
3776   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3777     return false;
3778
3779   return true;
3780 }
3781
3782 /* Output the call frame information used to record information
3783    that relates to calculating the frame pointer, and records the
3784    location of saved registers.  */
3785
3786 static void
3787 output_call_frame_info (int for_eh)
3788 {
3789   unsigned int i;
3790   dw_fde_ref fde;
3791   dw_cfi_ref cfi;
3792   char l1[20], l2[20], section_start_label[20];
3793   bool any_lsda_needed = false;
3794   char augmentation[6];
3795   int augmentation_size;
3796   int fde_encoding = DW_EH_PE_absptr;
3797   int per_encoding = DW_EH_PE_absptr;
3798   int lsda_encoding = DW_EH_PE_absptr;
3799   int return_reg;
3800   rtx personality = NULL;
3801   int dw_cie_version;
3802
3803   /* Don't emit a CIE if there won't be any FDEs.  */
3804   if (fde_table_in_use == 0)
3805     return;
3806
3807   /* Nothing to do if the assembler's doing it all.  */
3808   if (dwarf2out_do_cfi_asm ())
3809     return;
3810
3811   /* If we don't have any functions we'll want to unwind out of, don't emit
3812      any EH unwind information.  If we make FDEs linkonce, we may have to
3813      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3814      want to avoid having an FDE kept around when the function it refers to
3815      is discarded.  Example where this matters: a primary function template
3816      in C++ requires EH information, an explicit specialization doesn't.  */
3817   if (for_eh)
3818     {
3819       bool any_eh_needed = false;
3820
3821       for (i = 0; i < fde_table_in_use; i++)
3822         if (fde_table[i].uses_eh_lsda)
3823           any_eh_needed = any_lsda_needed = true;
3824         else if (fde_needed_for_eh_p (&fde_table[i]))
3825           any_eh_needed = true;
3826         else if (TARGET_USES_WEAK_UNWIND_INFO)
3827           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3828                                              1, 1);
3829
3830       if (!any_eh_needed)
3831         return;
3832     }
3833
3834   /* We're going to be generating comments, so turn on app.  */
3835   if (flag_debug_asm)
3836     app_enable ();
3837
3838   /* Switch to the proper frame section, first time.  */
3839   switch_to_frame_table_section (for_eh, false);
3840
3841   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3842   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3843
3844   /* Output the CIE.  */
3845   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3846   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3847   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3848     dw2_asm_output_data (4, 0xffffffff,
3849       "Initial length escape value indicating 64-bit DWARF extension");
3850   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3851                         "Length of Common Information Entry");
3852   ASM_OUTPUT_LABEL (asm_out_file, l1);
3853
3854   /* Now that the CIE pointer is PC-relative for EH,
3855      use 0 to identify the CIE.  */
3856   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3857                        (for_eh ? 0 : DWARF_CIE_ID),
3858                        "CIE Identifier Tag");
3859
3860   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3861      use CIE version 1, unless that would produce incorrect results
3862      due to overflowing the return register column.  */
3863   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3864   dw_cie_version = 1;
3865   if (return_reg >= 256 || dwarf_version > 2)
3866     dw_cie_version = 3;
3867   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3868
3869   augmentation[0] = 0;
3870   augmentation_size = 0;
3871
3872   personality = current_unit_personality;
3873   if (for_eh)
3874     {
3875       char *p;
3876
3877       /* Augmentation:
3878          z      Indicates that a uleb128 is present to size the
3879                 augmentation section.
3880          L      Indicates the encoding (and thus presence) of
3881                 an LSDA pointer in the FDE augmentation.
3882          R      Indicates a non-default pointer encoding for
3883                 FDE code pointers.
3884          P      Indicates the presence of an encoding + language
3885                 personality routine in the CIE augmentation.  */
3886
3887       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3888       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3889       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3890
3891       p = augmentation + 1;
3892       if (personality)
3893         {
3894           *p++ = 'P';
3895           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3896           assemble_external_libcall (personality);
3897         }
3898       if (any_lsda_needed)
3899         {
3900           *p++ = 'L';
3901           augmentation_size += 1;
3902         }
3903       if (fde_encoding != DW_EH_PE_absptr)
3904         {
3905           *p++ = 'R';
3906           augmentation_size += 1;
3907         }
3908       if (p > augmentation + 1)
3909         {
3910           augmentation[0] = 'z';
3911           *p = '\0';
3912         }
3913
3914       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3915       if (personality && per_encoding == DW_EH_PE_aligned)
3916         {
3917           int offset = (  4             /* Length */
3918                         + 4             /* CIE Id */
3919                         + 1             /* CIE version */
3920                         + strlen (augmentation) + 1     /* Augmentation */
3921                         + size_of_uleb128 (1)           /* Code alignment */
3922                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3923                         + 1             /* RA column */
3924                         + 1             /* Augmentation size */
3925                         + 1             /* Personality encoding */ );
3926           int pad = -offset & (PTR_SIZE - 1);
3927
3928           augmentation_size += pad;
3929
3930           /* Augmentations should be small, so there's scarce need to
3931              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3932           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3933         }
3934     }
3935
3936   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3937   if (dw_cie_version >= 4)
3938     {
3939       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3940       dw2_asm_output_data (1, 0, "CIE Segment Size");
3941     }
3942   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3943   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3944                                "CIE Data Alignment Factor");
3945
3946   if (dw_cie_version == 1)
3947     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3948   else
3949     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3950
3951   if (augmentation[0])
3952     {
3953       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3954       if (personality)
3955         {
3956           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3957                                eh_data_format_name (per_encoding));
3958           dw2_asm_output_encoded_addr_rtx (per_encoding,
3959                                            personality,
3960                                            true, NULL);
3961         }
3962
3963       if (any_lsda_needed)
3964         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3965                              eh_data_format_name (lsda_encoding));
3966
3967       if (fde_encoding != DW_EH_PE_absptr)
3968         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3969                              eh_data_format_name (fde_encoding));
3970     }
3971
3972   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3973     output_cfi (cfi, NULL, for_eh);
3974
3975   /* Pad the CIE out to an address sized boundary.  */
3976   ASM_OUTPUT_ALIGN (asm_out_file,
3977                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3978   ASM_OUTPUT_LABEL (asm_out_file, l2);
3979
3980   /* Loop through all of the FDE's.  */
3981   for (i = 0; i < fde_table_in_use; i++)
3982     {
3983       unsigned int k;
3984       fde = &fde_table[i];
3985
3986       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3987       if (for_eh && !fde_needed_for_eh_p (fde))
3988         continue;
3989
3990       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3991         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3992                     augmentation, any_lsda_needed, lsda_encoding);
3993     }
3994
3995   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3996     dw2_asm_output_data (4, 0, "End of Table");
3997 #ifdef MIPS_DEBUGGING_INFO
3998   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3999      get a value of 0.  Putting .align 0 after the label fixes it.  */
4000   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4001 #endif
4002
4003   /* Turn off app to make assembly quicker.  */
4004   if (flag_debug_asm)
4005     app_disable ();
4006 }
4007
4008 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4009
4010 static void
4011 dwarf2out_do_cfi_startproc (bool second)
4012 {
4013   int enc;
4014   rtx ref;
4015   rtx personality = get_personality_function (current_function_decl);
4016
4017   fprintf (asm_out_file, "\t.cfi_startproc\n");
4018
4019   if (personality)
4020     {
4021       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4022       ref = personality;
4023
4024       /* ??? The GAS support isn't entirely consistent.  We have to
4025          handle indirect support ourselves, but PC-relative is done
4026          in the assembler.  Further, the assembler can't handle any
4027          of the weirder relocation types.  */
4028       if (enc & DW_EH_PE_indirect)
4029         ref = dw2_force_const_mem (ref, true);
4030
4031       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4032       output_addr_const (asm_out_file, ref);
4033       fputc ('\n', asm_out_file);
4034     }
4035
4036   if (crtl->uses_eh_lsda)
4037     {
4038       char lab[20];
4039
4040       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4041       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4042                                    current_function_funcdef_no);
4043       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4044       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4045
4046       if (enc & DW_EH_PE_indirect)
4047         ref = dw2_force_const_mem (ref, true);
4048
4049       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4050       output_addr_const (asm_out_file, ref);
4051       fputc ('\n', asm_out_file);
4052     }
4053 }
4054
4055 /* Output a marker (i.e. a label) for the beginning of a function, before
4056    the prologue.  */
4057
4058 void
4059 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4060                           const char *file ATTRIBUTE_UNUSED)
4061 {
4062   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4063   char * dup_label;
4064   dw_fde_ref fde;
4065   section *fnsec;
4066   bool do_frame;
4067
4068   current_function_func_begin_label = NULL;
4069
4070   do_frame = dwarf2out_do_frame ();
4071
4072   /* ??? current_function_func_begin_label is also used by except.c for
4073      call-site information.  We must emit this label if it might be used.  */
4074   if (!do_frame
4075       && (!flag_exceptions
4076           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4077     return;
4078
4079   fnsec = function_section (current_function_decl);
4080   switch_to_section (fnsec);
4081   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4082                                current_function_funcdef_no);
4083   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4084                           current_function_funcdef_no);
4085   dup_label = xstrdup (label);
4086   current_function_func_begin_label = dup_label;
4087
4088   /* We can elide the fde allocation if we're not emitting debug info.  */
4089   if (!do_frame)
4090     return;
4091
4092   /* Expand the fde table if necessary.  */
4093   if (fde_table_in_use == fde_table_allocated)
4094     {
4095       fde_table_allocated += FDE_TABLE_INCREMENT;
4096       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4097       memset (fde_table + fde_table_in_use, 0,
4098               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4099     }
4100
4101   /* Record the FDE associated with this function.  */
4102   current_funcdef_fde = fde_table_in_use;
4103
4104   /* Add the new FDE at the end of the fde_table.  */
4105   fde = &fde_table[fde_table_in_use++];
4106   fde->decl = current_function_decl;
4107   fde->dw_fde_begin = dup_label;
4108   fde->dw_fde_current_label = dup_label;
4109   fde->dw_fde_hot_section_label = NULL;
4110   fde->dw_fde_hot_section_end_label = NULL;
4111   fde->dw_fde_unlikely_section_label = NULL;
4112   fde->dw_fde_unlikely_section_end_label = NULL;
4113   fde->dw_fde_switched_sections = 0;
4114   fde->dw_fde_switched_cold_to_hot = 0;
4115   fde->dw_fde_end = NULL;
4116   fde->dw_fde_vms_end_prologue = NULL;
4117   fde->dw_fde_vms_begin_epilogue = NULL;
4118   fde->dw_fde_cfi = NULL;
4119   fde->dw_fde_switch_cfi = NULL;
4120   fde->funcdef_number = current_function_funcdef_no;
4121   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4122   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4123   fde->nothrow = crtl->nothrow;
4124   fde->drap_reg = INVALID_REGNUM;
4125   fde->vdrap_reg = INVALID_REGNUM;
4126   if (flag_reorder_blocks_and_partition)
4127     {
4128       section *unlikelysec;
4129       if (first_function_block_is_cold)
4130         fde->in_std_section = 1;
4131       else
4132         fde->in_std_section
4133           = (fnsec == text_section
4134              || (cold_text_section && fnsec == cold_text_section));
4135       unlikelysec = unlikely_text_section ();
4136       fde->cold_in_std_section
4137         = (unlikelysec == text_section
4138            || (cold_text_section && unlikelysec == cold_text_section));
4139     }
4140   else
4141     {
4142       fde->in_std_section
4143         = (fnsec == text_section
4144            || (cold_text_section && fnsec == cold_text_section));
4145       fde->cold_in_std_section = 0;
4146     }
4147
4148   args_size = old_args_size = 0;
4149
4150   /* We only want to output line number information for the genuine dwarf2
4151      prologue case, not the eh frame case.  */
4152 #ifdef DWARF2_DEBUGGING_INFO
4153   if (file)
4154     dwarf2out_source_line (line, file, 0, true);
4155 #endif
4156
4157   if (dwarf2out_do_cfi_asm ())
4158     dwarf2out_do_cfi_startproc (false);
4159   else
4160     {
4161       rtx personality = get_personality_function (current_function_decl);
4162       if (!current_unit_personality)
4163         current_unit_personality = personality;
4164
4165       /* We cannot keep a current personality per function as without CFI
4166          asm, at the point where we emit the CFI data, there is no current
4167          function anymore.  */
4168       if (personality && current_unit_personality != personality)
4169         sorry ("multiple EH personalities are supported only with assemblers "
4170                "supporting .cfi_personality directive");
4171     }
4172 }
4173
4174 /* Output a marker (i.e. a label) for the end of the generated code
4175    for a function prologue.  This gets called *after* the prologue code has
4176    been generated.  */
4177
4178 void
4179 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4180                         const char *file ATTRIBUTE_UNUSED)
4181 {
4182   dw_fde_ref fde;
4183   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4184
4185   /* Output a label to mark the endpoint of the code generated for this
4186      function.  */
4187   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4188                                current_function_funcdef_no);
4189   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4190                           current_function_funcdef_no);
4191   fde = &fde_table[fde_table_in_use - 1];
4192   fde->dw_fde_vms_end_prologue = xstrdup (label);
4193 }
4194
4195 /* Output a marker (i.e. a label) for the beginning of the generated code
4196    for a function epilogue.  This gets called *before* the prologue code has
4197    been generated.  */
4198
4199 void
4200 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4201                           const char *file ATTRIBUTE_UNUSED)
4202 {
4203   dw_fde_ref fde;
4204   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4205
4206   fde = &fde_table[fde_table_in_use - 1];
4207   if (fde->dw_fde_vms_begin_epilogue)
4208     return;
4209
4210   /* Output a label to mark the endpoint of the code generated for this
4211      function.  */
4212   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4213                                current_function_funcdef_no);
4214   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4215                           current_function_funcdef_no);
4216   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4217 }
4218
4219 /* Output a marker (i.e. a label) for the absolute end of the generated code
4220    for a function definition.  This gets called *after* the epilogue code has
4221    been generated.  */
4222
4223 void
4224 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4225                         const char *file ATTRIBUTE_UNUSED)
4226 {
4227   dw_fde_ref fde;
4228   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4229
4230   last_var_location_insn = NULL_RTX;
4231
4232   if (dwarf2out_do_cfi_asm ())
4233     fprintf (asm_out_file, "\t.cfi_endproc\n");
4234
4235   /* Output a label to mark the endpoint of the code generated for this
4236      function.  */
4237   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4238                                current_function_funcdef_no);
4239   ASM_OUTPUT_LABEL (asm_out_file, label);
4240   fde = current_fde ();
4241   gcc_assert (fde != NULL);
4242   fde->dw_fde_end = xstrdup (label);
4243 }
4244
4245 void
4246 dwarf2out_frame_init (void)
4247 {
4248   /* Allocate the initial hunk of the fde_table.  */
4249   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4250   fde_table_allocated = FDE_TABLE_INCREMENT;
4251   fde_table_in_use = 0;
4252
4253   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4254      sake of lookup_cfa.  */
4255
4256   /* On entry, the Canonical Frame Address is at SP.  */
4257   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4258
4259   if (targetm.debug_unwind_info () == UI_DWARF2
4260       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4261     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4262 }
4263
4264 void
4265 dwarf2out_frame_finish (void)
4266 {
4267   /* Output call frame information.  */
4268   if (targetm.debug_unwind_info () == UI_DWARF2)
4269     output_call_frame_info (0);
4270
4271   /* Output another copy for the unwinder.  */
4272   if ((flag_unwind_tables || flag_exceptions)
4273       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4274     output_call_frame_info (1);
4275 }
4276
4277 /* Note that the current function section is being used for code.  */
4278
4279 static void
4280 dwarf2out_note_section_used (void)
4281 {
4282   section *sec = current_function_section ();
4283   if (sec == text_section)
4284     text_section_used = true;
4285   else if (sec == cold_text_section)
4286     cold_text_section_used = true;
4287 }
4288
4289 void
4290 dwarf2out_switch_text_section (void)
4291 {
4292   dw_fde_ref fde = current_fde ();
4293
4294   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4295
4296   fde->dw_fde_switched_sections = 1;
4297   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4298
4299   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4300   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4301   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4302   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4303   have_multiple_function_sections = true;
4304
4305   /* Reset the current label on switching text sections, so that we
4306      don't attempt to advance_loc4 between labels in different sections.  */
4307   fde->dw_fde_current_label = NULL;
4308
4309   /* There is no need to mark used sections when not debugging.  */
4310   if (cold_text_section != NULL)
4311     dwarf2out_note_section_used ();
4312
4313   if (dwarf2out_do_cfi_asm ())
4314     fprintf (asm_out_file, "\t.cfi_endproc\n");
4315
4316   /* Now do the real section switch.  */
4317   switch_to_section (current_function_section ());
4318
4319   if (dwarf2out_do_cfi_asm ())
4320     {
4321       dwarf2out_do_cfi_startproc (true);
4322       /* As this is a different FDE, insert all current CFI instructions
4323          again.  */
4324       output_cfis (fde->dw_fde_cfi, true, fde, true);
4325     }
4326   else
4327     {
4328       dw_cfi_ref cfi = fde->dw_fde_cfi;
4329
4330       cfi = fde->dw_fde_cfi;
4331       if (cfi)
4332         while (cfi->dw_cfi_next != NULL)
4333           cfi = cfi->dw_cfi_next;
4334       fde->dw_fde_switch_cfi = cfi;
4335     }
4336 }
4337 \f
4338 /* And now, the subset of the debugging information support code necessary
4339    for emitting location expressions.  */
4340
4341 /* Data about a single source file.  */
4342 struct GTY(()) dwarf_file_data {
4343   const char * filename;
4344   int emitted_number;
4345 };
4346
4347 typedef struct dw_val_struct *dw_val_ref;
4348 typedef struct die_struct *dw_die_ref;
4349 typedef const struct die_struct *const_dw_die_ref;
4350 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4351 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4352
4353 typedef struct GTY(()) deferred_locations_struct
4354 {
4355   tree variable;
4356   dw_die_ref die;
4357 } deferred_locations;
4358
4359 DEF_VEC_O(deferred_locations);
4360 DEF_VEC_ALLOC_O(deferred_locations,gc);
4361
4362 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4363
4364 DEF_VEC_P(dw_die_ref);
4365 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4366
4367 /* Each DIE may have a series of attribute/value pairs.  Values
4368    can take on several forms.  The forms that are used in this
4369    implementation are listed below.  */
4370
4371 enum dw_val_class
4372 {
4373   dw_val_class_addr,
4374   dw_val_class_offset,
4375   dw_val_class_loc,
4376   dw_val_class_loc_list,
4377   dw_val_class_range_list,
4378   dw_val_class_const,
4379   dw_val_class_unsigned_const,
4380   dw_val_class_const_double,
4381   dw_val_class_vec,
4382   dw_val_class_flag,
4383   dw_val_class_die_ref,
4384   dw_val_class_fde_ref,
4385   dw_val_class_lbl_id,
4386   dw_val_class_lineptr,
4387   dw_val_class_str,
4388   dw_val_class_macptr,
4389   dw_val_class_file,
4390   dw_val_class_data8,
4391   dw_val_class_decl_ref,
4392   dw_val_class_vms_delta
4393 };
4394
4395 /* Describe a floating point constant value, or a vector constant value.  */
4396
4397 typedef struct GTY(()) dw_vec_struct {
4398   unsigned char * GTY((length ("%h.length"))) array;
4399   unsigned length;
4400   unsigned elt_size;
4401 }
4402 dw_vec_const;
4403
4404 /* The dw_val_node describes an attribute's value, as it is
4405    represented internally.  */
4406
4407 typedef struct GTY(()) dw_val_struct {
4408   enum dw_val_class val_class;
4409   union dw_val_struct_union
4410     {
4411       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4412       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4413       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4414       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4415       HOST_WIDE_INT GTY ((default)) val_int;
4416       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4417       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4418       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4419       struct dw_val_die_union
4420         {
4421           dw_die_ref die;
4422           int external;
4423         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4424       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4425       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4426       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4427       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4428       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4429       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4430       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4431       struct dw_val_vms_delta_union
4432         {
4433           char * lbl1;
4434           char * lbl2;
4435         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4436     }
4437   GTY ((desc ("%1.val_class"))) v;
4438 }
4439 dw_val_node;
4440
4441 /* Locations in memory are described using a sequence of stack machine
4442    operations.  */
4443
4444 typedef struct GTY(()) dw_loc_descr_struct {
4445   dw_loc_descr_ref dw_loc_next;
4446   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4447   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4448      from DW_OP_addr with a dtp-relative symbol relocation.  */
4449   unsigned int dtprel : 1;
4450   int dw_loc_addr;
4451   dw_val_node dw_loc_oprnd1;
4452   dw_val_node dw_loc_oprnd2;
4453 }
4454 dw_loc_descr_node;
4455
4456 /* Location lists are ranges + location descriptions for that range,
4457    so you can track variables that are in different places over
4458    their entire life.  */
4459 typedef struct GTY(()) dw_loc_list_struct {
4460   dw_loc_list_ref dw_loc_next;
4461   const char *begin; /* Label for begin address of range */
4462   const char *end;  /* Label for end address of range */
4463   char *ll_symbol; /* Label for beginning of location list.
4464                       Only on head of list */
4465   const char *section; /* Section this loclist is relative to */
4466   dw_loc_descr_ref expr;
4467   hashval_t hash;
4468   bool emitted;
4469 } dw_loc_list_node;
4470
4471 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4472
4473 /* Convert a DWARF stack opcode into its string name.  */
4474
4475 static const char *
4476 dwarf_stack_op_name (unsigned int op)
4477 {
4478   switch (op)
4479     {
4480     case DW_OP_addr:
4481       return "DW_OP_addr";
4482     case DW_OP_deref:
4483       return "DW_OP_deref";
4484     case DW_OP_const1u:
4485       return "DW_OP_const1u";
4486     case DW_OP_const1s:
4487       return "DW_OP_const1s";
4488     case DW_OP_const2u:
4489       return "DW_OP_const2u";
4490     case DW_OP_const2s:
4491       return "DW_OP_const2s";
4492     case DW_OP_const4u:
4493       return "DW_OP_const4u";
4494     case DW_OP_const4s:
4495       return "DW_OP_const4s";
4496     case DW_OP_const8u:
4497       return "DW_OP_const8u";
4498     case DW_OP_const8s:
4499       return "DW_OP_const8s";
4500     case DW_OP_constu:
4501       return "DW_OP_constu";
4502     case DW_OP_consts:
4503       return "DW_OP_consts";
4504     case DW_OP_dup:
4505       return "DW_OP_dup";
4506     case DW_OP_drop:
4507       return "DW_OP_drop";
4508     case DW_OP_over:
4509       return "DW_OP_over";
4510     case DW_OP_pick:
4511       return "DW_OP_pick";
4512     case DW_OP_swap:
4513       return "DW_OP_swap";
4514     case DW_OP_rot:
4515       return "DW_OP_rot";
4516     case DW_OP_xderef:
4517       return "DW_OP_xderef";
4518     case DW_OP_abs:
4519       return "DW_OP_abs";
4520     case DW_OP_and:
4521       return "DW_OP_and";
4522     case DW_OP_div:
4523       return "DW_OP_div";
4524     case DW_OP_minus:
4525       return "DW_OP_minus";
4526     case DW_OP_mod:
4527       return "DW_OP_mod";
4528     case DW_OP_mul:
4529       return "DW_OP_mul";
4530     case DW_OP_neg:
4531       return "DW_OP_neg";
4532     case DW_OP_not:
4533       return "DW_OP_not";
4534     case DW_OP_or:
4535       return "DW_OP_or";
4536     case DW_OP_plus:
4537       return "DW_OP_plus";
4538     case DW_OP_plus_uconst:
4539       return "DW_OP_plus_uconst";
4540     case DW_OP_shl:
4541       return "DW_OP_shl";
4542     case DW_OP_shr:
4543       return "DW_OP_shr";
4544     case DW_OP_shra:
4545       return "DW_OP_shra";
4546     case DW_OP_xor:
4547       return "DW_OP_xor";
4548     case DW_OP_bra:
4549       return "DW_OP_bra";
4550     case DW_OP_eq:
4551       return "DW_OP_eq";
4552     case DW_OP_ge:
4553       return "DW_OP_ge";
4554     case DW_OP_gt:
4555       return "DW_OP_gt";
4556     case DW_OP_le:
4557       return "DW_OP_le";
4558     case DW_OP_lt:
4559       return "DW_OP_lt";
4560     case DW_OP_ne:
4561       return "DW_OP_ne";
4562     case DW_OP_skip:
4563       return "DW_OP_skip";
4564     case DW_OP_lit0:
4565       return "DW_OP_lit0";
4566     case DW_OP_lit1:
4567       return "DW_OP_lit1";
4568     case DW_OP_lit2:
4569       return "DW_OP_lit2";
4570     case DW_OP_lit3:
4571       return "DW_OP_lit3";
4572     case DW_OP_lit4:
4573       return "DW_OP_lit4";
4574     case DW_OP_lit5:
4575       return "DW_OP_lit5";
4576     case DW_OP_lit6:
4577       return "DW_OP_lit6";
4578     case DW_OP_lit7:
4579       return "DW_OP_lit7";
4580     case DW_OP_lit8:
4581       return "DW_OP_lit8";
4582     case DW_OP_lit9:
4583       return "DW_OP_lit9";
4584     case DW_OP_lit10:
4585       return "DW_OP_lit10";
4586     case DW_OP_lit11:
4587       return "DW_OP_lit11";
4588     case DW_OP_lit12:
4589       return "DW_OP_lit12";
4590     case DW_OP_lit13:
4591       return "DW_OP_lit13";
4592     case DW_OP_lit14:
4593       return "DW_OP_lit14";
4594     case DW_OP_lit15:
4595       return "DW_OP_lit15";
4596     case DW_OP_lit16:
4597       return "DW_OP_lit16";
4598     case DW_OP_lit17:
4599       return "DW_OP_lit17";
4600     case DW_OP_lit18:
4601       return "DW_OP_lit18";
4602     case DW_OP_lit19:
4603       return "DW_OP_lit19";
4604     case DW_OP_lit20:
4605       return "DW_OP_lit20";
4606     case DW_OP_lit21:
4607       return "DW_OP_lit21";
4608     case DW_OP_lit22:
4609       return "DW_OP_lit22";
4610     case DW_OP_lit23:
4611       return "DW_OP_lit23";
4612     case DW_OP_lit24:
4613       return "DW_OP_lit24";
4614     case DW_OP_lit25:
4615       return "DW_OP_lit25";
4616     case DW_OP_lit26:
4617       return "DW_OP_lit26";
4618     case DW_OP_lit27:
4619       return "DW_OP_lit27";
4620     case DW_OP_lit28:
4621       return "DW_OP_lit28";
4622     case DW_OP_lit29:
4623       return "DW_OP_lit29";
4624     case DW_OP_lit30:
4625       return "DW_OP_lit30";
4626     case DW_OP_lit31:
4627       return "DW_OP_lit31";
4628     case DW_OP_reg0:
4629       return "DW_OP_reg0";
4630     case DW_OP_reg1:
4631       return "DW_OP_reg1";
4632     case DW_OP_reg2:
4633       return "DW_OP_reg2";
4634     case DW_OP_reg3:
4635       return "DW_OP_reg3";
4636     case DW_OP_reg4:
4637       return "DW_OP_reg4";
4638     case DW_OP_reg5:
4639       return "DW_OP_reg5";
4640     case DW_OP_reg6:
4641       return "DW_OP_reg6";
4642     case DW_OP_reg7:
4643       return "DW_OP_reg7";
4644     case DW_OP_reg8:
4645       return "DW_OP_reg8";
4646     case DW_OP_reg9:
4647       return "DW_OP_reg9";
4648     case DW_OP_reg10:
4649       return "DW_OP_reg10";
4650     case DW_OP_reg11:
4651       return "DW_OP_reg11";
4652     case DW_OP_reg12:
4653       return "DW_OP_reg12";
4654     case DW_OP_reg13:
4655       return "DW_OP_reg13";
4656     case DW_OP_reg14:
4657       return "DW_OP_reg14";
4658     case DW_OP_reg15:
4659       return "DW_OP_reg15";
4660     case DW_OP_reg16:
4661       return "DW_OP_reg16";
4662     case DW_OP_reg17:
4663       return "DW_OP_reg17";
4664     case DW_OP_reg18:
4665       return "DW_OP_reg18";
4666     case DW_OP_reg19:
4667       return "DW_OP_reg19";
4668     case DW_OP_reg20:
4669       return "DW_OP_reg20";
4670     case DW_OP_reg21:
4671       return "DW_OP_reg21";
4672     case DW_OP_reg22:
4673       return "DW_OP_reg22";
4674     case DW_OP_reg23:
4675       return "DW_OP_reg23";
4676     case DW_OP_reg24:
4677       return "DW_OP_reg24";
4678     case DW_OP_reg25:
4679       return "DW_OP_reg25";
4680     case DW_OP_reg26:
4681       return "DW_OP_reg26";
4682     case DW_OP_reg27:
4683       return "DW_OP_reg27";
4684     case DW_OP_reg28:
4685       return "DW_OP_reg28";
4686     case DW_OP_reg29:
4687       return "DW_OP_reg29";
4688     case DW_OP_reg30:
4689       return "DW_OP_reg30";
4690     case DW_OP_reg31:
4691       return "DW_OP_reg31";
4692     case DW_OP_breg0:
4693       return "DW_OP_breg0";
4694     case DW_OP_breg1:
4695       return "DW_OP_breg1";
4696     case DW_OP_breg2:
4697       return "DW_OP_breg2";
4698     case DW_OP_breg3:
4699       return "DW_OP_breg3";
4700     case DW_OP_breg4:
4701       return "DW_OP_breg4";
4702     case DW_OP_breg5:
4703       return "DW_OP_breg5";
4704     case DW_OP_breg6:
4705       return "DW_OP_breg6";
4706     case DW_OP_breg7:
4707       return "DW_OP_breg7";
4708     case DW_OP_breg8:
4709       return "DW_OP_breg8";
4710     case DW_OP_breg9:
4711       return "DW_OP_breg9";
4712     case DW_OP_breg10:
4713       return "DW_OP_breg10";
4714     case DW_OP_breg11:
4715       return "DW_OP_breg11";
4716     case DW_OP_breg12:
4717       return "DW_OP_breg12";
4718     case DW_OP_breg13:
4719       return "DW_OP_breg13";
4720     case DW_OP_breg14:
4721       return "DW_OP_breg14";
4722     case DW_OP_breg15:
4723       return "DW_OP_breg15";
4724     case DW_OP_breg16:
4725       return "DW_OP_breg16";
4726     case DW_OP_breg17:
4727       return "DW_OP_breg17";
4728     case DW_OP_breg18:
4729       return "DW_OP_breg18";
4730     case DW_OP_breg19:
4731       return "DW_OP_breg19";
4732     case DW_OP_breg20:
4733       return "DW_OP_breg20";
4734     case DW_OP_breg21:
4735       return "DW_OP_breg21";
4736     case DW_OP_breg22:
4737       return "DW_OP_breg22";
4738     case DW_OP_breg23:
4739       return "DW_OP_breg23";
4740     case DW_OP_breg24:
4741       return "DW_OP_breg24";
4742     case DW_OP_breg25:
4743       return "DW_OP_breg25";
4744     case DW_OP_breg26:
4745       return "DW_OP_breg26";
4746     case DW_OP_breg27:
4747       return "DW_OP_breg27";
4748     case DW_OP_breg28:
4749       return "DW_OP_breg28";
4750     case DW_OP_breg29:
4751       return "DW_OP_breg29";
4752     case DW_OP_breg30:
4753       return "DW_OP_breg30";
4754     case DW_OP_breg31:
4755       return "DW_OP_breg31";
4756     case DW_OP_regx:
4757       return "DW_OP_regx";
4758     case DW_OP_fbreg:
4759       return "DW_OP_fbreg";
4760     case DW_OP_bregx:
4761       return "DW_OP_bregx";
4762     case DW_OP_piece:
4763       return "DW_OP_piece";
4764     case DW_OP_deref_size:
4765       return "DW_OP_deref_size";
4766     case DW_OP_xderef_size:
4767       return "DW_OP_xderef_size";
4768     case DW_OP_nop:
4769       return "DW_OP_nop";
4770
4771     case DW_OP_push_object_address:
4772       return "DW_OP_push_object_address";
4773     case DW_OP_call2:
4774       return "DW_OP_call2";
4775     case DW_OP_call4:
4776       return "DW_OP_call4";
4777     case DW_OP_call_ref:
4778       return "DW_OP_call_ref";
4779     case DW_OP_implicit_value:
4780       return "DW_OP_implicit_value";
4781     case DW_OP_stack_value:
4782       return "DW_OP_stack_value";
4783     case DW_OP_form_tls_address:
4784       return "DW_OP_form_tls_address";
4785     case DW_OP_call_frame_cfa:
4786       return "DW_OP_call_frame_cfa";
4787     case DW_OP_bit_piece:
4788       return "DW_OP_bit_piece";
4789
4790     case DW_OP_GNU_push_tls_address:
4791       return "DW_OP_GNU_push_tls_address";
4792     case DW_OP_GNU_uninit:
4793       return "DW_OP_GNU_uninit";
4794     case DW_OP_GNU_encoded_addr:
4795       return "DW_OP_GNU_encoded_addr";
4796     case DW_OP_GNU_implicit_pointer:
4797       return "DW_OP_GNU_implicit_pointer";
4798     case DW_OP_GNU_entry_value:
4799       return "DW_OP_GNU_entry_value";
4800
4801     default:
4802       return "OP_<unknown>";
4803     }
4804 }
4805
4806 /* Return a pointer to a newly allocated location description.  Location
4807    descriptions are simple expression terms that can be strung
4808    together to form more complicated location (address) descriptions.  */
4809
4810 static inline dw_loc_descr_ref
4811 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4812                unsigned HOST_WIDE_INT oprnd2)
4813 {
4814   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4815
4816   descr->dw_loc_opc = op;
4817   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4818   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4819   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4820   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4821
4822   return descr;
4823 }
4824
4825 /* Return a pointer to a newly allocated location description for
4826    REG and OFFSET.  */
4827
4828 static inline dw_loc_descr_ref
4829 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4830 {
4831   if (reg <= 31)
4832     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4833                           offset, 0);
4834   else
4835     return new_loc_descr (DW_OP_bregx, reg, offset);
4836 }
4837
4838 /* Add a location description term to a location description expression.  */
4839
4840 static inline void
4841 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4842 {
4843   dw_loc_descr_ref *d;
4844
4845   /* Find the end of the chain.  */
4846   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4847     ;
4848
4849   *d = descr;
4850 }
4851
4852 /* Add a constant OFFSET to a location expression.  */
4853
4854 static void
4855 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4856 {
4857   dw_loc_descr_ref loc;
4858   HOST_WIDE_INT *p;
4859
4860   gcc_assert (*list_head != NULL);
4861
4862   if (!offset)
4863     return;
4864
4865   /* Find the end of the chain.  */
4866   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4867     ;
4868
4869   p = NULL;
4870   if (loc->dw_loc_opc == DW_OP_fbreg
4871       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4872     p = &loc->dw_loc_oprnd1.v.val_int;
4873   else if (loc->dw_loc_opc == DW_OP_bregx)
4874     p = &loc->dw_loc_oprnd2.v.val_int;
4875
4876   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4877      offset.  Don't optimize if an signed integer overflow would happen.  */
4878   if (p != NULL
4879       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4880           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4881     *p += offset;
4882
4883   else if (offset > 0)
4884     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4885
4886   else
4887     {
4888       loc->dw_loc_next = int_loc_descriptor (-offset);
4889       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4890     }
4891 }
4892
4893 /* Add a constant OFFSET to a location list.  */
4894
4895 static void
4896 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4897 {
4898   dw_loc_list_ref d;
4899   for (d = list_head; d != NULL; d = d->dw_loc_next)
4900     loc_descr_plus_const (&d->expr, offset);
4901 }
4902
4903 #define DWARF_REF_SIZE  \
4904   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4905
4906 static unsigned long size_of_locs (dw_loc_descr_ref);
4907
4908 /* Return the size of a location descriptor.  */
4909
4910 static unsigned long
4911 size_of_loc_descr (dw_loc_descr_ref loc)
4912 {
4913   unsigned long size = 1;
4914
4915   switch (loc->dw_loc_opc)
4916     {
4917     case DW_OP_addr:
4918       size += DWARF2_ADDR_SIZE;
4919       break;
4920     case DW_OP_const1u:
4921     case DW_OP_const1s:
4922       size += 1;
4923       break;
4924     case DW_OP_const2u:
4925     case DW_OP_const2s:
4926       size += 2;
4927       break;
4928     case DW_OP_const4u:
4929     case DW_OP_const4s:
4930       size += 4;
4931       break;
4932     case DW_OP_const8u:
4933     case DW_OP_const8s:
4934       size += 8;
4935       break;
4936     case DW_OP_constu:
4937       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4938       break;
4939     case DW_OP_consts:
4940       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4941       break;
4942     case DW_OP_pick:
4943       size += 1;
4944       break;
4945     case DW_OP_plus_uconst:
4946       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4947       break;
4948     case DW_OP_skip:
4949     case DW_OP_bra:
4950       size += 2;
4951       break;
4952     case DW_OP_breg0:
4953     case DW_OP_breg1:
4954     case DW_OP_breg2:
4955     case DW_OP_breg3:
4956     case DW_OP_breg4:
4957     case DW_OP_breg5:
4958     case DW_OP_breg6:
4959     case DW_OP_breg7:
4960     case DW_OP_breg8:
4961     case DW_OP_breg9:
4962     case DW_OP_breg10:
4963     case DW_OP_breg11:
4964     case DW_OP_breg12:
4965     case DW_OP_breg13:
4966     case DW_OP_breg14:
4967     case DW_OP_breg15:
4968     case DW_OP_breg16:
4969     case DW_OP_breg17:
4970     case DW_OP_breg18:
4971     case DW_OP_breg19:
4972     case DW_OP_breg20:
4973     case DW_OP_breg21:
4974     case DW_OP_breg22:
4975     case DW_OP_breg23:
4976     case DW_OP_breg24:
4977     case DW_OP_breg25:
4978     case DW_OP_breg26:
4979     case DW_OP_breg27:
4980     case DW_OP_breg28:
4981     case DW_OP_breg29:
4982     case DW_OP_breg30:
4983     case DW_OP_breg31:
4984       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4985       break;
4986     case DW_OP_regx:
4987       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4988       break;
4989     case DW_OP_fbreg:
4990       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4991       break;
4992     case DW_OP_bregx:
4993       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4994       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4995       break;
4996     case DW_OP_piece:
4997       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4998       break;
4999     case DW_OP_bit_piece:
5000       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5001       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5002       break;
5003     case DW_OP_deref_size:
5004     case DW_OP_xderef_size:
5005       size += 1;
5006       break;
5007     case DW_OP_call2:
5008       size += 2;
5009       break;
5010     case DW_OP_call4:
5011       size += 4;
5012       break;
5013     case DW_OP_call_ref:
5014       size += DWARF_REF_SIZE;
5015       break;
5016     case DW_OP_implicit_value:
5017       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5018               + loc->dw_loc_oprnd1.v.val_unsigned;
5019       break;
5020     case DW_OP_GNU_implicit_pointer:
5021       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5022       break;
5023     case DW_OP_GNU_entry_value:
5024       {
5025         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5026         size += size_of_uleb128 (op_size) + op_size;
5027         break;
5028       }
5029     default:
5030       break;
5031     }
5032
5033   return size;
5034 }
5035
5036 /* Return the size of a series of location descriptors.  */
5037
5038 static unsigned long
5039 size_of_locs (dw_loc_descr_ref loc)
5040 {
5041   dw_loc_descr_ref l;
5042   unsigned long size;
5043
5044   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5045      field, to avoid writing to a PCH file.  */
5046   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5047     {
5048       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5049         break;
5050       size += size_of_loc_descr (l);
5051     }
5052   if (! l)
5053     return size;
5054
5055   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5056     {
5057       l->dw_loc_addr = size;
5058       size += size_of_loc_descr (l);
5059     }
5060
5061   return size;
5062 }
5063
5064 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5065 static void get_ref_die_offset_label (char *, dw_die_ref);
5066 static void output_loc_sequence (dw_loc_descr_ref, int);
5067
5068 /* Output location description stack opcode's operands (if any).
5069    The for_eh_or_skip parameter controls whether register numbers are
5070    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5071    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5072    info).  This should be suppressed for the cases that have not been converted
5073    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5074
5075 static void
5076 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5077 {
5078   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5079   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5080
5081   switch (loc->dw_loc_opc)
5082     {
5083 #ifdef DWARF2_DEBUGGING_INFO
5084     case DW_OP_const2u:
5085     case DW_OP_const2s:
5086       dw2_asm_output_data (2, val1->v.val_int, NULL);
5087       break;
5088     case DW_OP_const4u:
5089       if (loc->dtprel)
5090         {
5091           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5092           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5093                                                val1->v.val_addr);
5094           fputc ('\n', asm_out_file);
5095           break;
5096         }
5097       /* FALLTHRU */
5098     case DW_OP_const4s:
5099       dw2_asm_output_data (4, val1->v.val_int, NULL);
5100       break;
5101     case DW_OP_const8u:
5102       if (loc->dtprel)
5103         {
5104           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5105           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5106                                                val1->v.val_addr);
5107           fputc ('\n', asm_out_file);
5108           break;
5109         }
5110       /* FALLTHRU */
5111     case DW_OP_const8s:
5112       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5113       dw2_asm_output_data (8, val1->v.val_int, NULL);
5114       break;
5115     case DW_OP_skip:
5116     case DW_OP_bra:
5117       {
5118         int offset;
5119
5120         gcc_assert (val1->val_class == dw_val_class_loc);
5121         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5122
5123         dw2_asm_output_data (2, offset, NULL);
5124       }
5125       break;
5126     case DW_OP_implicit_value:
5127       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5128       switch (val2->val_class)
5129         {
5130         case dw_val_class_const:
5131           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5132           break;
5133         case dw_val_class_vec:
5134           {
5135             unsigned int elt_size = val2->v.val_vec.elt_size;
5136             unsigned int len = val2->v.val_vec.length;
5137             unsigned int i;
5138             unsigned char *p;
5139
5140             if (elt_size > sizeof (HOST_WIDE_INT))
5141               {
5142                 elt_size /= 2;
5143                 len *= 2;
5144               }
5145             for (i = 0, p = val2->v.val_vec.array;
5146                  i < len;
5147                  i++, p += elt_size)
5148               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5149                                    "fp or vector constant word %u", i);
5150           }
5151           break;
5152         case dw_val_class_const_double:
5153           {
5154             unsigned HOST_WIDE_INT first, second;
5155
5156             if (WORDS_BIG_ENDIAN)
5157               {
5158                 first = val2->v.val_double.high;
5159                 second = val2->v.val_double.low;
5160               }
5161             else
5162               {
5163                 first = val2->v.val_double.low;
5164                 second = val2->v.val_double.high;
5165               }
5166             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5167                                  first, NULL);
5168             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5169                                  second, NULL);
5170           }
5171           break;
5172         case dw_val_class_addr:
5173           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5174           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5175           break;
5176         default:
5177           gcc_unreachable ();
5178         }
5179       break;
5180 #else
5181     case DW_OP_const2u:
5182     case DW_OP_const2s:
5183     case DW_OP_const4u:
5184     case DW_OP_const4s:
5185     case DW_OP_const8u:
5186     case DW_OP_const8s:
5187     case DW_OP_skip:
5188     case DW_OP_bra:
5189     case DW_OP_implicit_value:
5190       /* We currently don't make any attempt to make sure these are
5191          aligned properly like we do for the main unwind info, so
5192          don't support emitting things larger than a byte if we're
5193          only doing unwinding.  */
5194       gcc_unreachable ();
5195 #endif
5196     case DW_OP_const1u:
5197     case DW_OP_const1s:
5198       dw2_asm_output_data (1, val1->v.val_int, NULL);
5199       break;
5200     case DW_OP_constu:
5201       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5202       break;
5203     case DW_OP_consts:
5204       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5205       break;
5206     case DW_OP_pick:
5207       dw2_asm_output_data (1, val1->v.val_int, NULL);
5208       break;
5209     case DW_OP_plus_uconst:
5210       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5211       break;
5212     case DW_OP_breg0:
5213     case DW_OP_breg1:
5214     case DW_OP_breg2:
5215     case DW_OP_breg3:
5216     case DW_OP_breg4:
5217     case DW_OP_breg5:
5218     case DW_OP_breg6:
5219     case DW_OP_breg7:
5220     case DW_OP_breg8:
5221     case DW_OP_breg9:
5222     case DW_OP_breg10:
5223     case DW_OP_breg11:
5224     case DW_OP_breg12:
5225     case DW_OP_breg13:
5226     case DW_OP_breg14:
5227     case DW_OP_breg15:
5228     case DW_OP_breg16:
5229     case DW_OP_breg17:
5230     case DW_OP_breg18:
5231     case DW_OP_breg19:
5232     case DW_OP_breg20:
5233     case DW_OP_breg21:
5234     case DW_OP_breg22:
5235     case DW_OP_breg23:
5236     case DW_OP_breg24:
5237     case DW_OP_breg25:
5238     case DW_OP_breg26:
5239     case DW_OP_breg27:
5240     case DW_OP_breg28:
5241     case DW_OP_breg29:
5242     case DW_OP_breg30:
5243     case DW_OP_breg31:
5244       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5245       break;
5246     case DW_OP_regx:
5247       {
5248         unsigned r = val1->v.val_unsigned;
5249         if (for_eh_or_skip >= 0)
5250           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5251         gcc_assert (size_of_uleb128 (r) 
5252                     == size_of_uleb128 (val1->v.val_unsigned));
5253         dw2_asm_output_data_uleb128 (r, NULL);  
5254       }
5255       break;
5256     case DW_OP_fbreg:
5257       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5258       break;
5259     case DW_OP_bregx:
5260       {
5261         unsigned r = val1->v.val_unsigned;
5262         if (for_eh_or_skip >= 0)
5263           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5264         gcc_assert (size_of_uleb128 (r) 
5265                     == size_of_uleb128 (val1->v.val_unsigned));
5266         dw2_asm_output_data_uleb128 (r, NULL);  
5267         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5268       }
5269       break;
5270     case DW_OP_piece:
5271       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5272       break;
5273     case DW_OP_bit_piece:
5274       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5275       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5276       break;
5277     case DW_OP_deref_size:
5278     case DW_OP_xderef_size:
5279       dw2_asm_output_data (1, val1->v.val_int, NULL);
5280       break;
5281
5282     case DW_OP_addr:
5283       if (loc->dtprel)
5284         {
5285           if (targetm.asm_out.output_dwarf_dtprel)
5286             {
5287               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5288                                                    DWARF2_ADDR_SIZE,
5289                                                    val1->v.val_addr);
5290               fputc ('\n', asm_out_file);
5291             }
5292           else
5293             gcc_unreachable ();
5294         }
5295       else
5296         {
5297 #ifdef DWARF2_DEBUGGING_INFO
5298           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5299 #else
5300           gcc_unreachable ();
5301 #endif
5302         }
5303       break;
5304
5305     case DW_OP_GNU_implicit_pointer:
5306       {
5307         char label[MAX_ARTIFICIAL_LABEL_BYTES
5308                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5309         gcc_assert (val1->val_class == dw_val_class_die_ref);
5310         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5311         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5312         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5313       }
5314       break;
5315
5316     case DW_OP_GNU_entry_value:
5317       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5318       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5319       break;
5320
5321     default:
5322       /* Other codes have no operands.  */
5323       break;
5324     }
5325 }
5326
5327 /* Output a sequence of location operations.  
5328    The for_eh_or_skip parameter controls whether register numbers are
5329    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5330    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5331    info).  This should be suppressed for the cases that have not been converted
5332    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5333
5334 static void
5335 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5336 {
5337   for (; loc != NULL; loc = loc->dw_loc_next)
5338     {
5339       enum dwarf_location_atom opc = loc->dw_loc_opc;
5340       /* Output the opcode.  */
5341       if (for_eh_or_skip >= 0 
5342           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5343         {
5344           unsigned r = (opc - DW_OP_breg0);
5345           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5346           gcc_assert (r <= 31);
5347           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5348         }
5349       else if (for_eh_or_skip >= 0 
5350                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5351         {
5352           unsigned r = (opc - DW_OP_reg0);
5353           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5354           gcc_assert (r <= 31);
5355           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5356         }
5357
5358       dw2_asm_output_data (1, opc,
5359                              "%s", dwarf_stack_op_name (opc));
5360
5361       /* Output the operand(s) (if any).  */
5362       output_loc_operands (loc, for_eh_or_skip);
5363     }
5364 }
5365
5366 /* Output location description stack opcode's operands (if any).
5367    The output is single bytes on a line, suitable for .cfi_escape.  */
5368
5369 static void
5370 output_loc_operands_raw (dw_loc_descr_ref loc)
5371 {
5372   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5373   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5374
5375   switch (loc->dw_loc_opc)
5376     {
5377     case DW_OP_addr:
5378     case DW_OP_implicit_value:
5379       /* We cannot output addresses in .cfi_escape, only bytes.  */
5380       gcc_unreachable ();
5381
5382     case DW_OP_const1u:
5383     case DW_OP_const1s:
5384     case DW_OP_pick:
5385     case DW_OP_deref_size:
5386     case DW_OP_xderef_size:
5387       fputc (',', asm_out_file);
5388       dw2_asm_output_data_raw (1, val1->v.val_int);
5389       break;
5390
5391     case DW_OP_const2u:
5392     case DW_OP_const2s:
5393       fputc (',', asm_out_file);
5394       dw2_asm_output_data_raw (2, val1->v.val_int);
5395       break;
5396
5397     case DW_OP_const4u:
5398     case DW_OP_const4s:
5399       fputc (',', asm_out_file);
5400       dw2_asm_output_data_raw (4, val1->v.val_int);
5401       break;
5402
5403     case DW_OP_const8u:
5404     case DW_OP_const8s:
5405       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5406       fputc (',', asm_out_file);
5407       dw2_asm_output_data_raw (8, val1->v.val_int);
5408       break;
5409
5410     case DW_OP_skip:
5411     case DW_OP_bra:
5412       {
5413         int offset;
5414
5415         gcc_assert (val1->val_class == dw_val_class_loc);
5416         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5417
5418         fputc (',', asm_out_file);
5419         dw2_asm_output_data_raw (2, offset);
5420       }
5421       break;
5422
5423     case DW_OP_regx:
5424       {
5425         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5426         gcc_assert (size_of_uleb128 (r) 
5427                     == size_of_uleb128 (val1->v.val_unsigned));
5428         fputc (',', asm_out_file);
5429         dw2_asm_output_data_uleb128_raw (r);
5430       }
5431       break;
5432       
5433     case DW_OP_constu:
5434     case DW_OP_plus_uconst:
5435     case DW_OP_piece:
5436       fputc (',', asm_out_file);
5437       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5438       break;
5439
5440     case DW_OP_bit_piece:
5441       fputc (',', asm_out_file);
5442       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5443       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5444       break;
5445
5446     case DW_OP_consts:
5447     case DW_OP_breg0:
5448     case DW_OP_breg1:
5449     case DW_OP_breg2:
5450     case DW_OP_breg3:
5451     case DW_OP_breg4:
5452     case DW_OP_breg5:
5453     case DW_OP_breg6:
5454     case DW_OP_breg7:
5455     case DW_OP_breg8:
5456     case DW_OP_breg9:
5457     case DW_OP_breg10:
5458     case DW_OP_breg11:
5459     case DW_OP_breg12:
5460     case DW_OP_breg13:
5461     case DW_OP_breg14:
5462     case DW_OP_breg15:
5463     case DW_OP_breg16:
5464     case DW_OP_breg17:
5465     case DW_OP_breg18:
5466     case DW_OP_breg19:
5467     case DW_OP_breg20:
5468     case DW_OP_breg21:
5469     case DW_OP_breg22:
5470     case DW_OP_breg23:
5471     case DW_OP_breg24:
5472     case DW_OP_breg25:
5473     case DW_OP_breg26:
5474     case DW_OP_breg27:
5475     case DW_OP_breg28:
5476     case DW_OP_breg29:
5477     case DW_OP_breg30:
5478     case DW_OP_breg31:
5479     case DW_OP_fbreg:
5480       fputc (',', asm_out_file);
5481       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5482       break;
5483
5484     case DW_OP_bregx:
5485       {
5486         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5487         gcc_assert (size_of_uleb128 (r) 
5488                     == size_of_uleb128 (val1->v.val_unsigned));
5489         fputc (',', asm_out_file);
5490         dw2_asm_output_data_uleb128_raw (r);
5491         fputc (',', asm_out_file);
5492         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5493       }
5494       break;
5495
5496     case DW_OP_GNU_implicit_pointer:
5497     case DW_OP_GNU_entry_value:
5498       gcc_unreachable ();
5499       break;
5500
5501     default:
5502       /* Other codes have no operands.  */
5503       break;
5504     }
5505 }
5506
5507 static void
5508 output_loc_sequence_raw (dw_loc_descr_ref loc)
5509 {
5510   while (1)
5511     {
5512       enum dwarf_location_atom opc = loc->dw_loc_opc;
5513       /* Output the opcode.  */
5514       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5515         {
5516           unsigned r = (opc - DW_OP_breg0);
5517           r = DWARF2_FRAME_REG_OUT (r, 1);
5518           gcc_assert (r <= 31);
5519           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5520         }
5521       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5522         {
5523           unsigned r = (opc - DW_OP_reg0);
5524           r = DWARF2_FRAME_REG_OUT (r, 1);
5525           gcc_assert (r <= 31);
5526           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5527         }
5528       /* Output the opcode.  */
5529       fprintf (asm_out_file, "%#x", opc);
5530       output_loc_operands_raw (loc);
5531
5532       if (!loc->dw_loc_next)
5533         break;
5534       loc = loc->dw_loc_next;
5535
5536       fputc (',', asm_out_file);
5537     }
5538 }
5539
5540 /* This routine will generate the correct assembly data for a location
5541    description based on a cfi entry with a complex address.  */
5542
5543 static void
5544 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5545 {
5546   dw_loc_descr_ref loc;
5547   unsigned long size;
5548
5549   if (cfi->dw_cfi_opc == DW_CFA_expression)
5550     {
5551       unsigned r = 
5552         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5553       dw2_asm_output_data (1, r, NULL);
5554       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5555     }
5556   else
5557     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5558
5559   /* Output the size of the block.  */
5560   size = size_of_locs (loc);
5561   dw2_asm_output_data_uleb128 (size, NULL);
5562
5563   /* Now output the operations themselves.  */
5564   output_loc_sequence (loc, for_eh);
5565 }
5566
5567 /* Similar, but used for .cfi_escape.  */
5568
5569 static void
5570 output_cfa_loc_raw (dw_cfi_ref cfi)
5571 {
5572   dw_loc_descr_ref loc;
5573   unsigned long size;
5574
5575   if (cfi->dw_cfi_opc == DW_CFA_expression)
5576     {
5577       unsigned r = 
5578         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5579       fprintf (asm_out_file, "%#x,", r);
5580       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5581     }
5582   else
5583     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5584
5585   /* Output the size of the block.  */
5586   size = size_of_locs (loc);
5587   dw2_asm_output_data_uleb128_raw (size);
5588   fputc (',', asm_out_file);
5589
5590   /* Now output the operations themselves.  */
5591   output_loc_sequence_raw (loc);
5592 }
5593
5594 /* This function builds a dwarf location descriptor sequence from a
5595    dw_cfa_location, adding the given OFFSET to the result of the
5596    expression.  */
5597
5598 static struct dw_loc_descr_struct *
5599 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5600 {
5601   struct dw_loc_descr_struct *head, *tmp;
5602
5603   offset += cfa->offset;
5604
5605   if (cfa->indirect)
5606     {
5607       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5608       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5609       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5610       add_loc_descr (&head, tmp);
5611       if (offset != 0)
5612         {
5613           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5614           add_loc_descr (&head, tmp);
5615         }
5616     }
5617   else
5618     head = new_reg_loc_descr (cfa->reg, offset);
5619
5620   return head;
5621 }
5622
5623 /* This function builds a dwarf location descriptor sequence for
5624    the address at OFFSET from the CFA when stack is aligned to
5625    ALIGNMENT byte.  */
5626
5627 static struct dw_loc_descr_struct *
5628 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5629 {
5630   struct dw_loc_descr_struct *head;
5631   unsigned int dwarf_fp
5632     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5633
5634  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5635   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5636     {
5637       head = new_reg_loc_descr (dwarf_fp, 0);
5638       add_loc_descr (&head, int_loc_descriptor (alignment));
5639       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5640       loc_descr_plus_const (&head, offset);
5641     }
5642   else
5643     head = new_reg_loc_descr (dwarf_fp, offset);
5644   return head;
5645 }
5646
5647 /* This function fills in aa dw_cfa_location structure from a dwarf location
5648    descriptor sequence.  */
5649
5650 static void
5651 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5652 {
5653   struct dw_loc_descr_struct *ptr;
5654   cfa->offset = 0;
5655   cfa->base_offset = 0;
5656   cfa->indirect = 0;
5657   cfa->reg = -1;
5658
5659   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5660     {
5661       enum dwarf_location_atom op = ptr->dw_loc_opc;
5662
5663       switch (op)
5664         {
5665         case DW_OP_reg0:
5666         case DW_OP_reg1:
5667         case DW_OP_reg2:
5668         case DW_OP_reg3:
5669         case DW_OP_reg4:
5670         case DW_OP_reg5:
5671         case DW_OP_reg6:
5672         case DW_OP_reg7:
5673         case DW_OP_reg8:
5674         case DW_OP_reg9:
5675         case DW_OP_reg10:
5676         case DW_OP_reg11:
5677         case DW_OP_reg12:
5678         case DW_OP_reg13:
5679         case DW_OP_reg14:
5680         case DW_OP_reg15:
5681         case DW_OP_reg16:
5682         case DW_OP_reg17:
5683         case DW_OP_reg18:
5684         case DW_OP_reg19:
5685         case DW_OP_reg20:
5686         case DW_OP_reg21:
5687         case DW_OP_reg22:
5688         case DW_OP_reg23:
5689         case DW_OP_reg24:
5690         case DW_OP_reg25:
5691         case DW_OP_reg26:
5692         case DW_OP_reg27:
5693         case DW_OP_reg28:
5694         case DW_OP_reg29:
5695         case DW_OP_reg30:
5696         case DW_OP_reg31:
5697           cfa->reg = op - DW_OP_reg0;
5698           break;
5699         case DW_OP_regx:
5700           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5701           break;
5702         case DW_OP_breg0:
5703         case DW_OP_breg1:
5704         case DW_OP_breg2:
5705         case DW_OP_breg3:
5706         case DW_OP_breg4:
5707         case DW_OP_breg5:
5708         case DW_OP_breg6:
5709         case DW_OP_breg7:
5710         case DW_OP_breg8:
5711         case DW_OP_breg9:
5712         case DW_OP_breg10:
5713         case DW_OP_breg11:
5714         case DW_OP_breg12:
5715         case DW_OP_breg13:
5716         case DW_OP_breg14:
5717         case DW_OP_breg15:
5718         case DW_OP_breg16:
5719         case DW_OP_breg17:
5720         case DW_OP_breg18:
5721         case DW_OP_breg19:
5722         case DW_OP_breg20:
5723         case DW_OP_breg21:
5724         case DW_OP_breg22:
5725         case DW_OP_breg23:
5726         case DW_OP_breg24:
5727         case DW_OP_breg25:
5728         case DW_OP_breg26:
5729         case DW_OP_breg27:
5730         case DW_OP_breg28:
5731         case DW_OP_breg29:
5732         case DW_OP_breg30:
5733         case DW_OP_breg31:
5734           cfa->reg = op - DW_OP_breg0;
5735           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5736           break;
5737         case DW_OP_bregx:
5738           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5739           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5740           break;
5741         case DW_OP_deref:
5742           cfa->indirect = 1;
5743           break;
5744         case DW_OP_plus_uconst:
5745           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5746           break;
5747         default:
5748           internal_error ("DW_LOC_OP %s not implemented",
5749                           dwarf_stack_op_name (ptr->dw_loc_opc));
5750         }
5751     }
5752 }
5753 \f
5754 /* And now, the support for symbolic debugging information.  */
5755
5756 /* .debug_str support.  */
5757 static int output_indirect_string (void **, void *);
5758
5759 static void dwarf2out_init (const char *);
5760 static void dwarf2out_finish (const char *);
5761 static void dwarf2out_assembly_start (void);
5762 static void dwarf2out_define (unsigned int, const char *);
5763 static void dwarf2out_undef (unsigned int, const char *);
5764 static void dwarf2out_start_source_file (unsigned, const char *);
5765 static void dwarf2out_end_source_file (unsigned);
5766 static void dwarf2out_function_decl (tree);
5767 static void dwarf2out_begin_block (unsigned, unsigned);
5768 static void dwarf2out_end_block (unsigned, unsigned);
5769 static bool dwarf2out_ignore_block (const_tree);
5770 static void dwarf2out_global_decl (tree);
5771 static void dwarf2out_type_decl (tree, int);
5772 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5773 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5774                                                  dw_die_ref);
5775 static void dwarf2out_abstract_function (tree);
5776 static void dwarf2out_var_location (rtx);
5777 static void dwarf2out_direct_call (tree);
5778 static void dwarf2out_virtual_call_token (tree, int);
5779 static void dwarf2out_copy_call_info (rtx, rtx);
5780 static void dwarf2out_virtual_call (int);
5781 static void dwarf2out_begin_function (tree);
5782 static void dwarf2out_set_name (tree, tree);
5783
5784 /* The debug hooks structure.  */
5785
5786 const struct gcc_debug_hooks dwarf2_debug_hooks =
5787 {
5788   dwarf2out_init,
5789   dwarf2out_finish,
5790   dwarf2out_assembly_start,
5791   dwarf2out_define,
5792   dwarf2out_undef,
5793   dwarf2out_start_source_file,
5794   dwarf2out_end_source_file,
5795   dwarf2out_begin_block,
5796   dwarf2out_end_block,
5797   dwarf2out_ignore_block,
5798   dwarf2out_source_line,
5799   dwarf2out_begin_prologue,
5800 #if VMS_DEBUGGING_INFO
5801   dwarf2out_vms_end_prologue,
5802   dwarf2out_vms_begin_epilogue,
5803 #else
5804   debug_nothing_int_charstar,
5805   debug_nothing_int_charstar,
5806 #endif
5807   dwarf2out_end_epilogue,
5808   dwarf2out_begin_function,
5809   debug_nothing_int,            /* end_function */
5810   dwarf2out_function_decl,      /* function_decl */
5811   dwarf2out_global_decl,
5812   dwarf2out_type_decl,          /* type_decl */
5813   dwarf2out_imported_module_or_decl,
5814   debug_nothing_tree,           /* deferred_inline_function */
5815   /* The DWARF 2 backend tries to reduce debugging bloat by not
5816      emitting the abstract description of inline functions until
5817      something tries to reference them.  */
5818   dwarf2out_abstract_function,  /* outlining_inline_function */
5819   debug_nothing_rtx,            /* label */
5820   debug_nothing_int,            /* handle_pch */
5821   dwarf2out_var_location,
5822   dwarf2out_switch_text_section,
5823   dwarf2out_direct_call,
5824   dwarf2out_virtual_call_token,
5825   dwarf2out_copy_call_info,
5826   dwarf2out_virtual_call,
5827   dwarf2out_set_name,
5828   1,                            /* start_end_main_source_file */
5829   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5830 };
5831 \f
5832 /* NOTE: In the comments in this file, many references are made to
5833    "Debugging Information Entries".  This term is abbreviated as `DIE'
5834    throughout the remainder of this file.  */
5835
5836 /* An internal representation of the DWARF output is built, and then
5837    walked to generate the DWARF debugging info.  The walk of the internal
5838    representation is done after the entire program has been compiled.
5839    The types below are used to describe the internal representation.  */
5840
5841 /* Various DIE's use offsets relative to the beginning of the
5842    .debug_info section to refer to each other.  */
5843
5844 typedef long int dw_offset;
5845
5846 /* Define typedefs here to avoid circular dependencies.  */
5847
5848 typedef struct dw_attr_struct *dw_attr_ref;
5849 typedef struct dw_line_info_struct *dw_line_info_ref;
5850 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5851 typedef struct pubname_struct *pubname_ref;
5852 typedef struct dw_ranges_struct *dw_ranges_ref;
5853 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5854 typedef struct comdat_type_struct *comdat_type_node_ref;
5855
5856 /* Each entry in the line_info_table maintains the file and
5857    line number associated with the label generated for that
5858    entry.  The label gives the PC value associated with
5859    the line number entry.  */
5860
5861 typedef struct GTY(()) dw_line_info_struct {
5862   unsigned long dw_file_num;
5863   unsigned long dw_line_num;
5864 }
5865 dw_line_info_entry;
5866
5867 /* Line information for functions in separate sections; each one gets its
5868    own sequence.  */
5869 typedef struct GTY(()) dw_separate_line_info_struct {
5870   unsigned long dw_file_num;
5871   unsigned long dw_line_num;
5872   unsigned long function;
5873 }
5874 dw_separate_line_info_entry;
5875
5876 /* Each DIE attribute has a field specifying the attribute kind,
5877    a link to the next attribute in the chain, and an attribute value.
5878    Attributes are typically linked below the DIE they modify.  */
5879
5880 typedef struct GTY(()) dw_attr_struct {
5881   enum dwarf_attribute dw_attr;
5882   dw_val_node dw_attr_val;
5883 }
5884 dw_attr_node;
5885
5886 DEF_VEC_O(dw_attr_node);
5887 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5888
5889 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5890    The children of each node form a circular list linked by
5891    die_sib.  die_child points to the node *before* the "first" child node.  */
5892
5893 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5894   union die_symbol_or_type_node
5895     {
5896       char * GTY ((tag ("0"))) die_symbol;
5897       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5898     }
5899   GTY ((desc ("dwarf_version >= 4"))) die_id;
5900   VEC(dw_attr_node,gc) * die_attr;
5901   dw_die_ref die_parent;
5902   dw_die_ref die_child;
5903   dw_die_ref die_sib;
5904   dw_die_ref die_definition; /* ref from a specification to its definition */
5905   dw_offset die_offset;
5906   unsigned long die_abbrev;
5907   int die_mark;
5908   /* Die is used and must not be pruned as unused.  */
5909   int die_perennial_p;
5910   unsigned int decl_id;
5911   enum dwarf_tag die_tag;
5912 }
5913 die_node;
5914
5915 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5916 #define FOR_EACH_CHILD(die, c, expr) do {       \
5917   c = die->die_child;                           \
5918   if (c) do {                                   \
5919     c = c->die_sib;                             \
5920     expr;                                       \
5921   } while (c != die->die_child);                \
5922 } while (0)
5923
5924 /* The pubname structure */
5925
5926 typedef struct GTY(()) pubname_struct {
5927   dw_die_ref die;
5928   const char *name;
5929 }
5930 pubname_entry;
5931
5932 DEF_VEC_O(pubname_entry);
5933 DEF_VEC_ALLOC_O(pubname_entry, gc);
5934
5935 struct GTY(()) dw_ranges_struct {
5936   /* If this is positive, it's a block number, otherwise it's a
5937      bitwise-negated index into dw_ranges_by_label.  */
5938   int num;
5939 };
5940
5941 /* A structure to hold a macinfo entry.  */
5942
5943 typedef struct GTY(()) macinfo_struct {
5944   unsigned HOST_WIDE_INT code;
5945   unsigned HOST_WIDE_INT lineno;
5946   const char *info;
5947 }
5948 macinfo_entry;
5949
5950 DEF_VEC_O(macinfo_entry);
5951 DEF_VEC_ALLOC_O(macinfo_entry, gc);
5952
5953 struct GTY(()) dw_ranges_by_label_struct {
5954   const char *begin;
5955   const char *end;
5956 };
5957
5958 /* The comdat type node structure.  */
5959 typedef struct GTY(()) comdat_type_struct
5960 {
5961   dw_die_ref root_die;
5962   dw_die_ref type_die;
5963   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5964   struct comdat_type_struct *next;
5965 }
5966 comdat_type_node;
5967
5968 /* The limbo die list structure.  */
5969 typedef struct GTY(()) limbo_die_struct {
5970   dw_die_ref die;
5971   tree created_for;
5972   struct limbo_die_struct *next;
5973 }
5974 limbo_die_node;
5975
5976 typedef struct skeleton_chain_struct
5977 {
5978   dw_die_ref old_die;
5979   dw_die_ref new_die;
5980   struct skeleton_chain_struct *parent;
5981 }
5982 skeleton_chain_node;
5983
5984 /* How to start an assembler comment.  */
5985 #ifndef ASM_COMMENT_START
5986 #define ASM_COMMENT_START ";#"
5987 #endif
5988
5989 /* Define a macro which returns nonzero for a TYPE_DECL which was
5990    implicitly generated for a tagged type.
5991
5992    Note that unlike the gcc front end (which generates a NULL named
5993    TYPE_DECL node for each complete tagged type, each array type, and
5994    each function type node created) the g++ front end generates a
5995    _named_ TYPE_DECL node for each tagged type node created.
5996    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5997    generate a DW_TAG_typedef DIE for them.  */
5998
5999 #define TYPE_DECL_IS_STUB(decl)                         \
6000   (DECL_NAME (decl) == NULL_TREE                        \
6001    || (DECL_ARTIFICIAL (decl)                           \
6002        && is_tagged_type (TREE_TYPE (decl))             \
6003        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
6004            /* This is necessary for stub decls that     \
6005               appear in nested inline functions.  */    \
6006            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6007                && (decl_ultimate_origin (decl)          \
6008                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6009
6010 /* Information concerning the compilation unit's programming
6011    language, and compiler version.  */
6012
6013 /* Fixed size portion of the DWARF compilation unit header.  */
6014 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6015   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6016
6017 /* Fixed size portion of the DWARF comdat type unit header.  */
6018 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6019   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6020    + DWARF_OFFSET_SIZE)
6021
6022 /* Fixed size portion of public names info.  */
6023 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6024
6025 /* Fixed size portion of the address range info.  */
6026 #define DWARF_ARANGES_HEADER_SIZE                                       \
6027   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6028                 DWARF2_ADDR_SIZE * 2)                                   \
6029    - DWARF_INITIAL_LENGTH_SIZE)
6030
6031 /* Size of padding portion in the address range info.  It must be
6032    aligned to twice the pointer size.  */
6033 #define DWARF_ARANGES_PAD_SIZE \
6034   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6035                 DWARF2_ADDR_SIZE * 2)                              \
6036    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6037
6038 /* Use assembler line directives if available.  */
6039 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6040 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6041 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6042 #else
6043 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6044 #endif
6045 #endif
6046
6047 /* Minimum line offset in a special line info. opcode.
6048    This value was chosen to give a reasonable range of values.  */
6049 #define DWARF_LINE_BASE  -10
6050
6051 /* First special line opcode - leave room for the standard opcodes.  */
6052 #define DWARF_LINE_OPCODE_BASE  10
6053
6054 /* Range of line offsets in a special line info. opcode.  */
6055 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6056
6057 /* Flag that indicates the initial value of the is_stmt_start flag.
6058    In the present implementation, we do not mark any lines as
6059    the beginning of a source statement, because that information
6060    is not made available by the GCC front-end.  */
6061 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6062
6063 /* Maximum number of operations per instruction bundle.  */
6064 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6065 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6066 #endif
6067
6068 /* This location is used by calc_die_sizes() to keep track
6069    the offset of each DIE within the .debug_info section.  */
6070 static unsigned long next_die_offset;
6071
6072 /* Record the root of the DIE's built for the current compilation unit.  */
6073 static GTY(()) dw_die_ref single_comp_unit_die;
6074
6075 /* A list of type DIEs that have been separated into comdat sections.  */
6076 static GTY(()) comdat_type_node *comdat_type_list;
6077
6078 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6079 static GTY(()) limbo_die_node *limbo_die_list;
6080
6081 /* A list of DIEs for which we may have to generate
6082    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6083 static GTY(()) limbo_die_node *deferred_asm_name;
6084
6085 /* Filenames referenced by this compilation unit.  */
6086 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6087
6088 /* A hash table of references to DIE's that describe declarations.
6089    The key is a DECL_UID() which is a unique number identifying each decl.  */
6090 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6091
6092 /* A hash table of references to DIE's that describe COMMON blocks.
6093    The key is DECL_UID() ^ die_parent.  */
6094 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6095
6096 typedef struct GTY(()) die_arg_entry_struct {
6097     dw_die_ref die;
6098     tree arg;
6099 } die_arg_entry;
6100
6101 DEF_VEC_O(die_arg_entry);
6102 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6103
6104 /* Node of the variable location list.  */
6105 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6106   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6107      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6108      in mode of the EXPR_LIST node and first EXPR_LIST operand
6109      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6110      location or NULL for padding.  For larger bitsizes,
6111      mode is 0 and first operand is a CONCAT with bitsize
6112      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6113      NULL as second operand.  */
6114   rtx GTY (()) loc;
6115   const char * GTY (()) label;
6116   struct var_loc_node * GTY (()) next;
6117 };
6118
6119 /* Variable location list.  */
6120 struct GTY (()) var_loc_list_def {
6121   struct var_loc_node * GTY (()) first;
6122
6123   /* Pointer to the last but one or last element of the
6124      chained list.  If the list is empty, both first and
6125      last are NULL, if the list contains just one node
6126      or the last node certainly is not redundant, it points
6127      to the last node, otherwise points to the last but one.
6128      Do not mark it for GC because it is marked through the chain.  */
6129   struct var_loc_node * GTY ((skip ("%h"))) last;
6130
6131   /* DECL_UID of the variable decl.  */
6132   unsigned int decl_id;
6133 };
6134 typedef struct var_loc_list_def var_loc_list;
6135
6136 /* Call argument location list.  */
6137 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6138   rtx GTY (()) call_arg_loc_note;
6139   const char * GTY (()) label;
6140   tree GTY (()) block;
6141   bool tail_call_p;
6142   rtx GTY (()) symbol_ref;
6143   struct call_arg_loc_node * GTY (()) next;
6144 };
6145
6146
6147 /* Table of decl location linked lists.  */
6148 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6149
6150 /* Head and tail of call_arg_loc chain.  */
6151 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6152 static struct call_arg_loc_node *call_arg_loc_last;
6153
6154 /* Number of call sites in the current function.  */
6155 static int call_site_count = -1;
6156 /* Number of tail call sites in the current function.  */
6157 static int tail_call_site_count = -1;
6158
6159 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6160    DIEs.  */
6161 static VEC (dw_die_ref, heap) *block_map;
6162
6163 /* A pointer to the base of a list of references to DIE's that
6164    are uniquely identified by their tag, presence/absence of
6165    children DIE's, and list of attribute/value pairs.  */
6166 static GTY((length ("abbrev_die_table_allocated")))
6167   dw_die_ref *abbrev_die_table;
6168
6169 /* Number of elements currently allocated for abbrev_die_table.  */
6170 static GTY(()) unsigned abbrev_die_table_allocated;
6171
6172 /* Number of elements in type_die_table currently in use.  */
6173 static GTY(()) unsigned abbrev_die_table_in_use;
6174
6175 /* Size (in elements) of increments by which we may expand the
6176    abbrev_die_table.  */
6177 #define ABBREV_DIE_TABLE_INCREMENT 256
6178
6179 /* A pointer to the base of a table that contains line information
6180    for each source code line in .text in the compilation unit.  */
6181 static GTY((length ("line_info_table_allocated")))
6182      dw_line_info_ref line_info_table;
6183
6184 /* Number of elements currently allocated for line_info_table.  */
6185 static GTY(()) unsigned line_info_table_allocated;
6186
6187 /* Number of elements in line_info_table currently in use.  */
6188 static GTY(()) unsigned line_info_table_in_use;
6189
6190 /* A pointer to the base of a table that contains line information
6191    for each source code line outside of .text in the compilation unit.  */
6192 static GTY ((length ("separate_line_info_table_allocated")))
6193      dw_separate_line_info_ref separate_line_info_table;
6194
6195 /* Number of elements currently allocated for separate_line_info_table.  */
6196 static GTY(()) unsigned separate_line_info_table_allocated;
6197
6198 /* Number of elements in separate_line_info_table currently in use.  */
6199 static GTY(()) unsigned separate_line_info_table_in_use;
6200
6201 /* Size (in elements) of increments by which we may expand the
6202    line_info_table.  */
6203 #define LINE_INFO_TABLE_INCREMENT 1024
6204
6205 /* A flag to tell pubnames/types export if there is an info section to
6206    refer to.  */
6207 static bool info_section_emitted;
6208
6209 /* A pointer to the base of a table that contains a list of publicly
6210    accessible names.  */
6211 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6212
6213 /* A pointer to the base of a table that contains a list of publicly
6214    accessible types.  */
6215 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6216
6217 /* A pointer to the base of a table that contains a list of macro
6218    defines/undefines (and file start/end markers).  */
6219 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6220
6221 /* Array of dies for which we should generate .debug_arange info.  */
6222 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
6223
6224 /* Number of elements currently allocated for arange_table.  */
6225 static GTY(()) unsigned arange_table_allocated;
6226
6227 /* Number of elements in arange_table currently in use.  */
6228 static GTY(()) unsigned arange_table_in_use;
6229
6230 /* Size (in elements) of increments by which we may expand the
6231    arange_table.  */
6232 #define ARANGE_TABLE_INCREMENT 64
6233
6234 /* Array of dies for which we should generate .debug_ranges info.  */
6235 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6236
6237 /* Number of elements currently allocated for ranges_table.  */
6238 static GTY(()) unsigned ranges_table_allocated;
6239
6240 /* Number of elements in ranges_table currently in use.  */
6241 static GTY(()) unsigned ranges_table_in_use;
6242
6243 /* Array of pairs of labels referenced in ranges_table.  */
6244 static GTY ((length ("ranges_by_label_allocated")))
6245      dw_ranges_by_label_ref ranges_by_label;
6246
6247 /* Number of elements currently allocated for ranges_by_label.  */
6248 static GTY(()) unsigned ranges_by_label_allocated;
6249
6250 /* Number of elements in ranges_by_label currently in use.  */
6251 static GTY(()) unsigned ranges_by_label_in_use;
6252
6253 /* Size (in elements) of increments by which we may expand the
6254    ranges_table.  */
6255 #define RANGES_TABLE_INCREMENT 64
6256
6257 /* Whether we have location lists that need outputting */
6258 static GTY(()) bool have_location_lists;
6259
6260 /* Unique label counter.  */
6261 static GTY(()) unsigned int loclabel_num;
6262
6263 /* Unique label counter for point-of-call tables.  */
6264 static GTY(()) unsigned int poc_label_num;
6265
6266 /* The direct call table structure.  */
6267
6268 typedef struct GTY(()) dcall_struct {
6269   unsigned int poc_label_num;
6270   tree poc_decl;
6271   dw_die_ref targ_die;
6272 }
6273 dcall_entry;
6274
6275 DEF_VEC_O(dcall_entry);
6276 DEF_VEC_ALLOC_O(dcall_entry, gc);
6277
6278 /* The virtual call table structure.  */
6279
6280 typedef struct GTY(()) vcall_struct {
6281   unsigned int poc_label_num;
6282   unsigned int vtable_slot;
6283 }
6284 vcall_entry;
6285
6286 DEF_VEC_O(vcall_entry);
6287 DEF_VEC_ALLOC_O(vcall_entry, gc);
6288
6289 /* Pointers to the direct and virtual call tables.  */
6290 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6291 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6292
6293 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6294
6295 struct GTY (()) vcall_insn {
6296   int insn_uid;
6297   unsigned int vtable_slot;
6298 };
6299
6300 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6301
6302 /* Record whether the function being analyzed contains inlined functions.  */
6303 static int current_function_has_inlines;
6304
6305 /* The last file entry emitted by maybe_emit_file().  */
6306 static GTY(()) struct dwarf_file_data * last_emitted_file;
6307
6308 /* Number of internal labels generated by gen_internal_sym().  */
6309 static GTY(()) int label_num;
6310
6311 /* Cached result of previous call to lookup_filename.  */
6312 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6313
6314 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6315
6316 /* Instances of generic types for which we need to generate debug
6317    info that describe their generic parameters and arguments. That
6318    generation needs to happen once all types are properly laid out so
6319    we do it at the end of compilation.  */
6320 static GTY(()) VEC(tree,gc) *generic_type_instances;
6321
6322 /* Offset from the "steady-state frame pointer" to the frame base,
6323    within the current function.  */
6324 static HOST_WIDE_INT frame_pointer_fb_offset;
6325
6326 /* Forward declarations for functions defined in this file.  */
6327
6328 static int is_pseudo_reg (const_rtx);
6329 static tree type_main_variant (tree);
6330 static int is_tagged_type (const_tree);
6331 static const char *dwarf_tag_name (unsigned);
6332 static const char *dwarf_attr_name (unsigned);
6333 static const char *dwarf_form_name (unsigned);
6334 static tree decl_ultimate_origin (const_tree);
6335 static tree decl_class_context (tree);
6336 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6337 static inline enum dw_val_class AT_class (dw_attr_ref);
6338 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6339 static inline unsigned AT_flag (dw_attr_ref);
6340 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6341 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6342 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6343 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6344 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6345                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6346 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6347                                unsigned int, unsigned char *);
6348 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6349 static hashval_t debug_str_do_hash (const void *);
6350 static int debug_str_eq (const void *, const void *);
6351 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6352 static inline const char *AT_string (dw_attr_ref);
6353 static enum dwarf_form AT_string_form (dw_attr_ref);
6354 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6355 static void add_AT_specification (dw_die_ref, dw_die_ref);
6356 static inline dw_die_ref AT_ref (dw_attr_ref);
6357 static inline int AT_ref_external (dw_attr_ref);
6358 static inline void set_AT_ref_external (dw_attr_ref, int);
6359 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6360 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6361 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6362 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6363                              dw_loc_list_ref);
6364 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6365 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6366 static inline rtx AT_addr (dw_attr_ref);
6367 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6368 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6369 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6370 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6371                            unsigned HOST_WIDE_INT);
6372 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6373                                unsigned long);
6374 static inline const char *AT_lbl (dw_attr_ref);
6375 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6376 static const char *get_AT_low_pc (dw_die_ref);
6377 static const char *get_AT_hi_pc (dw_die_ref);
6378 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6379 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6380 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6381 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6382 static bool is_cxx (void);
6383 static bool is_fortran (void);
6384 static bool is_ada (void);
6385 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6386 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6387 static void add_child_die (dw_die_ref, dw_die_ref);
6388 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6389 static dw_die_ref lookup_type_die (tree);
6390 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6391 static void equate_type_number_to_die (tree, dw_die_ref);
6392 static hashval_t decl_die_table_hash (const void *);
6393 static int decl_die_table_eq (const void *, const void *);
6394 static dw_die_ref lookup_decl_die (tree);
6395 static hashval_t common_block_die_table_hash (const void *);
6396 static int common_block_die_table_eq (const void *, const void *);
6397 static hashval_t decl_loc_table_hash (const void *);
6398 static int decl_loc_table_eq (const void *, const void *);
6399 static var_loc_list *lookup_decl_loc (const_tree);
6400 static void equate_decl_number_to_die (tree, dw_die_ref);
6401 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6402 static void print_spaces (FILE *);
6403 static void print_die (dw_die_ref, FILE *);
6404 static void print_dwarf_line_table (FILE *);
6405 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6406 static dw_die_ref pop_compile_unit (dw_die_ref);
6407 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6408 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6409 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6410 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6411 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6412 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6413 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6414                                    struct md5_ctx *, int *);
6415 struct checksum_attributes;
6416 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6417 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6418 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6419 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6420 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6421 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6422 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6423 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6424 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6425 static void compute_section_prefix (dw_die_ref);
6426 static int is_type_die (dw_die_ref);
6427 static int is_comdat_die (dw_die_ref);
6428 static int is_symbol_die (dw_die_ref);
6429 static void assign_symbol_names (dw_die_ref);
6430 static void break_out_includes (dw_die_ref);
6431 static int is_declaration_die (dw_die_ref);
6432 static int should_move_die_to_comdat (dw_die_ref);
6433 static dw_die_ref clone_as_declaration (dw_die_ref);
6434 static dw_die_ref clone_die (dw_die_ref);
6435 static dw_die_ref clone_tree (dw_die_ref);
6436 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6437 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6438 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6439 static dw_die_ref generate_skeleton (dw_die_ref);
6440 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6441                                                          dw_die_ref);
6442 static void break_out_comdat_types (dw_die_ref);
6443 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6444 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6445 static void copy_decls_for_unworthy_types (dw_die_ref);
6446
6447 static hashval_t htab_cu_hash (const void *);
6448 static int htab_cu_eq (const void *, const void *);
6449 static void htab_cu_del (void *);
6450 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6451 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6452 static void add_sibling_attributes (dw_die_ref);
6453 static void build_abbrev_table (dw_die_ref);
6454 static void output_location_lists (dw_die_ref);
6455 static int constant_size (unsigned HOST_WIDE_INT);
6456 static unsigned long size_of_die (dw_die_ref);
6457 static void calc_die_sizes (dw_die_ref);
6458 static void mark_dies (dw_die_ref);
6459 static void unmark_dies (dw_die_ref);
6460 static void unmark_all_dies (dw_die_ref);
6461 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6462 static unsigned long size_of_aranges (void);
6463 static enum dwarf_form value_format (dw_attr_ref);
6464 static void output_value_format (dw_attr_ref);
6465 static void output_abbrev_section (void);
6466 static void output_die_symbol (dw_die_ref);
6467 static void output_die (dw_die_ref);
6468 static void output_compilation_unit_header (void);
6469 static void output_comp_unit (dw_die_ref, int);
6470 static void output_comdat_type_unit (comdat_type_node *);
6471 static const char *dwarf2_name (tree, int);
6472 static void add_pubname (tree, dw_die_ref);
6473 static void add_pubname_string (const char *, dw_die_ref);
6474 static void add_pubtype (tree, dw_die_ref);
6475 static void output_pubnames (VEC (pubname_entry,gc) *);
6476 static void add_arange (tree, dw_die_ref);
6477 static void output_aranges (void);
6478 static unsigned int add_ranges_num (int);
6479 static unsigned int add_ranges (const_tree);
6480 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6481                                   bool *);
6482 static void output_ranges (void);
6483 static void output_line_info (void);
6484 static void output_file_names (void);
6485 static dw_die_ref base_type_die (tree);
6486 static int is_base_type (tree);
6487 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6488 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6489 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6490 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6491 static int type_is_enum (const_tree);
6492 static unsigned int dbx_reg_number (const_rtx);
6493 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6494 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6495 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6496                                                 enum var_init_status);
6497 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6498                                                      enum var_init_status);
6499 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6500                                          enum var_init_status);
6501 static int is_based_loc (const_rtx);
6502 static int resolve_one_addr (rtx *, void *);
6503 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6504                                                enum var_init_status);
6505 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6506                                         enum var_init_status);
6507 static dw_loc_list_ref loc_list_from_tree (tree, int);
6508 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6509 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6510 static tree field_type (const_tree);
6511 static unsigned int simple_type_align_in_bits (const_tree);
6512 static unsigned int simple_decl_align_in_bits (const_tree);
6513 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6514 static HOST_WIDE_INT field_byte_offset (const_tree);
6515 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6516                                          dw_loc_list_ref);
6517 static void add_data_member_location_attribute (dw_die_ref, tree);
6518 static bool add_const_value_attribute (dw_die_ref, rtx);
6519 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6520 static void insert_double (double_int, unsigned char *);
6521 static void insert_float (const_rtx, unsigned char *);
6522 static rtx rtl_for_decl_location (tree);
6523 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6524                                                    enum dwarf_attribute);
6525 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6526 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6527 static void add_name_attribute (dw_die_ref, const char *);
6528 static void add_comp_dir_attribute (dw_die_ref);
6529 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6530 static void add_subscript_info (dw_die_ref, tree, bool);
6531 static void add_byte_size_attribute (dw_die_ref, tree);
6532 static void add_bit_offset_attribute (dw_die_ref, tree);
6533 static void add_bit_size_attribute (dw_die_ref, tree);
6534 static void add_prototyped_attribute (dw_die_ref, tree);
6535 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6536 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6537 static void add_src_coords_attributes (dw_die_ref, tree);
6538 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6539 static void push_decl_scope (tree);
6540 static void pop_decl_scope (void);
6541 static dw_die_ref scope_die_for (tree, dw_die_ref);
6542 static inline int local_scope_p (dw_die_ref);
6543 static inline int class_scope_p (dw_die_ref);
6544 static inline int class_or_namespace_scope_p (dw_die_ref);
6545 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6546 static void add_calling_convention_attribute (dw_die_ref, tree);
6547 static const char *type_tag (const_tree);
6548 static tree member_declared_type (const_tree);
6549 #if 0
6550 static const char *decl_start_label (tree);
6551 #endif
6552 static void gen_array_type_die (tree, dw_die_ref);
6553 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6554 #if 0
6555 static void gen_entry_point_die (tree, dw_die_ref);
6556 #endif
6557 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6558 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6559 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6560 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6561 static void gen_formal_types_die (tree, dw_die_ref);
6562 static void gen_subprogram_die (tree, dw_die_ref);
6563 static void gen_variable_die (tree, tree, dw_die_ref);
6564 static void gen_const_die (tree, dw_die_ref);
6565 static void gen_label_die (tree, dw_die_ref);
6566 static void gen_lexical_block_die (tree, dw_die_ref, int);
6567 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6568 static void gen_field_die (tree, dw_die_ref);
6569 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6570 static dw_die_ref gen_compile_unit_die (const char *);
6571 static void gen_inheritance_die (tree, tree, dw_die_ref);
6572 static void gen_member_die (tree, dw_die_ref);
6573 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6574                                                 enum debug_info_usage);
6575 static void gen_subroutine_type_die (tree, dw_die_ref);
6576 static void gen_typedef_die (tree, dw_die_ref);
6577 static void gen_type_die (tree, dw_die_ref);
6578 static void gen_block_die (tree, dw_die_ref, int);
6579 static void decls_for_scope (tree, dw_die_ref, int);
6580 static int is_redundant_typedef (const_tree);
6581 static bool is_naming_typedef_decl (const_tree);
6582 static inline dw_die_ref get_context_die (tree);
6583 static void gen_namespace_die (tree, dw_die_ref);
6584 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6585 static dw_die_ref force_decl_die (tree);
6586 static dw_die_ref force_type_die (tree);
6587 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6588 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6589 static struct dwarf_file_data * lookup_filename (const char *);
6590 static void retry_incomplete_types (void);
6591 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6592 static void gen_generic_params_dies (tree);
6593 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6594 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6595 static void splice_child_die (dw_die_ref, dw_die_ref);
6596 static int file_info_cmp (const void *, const void *);
6597 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6598                                      const char *, const char *);
6599 static void output_loc_list (dw_loc_list_ref);
6600 static char *gen_internal_sym (const char *);
6601
6602 static void prune_unmark_dies (dw_die_ref);
6603 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6604 static void prune_unused_types_mark (dw_die_ref, int);
6605 static void prune_unused_types_walk (dw_die_ref);
6606 static void prune_unused_types_walk_attribs (dw_die_ref);
6607 static void prune_unused_types_prune (dw_die_ref);
6608 static void prune_unused_types (void);
6609 static int maybe_emit_file (struct dwarf_file_data *fd);
6610 static inline const char *AT_vms_delta1 (dw_attr_ref);
6611 static inline const char *AT_vms_delta2 (dw_attr_ref);
6612 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6613                                      const char *, const char *);
6614 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6615 static void gen_remaining_tmpl_value_param_die_attribute (void);
6616 static bool generic_type_p (tree);
6617 static void schedule_generic_params_dies_gen (tree t);
6618 static void gen_scheduled_generic_parms_dies (void);
6619
6620 /* Section names used to hold DWARF debugging information.  */
6621 #ifndef DEBUG_INFO_SECTION
6622 #define DEBUG_INFO_SECTION      ".debug_info"
6623 #endif
6624 #ifndef DEBUG_ABBREV_SECTION
6625 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6626 #endif
6627 #ifndef DEBUG_ARANGES_SECTION
6628 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6629 #endif
6630 #ifndef DEBUG_MACINFO_SECTION
6631 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6632 #endif
6633 #ifndef DEBUG_LINE_SECTION
6634 #define DEBUG_LINE_SECTION      ".debug_line"
6635 #endif
6636 #ifndef DEBUG_LOC_SECTION
6637 #define DEBUG_LOC_SECTION       ".debug_loc"
6638 #endif
6639 #ifndef DEBUG_PUBNAMES_SECTION
6640 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6641 #endif
6642 #ifndef DEBUG_PUBTYPES_SECTION
6643 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6644 #endif
6645 #ifndef DEBUG_DCALL_SECTION
6646 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6647 #endif
6648 #ifndef DEBUG_VCALL_SECTION
6649 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6650 #endif
6651 #ifndef DEBUG_STR_SECTION
6652 #define DEBUG_STR_SECTION       ".debug_str"
6653 #endif
6654 #ifndef DEBUG_RANGES_SECTION
6655 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6656 #endif
6657
6658 /* Standard ELF section names for compiled code and data.  */
6659 #ifndef TEXT_SECTION_NAME
6660 #define TEXT_SECTION_NAME       ".text"
6661 #endif
6662
6663 /* Section flags for .debug_str section.  */
6664 #define DEBUG_STR_SECTION_FLAGS \
6665   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6666    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6667    : SECTION_DEBUG)
6668
6669 /* Labels we insert at beginning sections we can reference instead of
6670    the section names themselves.  */
6671
6672 #ifndef TEXT_SECTION_LABEL
6673 #define TEXT_SECTION_LABEL              "Ltext"
6674 #endif
6675 #ifndef COLD_TEXT_SECTION_LABEL
6676 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6677 #endif
6678 #ifndef DEBUG_LINE_SECTION_LABEL
6679 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6680 #endif
6681 #ifndef DEBUG_INFO_SECTION_LABEL
6682 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6683 #endif
6684 #ifndef DEBUG_ABBREV_SECTION_LABEL
6685 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6686 #endif
6687 #ifndef DEBUG_LOC_SECTION_LABEL
6688 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6689 #endif
6690 #ifndef DEBUG_RANGES_SECTION_LABEL
6691 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6692 #endif
6693 #ifndef DEBUG_MACINFO_SECTION_LABEL
6694 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6695 #endif
6696
6697
6698 /* Definitions of defaults for formats and names of various special
6699    (artificial) labels which may be generated within this file (when the -g
6700    options is used and DWARF2_DEBUGGING_INFO is in effect.
6701    If necessary, these may be overridden from within the tm.h file, but
6702    typically, overriding these defaults is unnecessary.  */
6703
6704 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6705 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6706 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6707 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6708 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6709 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6710 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6711 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6712 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6713 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6714
6715 #ifndef TEXT_END_LABEL
6716 #define TEXT_END_LABEL          "Letext"
6717 #endif
6718 #ifndef COLD_END_LABEL
6719 #define COLD_END_LABEL          "Letext_cold"
6720 #endif
6721 #ifndef BLOCK_BEGIN_LABEL
6722 #define BLOCK_BEGIN_LABEL       "LBB"
6723 #endif
6724 #ifndef BLOCK_END_LABEL
6725 #define BLOCK_END_LABEL         "LBE"
6726 #endif
6727 #ifndef LINE_CODE_LABEL
6728 #define LINE_CODE_LABEL         "LM"
6729 #endif
6730 #ifndef SEPARATE_LINE_CODE_LABEL
6731 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6732 #endif
6733
6734 \f
6735 /* Return the root of the DIE's built for the current compilation unit.  */
6736 static dw_die_ref
6737 comp_unit_die (void)
6738 {
6739   if (!single_comp_unit_die)
6740     single_comp_unit_die = gen_compile_unit_die (NULL);
6741   return single_comp_unit_die;
6742 }
6743
6744 /* We allow a language front-end to designate a function that is to be
6745    called to "demangle" any name before it is put into a DIE.  */
6746
6747 static const char *(*demangle_name_func) (const char *);
6748
6749 void
6750 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6751 {
6752   demangle_name_func = func;
6753 }
6754
6755 /* Test if rtl node points to a pseudo register.  */
6756
6757 static inline int
6758 is_pseudo_reg (const_rtx rtl)
6759 {
6760   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6761           || (GET_CODE (rtl) == SUBREG
6762               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6763 }
6764
6765 /* Return a reference to a type, with its const and volatile qualifiers
6766    removed.  */
6767
6768 static inline tree
6769 type_main_variant (tree type)
6770 {
6771   type = TYPE_MAIN_VARIANT (type);
6772
6773   /* ??? There really should be only one main variant among any group of
6774      variants of a given type (and all of the MAIN_VARIANT values for all
6775      members of the group should point to that one type) but sometimes the C
6776      front-end messes this up for array types, so we work around that bug
6777      here.  */
6778   if (TREE_CODE (type) == ARRAY_TYPE)
6779     while (type != TYPE_MAIN_VARIANT (type))
6780       type = TYPE_MAIN_VARIANT (type);
6781
6782   return type;
6783 }
6784
6785 /* Return nonzero if the given type node represents a tagged type.  */
6786
6787 static inline int
6788 is_tagged_type (const_tree type)
6789 {
6790   enum tree_code code = TREE_CODE (type);
6791
6792   return (code == RECORD_TYPE || code == UNION_TYPE
6793           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6794 }
6795
6796 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6797
6798 static void
6799 get_ref_die_offset_label (char *label, dw_die_ref ref)
6800 {
6801   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6802 }
6803
6804 /* Convert a DIE tag into its string name.  */
6805
6806 static const char *
6807 dwarf_tag_name (unsigned int tag)
6808 {
6809   switch (tag)
6810     {
6811     case DW_TAG_padding:
6812       return "DW_TAG_padding";
6813     case DW_TAG_array_type:
6814       return "DW_TAG_array_type";
6815     case DW_TAG_class_type:
6816       return "DW_TAG_class_type";
6817     case DW_TAG_entry_point:
6818       return "DW_TAG_entry_point";
6819     case DW_TAG_enumeration_type:
6820       return "DW_TAG_enumeration_type";
6821     case DW_TAG_formal_parameter:
6822       return "DW_TAG_formal_parameter";
6823     case DW_TAG_imported_declaration:
6824       return "DW_TAG_imported_declaration";
6825     case DW_TAG_label:
6826       return "DW_TAG_label";
6827     case DW_TAG_lexical_block:
6828       return "DW_TAG_lexical_block";
6829     case DW_TAG_member:
6830       return "DW_TAG_member";
6831     case DW_TAG_pointer_type:
6832       return "DW_TAG_pointer_type";
6833     case DW_TAG_reference_type:
6834       return "DW_TAG_reference_type";
6835     case DW_TAG_compile_unit:
6836       return "DW_TAG_compile_unit";
6837     case DW_TAG_string_type:
6838       return "DW_TAG_string_type";
6839     case DW_TAG_structure_type:
6840       return "DW_TAG_structure_type";
6841     case DW_TAG_subroutine_type:
6842       return "DW_TAG_subroutine_type";
6843     case DW_TAG_typedef:
6844       return "DW_TAG_typedef";
6845     case DW_TAG_union_type:
6846       return "DW_TAG_union_type";
6847     case DW_TAG_unspecified_parameters:
6848       return "DW_TAG_unspecified_parameters";
6849     case DW_TAG_variant:
6850       return "DW_TAG_variant";
6851     case DW_TAG_common_block:
6852       return "DW_TAG_common_block";
6853     case DW_TAG_common_inclusion:
6854       return "DW_TAG_common_inclusion";
6855     case DW_TAG_inheritance:
6856       return "DW_TAG_inheritance";
6857     case DW_TAG_inlined_subroutine:
6858       return "DW_TAG_inlined_subroutine";
6859     case DW_TAG_module:
6860       return "DW_TAG_module";
6861     case DW_TAG_ptr_to_member_type:
6862       return "DW_TAG_ptr_to_member_type";
6863     case DW_TAG_set_type:
6864       return "DW_TAG_set_type";
6865     case DW_TAG_subrange_type:
6866       return "DW_TAG_subrange_type";
6867     case DW_TAG_with_stmt:
6868       return "DW_TAG_with_stmt";
6869     case DW_TAG_access_declaration:
6870       return "DW_TAG_access_declaration";
6871     case DW_TAG_base_type:
6872       return "DW_TAG_base_type";
6873     case DW_TAG_catch_block:
6874       return "DW_TAG_catch_block";
6875     case DW_TAG_const_type:
6876       return "DW_TAG_const_type";
6877     case DW_TAG_constant:
6878       return "DW_TAG_constant";
6879     case DW_TAG_enumerator:
6880       return "DW_TAG_enumerator";
6881     case DW_TAG_file_type:
6882       return "DW_TAG_file_type";
6883     case DW_TAG_friend:
6884       return "DW_TAG_friend";
6885     case DW_TAG_namelist:
6886       return "DW_TAG_namelist";
6887     case DW_TAG_namelist_item:
6888       return "DW_TAG_namelist_item";
6889     case DW_TAG_packed_type:
6890       return "DW_TAG_packed_type";
6891     case DW_TAG_subprogram:
6892       return "DW_TAG_subprogram";
6893     case DW_TAG_template_type_param:
6894       return "DW_TAG_template_type_param";
6895     case DW_TAG_template_value_param:
6896       return "DW_TAG_template_value_param";
6897     case DW_TAG_thrown_type:
6898       return "DW_TAG_thrown_type";
6899     case DW_TAG_try_block:
6900       return "DW_TAG_try_block";
6901     case DW_TAG_variant_part:
6902       return "DW_TAG_variant_part";
6903     case DW_TAG_variable:
6904       return "DW_TAG_variable";
6905     case DW_TAG_volatile_type:
6906       return "DW_TAG_volatile_type";
6907     case DW_TAG_dwarf_procedure:
6908       return "DW_TAG_dwarf_procedure";
6909     case DW_TAG_restrict_type:
6910       return "DW_TAG_restrict_type";
6911     case DW_TAG_interface_type:
6912       return "DW_TAG_interface_type";
6913     case DW_TAG_namespace:
6914       return "DW_TAG_namespace";
6915     case DW_TAG_imported_module:
6916       return "DW_TAG_imported_module";
6917     case DW_TAG_unspecified_type:
6918       return "DW_TAG_unspecified_type";
6919     case DW_TAG_partial_unit:
6920       return "DW_TAG_partial_unit";
6921     case DW_TAG_imported_unit:
6922       return "DW_TAG_imported_unit";
6923     case DW_TAG_condition:
6924       return "DW_TAG_condition";
6925     case DW_TAG_shared_type:
6926       return "DW_TAG_shared_type";
6927     case DW_TAG_type_unit:
6928       return "DW_TAG_type_unit";
6929     case DW_TAG_rvalue_reference_type:
6930       return "DW_TAG_rvalue_reference_type";
6931     case DW_TAG_template_alias:
6932       return "DW_TAG_template_alias";
6933     case DW_TAG_GNU_template_parameter_pack:
6934       return "DW_TAG_GNU_template_parameter_pack";
6935     case DW_TAG_GNU_formal_parameter_pack:
6936       return "DW_TAG_GNU_formal_parameter_pack";
6937     case DW_TAG_MIPS_loop:
6938       return "DW_TAG_MIPS_loop";
6939     case DW_TAG_format_label:
6940       return "DW_TAG_format_label";
6941     case DW_TAG_function_template:
6942       return "DW_TAG_function_template";
6943     case DW_TAG_class_template:
6944       return "DW_TAG_class_template";
6945     case DW_TAG_GNU_BINCL:
6946       return "DW_TAG_GNU_BINCL";
6947     case DW_TAG_GNU_EINCL:
6948       return "DW_TAG_GNU_EINCL";
6949     case DW_TAG_GNU_template_template_param:
6950       return "DW_TAG_GNU_template_template_param";
6951     case DW_TAG_GNU_call_site:
6952       return "DW_TAG_GNU_call_site";
6953     case DW_TAG_GNU_call_site_parameter:
6954       return "DW_TAG_GNU_call_site_parameter";
6955     default:
6956       return "DW_TAG_<unknown>";
6957     }
6958 }
6959
6960 /* Convert a DWARF attribute code into its string name.  */
6961
6962 static const char *
6963 dwarf_attr_name (unsigned int attr)
6964 {
6965   switch (attr)
6966     {
6967     case DW_AT_sibling:
6968       return "DW_AT_sibling";
6969     case DW_AT_location:
6970       return "DW_AT_location";
6971     case DW_AT_name:
6972       return "DW_AT_name";
6973     case DW_AT_ordering:
6974       return "DW_AT_ordering";
6975     case DW_AT_subscr_data:
6976       return "DW_AT_subscr_data";
6977     case DW_AT_byte_size:
6978       return "DW_AT_byte_size";
6979     case DW_AT_bit_offset:
6980       return "DW_AT_bit_offset";
6981     case DW_AT_bit_size:
6982       return "DW_AT_bit_size";
6983     case DW_AT_element_list:
6984       return "DW_AT_element_list";
6985     case DW_AT_stmt_list:
6986       return "DW_AT_stmt_list";
6987     case DW_AT_low_pc:
6988       return "DW_AT_low_pc";
6989     case DW_AT_high_pc:
6990       return "DW_AT_high_pc";
6991     case DW_AT_language:
6992       return "DW_AT_language";
6993     case DW_AT_member:
6994       return "DW_AT_member";
6995     case DW_AT_discr:
6996       return "DW_AT_discr";
6997     case DW_AT_discr_value:
6998       return "DW_AT_discr_value";
6999     case DW_AT_visibility:
7000       return "DW_AT_visibility";
7001     case DW_AT_import:
7002       return "DW_AT_import";
7003     case DW_AT_string_length:
7004       return "DW_AT_string_length";
7005     case DW_AT_common_reference:
7006       return "DW_AT_common_reference";
7007     case DW_AT_comp_dir:
7008       return "DW_AT_comp_dir";
7009     case DW_AT_const_value:
7010       return "DW_AT_const_value";
7011     case DW_AT_containing_type:
7012       return "DW_AT_containing_type";
7013     case DW_AT_default_value:
7014       return "DW_AT_default_value";
7015     case DW_AT_inline:
7016       return "DW_AT_inline";
7017     case DW_AT_is_optional:
7018       return "DW_AT_is_optional";
7019     case DW_AT_lower_bound:
7020       return "DW_AT_lower_bound";
7021     case DW_AT_producer:
7022       return "DW_AT_producer";
7023     case DW_AT_prototyped:
7024       return "DW_AT_prototyped";
7025     case DW_AT_return_addr:
7026       return "DW_AT_return_addr";
7027     case DW_AT_start_scope:
7028       return "DW_AT_start_scope";
7029     case DW_AT_bit_stride:
7030       return "DW_AT_bit_stride";
7031     case DW_AT_upper_bound:
7032       return "DW_AT_upper_bound";
7033     case DW_AT_abstract_origin:
7034       return "DW_AT_abstract_origin";
7035     case DW_AT_accessibility:
7036       return "DW_AT_accessibility";
7037     case DW_AT_address_class:
7038       return "DW_AT_address_class";
7039     case DW_AT_artificial:
7040       return "DW_AT_artificial";
7041     case DW_AT_base_types:
7042       return "DW_AT_base_types";
7043     case DW_AT_calling_convention:
7044       return "DW_AT_calling_convention";
7045     case DW_AT_count:
7046       return "DW_AT_count";
7047     case DW_AT_data_member_location:
7048       return "DW_AT_data_member_location";
7049     case DW_AT_decl_column:
7050       return "DW_AT_decl_column";
7051     case DW_AT_decl_file:
7052       return "DW_AT_decl_file";
7053     case DW_AT_decl_line:
7054       return "DW_AT_decl_line";
7055     case DW_AT_declaration:
7056       return "DW_AT_declaration";
7057     case DW_AT_discr_list:
7058       return "DW_AT_discr_list";
7059     case DW_AT_encoding:
7060       return "DW_AT_encoding";
7061     case DW_AT_external:
7062       return "DW_AT_external";
7063     case DW_AT_explicit:
7064       return "DW_AT_explicit";
7065     case DW_AT_frame_base:
7066       return "DW_AT_frame_base";
7067     case DW_AT_friend:
7068       return "DW_AT_friend";
7069     case DW_AT_identifier_case:
7070       return "DW_AT_identifier_case";
7071     case DW_AT_macro_info:
7072       return "DW_AT_macro_info";
7073     case DW_AT_namelist_items:
7074       return "DW_AT_namelist_items";
7075     case DW_AT_priority:
7076       return "DW_AT_priority";
7077     case DW_AT_segment:
7078       return "DW_AT_segment";
7079     case DW_AT_specification:
7080       return "DW_AT_specification";
7081     case DW_AT_static_link:
7082       return "DW_AT_static_link";
7083     case DW_AT_type:
7084       return "DW_AT_type";
7085     case DW_AT_use_location:
7086       return "DW_AT_use_location";
7087     case DW_AT_variable_parameter:
7088       return "DW_AT_variable_parameter";
7089     case DW_AT_virtuality:
7090       return "DW_AT_virtuality";
7091     case DW_AT_vtable_elem_location:
7092       return "DW_AT_vtable_elem_location";
7093
7094     case DW_AT_allocated:
7095       return "DW_AT_allocated";
7096     case DW_AT_associated:
7097       return "DW_AT_associated";
7098     case DW_AT_data_location:
7099       return "DW_AT_data_location";
7100     case DW_AT_byte_stride:
7101       return "DW_AT_byte_stride";
7102     case DW_AT_entry_pc:
7103       return "DW_AT_entry_pc";
7104     case DW_AT_use_UTF8:
7105       return "DW_AT_use_UTF8";
7106     case DW_AT_extension:
7107       return "DW_AT_extension";
7108     case DW_AT_ranges:
7109       return "DW_AT_ranges";
7110     case DW_AT_trampoline:
7111       return "DW_AT_trampoline";
7112     case DW_AT_call_column:
7113       return "DW_AT_call_column";
7114     case DW_AT_call_file:
7115       return "DW_AT_call_file";
7116     case DW_AT_call_line:
7117       return "DW_AT_call_line";
7118     case DW_AT_object_pointer:
7119       return "DW_AT_object_pointer";
7120
7121     case DW_AT_signature:
7122       return "DW_AT_signature";
7123     case DW_AT_main_subprogram:
7124       return "DW_AT_main_subprogram";
7125     case DW_AT_data_bit_offset:
7126       return "DW_AT_data_bit_offset";
7127     case DW_AT_const_expr:
7128       return "DW_AT_const_expr";
7129     case DW_AT_enum_class:
7130       return "DW_AT_enum_class";
7131     case DW_AT_linkage_name:
7132       return "DW_AT_linkage_name";
7133
7134     case DW_AT_MIPS_fde:
7135       return "DW_AT_MIPS_fde";
7136     case DW_AT_MIPS_loop_begin:
7137       return "DW_AT_MIPS_loop_begin";
7138     case DW_AT_MIPS_tail_loop_begin:
7139       return "DW_AT_MIPS_tail_loop_begin";
7140     case DW_AT_MIPS_epilog_begin:
7141       return "DW_AT_MIPS_epilog_begin";
7142 #if VMS_DEBUGGING_INFO
7143     case DW_AT_HP_prologue:
7144       return "DW_AT_HP_prologue";
7145 #else
7146     case DW_AT_MIPS_loop_unroll_factor:
7147       return "DW_AT_MIPS_loop_unroll_factor";
7148 #endif
7149     case DW_AT_MIPS_software_pipeline_depth:
7150       return "DW_AT_MIPS_software_pipeline_depth";
7151     case DW_AT_MIPS_linkage_name:
7152       return "DW_AT_MIPS_linkage_name";
7153 #if VMS_DEBUGGING_INFO
7154     case DW_AT_HP_epilogue:
7155       return "DW_AT_HP_epilogue";
7156 #else
7157     case DW_AT_MIPS_stride:
7158       return "DW_AT_MIPS_stride";
7159 #endif
7160     case DW_AT_MIPS_abstract_name:
7161       return "DW_AT_MIPS_abstract_name";
7162     case DW_AT_MIPS_clone_origin:
7163       return "DW_AT_MIPS_clone_origin";
7164     case DW_AT_MIPS_has_inlines:
7165       return "DW_AT_MIPS_has_inlines";
7166
7167     case DW_AT_sf_names:
7168       return "DW_AT_sf_names";
7169     case DW_AT_src_info:
7170       return "DW_AT_src_info";
7171     case DW_AT_mac_info:
7172       return "DW_AT_mac_info";
7173     case DW_AT_src_coords:
7174       return "DW_AT_src_coords";
7175     case DW_AT_body_begin:
7176       return "DW_AT_body_begin";
7177     case DW_AT_body_end:
7178       return "DW_AT_body_end";
7179     case DW_AT_GNU_vector:
7180       return "DW_AT_GNU_vector";
7181     case DW_AT_GNU_guarded_by:
7182       return "DW_AT_GNU_guarded_by";
7183     case DW_AT_GNU_pt_guarded_by:
7184       return "DW_AT_GNU_pt_guarded_by";
7185     case DW_AT_GNU_guarded:
7186       return "DW_AT_GNU_guarded";
7187     case DW_AT_GNU_pt_guarded:
7188       return "DW_AT_GNU_pt_guarded";
7189     case DW_AT_GNU_locks_excluded:
7190       return "DW_AT_GNU_locks_excluded";
7191     case DW_AT_GNU_exclusive_locks_required:
7192       return "DW_AT_GNU_exclusive_locks_required";
7193     case DW_AT_GNU_shared_locks_required:
7194       return "DW_AT_GNU_shared_locks_required";
7195     case DW_AT_GNU_odr_signature:
7196       return "DW_AT_GNU_odr_signature";
7197     case DW_AT_GNU_template_name:
7198       return "DW_AT_GNU_template_name";
7199     case DW_AT_GNU_call_site_value:
7200       return "DW_AT_GNU_call_site_value";
7201     case DW_AT_GNU_call_site_data_value:
7202       return "DW_AT_GNU_call_site_data_value";
7203     case DW_AT_GNU_call_site_target:
7204       return "DW_AT_GNU_call_site_target";
7205     case DW_AT_GNU_call_site_target_clobbered:
7206       return "DW_AT_GNU_call_site_target_clobbered";
7207     case DW_AT_GNU_tail_call:
7208       return "DW_AT_GNU_tail_call";
7209     case DW_AT_GNU_all_tail_call_sites:
7210       return "DW_AT_GNU_all_tail_call_sites";
7211     case DW_AT_GNU_all_call_sites:
7212       return "DW_AT_GNU_all_call_sites";
7213     case DW_AT_GNU_all_source_call_sites:
7214       return "DW_AT_GNU_all_source_call_sites";
7215
7216     case DW_AT_VMS_rtnbeg_pd_address:
7217       return "DW_AT_VMS_rtnbeg_pd_address";
7218
7219     default:
7220       return "DW_AT_<unknown>";
7221     }
7222 }
7223
7224 /* Convert a DWARF value form code into its string name.  */
7225
7226 static const char *
7227 dwarf_form_name (unsigned int form)
7228 {
7229   switch (form)
7230     {
7231     case DW_FORM_addr:
7232       return "DW_FORM_addr";
7233     case DW_FORM_block2:
7234       return "DW_FORM_block2";
7235     case DW_FORM_block4:
7236       return "DW_FORM_block4";
7237     case DW_FORM_data2:
7238       return "DW_FORM_data2";
7239     case DW_FORM_data4:
7240       return "DW_FORM_data4";
7241     case DW_FORM_data8:
7242       return "DW_FORM_data8";
7243     case DW_FORM_string:
7244       return "DW_FORM_string";
7245     case DW_FORM_block:
7246       return "DW_FORM_block";
7247     case DW_FORM_block1:
7248       return "DW_FORM_block1";
7249     case DW_FORM_data1:
7250       return "DW_FORM_data1";
7251     case DW_FORM_flag:
7252       return "DW_FORM_flag";
7253     case DW_FORM_sdata:
7254       return "DW_FORM_sdata";
7255     case DW_FORM_strp:
7256       return "DW_FORM_strp";
7257     case DW_FORM_udata:
7258       return "DW_FORM_udata";
7259     case DW_FORM_ref_addr:
7260       return "DW_FORM_ref_addr";
7261     case DW_FORM_ref1:
7262       return "DW_FORM_ref1";
7263     case DW_FORM_ref2:
7264       return "DW_FORM_ref2";
7265     case DW_FORM_ref4:
7266       return "DW_FORM_ref4";
7267     case DW_FORM_ref8:
7268       return "DW_FORM_ref8";
7269     case DW_FORM_ref_udata:
7270       return "DW_FORM_ref_udata";
7271     case DW_FORM_indirect:
7272       return "DW_FORM_indirect";
7273     case DW_FORM_sec_offset:
7274       return "DW_FORM_sec_offset";
7275     case DW_FORM_exprloc:
7276       return "DW_FORM_exprloc";
7277     case DW_FORM_flag_present:
7278       return "DW_FORM_flag_present";
7279     case DW_FORM_ref_sig8:
7280       return "DW_FORM_ref_sig8";
7281     default:
7282       return "DW_FORM_<unknown>";
7283     }
7284 }
7285 \f
7286 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7287    instance of an inlined instance of a decl which is local to an inline
7288    function, so we have to trace all of the way back through the origin chain
7289    to find out what sort of node actually served as the original seed for the
7290    given block.  */
7291
7292 static tree
7293 decl_ultimate_origin (const_tree decl)
7294 {
7295   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7296     return NULL_TREE;
7297
7298   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7299      nodes in the function to point to themselves; ignore that if
7300      we're trying to output the abstract instance of this function.  */
7301   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7302     return NULL_TREE;
7303
7304   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7305      most distant ancestor, this should never happen.  */
7306   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7307
7308   return DECL_ABSTRACT_ORIGIN (decl);
7309 }
7310
7311 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7312    of a virtual function may refer to a base class, so we check the 'this'
7313    parameter.  */
7314
7315 static tree
7316 decl_class_context (tree decl)
7317 {
7318   tree context = NULL_TREE;
7319
7320   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7321     context = DECL_CONTEXT (decl);
7322   else
7323     context = TYPE_MAIN_VARIANT
7324       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7325
7326   if (context && !TYPE_P (context))
7327     context = NULL_TREE;
7328
7329   return context;
7330 }
7331 \f
7332 /* Add an attribute/value pair to a DIE.  */
7333
7334 static inline void
7335 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7336 {
7337   /* Maybe this should be an assert?  */
7338   if (die == NULL)
7339     return;
7340
7341   if (die->die_attr == NULL)
7342     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7343   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7344 }
7345
7346 static inline enum dw_val_class
7347 AT_class (dw_attr_ref a)
7348 {
7349   return a->dw_attr_val.val_class;
7350 }
7351
7352 /* Add a flag value attribute to a DIE.  */
7353
7354 static inline void
7355 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7356 {
7357   dw_attr_node attr;
7358
7359   attr.dw_attr = attr_kind;
7360   attr.dw_attr_val.val_class = dw_val_class_flag;
7361   attr.dw_attr_val.v.val_flag = flag;
7362   add_dwarf_attr (die, &attr);
7363 }
7364
7365 static inline unsigned
7366 AT_flag (dw_attr_ref a)
7367 {
7368   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7369   return a->dw_attr_val.v.val_flag;
7370 }
7371
7372 /* Add a signed integer attribute value to a DIE.  */
7373
7374 static inline void
7375 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7376 {
7377   dw_attr_node attr;
7378
7379   attr.dw_attr = attr_kind;
7380   attr.dw_attr_val.val_class = dw_val_class_const;
7381   attr.dw_attr_val.v.val_int = int_val;
7382   add_dwarf_attr (die, &attr);
7383 }
7384
7385 static inline HOST_WIDE_INT
7386 AT_int (dw_attr_ref a)
7387 {
7388   gcc_assert (a && AT_class (a) == dw_val_class_const);
7389   return a->dw_attr_val.v.val_int;
7390 }
7391
7392 /* Add an unsigned integer attribute value to a DIE.  */
7393
7394 static inline void
7395 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7396                  unsigned HOST_WIDE_INT unsigned_val)
7397 {
7398   dw_attr_node attr;
7399
7400   attr.dw_attr = attr_kind;
7401   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7402   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7403   add_dwarf_attr (die, &attr);
7404 }
7405
7406 static inline unsigned HOST_WIDE_INT
7407 AT_unsigned (dw_attr_ref a)
7408 {
7409   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7410   return a->dw_attr_val.v.val_unsigned;
7411 }
7412
7413 /* Add an unsigned double integer attribute value to a DIE.  */
7414
7415 static inline void
7416 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7417                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7418 {
7419   dw_attr_node attr;
7420
7421   attr.dw_attr = attr_kind;
7422   attr.dw_attr_val.val_class = dw_val_class_const_double;
7423   attr.dw_attr_val.v.val_double.high = high;
7424   attr.dw_attr_val.v.val_double.low = low;
7425   add_dwarf_attr (die, &attr);
7426 }
7427
7428 /* Add a floating point attribute value to a DIE and return it.  */
7429
7430 static inline void
7431 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7432             unsigned int length, unsigned int elt_size, unsigned char *array)
7433 {
7434   dw_attr_node attr;
7435
7436   attr.dw_attr = attr_kind;
7437   attr.dw_attr_val.val_class = dw_val_class_vec;
7438   attr.dw_attr_val.v.val_vec.length = length;
7439   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7440   attr.dw_attr_val.v.val_vec.array = array;
7441   add_dwarf_attr (die, &attr);
7442 }
7443
7444 /* Add an 8-byte data attribute value to a DIE.  */
7445
7446 static inline void
7447 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7448               unsigned char data8[8])
7449 {
7450   dw_attr_node attr;
7451
7452   attr.dw_attr = attr_kind;
7453   attr.dw_attr_val.val_class = dw_val_class_data8;
7454   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7455   add_dwarf_attr (die, &attr);
7456 }
7457
7458 /* Hash and equality functions for debug_str_hash.  */
7459
7460 static hashval_t
7461 debug_str_do_hash (const void *x)
7462 {
7463   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7464 }
7465
7466 static int
7467 debug_str_eq (const void *x1, const void *x2)
7468 {
7469   return strcmp ((((const struct indirect_string_node *)x1)->str),
7470                  (const char *)x2) == 0;
7471 }
7472
7473 /* Add STR to the indirect string hash table.  */
7474
7475 static struct indirect_string_node *
7476 find_AT_string (const char *str)
7477 {
7478   struct indirect_string_node *node;
7479   void **slot;
7480
7481   if (! debug_str_hash)
7482     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7483                                       debug_str_eq, NULL);
7484
7485   slot = htab_find_slot_with_hash (debug_str_hash, str,
7486                                    htab_hash_string (str), INSERT);
7487   if (*slot == NULL)
7488     {
7489       node = ggc_alloc_cleared_indirect_string_node ();
7490       node->str = ggc_strdup (str);
7491       *slot = node;
7492     }
7493   else
7494     node = (struct indirect_string_node *) *slot;
7495
7496   node->refcount++;
7497   return node;
7498 }
7499
7500 /* Add a string attribute value to a DIE.  */
7501
7502 static inline void
7503 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7504 {
7505   dw_attr_node attr;
7506   struct indirect_string_node *node;
7507
7508   node = find_AT_string (str);
7509
7510   attr.dw_attr = attr_kind;
7511   attr.dw_attr_val.val_class = dw_val_class_str;
7512   attr.dw_attr_val.v.val_str = node;
7513   add_dwarf_attr (die, &attr);
7514 }
7515
7516 /* Create a label for an indirect string node, ensuring it is going to
7517    be output, unless its reference count goes down to zero.  */
7518
7519 static inline void
7520 gen_label_for_indirect_string (struct indirect_string_node *node)
7521 {
7522   char label[32];
7523
7524   if (node->label)
7525     return;
7526
7527   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7528   ++dw2_string_counter;
7529   node->label = xstrdup (label);
7530 }
7531
7532 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7533    debug string STR.  */
7534
7535 static inline rtx
7536 get_debug_string_label (const char *str)
7537 {
7538   struct indirect_string_node *node = find_AT_string (str);
7539
7540   debug_str_hash_forced = true;
7541
7542   gen_label_for_indirect_string (node);
7543
7544   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7545 }
7546
7547 static inline const char *
7548 AT_string (dw_attr_ref a)
7549 {
7550   gcc_assert (a && AT_class (a) == dw_val_class_str);
7551   return a->dw_attr_val.v.val_str->str;
7552 }
7553
7554 /* Find out whether a string should be output inline in DIE
7555    or out-of-line in .debug_str section.  */
7556
7557 static enum dwarf_form
7558 AT_string_form (dw_attr_ref a)
7559 {
7560   struct indirect_string_node *node;
7561   unsigned int len;
7562
7563   gcc_assert (a && AT_class (a) == dw_val_class_str);
7564
7565   node = a->dw_attr_val.v.val_str;
7566   if (node->form)
7567     return node->form;
7568
7569   len = strlen (node->str) + 1;
7570
7571   /* If the string is shorter or equal to the size of the reference, it is
7572      always better to put it inline.  */
7573   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7574     return node->form = DW_FORM_string;
7575
7576   /* If we cannot expect the linker to merge strings in .debug_str
7577      section, only put it into .debug_str if it is worth even in this
7578      single module.  */
7579   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7580       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7581       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7582     return node->form = DW_FORM_string;
7583
7584   gen_label_for_indirect_string (node);
7585
7586   return node->form = DW_FORM_strp;
7587 }
7588
7589 /* Add a DIE reference attribute value to a DIE.  */
7590
7591 static inline void
7592 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7593 {
7594   dw_attr_node attr;
7595
7596 #ifdef ENABLE_CHECKING
7597   gcc_assert (targ_die != NULL);
7598 #else
7599   /* With LTO we can end up trying to reference something we didn't create
7600      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7601   if (targ_die == NULL)
7602     return;
7603 #endif
7604
7605   attr.dw_attr = attr_kind;
7606   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7607   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7608   attr.dw_attr_val.v.val_die_ref.external = 0;
7609   add_dwarf_attr (die, &attr);
7610 }
7611
7612 /* Add an AT_specification attribute to a DIE, and also make the back
7613    pointer from the specification to the definition.  */
7614
7615 static inline void
7616 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7617 {
7618   add_AT_die_ref (die, DW_AT_specification, targ_die);
7619   gcc_assert (!targ_die->die_definition);
7620   targ_die->die_definition = die;
7621 }
7622
7623 static inline dw_die_ref
7624 AT_ref (dw_attr_ref a)
7625 {
7626   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7627   return a->dw_attr_val.v.val_die_ref.die;
7628 }
7629
7630 static inline int
7631 AT_ref_external (dw_attr_ref a)
7632 {
7633   if (a && AT_class (a) == dw_val_class_die_ref)
7634     return a->dw_attr_val.v.val_die_ref.external;
7635
7636   return 0;
7637 }
7638
7639 static inline void
7640 set_AT_ref_external (dw_attr_ref a, int i)
7641 {
7642   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7643   a->dw_attr_val.v.val_die_ref.external = i;
7644 }
7645
7646 /* Add an FDE reference attribute value to a DIE.  */
7647
7648 static inline void
7649 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7650 {
7651   dw_attr_node attr;
7652
7653   attr.dw_attr = attr_kind;
7654   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7655   attr.dw_attr_val.v.val_fde_index = targ_fde;
7656   add_dwarf_attr (die, &attr);
7657 }
7658
7659 /* Add a location description attribute value to a DIE.  */
7660
7661 static inline void
7662 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7663 {
7664   dw_attr_node attr;
7665
7666   attr.dw_attr = attr_kind;
7667   attr.dw_attr_val.val_class = dw_val_class_loc;
7668   attr.dw_attr_val.v.val_loc = loc;
7669   add_dwarf_attr (die, &attr);
7670 }
7671
7672 static inline dw_loc_descr_ref
7673 AT_loc (dw_attr_ref a)
7674 {
7675   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7676   return a->dw_attr_val.v.val_loc;
7677 }
7678
7679 static inline void
7680 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7681 {
7682   dw_attr_node attr;
7683
7684   attr.dw_attr = attr_kind;
7685   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7686   attr.dw_attr_val.v.val_loc_list = loc_list;
7687   add_dwarf_attr (die, &attr);
7688   have_location_lists = true;
7689 }
7690
7691 static inline dw_loc_list_ref
7692 AT_loc_list (dw_attr_ref a)
7693 {
7694   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7695   return a->dw_attr_val.v.val_loc_list;
7696 }
7697
7698 static inline dw_loc_list_ref *
7699 AT_loc_list_ptr (dw_attr_ref a)
7700 {
7701   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7702   return &a->dw_attr_val.v.val_loc_list;
7703 }
7704
7705 /* Add an address constant attribute value to a DIE.  */
7706
7707 static inline void
7708 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7709 {
7710   dw_attr_node attr;
7711
7712   attr.dw_attr = attr_kind;
7713   attr.dw_attr_val.val_class = dw_val_class_addr;
7714   attr.dw_attr_val.v.val_addr = addr;
7715   add_dwarf_attr (die, &attr);
7716 }
7717
7718 /* Get the RTX from to an address DIE attribute.  */
7719
7720 static inline rtx
7721 AT_addr (dw_attr_ref a)
7722 {
7723   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7724   return a->dw_attr_val.v.val_addr;
7725 }
7726
7727 /* Add a file attribute value to a DIE.  */
7728
7729 static inline void
7730 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7731              struct dwarf_file_data *fd)
7732 {
7733   dw_attr_node attr;
7734
7735   attr.dw_attr = attr_kind;
7736   attr.dw_attr_val.val_class = dw_val_class_file;
7737   attr.dw_attr_val.v.val_file = fd;
7738   add_dwarf_attr (die, &attr);
7739 }
7740
7741 /* Get the dwarf_file_data from a file DIE attribute.  */
7742
7743 static inline struct dwarf_file_data *
7744 AT_file (dw_attr_ref a)
7745 {
7746   gcc_assert (a && AT_class (a) == dw_val_class_file);
7747   return a->dw_attr_val.v.val_file;
7748 }
7749
7750 /* Add a vms delta attribute value to a DIE.  */
7751
7752 static inline void
7753 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7754                   const char *lbl1, const char *lbl2)
7755 {
7756   dw_attr_node attr;
7757
7758   attr.dw_attr = attr_kind;
7759   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7760   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7761   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7762   add_dwarf_attr (die, &attr);
7763 }
7764
7765 /* Add a label identifier attribute value to a DIE.  */
7766
7767 static inline void
7768 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7769 {
7770   dw_attr_node attr;
7771
7772   attr.dw_attr = attr_kind;
7773   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7774   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7775   add_dwarf_attr (die, &attr);
7776 }
7777
7778 /* Add a section offset attribute value to a DIE, an offset into the
7779    debug_line section.  */
7780
7781 static inline void
7782 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7783                 const char *label)
7784 {
7785   dw_attr_node attr;
7786
7787   attr.dw_attr = attr_kind;
7788   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7789   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7790   add_dwarf_attr (die, &attr);
7791 }
7792
7793 /* Add a section offset attribute value to a DIE, an offset into the
7794    debug_macinfo section.  */
7795
7796 static inline void
7797 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7798                const char *label)
7799 {
7800   dw_attr_node attr;
7801
7802   attr.dw_attr = attr_kind;
7803   attr.dw_attr_val.val_class = dw_val_class_macptr;
7804   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7805   add_dwarf_attr (die, &attr);
7806 }
7807
7808 /* Add an offset attribute value to a DIE.  */
7809
7810 static inline void
7811 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7812                unsigned HOST_WIDE_INT offset)
7813 {
7814   dw_attr_node attr;
7815
7816   attr.dw_attr = attr_kind;
7817   attr.dw_attr_val.val_class = dw_val_class_offset;
7818   attr.dw_attr_val.v.val_offset = offset;
7819   add_dwarf_attr (die, &attr);
7820 }
7821
7822 /* Add an range_list attribute value to a DIE.  */
7823
7824 static void
7825 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7826                    long unsigned int offset)
7827 {
7828   dw_attr_node attr;
7829
7830   attr.dw_attr = attr_kind;
7831   attr.dw_attr_val.val_class = dw_val_class_range_list;
7832   attr.dw_attr_val.v.val_offset = offset;
7833   add_dwarf_attr (die, &attr);
7834 }
7835
7836 /* Return the start label of a delta attribute.  */
7837
7838 static inline const char *
7839 AT_vms_delta1 (dw_attr_ref a)
7840 {
7841   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7842   return a->dw_attr_val.v.val_vms_delta.lbl1;
7843 }
7844
7845 /* Return the end label of a delta attribute.  */
7846
7847 static inline const char *
7848 AT_vms_delta2 (dw_attr_ref a)
7849 {
7850   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7851   return a->dw_attr_val.v.val_vms_delta.lbl2;
7852 }
7853
7854 static inline const char *
7855 AT_lbl (dw_attr_ref a)
7856 {
7857   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7858                     || AT_class (a) == dw_val_class_lineptr
7859                     || AT_class (a) == dw_val_class_macptr));
7860   return a->dw_attr_val.v.val_lbl_id;
7861 }
7862
7863 /* Get the attribute of type attr_kind.  */
7864
7865 static dw_attr_ref
7866 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7867 {
7868   dw_attr_ref a;
7869   unsigned ix;
7870   dw_die_ref spec = NULL;
7871
7872   if (! die)
7873     return NULL;
7874
7875   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7876     if (a->dw_attr == attr_kind)
7877       return a;
7878     else if (a->dw_attr == DW_AT_specification
7879              || a->dw_attr == DW_AT_abstract_origin)
7880       spec = AT_ref (a);
7881
7882   if (spec)
7883     return get_AT (spec, attr_kind);
7884
7885   return NULL;
7886 }
7887
7888 /* Return the "low pc" attribute value, typically associated with a subprogram
7889    DIE.  Return null if the "low pc" attribute is either not present, or if it
7890    cannot be represented as an assembler label identifier.  */
7891
7892 static inline const char *
7893 get_AT_low_pc (dw_die_ref die)
7894 {
7895   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7896
7897   return a ? AT_lbl (a) : NULL;
7898 }
7899
7900 /* Return the "high pc" attribute value, typically associated with a subprogram
7901    DIE.  Return null if the "high pc" attribute is either not present, or if it
7902    cannot be represented as an assembler label identifier.  */
7903
7904 static inline const char *
7905 get_AT_hi_pc (dw_die_ref die)
7906 {
7907   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7908
7909   return a ? AT_lbl (a) : NULL;
7910 }
7911
7912 /* Return the value of the string attribute designated by ATTR_KIND, or
7913    NULL if it is not present.  */
7914
7915 static inline const char *
7916 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7917 {
7918   dw_attr_ref a = get_AT (die, attr_kind);
7919
7920   return a ? AT_string (a) : NULL;
7921 }
7922
7923 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7924    if it is not present.  */
7925
7926 static inline int
7927 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7928 {
7929   dw_attr_ref a = get_AT (die, attr_kind);
7930
7931   return a ? AT_flag (a) : 0;
7932 }
7933
7934 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7935    if it is not present.  */
7936
7937 static inline unsigned
7938 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7939 {
7940   dw_attr_ref a = get_AT (die, attr_kind);
7941
7942   return a ? AT_unsigned (a) : 0;
7943 }
7944
7945 static inline dw_die_ref
7946 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7947 {
7948   dw_attr_ref a = get_AT (die, attr_kind);
7949
7950   return a ? AT_ref (a) : NULL;
7951 }
7952
7953 static inline struct dwarf_file_data *
7954 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7955 {
7956   dw_attr_ref a = get_AT (die, attr_kind);
7957
7958   return a ? AT_file (a) : NULL;
7959 }
7960
7961 /* Return TRUE if the language is C++.  */
7962
7963 static inline bool
7964 is_cxx (void)
7965 {
7966   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7967
7968   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7969 }
7970
7971 /* Return TRUE if the language is Fortran.  */
7972
7973 static inline bool
7974 is_fortran (void)
7975 {
7976   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7977
7978   return (lang == DW_LANG_Fortran77
7979           || lang == DW_LANG_Fortran90
7980           || lang == DW_LANG_Fortran95);
7981 }
7982
7983 /* Return TRUE if the language is Ada.  */
7984
7985 static inline bool
7986 is_ada (void)
7987 {
7988   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
7989
7990   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7991 }
7992
7993 /* Remove the specified attribute if present.  */
7994
7995 static void
7996 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7997 {
7998   dw_attr_ref a;
7999   unsigned ix;
8000
8001   if (! die)
8002     return;
8003
8004   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8005     if (a->dw_attr == attr_kind)
8006       {
8007         if (AT_class (a) == dw_val_class_str)
8008           if (a->dw_attr_val.v.val_str->refcount)
8009             a->dw_attr_val.v.val_str->refcount--;
8010
8011         /* VEC_ordered_remove should help reduce the number of abbrevs
8012            that are needed.  */
8013         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
8014         return;
8015       }
8016 }
8017
8018 /* Remove CHILD from its parent.  PREV must have the property that
8019    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
8020
8021 static void
8022 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
8023 {
8024   gcc_assert (child->die_parent == prev->die_parent);
8025   gcc_assert (prev->die_sib == child);
8026   if (prev == child)
8027     {
8028       gcc_assert (child->die_parent->die_child == child);
8029       prev = NULL;
8030     }
8031   else
8032     prev->die_sib = child->die_sib;
8033   if (child->die_parent->die_child == child)
8034     child->die_parent->die_child = prev;
8035 }
8036
8037 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
8038    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
8039
8040 static void
8041 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8042 {
8043   dw_die_ref parent = old_child->die_parent;
8044
8045   gcc_assert (parent == prev->die_parent);
8046   gcc_assert (prev->die_sib == old_child);
8047
8048   new_child->die_parent = parent;
8049   if (prev == old_child)
8050     {
8051       gcc_assert (parent->die_child == old_child);
8052       new_child->die_sib = new_child;
8053     }
8054   else
8055     {
8056       prev->die_sib = new_child;
8057       new_child->die_sib = old_child->die_sib;
8058     }
8059   if (old_child->die_parent->die_child == old_child)
8060     old_child->die_parent->die_child = new_child;
8061 }
8062
8063 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8064
8065 static void
8066 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8067 {
8068   dw_die_ref c;
8069   new_parent->die_child = old_parent->die_child;
8070   old_parent->die_child = NULL;
8071   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8072 }
8073
8074 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8075    matches TAG.  */
8076
8077 static void
8078 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8079 {
8080   dw_die_ref c;
8081
8082   c = die->die_child;
8083   if (c) do {
8084     dw_die_ref prev = c;
8085     c = c->die_sib;
8086     while (c->die_tag == tag)
8087       {
8088         remove_child_with_prev (c, prev);
8089         /* Might have removed every child.  */
8090         if (c == c->die_sib)
8091           return;
8092         c = c->die_sib;
8093       }
8094   } while (c != die->die_child);
8095 }
8096
8097 /* Add a CHILD_DIE as the last child of DIE.  */
8098
8099 static void
8100 add_child_die (dw_die_ref die, dw_die_ref child_die)
8101 {
8102   /* FIXME this should probably be an assert.  */
8103   if (! die || ! child_die)
8104     return;
8105   gcc_assert (die != child_die);
8106
8107   child_die->die_parent = die;
8108   if (die->die_child)
8109     {
8110       child_die->die_sib = die->die_child->die_sib;
8111       die->die_child->die_sib = child_die;
8112     }
8113   else
8114     child_die->die_sib = child_die;
8115   die->die_child = child_die;
8116 }
8117
8118 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8119    is the specification, to the end of PARENT's list of children.
8120    This is done by removing and re-adding it.  */
8121
8122 static void
8123 splice_child_die (dw_die_ref parent, dw_die_ref child)
8124 {
8125   dw_die_ref p;
8126
8127   /* We want the declaration DIE from inside the class, not the
8128      specification DIE at toplevel.  */
8129   if (child->die_parent != parent)
8130     {
8131       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8132
8133       if (tmp)
8134         child = tmp;
8135     }
8136
8137   gcc_assert (child->die_parent == parent
8138               || (child->die_parent
8139                   == get_AT_ref (parent, DW_AT_specification)));
8140
8141   for (p = child->die_parent->die_child; ; p = p->die_sib)
8142     if (p->die_sib == child)
8143       {
8144         remove_child_with_prev (child, p);
8145         break;
8146       }
8147
8148   add_child_die (parent, child);
8149 }
8150
8151 /* Return a pointer to a newly created DIE node.  */
8152
8153 static inline dw_die_ref
8154 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8155 {
8156   dw_die_ref die = ggc_alloc_cleared_die_node ();
8157
8158   die->die_tag = tag_value;
8159
8160   if (parent_die != NULL)
8161     add_child_die (parent_die, die);
8162   else
8163     {
8164       limbo_die_node *limbo_node;
8165
8166       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8167       limbo_node->die = die;
8168       limbo_node->created_for = t;
8169       limbo_node->next = limbo_die_list;
8170       limbo_die_list = limbo_node;
8171     }
8172
8173   return die;
8174 }
8175
8176 /* Return the DIE associated with the given type specifier.  */
8177
8178 static inline dw_die_ref
8179 lookup_type_die (tree type)
8180 {
8181   return TYPE_SYMTAB_DIE (type);
8182 }
8183
8184 /* Like lookup_type_die, but if type is an anonymous type named by a
8185    typedef[1], return the DIE of the anonymous type instead the one of
8186    the naming typedef.  This is because in gen_typedef_die, we did
8187    equate the anonymous struct named by the typedef with the DIE of
8188    the naming typedef. So by default, lookup_type_die on an anonymous
8189    struct yields the DIE of the naming typedef.
8190
8191    [1]: Read the comment of is_naming_typedef_decl to learn about what
8192    a naming typedef is.  */
8193
8194 static inline dw_die_ref
8195 lookup_type_die_strip_naming_typedef (tree type)
8196 {
8197   dw_die_ref die = lookup_type_die (type);
8198   if (TREE_CODE (type) == RECORD_TYPE
8199       && die->die_tag == DW_TAG_typedef
8200       && is_naming_typedef_decl (TYPE_NAME (type)))
8201     die = get_AT_ref (die, DW_AT_type);
8202   return die;
8203 }
8204
8205 /* Equate a DIE to a given type specifier.  */
8206
8207 static inline void
8208 equate_type_number_to_die (tree type, dw_die_ref type_die)
8209 {
8210   TYPE_SYMTAB_DIE (type) = type_die;
8211 }
8212
8213 /* Returns a hash value for X (which really is a die_struct).  */
8214
8215 static hashval_t
8216 decl_die_table_hash (const void *x)
8217 {
8218   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8219 }
8220
8221 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8222
8223 static int
8224 decl_die_table_eq (const void *x, const void *y)
8225 {
8226   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8227 }
8228
8229 /* Return the DIE associated with a given declaration.  */
8230
8231 static inline dw_die_ref
8232 lookup_decl_die (tree decl)
8233 {
8234   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8235 }
8236
8237 /* Returns a hash value for X (which really is a var_loc_list).  */
8238
8239 static hashval_t
8240 decl_loc_table_hash (const void *x)
8241 {
8242   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8243 }
8244
8245 /* Return nonzero if decl_id of var_loc_list X is the same as
8246    UID of decl *Y.  */
8247
8248 static int
8249 decl_loc_table_eq (const void *x, const void *y)
8250 {
8251   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8252 }
8253
8254 /* Return the var_loc list associated with a given declaration.  */
8255
8256 static inline var_loc_list *
8257 lookup_decl_loc (const_tree decl)
8258 {
8259   if (!decl_loc_table)
8260     return NULL;
8261   return (var_loc_list *)
8262     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8263 }
8264
8265 /* Equate a DIE to a particular declaration.  */
8266
8267 static void
8268 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8269 {
8270   unsigned int decl_id = DECL_UID (decl);
8271   void **slot;
8272
8273   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8274   *slot = decl_die;
8275   decl_die->decl_id = decl_id;
8276 }
8277
8278 /* Return how many bits covers PIECE EXPR_LIST.  */
8279
8280 static int
8281 decl_piece_bitsize (rtx piece)
8282 {
8283   int ret = (int) GET_MODE (piece);
8284   if (ret)
8285     return ret;
8286   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8287               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8288   return INTVAL (XEXP (XEXP (piece, 0), 0));
8289 }
8290
8291 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8292
8293 static rtx *
8294 decl_piece_varloc_ptr (rtx piece)
8295 {
8296   if ((int) GET_MODE (piece))
8297     return &XEXP (piece, 0);
8298   else
8299     return &XEXP (XEXP (piece, 0), 1);
8300 }
8301
8302 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8303    Next is the chain of following piece nodes.  */
8304
8305 static rtx
8306 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8307 {
8308   if (bitsize <= (int) MAX_MACHINE_MODE)
8309     return alloc_EXPR_LIST (bitsize, loc_note, next);
8310   else
8311     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8312                                                GEN_INT (bitsize),
8313                                                loc_note), next);
8314 }
8315
8316 /* Return rtx that should be stored into loc field for
8317    LOC_NOTE and BITPOS/BITSIZE.  */
8318
8319 static rtx
8320 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8321                       HOST_WIDE_INT bitsize)
8322 {
8323   if (bitsize != -1)
8324     {
8325       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8326       if (bitpos != 0)
8327         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8328     }
8329   return loc_note;
8330 }
8331
8332 /* This function either modifies location piece list *DEST in
8333    place (if SRC and INNER is NULL), or copies location piece list
8334    *SRC to *DEST while modifying it.  Location BITPOS is modified
8335    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8336    not copied and if needed some padding around it is added.
8337    When modifying in place, DEST should point to EXPR_LIST where
8338    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8339    to the start of the whole list and INNER points to the EXPR_LIST
8340    where earlier pieces cover PIECE_BITPOS bits.  */
8341
8342 static void
8343 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8344                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8345                    HOST_WIDE_INT bitsize, rtx loc_note)
8346 {
8347   int diff;
8348   bool copy = inner != NULL;
8349
8350   if (copy)
8351     {
8352       /* First copy all nodes preceeding the current bitpos.  */
8353       while (src != inner)
8354         {
8355           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8356                                    decl_piece_bitsize (*src), NULL_RTX);
8357           dest = &XEXP (*dest, 1);
8358           src = &XEXP (*src, 1);
8359         }
8360     }
8361   /* Add padding if needed.  */
8362   if (bitpos != piece_bitpos)
8363     {
8364       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8365                                copy ? NULL_RTX : *dest);
8366       dest = &XEXP (*dest, 1);
8367     }
8368   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8369     {
8370       gcc_assert (!copy);
8371       /* A piece with correct bitpos and bitsize already exist,
8372          just update the location for it and return.  */
8373       *decl_piece_varloc_ptr (*dest) = loc_note;
8374       return;
8375     }
8376   /* Add the piece that changed.  */
8377   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8378   dest = &XEXP (*dest, 1);
8379   /* Skip over pieces that overlap it.  */
8380   diff = bitpos - piece_bitpos + bitsize;
8381   if (!copy)
8382     src = dest;
8383   while (diff > 0 && *src)
8384     {
8385       rtx piece = *src;
8386       diff -= decl_piece_bitsize (piece);
8387       if (copy)
8388         src = &XEXP (piece, 1);
8389       else
8390         {
8391           *src = XEXP (piece, 1);
8392           free_EXPR_LIST_node (piece);
8393         }
8394     }
8395   /* Add padding if needed.  */
8396   if (diff < 0 && *src)
8397     {
8398       if (!copy)
8399         dest = src;
8400       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8401       dest = &XEXP (*dest, 1);
8402     }
8403   if (!copy)
8404     return;
8405   /* Finally copy all nodes following it.  */
8406   while (*src)
8407     {
8408       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8409                                decl_piece_bitsize (*src), NULL_RTX);
8410       dest = &XEXP (*dest, 1);
8411       src = &XEXP (*src, 1);
8412     }
8413 }
8414
8415 /* Add a variable location node to the linked list for DECL.  */
8416
8417 static struct var_loc_node *
8418 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8419 {
8420   unsigned int decl_id;
8421   var_loc_list *temp;
8422   void **slot;
8423   struct var_loc_node *loc = NULL;
8424   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8425
8426   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8427     {
8428       tree realdecl = DECL_DEBUG_EXPR (decl);
8429       if (realdecl && handled_component_p (realdecl))
8430         {
8431           HOST_WIDE_INT maxsize;
8432           tree innerdecl;
8433           innerdecl
8434             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8435           if (!DECL_P (innerdecl)
8436               || DECL_IGNORED_P (innerdecl)
8437               || TREE_STATIC (innerdecl)
8438               || bitsize <= 0
8439               || bitpos + bitsize > 256
8440               || bitsize != maxsize)
8441             return NULL;
8442           decl = innerdecl;
8443         }
8444     }
8445
8446   decl_id = DECL_UID (decl);
8447   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8448   if (*slot == NULL)
8449     {
8450       temp = ggc_alloc_cleared_var_loc_list ();
8451       temp->decl_id = decl_id;
8452       *slot = temp;
8453     }
8454   else
8455     temp = (var_loc_list *) *slot;
8456
8457   if (temp->last)
8458     {
8459       struct var_loc_node *last = temp->last, *unused = NULL;
8460       rtx *piece_loc = NULL, last_loc_note;
8461       int piece_bitpos = 0;
8462       if (last->next)
8463         {
8464           last = last->next;
8465           gcc_assert (last->next == NULL);
8466         }
8467       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8468         {
8469           piece_loc = &last->loc;
8470           do
8471             {
8472               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8473               if (piece_bitpos + cur_bitsize > bitpos)
8474                 break;
8475               piece_bitpos += cur_bitsize;
8476               piece_loc = &XEXP (*piece_loc, 1);
8477             }
8478           while (*piece_loc);
8479         }
8480       /* TEMP->LAST here is either pointer to the last but one or
8481          last element in the chained list, LAST is pointer to the
8482          last element.  */
8483       if (label && strcmp (last->label, label) == 0)
8484         {
8485           /* For SRA optimized variables if there weren't any real
8486              insns since last note, just modify the last node.  */
8487           if (piece_loc != NULL)
8488             {
8489               adjust_piece_list (piece_loc, NULL, NULL,
8490                                  bitpos, piece_bitpos, bitsize, loc_note);
8491               return NULL;
8492             }
8493           /* If the last note doesn't cover any instructions, remove it.  */
8494           if (temp->last != last)
8495             {
8496               temp->last->next = NULL;
8497               unused = last;
8498               last = temp->last;
8499               gcc_assert (strcmp (last->label, label) != 0);
8500             }
8501           else
8502             {
8503               gcc_assert (temp->first == temp->last);
8504               memset (temp->last, '\0', sizeof (*temp->last));
8505               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8506               return temp->last;
8507             }
8508         }
8509       if (bitsize == -1 && NOTE_P (last->loc))
8510         last_loc_note = last->loc;
8511       else if (piece_loc != NULL
8512                && *piece_loc != NULL_RTX
8513                && piece_bitpos == bitpos
8514                && decl_piece_bitsize (*piece_loc) == bitsize)
8515         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8516       else
8517         last_loc_note = NULL_RTX;
8518       /* If the current location is the same as the end of the list,
8519          and either both or neither of the locations is uninitialized,
8520          we have nothing to do.  */
8521       if (last_loc_note == NULL_RTX
8522           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8523                             NOTE_VAR_LOCATION_LOC (loc_note)))
8524           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8525                != NOTE_VAR_LOCATION_STATUS (loc_note))
8526               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8527                    == VAR_INIT_STATUS_UNINITIALIZED)
8528                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8529                       == VAR_INIT_STATUS_UNINITIALIZED))))
8530         {
8531           /* Add LOC to the end of list and update LAST.  If the last
8532              element of the list has been removed above, reuse its
8533              memory for the new node, otherwise allocate a new one.  */
8534           if (unused)
8535             {
8536               loc = unused;
8537               memset (loc, '\0', sizeof (*loc));
8538             }
8539           else
8540             loc = ggc_alloc_cleared_var_loc_node ();
8541           if (bitsize == -1 || piece_loc == NULL)
8542             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8543           else
8544             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8545                                bitpos, piece_bitpos, bitsize, loc_note);
8546           last->next = loc;
8547           /* Ensure TEMP->LAST will point either to the new last but one
8548              element of the chain, or to the last element in it.  */
8549           if (last != temp->last)
8550             temp->last = last;
8551         }
8552       else if (unused)
8553         ggc_free (unused);
8554     }
8555   else
8556     {
8557       loc = ggc_alloc_cleared_var_loc_node ();
8558       temp->first = loc;
8559       temp->last = loc;
8560       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8561     }
8562   return loc;
8563 }
8564 \f
8565 /* Keep track of the number of spaces used to indent the
8566    output of the debugging routines that print the structure of
8567    the DIE internal representation.  */
8568 static int print_indent;
8569
8570 /* Indent the line the number of spaces given by print_indent.  */
8571
8572 static inline void
8573 print_spaces (FILE *outfile)
8574 {
8575   fprintf (outfile, "%*s", print_indent, "");
8576 }
8577
8578 /* Print a type signature in hex.  */
8579
8580 static inline void
8581 print_signature (FILE *outfile, char *sig)
8582 {
8583   int i;
8584
8585   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8586     fprintf (outfile, "%02x", sig[i] & 0xff);
8587 }
8588
8589 /* Print the information associated with a given DIE, and its children.
8590    This routine is a debugging aid only.  */
8591
8592 static void
8593 print_die (dw_die_ref die, FILE *outfile)
8594 {
8595   dw_attr_ref a;
8596   dw_die_ref c;
8597   unsigned ix;
8598
8599   print_spaces (outfile);
8600   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8601            die->die_offset, dwarf_tag_name (die->die_tag),
8602            (void*) die);
8603   print_spaces (outfile);
8604   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8605   fprintf (outfile, " offset: %ld", die->die_offset);
8606   fprintf (outfile, " mark: %d\n", die->die_mark);
8607
8608   if (dwarf_version >= 4 && die->die_id.die_type_node)
8609     {
8610       print_spaces (outfile);
8611       fprintf (outfile, "  signature: ");
8612       print_signature (outfile, die->die_id.die_type_node->signature);
8613       fprintf (outfile, "\n");
8614     }
8615
8616   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8617     {
8618       print_spaces (outfile);
8619       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8620
8621       switch (AT_class (a))
8622         {
8623         case dw_val_class_addr:
8624           fprintf (outfile, "address");
8625           break;
8626         case dw_val_class_offset:
8627           fprintf (outfile, "offset");
8628           break;
8629         case dw_val_class_loc:
8630           fprintf (outfile, "location descriptor");
8631           break;
8632         case dw_val_class_loc_list:
8633           fprintf (outfile, "location list -> label:%s",
8634                    AT_loc_list (a)->ll_symbol);
8635           break;
8636         case dw_val_class_range_list:
8637           fprintf (outfile, "range list");
8638           break;
8639         case dw_val_class_const:
8640           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8641           break;
8642         case dw_val_class_unsigned_const:
8643           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8644           break;
8645         case dw_val_class_const_double:
8646           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8647                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8648                    a->dw_attr_val.v.val_double.high,
8649                    a->dw_attr_val.v.val_double.low);
8650           break;
8651         case dw_val_class_vec:
8652           fprintf (outfile, "floating-point or vector constant");
8653           break;
8654         case dw_val_class_flag:
8655           fprintf (outfile, "%u", AT_flag (a));
8656           break;
8657         case dw_val_class_die_ref:
8658           if (AT_ref (a) != NULL)
8659             {
8660               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8661                 {
8662                   fprintf (outfile, "die -> signature: ");
8663                   print_signature (outfile,
8664                                    AT_ref (a)->die_id.die_type_node->signature);
8665                 }
8666               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8667                 fprintf (outfile, "die -> label: %s",
8668                          AT_ref (a)->die_id.die_symbol);
8669               else
8670                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8671               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8672             }
8673           else
8674             fprintf (outfile, "die -> <null>");
8675           break;
8676         case dw_val_class_vms_delta:
8677           fprintf (outfile, "delta: @slotcount(%s-%s)",
8678                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8679           break;
8680         case dw_val_class_lbl_id:
8681         case dw_val_class_lineptr:
8682         case dw_val_class_macptr:
8683           fprintf (outfile, "label: %s", AT_lbl (a));
8684           break;
8685         case dw_val_class_str:
8686           if (AT_string (a) != NULL)
8687             fprintf (outfile, "\"%s\"", AT_string (a));
8688           else
8689             fprintf (outfile, "<null>");
8690           break;
8691         case dw_val_class_file:
8692           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8693                    AT_file (a)->emitted_number);
8694           break;
8695         case dw_val_class_data8:
8696           {
8697             int i;
8698
8699             for (i = 0; i < 8; i++)
8700               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8701             break;
8702           }
8703         default:
8704           break;
8705         }
8706
8707       fprintf (outfile, "\n");
8708     }
8709
8710   if (die->die_child != NULL)
8711     {
8712       print_indent += 4;
8713       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8714       print_indent -= 4;
8715     }
8716   if (print_indent == 0)
8717     fprintf (outfile, "\n");
8718 }
8719
8720 /* Print the contents of the source code line number correspondence table.
8721    This routine is a debugging aid only.  */
8722
8723 static void
8724 print_dwarf_line_table (FILE *outfile)
8725 {
8726   unsigned i;
8727   dw_line_info_ref line_info;
8728
8729   fprintf (outfile, "\n\nDWARF source line information\n");
8730   for (i = 1; i < line_info_table_in_use; i++)
8731     {
8732       line_info = &line_info_table[i];
8733       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8734                line_info->dw_file_num,
8735                line_info->dw_line_num);
8736     }
8737
8738   fprintf (outfile, "\n\n");
8739 }
8740
8741 /* Print the information collected for a given DIE.  */
8742
8743 DEBUG_FUNCTION void
8744 debug_dwarf_die (dw_die_ref die)
8745 {
8746   print_die (die, stderr);
8747 }
8748
8749 /* Print all DWARF information collected for the compilation unit.
8750    This routine is a debugging aid only.  */
8751
8752 DEBUG_FUNCTION void
8753 debug_dwarf (void)
8754 {
8755   print_indent = 0;
8756   print_die (comp_unit_die (), stderr);
8757   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8758     print_dwarf_line_table (stderr);
8759 }
8760 \f
8761 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8762    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8763    DIE that marks the start of the DIEs for this include file.  */
8764
8765 static dw_die_ref
8766 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8767 {
8768   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8769   dw_die_ref new_unit = gen_compile_unit_die (filename);
8770
8771   new_unit->die_sib = old_unit;
8772   return new_unit;
8773 }
8774
8775 /* Close an include-file CU and reopen the enclosing one.  */
8776
8777 static dw_die_ref
8778 pop_compile_unit (dw_die_ref old_unit)
8779 {
8780   dw_die_ref new_unit = old_unit->die_sib;
8781
8782   old_unit->die_sib = NULL;
8783   return new_unit;
8784 }
8785
8786 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8787 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8788
8789 /* Calculate the checksum of a location expression.  */
8790
8791 static inline void
8792 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8793 {
8794   int tem;
8795
8796   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8797   CHECKSUM (tem);
8798   CHECKSUM (loc->dw_loc_oprnd1);
8799   CHECKSUM (loc->dw_loc_oprnd2);
8800 }
8801
8802 /* Calculate the checksum of an attribute.  */
8803
8804 static void
8805 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8806 {
8807   dw_loc_descr_ref loc;
8808   rtx r;
8809
8810   CHECKSUM (at->dw_attr);
8811
8812   /* We don't care that this was compiled with a different compiler
8813      snapshot; if the output is the same, that's what matters.  */
8814   if (at->dw_attr == DW_AT_producer)
8815     return;
8816
8817   switch (AT_class (at))
8818     {
8819     case dw_val_class_const:
8820       CHECKSUM (at->dw_attr_val.v.val_int);
8821       break;
8822     case dw_val_class_unsigned_const:
8823       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8824       break;
8825     case dw_val_class_const_double:
8826       CHECKSUM (at->dw_attr_val.v.val_double);
8827       break;
8828     case dw_val_class_vec:
8829       CHECKSUM (at->dw_attr_val.v.val_vec);
8830       break;
8831     case dw_val_class_flag:
8832       CHECKSUM (at->dw_attr_val.v.val_flag);
8833       break;
8834     case dw_val_class_str:
8835       CHECKSUM_STRING (AT_string (at));
8836       break;
8837
8838     case dw_val_class_addr:
8839       r = AT_addr (at);
8840       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8841       CHECKSUM_STRING (XSTR (r, 0));
8842       break;
8843
8844     case dw_val_class_offset:
8845       CHECKSUM (at->dw_attr_val.v.val_offset);
8846       break;
8847
8848     case dw_val_class_loc:
8849       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8850         loc_checksum (loc, ctx);
8851       break;
8852
8853     case dw_val_class_die_ref:
8854       die_checksum (AT_ref (at), ctx, mark);
8855       break;
8856
8857     case dw_val_class_fde_ref:
8858     case dw_val_class_vms_delta:
8859     case dw_val_class_lbl_id:
8860     case dw_val_class_lineptr:
8861     case dw_val_class_macptr:
8862       break;
8863
8864     case dw_val_class_file:
8865       CHECKSUM_STRING (AT_file (at)->filename);
8866       break;
8867
8868     case dw_val_class_data8:
8869       CHECKSUM (at->dw_attr_val.v.val_data8);
8870       break;
8871
8872     default:
8873       break;
8874     }
8875 }
8876
8877 /* Calculate the checksum of a DIE.  */
8878
8879 static void
8880 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8881 {
8882   dw_die_ref c;
8883   dw_attr_ref a;
8884   unsigned ix;
8885
8886   /* To avoid infinite recursion.  */
8887   if (die->die_mark)
8888     {
8889       CHECKSUM (die->die_mark);
8890       return;
8891     }
8892   die->die_mark = ++(*mark);
8893
8894   CHECKSUM (die->die_tag);
8895
8896   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8897     attr_checksum (a, ctx, mark);
8898
8899   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8900 }
8901
8902 #undef CHECKSUM
8903 #undef CHECKSUM_STRING
8904
8905 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8906 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8907 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8908 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8909 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8910 #define CHECKSUM_ATTR(FOO) \
8911   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8912
8913 /* Calculate the checksum of a number in signed LEB128 format.  */
8914
8915 static void
8916 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8917 {
8918   unsigned char byte;
8919   bool more;
8920
8921   while (1)
8922     {
8923       byte = (value & 0x7f);
8924       value >>= 7;
8925       more = !((value == 0 && (byte & 0x40) == 0)
8926                 || (value == -1 && (byte & 0x40) != 0));
8927       if (more)
8928         byte |= 0x80;
8929       CHECKSUM (byte);
8930       if (!more)
8931         break;
8932     }
8933 }
8934
8935 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8936
8937 static void
8938 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8939 {
8940   while (1)
8941     {
8942       unsigned char byte = (value & 0x7f);
8943       value >>= 7;
8944       if (value != 0)
8945         /* More bytes to follow.  */
8946         byte |= 0x80;
8947       CHECKSUM (byte);
8948       if (value == 0)
8949         break;
8950     }
8951 }
8952
8953 /* Checksum the context of the DIE.  This adds the names of any
8954    surrounding namespaces or structures to the checksum.  */
8955
8956 static void
8957 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8958 {
8959   const char *name;
8960   dw_die_ref spec;
8961   int tag = die->die_tag;
8962
8963   if (tag != DW_TAG_namespace
8964       && tag != DW_TAG_structure_type
8965       && tag != DW_TAG_class_type)
8966     return;
8967
8968   name = get_AT_string (die, DW_AT_name);
8969
8970   spec = get_AT_ref (die, DW_AT_specification);
8971   if (spec != NULL)
8972     die = spec;
8973
8974   if (die->die_parent != NULL)
8975     checksum_die_context (die->die_parent, ctx);
8976
8977   CHECKSUM_ULEB128 ('C');
8978   CHECKSUM_ULEB128 (tag);
8979   if (name != NULL)
8980     CHECKSUM_STRING (name);
8981 }
8982
8983 /* Calculate the checksum of a location expression.  */
8984
8985 static inline void
8986 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8987 {
8988   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8989      were emitted as a DW_FORM_sdata instead of a location expression.  */
8990   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8991     {
8992       CHECKSUM_ULEB128 (DW_FORM_sdata);
8993       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8994       return;
8995     }
8996
8997   /* Otherwise, just checksum the raw location expression.  */
8998   while (loc != NULL)
8999     {
9000       CHECKSUM_ULEB128 (loc->dw_loc_opc);
9001       CHECKSUM (loc->dw_loc_oprnd1);
9002       CHECKSUM (loc->dw_loc_oprnd2);
9003       loc = loc->dw_loc_next;
9004     }
9005 }
9006
9007 /* Calculate the checksum of an attribute.  */
9008
9009 static void
9010 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
9011                        struct md5_ctx *ctx, int *mark)
9012 {
9013   dw_loc_descr_ref loc;
9014   rtx r;
9015
9016   if (AT_class (at) == dw_val_class_die_ref)
9017     {
9018       dw_die_ref target_die = AT_ref (at);
9019
9020       /* For pointer and reference types, we checksum only the (qualified)
9021          name of the target type (if there is a name).  For friend entries,
9022          we checksum only the (qualified) name of the target type or function.
9023          This allows the checksum to remain the same whether the target type
9024          is complete or not.  */
9025       if ((at->dw_attr == DW_AT_type
9026            && (tag == DW_TAG_pointer_type
9027                || tag == DW_TAG_reference_type
9028                || tag == DW_TAG_rvalue_reference_type
9029                || tag == DW_TAG_ptr_to_member_type))
9030           || (at->dw_attr == DW_AT_friend
9031               && tag == DW_TAG_friend))
9032         {
9033           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9034
9035           if (name_attr != NULL)
9036             {
9037               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9038
9039               if (decl == NULL)
9040                 decl = target_die;
9041               CHECKSUM_ULEB128 ('N');
9042               CHECKSUM_ULEB128 (at->dw_attr);
9043               if (decl->die_parent != NULL)
9044                 checksum_die_context (decl->die_parent, ctx);
9045               CHECKSUM_ULEB128 ('E');
9046               CHECKSUM_STRING (AT_string (name_attr));
9047               return;
9048             }
9049         }
9050
9051       /* For all other references to another DIE, we check to see if the
9052          target DIE has already been visited.  If it has, we emit a
9053          backward reference; if not, we descend recursively.  */
9054       if (target_die->die_mark > 0)
9055         {
9056           CHECKSUM_ULEB128 ('R');
9057           CHECKSUM_ULEB128 (at->dw_attr);
9058           CHECKSUM_ULEB128 (target_die->die_mark);
9059         }
9060       else
9061         {
9062           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9063
9064           if (decl == NULL)
9065             decl = target_die;
9066           target_die->die_mark = ++(*mark);
9067           CHECKSUM_ULEB128 ('T');
9068           CHECKSUM_ULEB128 (at->dw_attr);
9069           if (decl->die_parent != NULL)
9070             checksum_die_context (decl->die_parent, ctx);
9071           die_checksum_ordered (target_die, ctx, mark);
9072         }
9073       return;
9074     }
9075
9076   CHECKSUM_ULEB128 ('A');
9077   CHECKSUM_ULEB128 (at->dw_attr);
9078
9079   switch (AT_class (at))
9080     {
9081     case dw_val_class_const:
9082       CHECKSUM_ULEB128 (DW_FORM_sdata);
9083       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9084       break;
9085
9086     case dw_val_class_unsigned_const:
9087       CHECKSUM_ULEB128 (DW_FORM_sdata);
9088       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9089       break;
9090
9091     case dw_val_class_const_double:
9092       CHECKSUM_ULEB128 (DW_FORM_block);
9093       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9094       CHECKSUM (at->dw_attr_val.v.val_double);
9095       break;
9096
9097     case dw_val_class_vec:
9098       CHECKSUM_ULEB128 (DW_FORM_block);
9099       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9100       CHECKSUM (at->dw_attr_val.v.val_vec);
9101       break;
9102
9103     case dw_val_class_flag:
9104       CHECKSUM_ULEB128 (DW_FORM_flag);
9105       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9106       break;
9107
9108     case dw_val_class_str:
9109       CHECKSUM_ULEB128 (DW_FORM_string);
9110       CHECKSUM_STRING (AT_string (at));
9111       break;
9112
9113     case dw_val_class_addr:
9114       r = AT_addr (at);
9115       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9116       CHECKSUM_ULEB128 (DW_FORM_string);
9117       CHECKSUM_STRING (XSTR (r, 0));
9118       break;
9119
9120     case dw_val_class_offset:
9121       CHECKSUM_ULEB128 (DW_FORM_sdata);
9122       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9123       break;
9124
9125     case dw_val_class_loc:
9126       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9127         loc_checksum_ordered (loc, ctx);
9128       break;
9129
9130     case dw_val_class_fde_ref:
9131     case dw_val_class_lbl_id:
9132     case dw_val_class_lineptr:
9133     case dw_val_class_macptr:
9134       break;
9135
9136     case dw_val_class_file:
9137       CHECKSUM_ULEB128 (DW_FORM_string);
9138       CHECKSUM_STRING (AT_file (at)->filename);
9139       break;
9140
9141     case dw_val_class_data8:
9142       CHECKSUM (at->dw_attr_val.v.val_data8);
9143       break;
9144
9145     default:
9146       break;
9147     }
9148 }
9149
9150 struct checksum_attributes
9151 {
9152   dw_attr_ref at_name;
9153   dw_attr_ref at_type;
9154   dw_attr_ref at_friend;
9155   dw_attr_ref at_accessibility;
9156   dw_attr_ref at_address_class;
9157   dw_attr_ref at_allocated;
9158   dw_attr_ref at_artificial;
9159   dw_attr_ref at_associated;
9160   dw_attr_ref at_binary_scale;
9161   dw_attr_ref at_bit_offset;
9162   dw_attr_ref at_bit_size;
9163   dw_attr_ref at_bit_stride;
9164   dw_attr_ref at_byte_size;
9165   dw_attr_ref at_byte_stride;
9166   dw_attr_ref at_const_value;
9167   dw_attr_ref at_containing_type;
9168   dw_attr_ref at_count;
9169   dw_attr_ref at_data_location;
9170   dw_attr_ref at_data_member_location;
9171   dw_attr_ref at_decimal_scale;
9172   dw_attr_ref at_decimal_sign;
9173   dw_attr_ref at_default_value;
9174   dw_attr_ref at_digit_count;
9175   dw_attr_ref at_discr;
9176   dw_attr_ref at_discr_list;
9177   dw_attr_ref at_discr_value;
9178   dw_attr_ref at_encoding;
9179   dw_attr_ref at_endianity;
9180   dw_attr_ref at_explicit;
9181   dw_attr_ref at_is_optional;
9182   dw_attr_ref at_location;
9183   dw_attr_ref at_lower_bound;
9184   dw_attr_ref at_mutable;
9185   dw_attr_ref at_ordering;
9186   dw_attr_ref at_picture_string;
9187   dw_attr_ref at_prototyped;
9188   dw_attr_ref at_small;
9189   dw_attr_ref at_segment;
9190   dw_attr_ref at_string_length;
9191   dw_attr_ref at_threads_scaled;
9192   dw_attr_ref at_upper_bound;
9193   dw_attr_ref at_use_location;
9194   dw_attr_ref at_use_UTF8;
9195   dw_attr_ref at_variable_parameter;
9196   dw_attr_ref at_virtuality;
9197   dw_attr_ref at_visibility;
9198   dw_attr_ref at_vtable_elem_location;
9199 };
9200
9201 /* Collect the attributes that we will want to use for the checksum.  */
9202
9203 static void
9204 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9205 {
9206   dw_attr_ref a;
9207   unsigned ix;
9208
9209   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9210     {
9211       switch (a->dw_attr)
9212         {
9213         case DW_AT_name:
9214           attrs->at_name = a;
9215           break;
9216         case DW_AT_type:
9217           attrs->at_type = a;
9218           break;
9219         case DW_AT_friend:
9220           attrs->at_friend = a;
9221           break;
9222         case DW_AT_accessibility:
9223           attrs->at_accessibility = a;
9224           break;
9225         case DW_AT_address_class:
9226           attrs->at_address_class = a;
9227           break;
9228         case DW_AT_allocated:
9229           attrs->at_allocated = a;
9230           break;
9231         case DW_AT_artificial:
9232           attrs->at_artificial = a;
9233           break;
9234         case DW_AT_associated:
9235           attrs->at_associated = a;
9236           break;
9237         case DW_AT_binary_scale:
9238           attrs->at_binary_scale = a;
9239           break;
9240         case DW_AT_bit_offset:
9241           attrs->at_bit_offset = a;
9242           break;
9243         case DW_AT_bit_size:
9244           attrs->at_bit_size = a;
9245           break;
9246         case DW_AT_bit_stride:
9247           attrs->at_bit_stride = a;
9248           break;
9249         case DW_AT_byte_size:
9250           attrs->at_byte_size = a;
9251           break;
9252         case DW_AT_byte_stride:
9253           attrs->at_byte_stride = a;
9254           break;
9255         case DW_AT_const_value:
9256           attrs->at_const_value = a;
9257           break;
9258         case DW_AT_containing_type:
9259           attrs->at_containing_type = a;
9260           break;
9261         case DW_AT_count:
9262           attrs->at_count = a;
9263           break;
9264         case DW_AT_data_location:
9265           attrs->at_data_location = a;
9266           break;
9267         case DW_AT_data_member_location:
9268           attrs->at_data_member_location = a;
9269           break;
9270         case DW_AT_decimal_scale:
9271           attrs->at_decimal_scale = a;
9272           break;
9273         case DW_AT_decimal_sign:
9274           attrs->at_decimal_sign = a;
9275           break;
9276         case DW_AT_default_value:
9277           attrs->at_default_value = a;
9278           break;
9279         case DW_AT_digit_count:
9280           attrs->at_digit_count = a;
9281           break;
9282         case DW_AT_discr:
9283           attrs->at_discr = a;
9284           break;
9285         case DW_AT_discr_list:
9286           attrs->at_discr_list = a;
9287           break;
9288         case DW_AT_discr_value:
9289           attrs->at_discr_value = a;
9290           break;
9291         case DW_AT_encoding:
9292           attrs->at_encoding = a;
9293           break;
9294         case DW_AT_endianity:
9295           attrs->at_endianity = a;
9296           break;
9297         case DW_AT_explicit:
9298           attrs->at_explicit = a;
9299           break;
9300         case DW_AT_is_optional:
9301           attrs->at_is_optional = a;
9302           break;
9303         case DW_AT_location:
9304           attrs->at_location = a;
9305           break;
9306         case DW_AT_lower_bound:
9307           attrs->at_lower_bound = a;
9308           break;
9309         case DW_AT_mutable:
9310           attrs->at_mutable = a;
9311           break;
9312         case DW_AT_ordering:
9313           attrs->at_ordering = a;
9314           break;
9315         case DW_AT_picture_string:
9316           attrs->at_picture_string = a;
9317           break;
9318         case DW_AT_prototyped:
9319           attrs->at_prototyped = a;
9320           break;
9321         case DW_AT_small:
9322           attrs->at_small = a;
9323           break;
9324         case DW_AT_segment:
9325           attrs->at_segment = a;
9326           break;
9327         case DW_AT_string_length:
9328           attrs->at_string_length = a;
9329           break;
9330         case DW_AT_threads_scaled:
9331           attrs->at_threads_scaled = a;
9332           break;
9333         case DW_AT_upper_bound:
9334           attrs->at_upper_bound = a;
9335           break;
9336         case DW_AT_use_location:
9337           attrs->at_use_location = a;
9338           break;
9339         case DW_AT_use_UTF8:
9340           attrs->at_use_UTF8 = a;
9341           break;
9342         case DW_AT_variable_parameter:
9343           attrs->at_variable_parameter = a;
9344           break;
9345         case DW_AT_virtuality:
9346           attrs->at_virtuality = a;
9347           break;
9348         case DW_AT_visibility:
9349           attrs->at_visibility = a;
9350           break;
9351         case DW_AT_vtable_elem_location:
9352           attrs->at_vtable_elem_location = a;
9353           break;
9354         default:
9355           break;
9356         }
9357     }
9358 }
9359
9360 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9361
9362 static void
9363 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9364 {
9365   dw_die_ref c;
9366   dw_die_ref decl;
9367   struct checksum_attributes attrs;
9368
9369   CHECKSUM_ULEB128 ('D');
9370   CHECKSUM_ULEB128 (die->die_tag);
9371
9372   memset (&attrs, 0, sizeof (attrs));
9373
9374   decl = get_AT_ref (die, DW_AT_specification);
9375   if (decl != NULL)
9376     collect_checksum_attributes (&attrs, decl);
9377   collect_checksum_attributes (&attrs, die);
9378
9379   CHECKSUM_ATTR (attrs.at_name);
9380   CHECKSUM_ATTR (attrs.at_accessibility);
9381   CHECKSUM_ATTR (attrs.at_address_class);
9382   CHECKSUM_ATTR (attrs.at_allocated);
9383   CHECKSUM_ATTR (attrs.at_artificial);
9384   CHECKSUM_ATTR (attrs.at_associated);
9385   CHECKSUM_ATTR (attrs.at_binary_scale);
9386   CHECKSUM_ATTR (attrs.at_bit_offset);
9387   CHECKSUM_ATTR (attrs.at_bit_size);
9388   CHECKSUM_ATTR (attrs.at_bit_stride);
9389   CHECKSUM_ATTR (attrs.at_byte_size);
9390   CHECKSUM_ATTR (attrs.at_byte_stride);
9391   CHECKSUM_ATTR (attrs.at_const_value);
9392   CHECKSUM_ATTR (attrs.at_containing_type);
9393   CHECKSUM_ATTR (attrs.at_count);
9394   CHECKSUM_ATTR (attrs.at_data_location);
9395   CHECKSUM_ATTR (attrs.at_data_member_location);
9396   CHECKSUM_ATTR (attrs.at_decimal_scale);
9397   CHECKSUM_ATTR (attrs.at_decimal_sign);
9398   CHECKSUM_ATTR (attrs.at_default_value);
9399   CHECKSUM_ATTR (attrs.at_digit_count);
9400   CHECKSUM_ATTR (attrs.at_discr);
9401   CHECKSUM_ATTR (attrs.at_discr_list);
9402   CHECKSUM_ATTR (attrs.at_discr_value);
9403   CHECKSUM_ATTR (attrs.at_encoding);
9404   CHECKSUM_ATTR (attrs.at_endianity);
9405   CHECKSUM_ATTR (attrs.at_explicit);
9406   CHECKSUM_ATTR (attrs.at_is_optional);
9407   CHECKSUM_ATTR (attrs.at_location);
9408   CHECKSUM_ATTR (attrs.at_lower_bound);
9409   CHECKSUM_ATTR (attrs.at_mutable);
9410   CHECKSUM_ATTR (attrs.at_ordering);
9411   CHECKSUM_ATTR (attrs.at_picture_string);
9412   CHECKSUM_ATTR (attrs.at_prototyped);
9413   CHECKSUM_ATTR (attrs.at_small);
9414   CHECKSUM_ATTR (attrs.at_segment);
9415   CHECKSUM_ATTR (attrs.at_string_length);
9416   CHECKSUM_ATTR (attrs.at_threads_scaled);
9417   CHECKSUM_ATTR (attrs.at_upper_bound);
9418   CHECKSUM_ATTR (attrs.at_use_location);
9419   CHECKSUM_ATTR (attrs.at_use_UTF8);
9420   CHECKSUM_ATTR (attrs.at_variable_parameter);
9421   CHECKSUM_ATTR (attrs.at_virtuality);
9422   CHECKSUM_ATTR (attrs.at_visibility);
9423   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9424   CHECKSUM_ATTR (attrs.at_type);
9425   CHECKSUM_ATTR (attrs.at_friend);
9426
9427   /* Checksum the child DIEs, except for nested types and member functions.  */
9428   c = die->die_child;
9429   if (c) do {
9430     dw_attr_ref name_attr;
9431
9432     c = c->die_sib;
9433     name_attr = get_AT (c, DW_AT_name);
9434     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9435         && name_attr != NULL)
9436       {
9437         CHECKSUM_ULEB128 ('S');
9438         CHECKSUM_ULEB128 (c->die_tag);
9439         CHECKSUM_STRING (AT_string (name_attr));
9440       }
9441     else
9442       {
9443         /* Mark this DIE so it gets processed when unmarking.  */
9444         if (c->die_mark == 0)
9445           c->die_mark = -1;
9446         die_checksum_ordered (c, ctx, mark);
9447       }
9448   } while (c != die->die_child);
9449
9450   CHECKSUM_ULEB128 (0);
9451 }
9452
9453 #undef CHECKSUM
9454 #undef CHECKSUM_STRING
9455 #undef CHECKSUM_ATTR
9456 #undef CHECKSUM_LEB128
9457 #undef CHECKSUM_ULEB128
9458
9459 /* Generate the type signature for DIE.  This is computed by generating an
9460    MD5 checksum over the DIE's tag, its relevant attributes, and its
9461    children.  Attributes that are references to other DIEs are processed
9462    by recursion, using the MARK field to prevent infinite recursion.
9463    If the DIE is nested inside a namespace or another type, we also
9464    need to include that context in the signature.  The lower 64 bits
9465    of the resulting MD5 checksum comprise the signature.  */
9466
9467 static void
9468 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9469 {
9470   int mark;
9471   const char *name;
9472   unsigned char checksum[16];
9473   struct md5_ctx ctx;
9474   dw_die_ref decl;
9475
9476   name = get_AT_string (die, DW_AT_name);
9477   decl = get_AT_ref (die, DW_AT_specification);
9478
9479   /* First, compute a signature for just the type name (and its surrounding
9480      context, if any.  This is stored in the type unit DIE for link-time
9481      ODR (one-definition rule) checking.  */
9482
9483   if (is_cxx() && name != NULL)
9484     {
9485       md5_init_ctx (&ctx);
9486
9487       /* Checksum the names of surrounding namespaces and structures.  */
9488       if (decl != NULL && decl->die_parent != NULL)
9489         checksum_die_context (decl->die_parent, &ctx);
9490
9491       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9492       md5_process_bytes (name, strlen (name) + 1, &ctx);
9493       md5_finish_ctx (&ctx, checksum);
9494
9495       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9496     }
9497
9498   /* Next, compute the complete type signature.  */
9499
9500   md5_init_ctx (&ctx);
9501   mark = 1;
9502   die->die_mark = mark;
9503
9504   /* Checksum the names of surrounding namespaces and structures.  */
9505   if (decl != NULL && decl->die_parent != NULL)
9506     checksum_die_context (decl->die_parent, &ctx);
9507
9508   /* Checksum the DIE and its children.  */
9509   die_checksum_ordered (die, &ctx, &mark);
9510   unmark_all_dies (die);
9511   md5_finish_ctx (&ctx, checksum);
9512
9513   /* Store the signature in the type node and link the type DIE and the
9514      type node together.  */
9515   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9516           DWARF_TYPE_SIGNATURE_SIZE);
9517   die->die_id.die_type_node = type_node;
9518   type_node->type_die = die;
9519
9520   /* If the DIE is a specification, link its declaration to the type node
9521      as well.  */
9522   if (decl != NULL)
9523     decl->die_id.die_type_node = type_node;
9524 }
9525
9526 /* Do the location expressions look same?  */
9527 static inline int
9528 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9529 {
9530   return loc1->dw_loc_opc == loc2->dw_loc_opc
9531          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9532          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9533 }
9534
9535 /* Do the values look the same?  */
9536 static int
9537 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9538 {
9539   dw_loc_descr_ref loc1, loc2;
9540   rtx r1, r2;
9541
9542   if (v1->val_class != v2->val_class)
9543     return 0;
9544
9545   switch (v1->val_class)
9546     {
9547     case dw_val_class_const:
9548       return v1->v.val_int == v2->v.val_int;
9549     case dw_val_class_unsigned_const:
9550       return v1->v.val_unsigned == v2->v.val_unsigned;
9551     case dw_val_class_const_double:
9552       return v1->v.val_double.high == v2->v.val_double.high
9553              && v1->v.val_double.low == v2->v.val_double.low;
9554     case dw_val_class_vec:
9555       if (v1->v.val_vec.length != v2->v.val_vec.length
9556           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9557         return 0;
9558       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9559                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9560         return 0;
9561       return 1;
9562     case dw_val_class_flag:
9563       return v1->v.val_flag == v2->v.val_flag;
9564     case dw_val_class_str:
9565       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9566
9567     case dw_val_class_addr:
9568       r1 = v1->v.val_addr;
9569       r2 = v2->v.val_addr;
9570       if (GET_CODE (r1) != GET_CODE (r2))
9571         return 0;
9572       return !rtx_equal_p (r1, r2);
9573
9574     case dw_val_class_offset:
9575       return v1->v.val_offset == v2->v.val_offset;
9576
9577     case dw_val_class_loc:
9578       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9579            loc1 && loc2;
9580            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9581         if (!same_loc_p (loc1, loc2, mark))
9582           return 0;
9583       return !loc1 && !loc2;
9584
9585     case dw_val_class_die_ref:
9586       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9587
9588     case dw_val_class_fde_ref:
9589     case dw_val_class_vms_delta:
9590     case dw_val_class_lbl_id:
9591     case dw_val_class_lineptr:
9592     case dw_val_class_macptr:
9593       return 1;
9594
9595     case dw_val_class_file:
9596       return v1->v.val_file == v2->v.val_file;
9597
9598     case dw_val_class_data8:
9599       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9600
9601     default:
9602       return 1;
9603     }
9604 }
9605
9606 /* Do the attributes look the same?  */
9607
9608 static int
9609 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9610 {
9611   if (at1->dw_attr != at2->dw_attr)
9612     return 0;
9613
9614   /* We don't care that this was compiled with a different compiler
9615      snapshot; if the output is the same, that's what matters. */
9616   if (at1->dw_attr == DW_AT_producer)
9617     return 1;
9618
9619   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9620 }
9621
9622 /* Do the dies look the same?  */
9623
9624 static int
9625 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9626 {
9627   dw_die_ref c1, c2;
9628   dw_attr_ref a1;
9629   unsigned ix;
9630
9631   /* To avoid infinite recursion.  */
9632   if (die1->die_mark)
9633     return die1->die_mark == die2->die_mark;
9634   die1->die_mark = die2->die_mark = ++(*mark);
9635
9636   if (die1->die_tag != die2->die_tag)
9637     return 0;
9638
9639   if (VEC_length (dw_attr_node, die1->die_attr)
9640       != VEC_length (dw_attr_node, die2->die_attr))
9641     return 0;
9642
9643   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9644     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9645       return 0;
9646
9647   c1 = die1->die_child;
9648   c2 = die2->die_child;
9649   if (! c1)
9650     {
9651       if (c2)
9652         return 0;
9653     }
9654   else
9655     for (;;)
9656       {
9657         if (!same_die_p (c1, c2, mark))
9658           return 0;
9659         c1 = c1->die_sib;
9660         c2 = c2->die_sib;
9661         if (c1 == die1->die_child)
9662           {
9663             if (c2 == die2->die_child)
9664               break;
9665             else
9666               return 0;
9667           }
9668     }
9669
9670   return 1;
9671 }
9672
9673 /* Do the dies look the same?  Wrapper around same_die_p.  */
9674
9675 static int
9676 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9677 {
9678   int mark = 0;
9679   int ret = same_die_p (die1, die2, &mark);
9680
9681   unmark_all_dies (die1);
9682   unmark_all_dies (die2);
9683
9684   return ret;
9685 }
9686
9687 /* The prefix to attach to symbols on DIEs in the current comdat debug
9688    info section.  */
9689 static char *comdat_symbol_id;
9690
9691 /* The index of the current symbol within the current comdat CU.  */
9692 static unsigned int comdat_symbol_number;
9693
9694 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9695    children, and set comdat_symbol_id accordingly.  */
9696
9697 static void
9698 compute_section_prefix (dw_die_ref unit_die)
9699 {
9700   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9701   const char *base = die_name ? lbasename (die_name) : "anonymous";
9702   char *name = XALLOCAVEC (char, strlen (base) + 64);
9703   char *p;
9704   int i, mark;
9705   unsigned char checksum[16];
9706   struct md5_ctx ctx;
9707
9708   /* Compute the checksum of the DIE, then append part of it as hex digits to
9709      the name filename of the unit.  */
9710
9711   md5_init_ctx (&ctx);
9712   mark = 0;
9713   die_checksum (unit_die, &ctx, &mark);
9714   unmark_all_dies (unit_die);
9715   md5_finish_ctx (&ctx, checksum);
9716
9717   sprintf (name, "%s.", base);
9718   clean_symbol_name (name);
9719
9720   p = name + strlen (name);
9721   for (i = 0; i < 4; i++)
9722     {
9723       sprintf (p, "%.2x", checksum[i]);
9724       p += 2;
9725     }
9726
9727   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9728   comdat_symbol_number = 0;
9729 }
9730
9731 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9732
9733 static int
9734 is_type_die (dw_die_ref die)
9735 {
9736   switch (die->die_tag)
9737     {
9738     case DW_TAG_array_type:
9739     case DW_TAG_class_type:
9740     case DW_TAG_interface_type:
9741     case DW_TAG_enumeration_type:
9742     case DW_TAG_pointer_type:
9743     case DW_TAG_reference_type:
9744     case DW_TAG_rvalue_reference_type:
9745     case DW_TAG_string_type:
9746     case DW_TAG_structure_type:
9747     case DW_TAG_subroutine_type:
9748     case DW_TAG_union_type:
9749     case DW_TAG_ptr_to_member_type:
9750     case DW_TAG_set_type:
9751     case DW_TAG_subrange_type:
9752     case DW_TAG_base_type:
9753     case DW_TAG_const_type:
9754     case DW_TAG_file_type:
9755     case DW_TAG_packed_type:
9756     case DW_TAG_volatile_type:
9757     case DW_TAG_typedef:
9758       return 1;
9759     default:
9760       return 0;
9761     }
9762 }
9763
9764 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9765    Basically, we want to choose the bits that are likely to be shared between
9766    compilations (types) and leave out the bits that are specific to individual
9767    compilations (functions).  */
9768
9769 static int
9770 is_comdat_die (dw_die_ref c)
9771 {
9772   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9773      we do for stabs.  The advantage is a greater likelihood of sharing between
9774      objects that don't include headers in the same order (and therefore would
9775      put the base types in a different comdat).  jason 8/28/00 */
9776
9777   if (c->die_tag == DW_TAG_base_type)
9778     return 0;
9779
9780   if (c->die_tag == DW_TAG_pointer_type
9781       || c->die_tag == DW_TAG_reference_type
9782       || c->die_tag == DW_TAG_rvalue_reference_type
9783       || c->die_tag == DW_TAG_const_type
9784       || c->die_tag == DW_TAG_volatile_type)
9785     {
9786       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9787
9788       return t ? is_comdat_die (t) : 0;
9789     }
9790
9791   return is_type_die (c);
9792 }
9793
9794 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9795    compilation unit.  */
9796
9797 static int
9798 is_symbol_die (dw_die_ref c)
9799 {
9800   return (is_type_die (c)
9801           || is_declaration_die (c)
9802           || c->die_tag == DW_TAG_namespace
9803           || c->die_tag == DW_TAG_module);
9804 }
9805
9806 /* Returns true iff C is a compile-unit DIE.  */
9807
9808 static inline bool
9809 is_cu_die (dw_die_ref c)
9810 {
9811   return c && c->die_tag == DW_TAG_compile_unit;
9812 }
9813
9814 static char *
9815 gen_internal_sym (const char *prefix)
9816 {
9817   char buf[256];
9818
9819   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9820   return xstrdup (buf);
9821 }
9822
9823 /* Assign symbols to all worthy DIEs under DIE.  */
9824
9825 static void
9826 assign_symbol_names (dw_die_ref die)
9827 {
9828   dw_die_ref c;
9829
9830   if (is_symbol_die (die))
9831     {
9832       if (comdat_symbol_id)
9833         {
9834           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9835
9836           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9837                    comdat_symbol_id, comdat_symbol_number++);
9838           die->die_id.die_symbol = xstrdup (p);
9839         }
9840       else
9841         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9842     }
9843
9844   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9845 }
9846
9847 struct cu_hash_table_entry
9848 {
9849   dw_die_ref cu;
9850   unsigned min_comdat_num, max_comdat_num;
9851   struct cu_hash_table_entry *next;
9852 };
9853
9854 /* Routines to manipulate hash table of CUs.  */
9855 static hashval_t
9856 htab_cu_hash (const void *of)
9857 {
9858   const struct cu_hash_table_entry *const entry =
9859     (const struct cu_hash_table_entry *) of;
9860
9861   return htab_hash_string (entry->cu->die_id.die_symbol);
9862 }
9863
9864 static int
9865 htab_cu_eq (const void *of1, const void *of2)
9866 {
9867   const struct cu_hash_table_entry *const entry1 =
9868     (const struct cu_hash_table_entry *) of1;
9869   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9870
9871   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9872 }
9873
9874 static void
9875 htab_cu_del (void *what)
9876 {
9877   struct cu_hash_table_entry *next,
9878     *entry = (struct cu_hash_table_entry *) what;
9879
9880   while (entry)
9881     {
9882       next = entry->next;
9883       free (entry);
9884       entry = next;
9885     }
9886 }
9887
9888 /* Check whether we have already seen this CU and set up SYM_NUM
9889    accordingly.  */
9890 static int
9891 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9892 {
9893   struct cu_hash_table_entry dummy;
9894   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9895
9896   dummy.max_comdat_num = 0;
9897
9898   slot = (struct cu_hash_table_entry **)
9899     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9900         INSERT);
9901   entry = *slot;
9902
9903   for (; entry; last = entry, entry = entry->next)
9904     {
9905       if (same_die_p_wrap (cu, entry->cu))
9906         break;
9907     }
9908
9909   if (entry)
9910     {
9911       *sym_num = entry->min_comdat_num;
9912       return 1;
9913     }
9914
9915   entry = XCNEW (struct cu_hash_table_entry);
9916   entry->cu = cu;
9917   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9918   entry->next = *slot;
9919   *slot = entry;
9920
9921   return 0;
9922 }
9923
9924 /* Record SYM_NUM to record of CU in HTABLE.  */
9925 static void
9926 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9927 {
9928   struct cu_hash_table_entry **slot, *entry;
9929
9930   slot = (struct cu_hash_table_entry **)
9931     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9932         NO_INSERT);
9933   entry = *slot;
9934
9935   entry->max_comdat_num = sym_num;
9936 }
9937
9938 /* Traverse the DIE (which is always comp_unit_die), and set up
9939    additional compilation units for each of the include files we see
9940    bracketed by BINCL/EINCL.  */
9941
9942 static void
9943 break_out_includes (dw_die_ref die)
9944 {
9945   dw_die_ref c;
9946   dw_die_ref unit = NULL;
9947   limbo_die_node *node, **pnode;
9948   htab_t cu_hash_table;
9949
9950   c = die->die_child;
9951   if (c) do {
9952     dw_die_ref prev = c;
9953     c = c->die_sib;
9954     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9955            || (unit && is_comdat_die (c)))
9956       {
9957         dw_die_ref next = c->die_sib;
9958
9959         /* This DIE is for a secondary CU; remove it from the main one.  */
9960         remove_child_with_prev (c, prev);
9961
9962         if (c->die_tag == DW_TAG_GNU_BINCL)
9963           unit = push_new_compile_unit (unit, c);
9964         else if (c->die_tag == DW_TAG_GNU_EINCL)
9965           unit = pop_compile_unit (unit);
9966         else
9967           add_child_die (unit, c);
9968         c = next;
9969         if (c == die->die_child)
9970           break;
9971       }
9972   } while (c != die->die_child);
9973
9974 #if 0
9975   /* We can only use this in debugging, since the frontend doesn't check
9976      to make sure that we leave every include file we enter.  */
9977   gcc_assert (!unit);
9978 #endif
9979
9980   assign_symbol_names (die);
9981   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9982   for (node = limbo_die_list, pnode = &limbo_die_list;
9983        node;
9984        node = node->next)
9985     {
9986       int is_dupl;
9987
9988       compute_section_prefix (node->die);
9989       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9990                         &comdat_symbol_number);
9991       assign_symbol_names (node->die);
9992       if (is_dupl)
9993         *pnode = node->next;
9994       else
9995         {
9996           pnode = &node->next;
9997           record_comdat_symbol_number (node->die, cu_hash_table,
9998                 comdat_symbol_number);
9999         }
10000     }
10001   htab_delete (cu_hash_table);
10002 }
10003
10004 /* Return non-zero if this DIE is a declaration.  */
10005
10006 static int
10007 is_declaration_die (dw_die_ref die)
10008 {
10009   dw_attr_ref a;
10010   unsigned ix;
10011
10012   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10013     if (a->dw_attr == DW_AT_declaration)
10014       return 1;
10015
10016   return 0;
10017 }
10018
10019 /* Return non-zero if this DIE is nested inside a subprogram.  */
10020
10021 static int
10022 is_nested_in_subprogram (dw_die_ref die)
10023 {
10024   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
10025
10026   if (decl == NULL)
10027     decl = die;
10028   return local_scope_p (decl);
10029 }
10030
10031 /* Return non-zero if this is a type DIE that should be moved to a
10032    COMDAT .debug_types section.  */
10033
10034 static int
10035 should_move_die_to_comdat (dw_die_ref die)
10036 {
10037   switch (die->die_tag)
10038     {
10039     case DW_TAG_class_type:
10040     case DW_TAG_structure_type:
10041     case DW_TAG_enumeration_type:
10042     case DW_TAG_union_type:
10043       /* Don't move declarations, inlined instances, or types nested in a
10044          subprogram.  */
10045       if (is_declaration_die (die)
10046           || get_AT (die, DW_AT_abstract_origin)
10047           || is_nested_in_subprogram (die))
10048         return 0;
10049       return 1;
10050     case DW_TAG_array_type:
10051     case DW_TAG_interface_type:
10052     case DW_TAG_pointer_type:
10053     case DW_TAG_reference_type:
10054     case DW_TAG_rvalue_reference_type:
10055     case DW_TAG_string_type:
10056     case DW_TAG_subroutine_type:
10057     case DW_TAG_ptr_to_member_type:
10058     case DW_TAG_set_type:
10059     case DW_TAG_subrange_type:
10060     case DW_TAG_base_type:
10061     case DW_TAG_const_type:
10062     case DW_TAG_file_type:
10063     case DW_TAG_packed_type:
10064     case DW_TAG_volatile_type:
10065     case DW_TAG_typedef:
10066     default:
10067       return 0;
10068     }
10069 }
10070
10071 /* Make a clone of DIE.  */
10072
10073 static dw_die_ref
10074 clone_die (dw_die_ref die)
10075 {
10076   dw_die_ref clone;
10077   dw_attr_ref a;
10078   unsigned ix;
10079
10080   clone = ggc_alloc_cleared_die_node ();
10081   clone->die_tag = die->die_tag;
10082
10083   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10084     add_dwarf_attr (clone, a);
10085
10086   return clone;
10087 }
10088
10089 /* Make a clone of the tree rooted at DIE.  */
10090
10091 static dw_die_ref
10092 clone_tree (dw_die_ref die)
10093 {
10094   dw_die_ref c;
10095   dw_die_ref clone = clone_die (die);
10096
10097   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10098
10099   return clone;
10100 }
10101
10102 /* Make a clone of DIE as a declaration.  */
10103
10104 static dw_die_ref
10105 clone_as_declaration (dw_die_ref die)
10106 {
10107   dw_die_ref clone;
10108   dw_die_ref decl;
10109   dw_attr_ref a;
10110   unsigned ix;
10111
10112   /* If the DIE is already a declaration, just clone it.  */
10113   if (is_declaration_die (die))
10114     return clone_die (die);
10115
10116   /* If the DIE is a specification, just clone its declaration DIE.  */
10117   decl = get_AT_ref (die, DW_AT_specification);
10118   if (decl != NULL)
10119     return clone_die (decl);
10120
10121   clone = ggc_alloc_cleared_die_node ();
10122   clone->die_tag = die->die_tag;
10123
10124   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10125     {
10126       /* We don't want to copy over all attributes.
10127          For example we don't want DW_AT_byte_size because otherwise we will no
10128          longer have a declaration and GDB will treat it as a definition.  */
10129
10130       switch (a->dw_attr)
10131         {
10132         case DW_AT_artificial:
10133         case DW_AT_containing_type:
10134         case DW_AT_external:
10135         case DW_AT_name:
10136         case DW_AT_type:
10137         case DW_AT_virtuality:
10138         case DW_AT_linkage_name:
10139         case DW_AT_MIPS_linkage_name:
10140           add_dwarf_attr (clone, a);
10141           break;
10142         case DW_AT_byte_size:
10143         default:
10144           break;
10145         }
10146     }
10147
10148   if (die->die_id.die_type_node)
10149     add_AT_die_ref (clone, DW_AT_signature, die);
10150
10151   add_AT_flag (clone, DW_AT_declaration, 1);
10152   return clone;
10153 }
10154
10155 /* Copy the declaration context to the new compile unit DIE.  This includes
10156    any surrounding namespace or type declarations.  If the DIE has an
10157    AT_specification attribute, it also includes attributes and children
10158    attached to the specification.  */
10159
10160 static void
10161 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10162 {
10163   dw_die_ref decl;
10164   dw_die_ref new_decl;
10165
10166   decl = get_AT_ref (die, DW_AT_specification);
10167   if (decl == NULL)
10168     decl = die;
10169   else
10170     {
10171       unsigned ix;
10172       dw_die_ref c;
10173       dw_attr_ref a;
10174
10175       /* Copy the type node pointer from the new DIE to the original
10176          declaration DIE so we can forward references later.  */
10177       decl->die_id.die_type_node = die->die_id.die_type_node;
10178
10179       remove_AT (die, DW_AT_specification);
10180
10181       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10182         {
10183           if (a->dw_attr != DW_AT_name
10184               && a->dw_attr != DW_AT_declaration
10185               && a->dw_attr != DW_AT_external)
10186             add_dwarf_attr (die, a);
10187         }
10188
10189       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10190     }
10191
10192   if (decl->die_parent != NULL
10193       && decl->die_parent->die_tag != DW_TAG_compile_unit
10194       && decl->die_parent->die_tag != DW_TAG_type_unit)
10195     {
10196       new_decl = copy_ancestor_tree (unit, decl, NULL);
10197       if (new_decl != NULL)
10198         {
10199           remove_AT (new_decl, DW_AT_signature);
10200           add_AT_specification (die, new_decl);
10201         }
10202     }
10203 }
10204
10205 /* Generate the skeleton ancestor tree for the given NODE, then clone
10206    the DIE and add the clone into the tree.  */
10207
10208 static void
10209 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10210 {
10211   if (node->new_die != NULL)
10212     return;
10213
10214   node->new_die = clone_as_declaration (node->old_die);
10215
10216   if (node->parent != NULL)
10217     {
10218       generate_skeleton_ancestor_tree (node->parent);
10219       add_child_die (node->parent->new_die, node->new_die);
10220     }
10221 }
10222
10223 /* Generate a skeleton tree of DIEs containing any declarations that are
10224    found in the original tree.  We traverse the tree looking for declaration
10225    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10226
10227 static void
10228 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10229 {
10230   skeleton_chain_node node;
10231   dw_die_ref c;
10232   dw_die_ref first;
10233   dw_die_ref prev = NULL;
10234   dw_die_ref next = NULL;
10235
10236   node.parent = parent;
10237
10238   first = c = parent->old_die->die_child;
10239   if (c)
10240     next = c->die_sib;
10241   if (c) do {
10242     if (prev == NULL || prev->die_sib == c)
10243       prev = c;
10244     c = next;
10245     next = (c == first ? NULL : c->die_sib);
10246     node.old_die = c;
10247     node.new_die = NULL;
10248     if (is_declaration_die (c))
10249       {
10250         /* Clone the existing DIE, move the original to the skeleton
10251            tree (which is in the main CU), and put the clone, with
10252            all the original's children, where the original came from.  */
10253         dw_die_ref clone = clone_die (c);
10254         move_all_children (c, clone);
10255
10256         replace_child (c, clone, prev);
10257         generate_skeleton_ancestor_tree (parent);
10258         add_child_die (parent->new_die, c);
10259         node.new_die = c;
10260         c = clone;
10261       }
10262     generate_skeleton_bottom_up (&node);
10263   } while (next != NULL);
10264 }
10265
10266 /* Wrapper function for generate_skeleton_bottom_up.  */
10267
10268 static dw_die_ref
10269 generate_skeleton (dw_die_ref die)
10270 {
10271   skeleton_chain_node node;
10272
10273   node.old_die = die;
10274   node.new_die = NULL;
10275   node.parent = NULL;
10276
10277   /* If this type definition is nested inside another type,
10278      always leave at least a declaration in its place.  */
10279   if (die->die_parent != NULL && is_type_die (die->die_parent))
10280     node.new_die = clone_as_declaration (die);
10281
10282   generate_skeleton_bottom_up (&node);
10283   return node.new_die;
10284 }
10285
10286 /* Remove the DIE from its parent, possibly replacing it with a cloned
10287    declaration.  The original DIE will be moved to a new compile unit
10288    so that existing references to it follow it to the new location.  If
10289    any of the original DIE's descendants is a declaration, we need to
10290    replace the original DIE with a skeleton tree and move the
10291    declarations back into the skeleton tree.  */
10292
10293 static dw_die_ref
10294 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10295 {
10296   dw_die_ref skeleton;
10297
10298   skeleton = generate_skeleton (child);
10299   if (skeleton == NULL)
10300     remove_child_with_prev (child, prev);
10301   else
10302     {
10303       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10304       replace_child (child, skeleton, prev);
10305     }
10306
10307   return skeleton;
10308 }
10309
10310 /* Traverse the DIE and set up additional .debug_types sections for each
10311    type worthy of being placed in a COMDAT section.  */
10312
10313 static void
10314 break_out_comdat_types (dw_die_ref die)
10315 {
10316   dw_die_ref c;
10317   dw_die_ref first;
10318   dw_die_ref prev = NULL;
10319   dw_die_ref next = NULL;
10320   dw_die_ref unit = NULL;
10321
10322   first = c = die->die_child;
10323   if (c)
10324     next = c->die_sib;
10325   if (c) do {
10326     if (prev == NULL || prev->die_sib == c)
10327       prev = c;
10328     c = next;
10329     next = (c == first ? NULL : c->die_sib);
10330     if (should_move_die_to_comdat (c))
10331       {
10332         dw_die_ref replacement;
10333         comdat_type_node_ref type_node;
10334
10335         /* Create a new type unit DIE as the root for the new tree, and
10336            add it to the list of comdat types.  */
10337         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10338         add_AT_unsigned (unit, DW_AT_language,
10339                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10340         type_node = ggc_alloc_cleared_comdat_type_node ();
10341         type_node->root_die = unit;
10342         type_node->next = comdat_type_list;
10343         comdat_type_list = type_node;
10344
10345         /* Generate the type signature.  */
10346         generate_type_signature (c, type_node);
10347
10348         /* Copy the declaration context, attributes, and children of the
10349            declaration into the new compile unit DIE.  */
10350         copy_declaration_context (unit, c);
10351
10352         /* Remove this DIE from the main CU.  */
10353         replacement = remove_child_or_replace_with_skeleton (c, prev);
10354
10355         /* Break out nested types into their own type units.  */
10356         break_out_comdat_types (c);
10357
10358         /* Add the DIE to the new compunit.  */
10359         add_child_die (unit, c);
10360
10361         if (replacement != NULL)
10362           c = replacement;
10363       }
10364     else if (c->die_tag == DW_TAG_namespace
10365              || c->die_tag == DW_TAG_class_type
10366              || c->die_tag == DW_TAG_structure_type
10367              || c->die_tag == DW_TAG_union_type)
10368       {
10369         /* Look for nested types that can be broken out.  */
10370         break_out_comdat_types (c);
10371       }
10372   } while (next != NULL);
10373 }
10374
10375 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10376
10377 struct decl_table_entry
10378 {
10379   dw_die_ref orig;
10380   dw_die_ref copy;
10381 };
10382
10383 /* Routines to manipulate hash table of copied declarations.  */
10384
10385 static hashval_t
10386 htab_decl_hash (const void *of)
10387 {
10388   const struct decl_table_entry *const entry =
10389     (const struct decl_table_entry *) of;
10390
10391   return htab_hash_pointer (entry->orig);
10392 }
10393
10394 static int
10395 htab_decl_eq (const void *of1, const void *of2)
10396 {
10397   const struct decl_table_entry *const entry1 =
10398     (const struct decl_table_entry *) of1;
10399   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10400
10401   return entry1->orig == entry2;
10402 }
10403
10404 static void
10405 htab_decl_del (void *what)
10406 {
10407   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10408
10409   free (entry);
10410 }
10411
10412 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10413    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10414    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10415    to check if the ancestor has already been copied into UNIT.  */
10416
10417 static dw_die_ref
10418 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10419 {
10420   dw_die_ref parent = die->die_parent;
10421   dw_die_ref new_parent = unit;
10422   dw_die_ref copy;
10423   void **slot = NULL;
10424   struct decl_table_entry *entry = NULL;
10425
10426   if (decl_table)
10427     {
10428       /* Check if the entry has already been copied to UNIT.  */
10429       slot = htab_find_slot_with_hash (decl_table, die,
10430                                        htab_hash_pointer (die), INSERT);
10431       if (*slot != HTAB_EMPTY_ENTRY)
10432         {
10433           entry = (struct decl_table_entry *) *slot;
10434           return entry->copy;
10435         }
10436
10437       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10438       entry = XCNEW (struct decl_table_entry);
10439       entry->orig = die;
10440       entry->copy = NULL;
10441       *slot = entry;
10442     }
10443
10444   if (parent != NULL)
10445     {
10446       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10447       if (spec != NULL)
10448         parent = spec;
10449       if (parent->die_tag != DW_TAG_compile_unit
10450           && parent->die_tag != DW_TAG_type_unit)
10451         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10452     }
10453
10454   copy = clone_as_declaration (die);
10455   add_child_die (new_parent, copy);
10456
10457   if (decl_table != NULL)
10458     {
10459       /* Record the pointer to the copy.  */
10460       entry->copy = copy;
10461     }
10462
10463   return copy;
10464 }
10465
10466 /* Walk the DIE and its children, looking for references to incomplete
10467    or trivial types that are unmarked (i.e., that are not in the current
10468    type_unit).  */
10469
10470 static void
10471 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10472 {
10473   dw_die_ref c;
10474   dw_attr_ref a;
10475   unsigned ix;
10476
10477   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10478     {
10479       if (AT_class (a) == dw_val_class_die_ref)
10480         {
10481           dw_die_ref targ = AT_ref (a);
10482           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10483           void **slot;
10484           struct decl_table_entry *entry;
10485
10486           if (targ->die_mark != 0 || type_node != NULL)
10487             continue;
10488
10489           slot = htab_find_slot_with_hash (decl_table, targ,
10490                                            htab_hash_pointer (targ), INSERT);
10491
10492           if (*slot != HTAB_EMPTY_ENTRY)
10493             {
10494               /* TARG has already been copied, so we just need to
10495                  modify the reference to point to the copy.  */
10496               entry = (struct decl_table_entry *) *slot;
10497               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10498             }
10499           else
10500             {
10501               dw_die_ref parent = unit;
10502               dw_die_ref copy = clone_tree (targ);
10503
10504               /* Make sure the cloned tree is marked as part of the
10505                  type unit.  */
10506               mark_dies (copy);
10507
10508               /* Record in DECL_TABLE that TARG has been copied.
10509                  Need to do this now, before the recursive call,
10510                  because DECL_TABLE may be expanded and SLOT
10511                  would no longer be a valid pointer.  */
10512               entry = XCNEW (struct decl_table_entry);
10513               entry->orig = targ;
10514               entry->copy = copy;
10515               *slot = entry;
10516
10517               /* If TARG has surrounding context, copy its ancestor tree
10518                  into the new type unit.  */
10519               if (targ->die_parent != NULL
10520                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10521                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10522                 parent = copy_ancestor_tree (unit, targ->die_parent,
10523                                              decl_table);
10524
10525               add_child_die (parent, copy);
10526               a->dw_attr_val.v.val_die_ref.die = copy;
10527
10528               /* Make sure the newly-copied DIE is walked.  If it was
10529                  installed in a previously-added context, it won't
10530                  get visited otherwise.  */
10531               if (parent != unit)
10532                 {
10533                   /* Find the highest point of the newly-added tree,
10534                      mark each node along the way, and walk from there.  */
10535                   parent->die_mark = 1;
10536                   while (parent->die_parent
10537                          && parent->die_parent->die_mark == 0)
10538                     {
10539                       parent = parent->die_parent;
10540                       parent->die_mark = 1;
10541                     }
10542                   copy_decls_walk (unit, parent, decl_table);
10543                 }
10544             }
10545         }
10546     }
10547
10548   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10549 }
10550
10551 /* Copy declarations for "unworthy" types into the new comdat section.
10552    Incomplete types, modified types, and certain other types aren't broken
10553    out into comdat sections of their own, so they don't have a signature,
10554    and we need to copy the declaration into the same section so that we
10555    don't have an external reference.  */
10556
10557 static void
10558 copy_decls_for_unworthy_types (dw_die_ref unit)
10559 {
10560   htab_t decl_table;
10561
10562   mark_dies (unit);
10563   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10564   copy_decls_walk (unit, unit, decl_table);
10565   htab_delete (decl_table);
10566   unmark_dies (unit);
10567 }
10568
10569 /* Traverse the DIE and add a sibling attribute if it may have the
10570    effect of speeding up access to siblings.  To save some space,
10571    avoid generating sibling attributes for DIE's without children.  */
10572
10573 static void
10574 add_sibling_attributes (dw_die_ref die)
10575 {
10576   dw_die_ref c;
10577
10578   if (! die->die_child)
10579     return;
10580
10581   if (die->die_parent && die != die->die_parent->die_child)
10582     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10583
10584   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10585 }
10586
10587 /* Output all location lists for the DIE and its children.  */
10588
10589 static void
10590 output_location_lists (dw_die_ref die)
10591 {
10592   dw_die_ref c;
10593   dw_attr_ref a;
10594   unsigned ix;
10595
10596   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10597     if (AT_class (a) == dw_val_class_loc_list)
10598       output_loc_list (AT_loc_list (a));
10599
10600   FOR_EACH_CHILD (die, c, output_location_lists (c));
10601 }
10602
10603 /* The format of each DIE (and its attribute value pairs) is encoded in an
10604    abbreviation table.  This routine builds the abbreviation table and assigns
10605    a unique abbreviation id for each abbreviation entry.  The children of each
10606    die are visited recursively.  */
10607
10608 static void
10609 build_abbrev_table (dw_die_ref die)
10610 {
10611   unsigned long abbrev_id;
10612   unsigned int n_alloc;
10613   dw_die_ref c;
10614   dw_attr_ref a;
10615   unsigned ix;
10616
10617   /* Scan the DIE references, and mark as external any that refer to
10618      DIEs from other CUs (i.e. those which are not marked).  */
10619   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10620     if (AT_class (a) == dw_val_class_die_ref
10621         && AT_ref (a)->die_mark == 0)
10622       {
10623         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10624         set_AT_ref_external (a, 1);
10625       }
10626
10627   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10628     {
10629       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10630       dw_attr_ref die_a, abbrev_a;
10631       unsigned ix;
10632       bool ok = true;
10633
10634       if (abbrev->die_tag != die->die_tag)
10635         continue;
10636       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10637         continue;
10638
10639       if (VEC_length (dw_attr_node, abbrev->die_attr)
10640           != VEC_length (dw_attr_node, die->die_attr))
10641         continue;
10642
10643       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10644         {
10645           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10646           if ((abbrev_a->dw_attr != die_a->dw_attr)
10647               || (value_format (abbrev_a) != value_format (die_a)))
10648             {
10649               ok = false;
10650               break;
10651             }
10652         }
10653       if (ok)
10654         break;
10655     }
10656
10657   if (abbrev_id >= abbrev_die_table_in_use)
10658     {
10659       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10660         {
10661           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10662           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10663                                             n_alloc);
10664
10665           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10666                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10667           abbrev_die_table_allocated = n_alloc;
10668         }
10669
10670       ++abbrev_die_table_in_use;
10671       abbrev_die_table[abbrev_id] = die;
10672     }
10673
10674   die->die_abbrev = abbrev_id;
10675   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10676 }
10677 \f
10678 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10679
10680 static int
10681 constant_size (unsigned HOST_WIDE_INT value)
10682 {
10683   int log;
10684
10685   if (value == 0)
10686     log = 0;
10687   else
10688     log = floor_log2 (value);
10689
10690   log = log / 8;
10691   log = 1 << (floor_log2 (log) + 1);
10692
10693   return log;
10694 }
10695
10696 /* Return the size of a DIE as it is represented in the
10697    .debug_info section.  */
10698
10699 static unsigned long
10700 size_of_die (dw_die_ref die)
10701 {
10702   unsigned long size = 0;
10703   dw_attr_ref a;
10704   unsigned ix;
10705
10706   size += size_of_uleb128 (die->die_abbrev);
10707   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10708     {
10709       switch (AT_class (a))
10710         {
10711         case dw_val_class_addr:
10712           size += DWARF2_ADDR_SIZE;
10713           break;
10714         case dw_val_class_offset:
10715           size += DWARF_OFFSET_SIZE;
10716           break;
10717         case dw_val_class_loc:
10718           {
10719             unsigned long lsize = size_of_locs (AT_loc (a));
10720
10721             /* Block length.  */
10722             if (dwarf_version >= 4)
10723               size += size_of_uleb128 (lsize);
10724             else
10725               size += constant_size (lsize);
10726             size += lsize;
10727           }
10728           break;
10729         case dw_val_class_loc_list:
10730           size += DWARF_OFFSET_SIZE;
10731           break;
10732         case dw_val_class_range_list:
10733           size += DWARF_OFFSET_SIZE;
10734           break;
10735         case dw_val_class_const:
10736           size += size_of_sleb128 (AT_int (a));
10737           break;
10738         case dw_val_class_unsigned_const:
10739           size += constant_size (AT_unsigned (a));
10740           break;
10741         case dw_val_class_const_double:
10742           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10743           if (HOST_BITS_PER_WIDE_INT >= 64)
10744             size++; /* block */
10745           break;
10746         case dw_val_class_vec:
10747           size += constant_size (a->dw_attr_val.v.val_vec.length
10748                                  * a->dw_attr_val.v.val_vec.elt_size)
10749                   + a->dw_attr_val.v.val_vec.length
10750                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10751           break;
10752         case dw_val_class_flag:
10753           if (dwarf_version >= 4)
10754             /* Currently all add_AT_flag calls pass in 1 as last argument,
10755                so DW_FORM_flag_present can be used.  If that ever changes,
10756                we'll need to use DW_FORM_flag and have some optimization
10757                in build_abbrev_table that will change those to
10758                DW_FORM_flag_present if it is set to 1 in all DIEs using
10759                the same abbrev entry.  */
10760             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10761           else
10762             size += 1;
10763           break;
10764         case dw_val_class_die_ref:
10765           if (AT_ref_external (a))
10766             {
10767               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10768                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10769                  is sized by target address length, whereas in DWARF3
10770                  it's always sized as an offset.  */
10771               if (dwarf_version >= 4)
10772                 size += DWARF_TYPE_SIGNATURE_SIZE;
10773               else if (dwarf_version == 2)
10774                 size += DWARF2_ADDR_SIZE;
10775               else
10776                 size += DWARF_OFFSET_SIZE;
10777             }
10778           else
10779             size += DWARF_OFFSET_SIZE;
10780           break;
10781         case dw_val_class_fde_ref:
10782           size += DWARF_OFFSET_SIZE;
10783           break;
10784         case dw_val_class_lbl_id:
10785           size += DWARF2_ADDR_SIZE;
10786           break;
10787         case dw_val_class_lineptr:
10788         case dw_val_class_macptr:
10789           size += DWARF_OFFSET_SIZE;
10790           break;
10791         case dw_val_class_str:
10792           if (AT_string_form (a) == DW_FORM_strp)
10793             size += DWARF_OFFSET_SIZE;
10794           else
10795             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10796           break;
10797         case dw_val_class_file:
10798           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10799           break;
10800         case dw_val_class_data8:
10801           size += 8;
10802           break;
10803         case dw_val_class_vms_delta:
10804           size += DWARF_OFFSET_SIZE;
10805           break;
10806         default:
10807           gcc_unreachable ();
10808         }
10809     }
10810
10811   return size;
10812 }
10813
10814 /* Size the debugging information associated with a given DIE.  Visits the
10815    DIE's children recursively.  Updates the global variable next_die_offset, on
10816    each time through.  Uses the current value of next_die_offset to update the
10817    die_offset field in each DIE.  */
10818
10819 static void
10820 calc_die_sizes (dw_die_ref die)
10821 {
10822   dw_die_ref c;
10823
10824   die->die_offset = next_die_offset;
10825   next_die_offset += size_of_die (die);
10826
10827   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10828
10829   if (die->die_child != NULL)
10830     /* Count the null byte used to terminate sibling lists.  */
10831     next_die_offset += 1;
10832 }
10833
10834 /* Set the marks for a die and its children.  We do this so
10835    that we know whether or not a reference needs to use FORM_ref_addr; only
10836    DIEs in the same CU will be marked.  We used to clear out the offset
10837    and use that as the flag, but ran into ordering problems.  */
10838
10839 static void
10840 mark_dies (dw_die_ref die)
10841 {
10842   dw_die_ref c;
10843
10844   gcc_assert (!die->die_mark);
10845
10846   die->die_mark = 1;
10847   FOR_EACH_CHILD (die, c, mark_dies (c));
10848 }
10849
10850 /* Clear the marks for a die and its children.  */
10851
10852 static void
10853 unmark_dies (dw_die_ref die)
10854 {
10855   dw_die_ref c;
10856
10857   if (dwarf_version < 4)
10858     gcc_assert (die->die_mark);
10859
10860   die->die_mark = 0;
10861   FOR_EACH_CHILD (die, c, unmark_dies (c));
10862 }
10863
10864 /* Clear the marks for a die, its children and referred dies.  */
10865
10866 static void
10867 unmark_all_dies (dw_die_ref die)
10868 {
10869   dw_die_ref c;
10870   dw_attr_ref a;
10871   unsigned ix;
10872
10873   if (!die->die_mark)
10874     return;
10875   die->die_mark = 0;
10876
10877   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10878
10879   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10880     if (AT_class (a) == dw_val_class_die_ref)
10881       unmark_all_dies (AT_ref (a));
10882 }
10883
10884 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10885    generated for the compilation unit.  */
10886
10887 static unsigned long
10888 size_of_pubnames (VEC (pubname_entry, gc) * names)
10889 {
10890   unsigned long size;
10891   unsigned i;
10892   pubname_ref p;
10893
10894   size = DWARF_PUBNAMES_HEADER_SIZE;
10895   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
10896     if (names != pubtype_table
10897         || p->die->die_offset != 0
10898         || !flag_eliminate_unused_debug_types)
10899       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10900
10901   size += DWARF_OFFSET_SIZE;
10902   return size;
10903 }
10904
10905 /* Return the size of the information in the .debug_aranges section.  */
10906
10907 static unsigned long
10908 size_of_aranges (void)
10909 {
10910   unsigned long size;
10911
10912   size = DWARF_ARANGES_HEADER_SIZE;
10913
10914   /* Count the address/length pair for this compilation unit.  */
10915   if (text_section_used)
10916     size += 2 * DWARF2_ADDR_SIZE;
10917   if (cold_text_section_used)
10918     size += 2 * DWARF2_ADDR_SIZE;
10919   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10920
10921   /* Count the two zero words used to terminated the address range table.  */
10922   size += 2 * DWARF2_ADDR_SIZE;
10923   return size;
10924 }
10925 \f
10926 /* Select the encoding of an attribute value.  */
10927
10928 static enum dwarf_form
10929 value_format (dw_attr_ref a)
10930 {
10931   switch (a->dw_attr_val.val_class)
10932     {
10933     case dw_val_class_addr:
10934       /* Only very few attributes allow DW_FORM_addr.  */
10935       switch (a->dw_attr)
10936         {
10937         case DW_AT_low_pc:
10938         case DW_AT_high_pc:
10939         case DW_AT_entry_pc:
10940         case DW_AT_trampoline:
10941           return DW_FORM_addr;
10942         default:
10943           break;
10944         }
10945       switch (DWARF2_ADDR_SIZE)
10946         {
10947         case 1:
10948           return DW_FORM_data1;
10949         case 2:
10950           return DW_FORM_data2;
10951         case 4:
10952           return DW_FORM_data4;
10953         case 8:
10954           return DW_FORM_data8;
10955         default:
10956           gcc_unreachable ();
10957         }
10958     case dw_val_class_range_list:
10959     case dw_val_class_loc_list:
10960       if (dwarf_version >= 4)
10961         return DW_FORM_sec_offset;
10962       /* FALLTHRU */
10963     case dw_val_class_vms_delta:
10964     case dw_val_class_offset:
10965       switch (DWARF_OFFSET_SIZE)
10966         {
10967         case 4:
10968           return DW_FORM_data4;
10969         case 8:
10970           return DW_FORM_data8;
10971         default:
10972           gcc_unreachable ();
10973         }
10974     case dw_val_class_loc:
10975       if (dwarf_version >= 4)
10976         return DW_FORM_exprloc;
10977       switch (constant_size (size_of_locs (AT_loc (a))))
10978         {
10979         case 1:
10980           return DW_FORM_block1;
10981         case 2:
10982           return DW_FORM_block2;
10983         default:
10984           gcc_unreachable ();
10985         }
10986     case dw_val_class_const:
10987       return DW_FORM_sdata;
10988     case dw_val_class_unsigned_const:
10989       switch (constant_size (AT_unsigned (a)))
10990         {
10991         case 1:
10992           return DW_FORM_data1;
10993         case 2:
10994           return DW_FORM_data2;
10995         case 4:
10996           return DW_FORM_data4;
10997         case 8:
10998           return DW_FORM_data8;
10999         default:
11000           gcc_unreachable ();
11001         }
11002     case dw_val_class_const_double:
11003       switch (HOST_BITS_PER_WIDE_INT)
11004         {
11005         case 8:
11006           return DW_FORM_data2;
11007         case 16:
11008           return DW_FORM_data4;
11009         case 32:
11010           return DW_FORM_data8;
11011         case 64:
11012         default:
11013           return DW_FORM_block1;
11014         }
11015     case dw_val_class_vec:
11016       switch (constant_size (a->dw_attr_val.v.val_vec.length
11017                              * a->dw_attr_val.v.val_vec.elt_size))
11018         {
11019         case 1:
11020           return DW_FORM_block1;
11021         case 2:
11022           return DW_FORM_block2;
11023         case 4:
11024           return DW_FORM_block4;
11025         default:
11026           gcc_unreachable ();
11027         }
11028     case dw_val_class_flag:
11029       if (dwarf_version >= 4)
11030         {
11031           /* Currently all add_AT_flag calls pass in 1 as last argument,
11032              so DW_FORM_flag_present can be used.  If that ever changes,
11033              we'll need to use DW_FORM_flag and have some optimization
11034              in build_abbrev_table that will change those to
11035              DW_FORM_flag_present if it is set to 1 in all DIEs using
11036              the same abbrev entry.  */
11037           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11038           return DW_FORM_flag_present;
11039         }
11040       return DW_FORM_flag;
11041     case dw_val_class_die_ref:
11042       if (AT_ref_external (a))
11043         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
11044       else
11045         return DW_FORM_ref;
11046     case dw_val_class_fde_ref:
11047       return DW_FORM_data;
11048     case dw_val_class_lbl_id:
11049       return DW_FORM_addr;
11050     case dw_val_class_lineptr:
11051     case dw_val_class_macptr:
11052       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11053     case dw_val_class_str:
11054       return AT_string_form (a);
11055     case dw_val_class_file:
11056       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11057         {
11058         case 1:
11059           return DW_FORM_data1;
11060         case 2:
11061           return DW_FORM_data2;
11062         case 4:
11063           return DW_FORM_data4;
11064         default:
11065           gcc_unreachable ();
11066         }
11067
11068     case dw_val_class_data8:
11069       return DW_FORM_data8;
11070
11071     default:
11072       gcc_unreachable ();
11073     }
11074 }
11075
11076 /* Output the encoding of an attribute value.  */
11077
11078 static void
11079 output_value_format (dw_attr_ref a)
11080 {
11081   enum dwarf_form form = value_format (a);
11082
11083   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11084 }
11085
11086 /* Output the .debug_abbrev section which defines the DIE abbreviation
11087    table.  */
11088
11089 static void
11090 output_abbrev_section (void)
11091 {
11092   unsigned long abbrev_id;
11093
11094   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11095     {
11096       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11097       unsigned ix;
11098       dw_attr_ref a_attr;
11099
11100       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11101       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11102                                    dwarf_tag_name (abbrev->die_tag));
11103
11104       if (abbrev->die_child != NULL)
11105         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11106       else
11107         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11108
11109       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11110            ix++)
11111         {
11112           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11113                                        dwarf_attr_name (a_attr->dw_attr));
11114           output_value_format (a_attr);
11115         }
11116
11117       dw2_asm_output_data (1, 0, NULL);
11118       dw2_asm_output_data (1, 0, NULL);
11119     }
11120
11121   /* Terminate the table.  */
11122   dw2_asm_output_data (1, 0, NULL);
11123 }
11124
11125 /* Output a symbol we can use to refer to this DIE from another CU.  */
11126
11127 static inline void
11128 output_die_symbol (dw_die_ref die)
11129 {
11130   char *sym = die->die_id.die_symbol;
11131
11132   if (sym == 0)
11133     return;
11134
11135   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11136     /* We make these global, not weak; if the target doesn't support
11137        .linkonce, it doesn't support combining the sections, so debugging
11138        will break.  */
11139     targetm.asm_out.globalize_label (asm_out_file, sym);
11140
11141   ASM_OUTPUT_LABEL (asm_out_file, sym);
11142 }
11143
11144 /* Return a new location list, given the begin and end range, and the
11145    expression.  */
11146
11147 static inline dw_loc_list_ref
11148 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11149               const char *section)
11150 {
11151   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11152
11153   retlist->begin = begin;
11154   retlist->end = end;
11155   retlist->expr = expr;
11156   retlist->section = section;
11157
11158   return retlist;
11159 }
11160
11161 /* Generate a new internal symbol for this location list node, if it
11162    hasn't got one yet.  */
11163
11164 static inline void
11165 gen_llsym (dw_loc_list_ref list)
11166 {
11167   gcc_assert (!list->ll_symbol);
11168   list->ll_symbol = gen_internal_sym ("LLST");
11169 }
11170
11171 /* Output the location list given to us.  */
11172
11173 static void
11174 output_loc_list (dw_loc_list_ref list_head)
11175 {
11176   dw_loc_list_ref curr = list_head;
11177
11178   if (list_head->emitted)
11179     return;
11180   list_head->emitted = true;
11181
11182   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11183
11184   /* Walk the location list, and output each range + expression.  */
11185   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11186     {
11187       unsigned long size;
11188       /* Don't output an entry that starts and ends at the same address.  */
11189       if (strcmp (curr->begin, curr->end) == 0)
11190         continue;
11191       if (!have_multiple_function_sections)
11192         {
11193           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11194                                 "Location list begin address (%s)",
11195                                 list_head->ll_symbol);
11196           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11197                                 "Location list end address (%s)",
11198                                 list_head->ll_symbol);
11199         }
11200       else
11201         {
11202           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11203                                "Location list begin address (%s)",
11204                                list_head->ll_symbol);
11205           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11206                                "Location list end address (%s)",
11207                                list_head->ll_symbol);
11208         }
11209       size = size_of_locs (curr->expr);
11210
11211       /* Output the block length for this list of location operations.  */
11212       gcc_assert (size <= 0xffff);
11213       dw2_asm_output_data (2, size, "%s", "Location expression size");
11214
11215       output_loc_sequence (curr->expr, -1);
11216     }
11217
11218   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11219                        "Location list terminator begin (%s)",
11220                        list_head->ll_symbol);
11221   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11222                        "Location list terminator end (%s)",
11223                        list_head->ll_symbol);
11224 }
11225
11226 /* Output a type signature.  */
11227
11228 static inline void
11229 output_signature (const char *sig, const char *name)
11230 {
11231   int i;
11232
11233   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11234     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11235 }
11236
11237 /* Output the DIE and its attributes.  Called recursively to generate
11238    the definitions of each child DIE.  */
11239
11240 static void
11241 output_die (dw_die_ref die)
11242 {
11243   dw_attr_ref a;
11244   dw_die_ref c;
11245   unsigned long size;
11246   unsigned ix;
11247
11248   /* If someone in another CU might refer to us, set up a symbol for
11249      them to point to.  */
11250   if (dwarf_version < 4 && die->die_id.die_symbol)
11251     output_die_symbol (die);
11252
11253   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11254                                (unsigned long)die->die_offset,
11255                                dwarf_tag_name (die->die_tag));
11256
11257   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11258     {
11259       const char *name = dwarf_attr_name (a->dw_attr);
11260
11261       switch (AT_class (a))
11262         {
11263         case dw_val_class_addr:
11264           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11265           break;
11266
11267         case dw_val_class_offset:
11268           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11269                                "%s", name);
11270           break;
11271
11272         case dw_val_class_range_list:
11273           {
11274             char *p = strchr (ranges_section_label, '\0');
11275
11276             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11277                      a->dw_attr_val.v.val_offset);
11278             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11279                                    debug_ranges_section, "%s", name);
11280             *p = '\0';
11281           }
11282           break;
11283
11284         case dw_val_class_loc:
11285           size = size_of_locs (AT_loc (a));
11286
11287           /* Output the block length for this list of location operations.  */
11288           if (dwarf_version >= 4)
11289             dw2_asm_output_data_uleb128 (size, "%s", name);
11290           else
11291             dw2_asm_output_data (constant_size (size), size, "%s", name);
11292
11293           output_loc_sequence (AT_loc (a), -1);
11294           break;
11295
11296         case dw_val_class_const:
11297           /* ??? It would be slightly more efficient to use a scheme like is
11298              used for unsigned constants below, but gdb 4.x does not sign
11299              extend.  Gdb 5.x does sign extend.  */
11300           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11301           break;
11302
11303         case dw_val_class_unsigned_const:
11304           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11305                                AT_unsigned (a), "%s", name);
11306           break;
11307
11308         case dw_val_class_const_double:
11309           {
11310             unsigned HOST_WIDE_INT first, second;
11311
11312             if (HOST_BITS_PER_WIDE_INT >= 64)
11313               dw2_asm_output_data (1,
11314                                    2 * HOST_BITS_PER_WIDE_INT
11315                                    / HOST_BITS_PER_CHAR,
11316                                    NULL);
11317
11318             if (WORDS_BIG_ENDIAN)
11319               {
11320                 first = a->dw_attr_val.v.val_double.high;
11321                 second = a->dw_attr_val.v.val_double.low;
11322               }
11323             else
11324               {
11325                 first = a->dw_attr_val.v.val_double.low;
11326                 second = a->dw_attr_val.v.val_double.high;
11327               }
11328
11329             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11330                                  first, name);
11331             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11332                                  second, NULL);
11333           }
11334           break;
11335
11336         case dw_val_class_vec:
11337           {
11338             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11339             unsigned int len = a->dw_attr_val.v.val_vec.length;
11340             unsigned int i;
11341             unsigned char *p;
11342
11343             dw2_asm_output_data (constant_size (len * elt_size),
11344                                  len * elt_size, "%s", name);
11345             if (elt_size > sizeof (HOST_WIDE_INT))
11346               {
11347                 elt_size /= 2;
11348                 len *= 2;
11349               }
11350             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11351                  i < len;
11352                  i++, p += elt_size)
11353               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11354                                    "fp or vector constant word %u", i);
11355             break;
11356           }
11357
11358         case dw_val_class_flag:
11359           if (dwarf_version >= 4)
11360             {
11361               /* Currently all add_AT_flag calls pass in 1 as last argument,
11362                  so DW_FORM_flag_present can be used.  If that ever changes,
11363                  we'll need to use DW_FORM_flag and have some optimization
11364                  in build_abbrev_table that will change those to
11365                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11366                  the same abbrev entry.  */
11367               gcc_assert (AT_flag (a) == 1);
11368               if (flag_debug_asm)
11369                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11370                          ASM_COMMENT_START, name);
11371               break;
11372             }
11373           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11374           break;
11375
11376         case dw_val_class_loc_list:
11377           {
11378             char *sym = AT_loc_list (a)->ll_symbol;
11379
11380             gcc_assert (sym);
11381             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11382                                    "%s", name);
11383           }
11384           break;
11385
11386         case dw_val_class_die_ref:
11387           if (AT_ref_external (a))
11388             {
11389               if (dwarf_version >= 4)
11390                 {
11391                   comdat_type_node_ref type_node =
11392                     AT_ref (a)->die_id.die_type_node;
11393
11394                   gcc_assert (type_node);
11395                   output_signature (type_node->signature, name);
11396                 }
11397               else
11398                 {
11399                   char *sym = AT_ref (a)->die_id.die_symbol;
11400                   int size;
11401
11402                   gcc_assert (sym);
11403                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11404                      length, whereas in DWARF3 it's always sized as an
11405                      offset.  */
11406                   if (dwarf_version == 2)
11407                     size = DWARF2_ADDR_SIZE;
11408                   else
11409                     size = DWARF_OFFSET_SIZE;
11410                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11411                                          name);
11412                 }
11413             }
11414           else
11415             {
11416               gcc_assert (AT_ref (a)->die_offset);
11417               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11418                                    "%s", name);
11419             }
11420           break;
11421
11422         case dw_val_class_fde_ref:
11423           {
11424             char l1[20];
11425
11426             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11427                                          a->dw_attr_val.v.val_fde_index * 2);
11428             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11429                                    "%s", name);
11430           }
11431           break;
11432
11433         case dw_val_class_vms_delta:
11434           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11435                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11436                                     "%s", name);
11437           break;
11438
11439         case dw_val_class_lbl_id:
11440           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11441           break;
11442
11443         case dw_val_class_lineptr:
11444           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11445                                  debug_line_section, "%s", name);
11446           break;
11447
11448         case dw_val_class_macptr:
11449           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11450                                  debug_macinfo_section, "%s", name);
11451           break;
11452
11453         case dw_val_class_str:
11454           if (AT_string_form (a) == DW_FORM_strp)
11455             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11456                                    a->dw_attr_val.v.val_str->label,
11457                                    debug_str_section,
11458                                    "%s: \"%s\"", name, AT_string (a));
11459           else
11460             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11461           break;
11462
11463         case dw_val_class_file:
11464           {
11465             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11466
11467             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11468                                  a->dw_attr_val.v.val_file->filename);
11469             break;
11470           }
11471
11472         case dw_val_class_data8:
11473           {
11474             int i;
11475
11476             for (i = 0; i < 8; i++)
11477               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11478                                    i == 0 ? "%s" : NULL, name);
11479             break;
11480           }
11481
11482         default:
11483           gcc_unreachable ();
11484         }
11485     }
11486
11487   FOR_EACH_CHILD (die, c, output_die (c));
11488
11489   /* Add null byte to terminate sibling list.  */
11490   if (die->die_child != NULL)
11491     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11492                          (unsigned long) die->die_offset);
11493 }
11494
11495 /* Output the compilation unit that appears at the beginning of the
11496    .debug_info section, and precedes the DIE descriptions.  */
11497
11498 static void
11499 output_compilation_unit_header (void)
11500 {
11501   int ver = dwarf_version;
11502
11503   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11504     dw2_asm_output_data (4, 0xffffffff,
11505       "Initial length escape value indicating 64-bit DWARF extension");
11506   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11507                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11508                        "Length of Compilation Unit Info");
11509   dw2_asm_output_data (2, ver, "DWARF version number");
11510   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11511                          debug_abbrev_section,
11512                          "Offset Into Abbrev. Section");
11513   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11514 }
11515
11516 /* Output the compilation unit DIE and its children.  */
11517
11518 static void
11519 output_comp_unit (dw_die_ref die, int output_if_empty)
11520 {
11521   const char *secname;
11522   char *oldsym, *tmp;
11523
11524   /* Unless we are outputting main CU, we may throw away empty ones.  */
11525   if (!output_if_empty && die->die_child == NULL)
11526     return;
11527
11528   /* Even if there are no children of this DIE, we must output the information
11529      about the compilation unit.  Otherwise, on an empty translation unit, we
11530      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11531      will then complain when examining the file.  First mark all the DIEs in
11532      this CU so we know which get local refs.  */
11533   mark_dies (die);
11534
11535   build_abbrev_table (die);
11536
11537   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11538   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11539   calc_die_sizes (die);
11540
11541   oldsym = die->die_id.die_symbol;
11542   if (oldsym)
11543     {
11544       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11545
11546       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11547       secname = tmp;
11548       die->die_id.die_symbol = NULL;
11549       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11550     }
11551   else
11552     {
11553       switch_to_section (debug_info_section);
11554       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11555       info_section_emitted = true;
11556     }
11557
11558   /* Output debugging information.  */
11559   output_compilation_unit_header ();
11560   output_die (die);
11561
11562   /* Leave the marks on the main CU, so we can check them in
11563      output_pubnames.  */
11564   if (oldsym)
11565     {
11566       unmark_dies (die);
11567       die->die_id.die_symbol = oldsym;
11568     }
11569 }
11570
11571 /* Output a comdat type unit DIE and its children.  */
11572
11573 static void
11574 output_comdat_type_unit (comdat_type_node *node)
11575 {
11576   const char *secname;
11577   char *tmp;
11578   int i;
11579 #if defined (OBJECT_FORMAT_ELF)
11580   tree comdat_key;
11581 #endif
11582
11583   /* First mark all the DIEs in this CU so we know which get local refs.  */
11584   mark_dies (node->root_die);
11585
11586   build_abbrev_table (node->root_die);
11587
11588   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11589   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11590   calc_die_sizes (node->root_die);
11591
11592 #if defined (OBJECT_FORMAT_ELF)
11593   secname = ".debug_types";
11594   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11595   sprintf (tmp, "wt.");
11596   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11597     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11598   comdat_key = get_identifier (tmp);
11599   targetm.asm_out.named_section (secname,
11600                                  SECTION_DEBUG | SECTION_LINKONCE,
11601                                  comdat_key);
11602 #else
11603   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11604   sprintf (tmp, ".gnu.linkonce.wt.");
11605   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11606     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11607   secname = tmp;
11608   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11609 #endif
11610
11611   /* Output debugging information.  */
11612   output_compilation_unit_header ();
11613   output_signature (node->signature, "Type Signature");
11614   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11615                        "Offset to Type DIE");
11616   output_die (node->root_die);
11617
11618   unmark_dies (node->root_die);
11619 }
11620
11621 /* Return the DWARF2/3 pubname associated with a decl.  */
11622
11623 static const char *
11624 dwarf2_name (tree decl, int scope)
11625 {
11626   if (DECL_NAMELESS (decl))
11627     return NULL;
11628   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11629 }
11630
11631 /* Add a new entry to .debug_pubnames if appropriate.  */
11632
11633 static void
11634 add_pubname_string (const char *str, dw_die_ref die)
11635 {
11636   if (targetm.want_debug_pub_sections)
11637     {
11638       pubname_entry e;
11639
11640       e.die = die;
11641       e.name = xstrdup (str);
11642       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11643     }
11644 }
11645
11646 static void
11647 add_pubname (tree decl, dw_die_ref die)
11648 {
11649   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11650     {
11651       const char *name = dwarf2_name (decl, 1);
11652       if (name)
11653         add_pubname_string (name, die);
11654     }
11655 }
11656
11657 /* Add a new entry to .debug_pubtypes if appropriate.  */
11658
11659 static void
11660 add_pubtype (tree decl, dw_die_ref die)
11661 {
11662   pubname_entry e;
11663
11664   if (!targetm.want_debug_pub_sections)
11665     return;
11666
11667   e.name = NULL;
11668   if ((TREE_PUBLIC (decl)
11669        || is_cu_die (die->die_parent))
11670       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11671     {
11672       e.die = die;
11673       if (TYPE_P (decl))
11674         {
11675           if (TYPE_NAME (decl))
11676             {
11677               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11678                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11679               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11680                        && DECL_NAME (TYPE_NAME (decl)))
11681                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11682               else
11683                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11684             }
11685         }
11686       else
11687         {
11688           e.name = dwarf2_name (decl, 1);
11689           if (e.name)
11690             e.name = xstrdup (e.name);
11691         }
11692
11693       /* If we don't have a name for the type, there's no point in adding
11694          it to the table.  */
11695       if (e.name && e.name[0] != '\0')
11696         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11697     }
11698 }
11699
11700 /* Output the public names table used to speed up access to externally
11701    visible names; or the public types table used to find type definitions.  */
11702
11703 static void
11704 output_pubnames (VEC (pubname_entry, gc) * names)
11705 {
11706   unsigned i;
11707   unsigned long pubnames_length = size_of_pubnames (names);
11708   pubname_ref pub;
11709
11710   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11711     dw2_asm_output_data (4, 0xffffffff,
11712       "Initial length escape value indicating 64-bit DWARF extension");
11713   if (names == pubname_table)
11714     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11715                          "Length of Public Names Info");
11716   else
11717     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11718                          "Length of Public Type Names Info");
11719   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11720   dw2_asm_output_data (2, 2, "DWARF Version");
11721   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11722                          debug_info_section,
11723                          "Offset of Compilation Unit Info");
11724   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11725                        "Compilation Unit Length");
11726
11727   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11728     {
11729       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11730       if (names == pubname_table)
11731         gcc_assert (pub->die->die_mark);
11732
11733       if (names != pubtype_table
11734           || pub->die->die_offset != 0
11735           || !flag_eliminate_unused_debug_types)
11736         {
11737           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11738                                "DIE offset");
11739
11740           dw2_asm_output_nstring (pub->name, -1, "external name");
11741         }
11742     }
11743
11744   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11745 }
11746
11747 /* Add a new entry to .debug_aranges if appropriate.  */
11748
11749 static void
11750 add_arange (tree decl, dw_die_ref die)
11751 {
11752   if (! DECL_SECTION_NAME (decl))
11753     return;
11754
11755   if (arange_table_in_use == arange_table_allocated)
11756     {
11757       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11758       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11759                                     arange_table_allocated);
11760       memset (arange_table + arange_table_in_use, 0,
11761               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11762     }
11763
11764   arange_table[arange_table_in_use++] = die;
11765 }
11766
11767 /* Output the information that goes into the .debug_aranges table.
11768    Namely, define the beginning and ending address range of the
11769    text section generated for this compilation unit.  */
11770
11771 static void
11772 output_aranges (void)
11773 {
11774   unsigned i;
11775   unsigned long aranges_length = size_of_aranges ();
11776
11777   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11778     dw2_asm_output_data (4, 0xffffffff,
11779       "Initial length escape value indicating 64-bit DWARF extension");
11780   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11781                        "Length of Address Ranges Info");
11782   /* Version number for aranges is still 2, even in DWARF3.  */
11783   dw2_asm_output_data (2, 2, "DWARF Version");
11784   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11785                          debug_info_section,
11786                          "Offset of Compilation Unit Info");
11787   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11788   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11789
11790   /* We need to align to twice the pointer size here.  */
11791   if (DWARF_ARANGES_PAD_SIZE)
11792     {
11793       /* Pad using a 2 byte words so that padding is correct for any
11794          pointer size.  */
11795       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11796                            2 * DWARF2_ADDR_SIZE);
11797       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11798         dw2_asm_output_data (2, 0, NULL);
11799     }
11800
11801   /* It is necessary not to output these entries if the sections were
11802      not used; if the sections were not used, the length will be 0 and
11803      the address may end up as 0 if the section is discarded by ld
11804      --gc-sections, leaving an invalid (0, 0) entry that can be
11805      confused with the terminator.  */
11806   if (text_section_used)
11807     {
11808       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11809       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11810                             text_section_label, "Length");
11811     }
11812   if (cold_text_section_used)
11813     {
11814       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11815                            "Address");
11816       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11817                             cold_text_section_label, "Length");
11818     }
11819
11820   for (i = 0; i < arange_table_in_use; i++)
11821     {
11822       dw_die_ref die = arange_table[i];
11823
11824       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11825       gcc_assert (die->die_mark);
11826
11827       if (die->die_tag == DW_TAG_subprogram)
11828         {
11829           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11830                                "Address");
11831           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11832                                 get_AT_low_pc (die), "Length");
11833         }
11834       else
11835         {
11836           /* A static variable; extract the symbol from DW_AT_location.
11837              Note that this code isn't currently hit, as we only emit
11838              aranges for functions (jason 9/23/99).  */
11839           dw_attr_ref a = get_AT (die, DW_AT_location);
11840           dw_loc_descr_ref loc;
11841
11842           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11843
11844           loc = AT_loc (a);
11845           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11846
11847           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11848                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11849           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11850                                get_AT_unsigned (die, DW_AT_byte_size),
11851                                "Length");
11852         }
11853     }
11854
11855   /* Output the terminator words.  */
11856   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11857   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11858 }
11859
11860 /* Add a new entry to .debug_ranges.  Return the offset at which it
11861    was placed.  */
11862
11863 static unsigned int
11864 add_ranges_num (int num)
11865 {
11866   unsigned int in_use = ranges_table_in_use;
11867
11868   if (in_use == ranges_table_allocated)
11869     {
11870       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11871       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11872                                     ranges_table_allocated);
11873       memset (ranges_table + ranges_table_in_use, 0,
11874               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11875     }
11876
11877   ranges_table[in_use].num = num;
11878   ranges_table_in_use = in_use + 1;
11879
11880   return in_use * 2 * DWARF2_ADDR_SIZE;
11881 }
11882
11883 /* Add a new entry to .debug_ranges corresponding to a block, or a
11884    range terminator if BLOCK is NULL.  */
11885
11886 static unsigned int
11887 add_ranges (const_tree block)
11888 {
11889   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11890 }
11891
11892 /* Add a new entry to .debug_ranges corresponding to a pair of
11893    labels.  */
11894
11895 static void
11896 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11897                       bool *added)
11898 {
11899   unsigned int in_use = ranges_by_label_in_use;
11900   unsigned int offset;
11901
11902   if (in_use == ranges_by_label_allocated)
11903     {
11904       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11905       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11906                                        ranges_by_label,
11907                                        ranges_by_label_allocated);
11908       memset (ranges_by_label + ranges_by_label_in_use, 0,
11909               RANGES_TABLE_INCREMENT
11910               * sizeof (struct dw_ranges_by_label_struct));
11911     }
11912
11913   ranges_by_label[in_use].begin = begin;
11914   ranges_by_label[in_use].end = end;
11915   ranges_by_label_in_use = in_use + 1;
11916
11917   offset = add_ranges_num (-(int)in_use - 1);
11918   if (!*added)
11919     {
11920       add_AT_range_list (die, DW_AT_ranges, offset);
11921       *added = true;
11922     }
11923 }
11924
11925 static void
11926 output_ranges (void)
11927 {
11928   unsigned i;
11929   static const char *const start_fmt = "Offset %#x";
11930   const char *fmt = start_fmt;
11931
11932   for (i = 0; i < ranges_table_in_use; i++)
11933     {
11934       int block_num = ranges_table[i].num;
11935
11936       if (block_num > 0)
11937         {
11938           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11939           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11940
11941           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11942           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11943
11944           /* If all code is in the text section, then the compilation
11945              unit base address defaults to DW_AT_low_pc, which is the
11946              base of the text section.  */
11947           if (!have_multiple_function_sections)
11948             {
11949               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11950                                     text_section_label,
11951                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11952               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11953                                     text_section_label, NULL);
11954             }
11955
11956           /* Otherwise, the compilation unit base address is zero,
11957              which allows us to use absolute addresses, and not worry
11958              about whether the target supports cross-section
11959              arithmetic.  */
11960           else
11961             {
11962               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11963                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11964               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11965             }
11966
11967           fmt = NULL;
11968         }
11969
11970       /* Negative block_num stands for an index into ranges_by_label.  */
11971       else if (block_num < 0)
11972         {
11973           int lab_idx = - block_num - 1;
11974
11975           if (!have_multiple_function_sections)
11976             {
11977               gcc_unreachable ();
11978 #if 0
11979               /* If we ever use add_ranges_by_labels () for a single
11980                  function section, all we have to do is to take out
11981                  the #if 0 above.  */
11982               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11983                                     ranges_by_label[lab_idx].begin,
11984                                     text_section_label,
11985                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11986               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11987                                     ranges_by_label[lab_idx].end,
11988                                     text_section_label, NULL);
11989 #endif
11990             }
11991           else
11992             {
11993               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11994                                    ranges_by_label[lab_idx].begin,
11995                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11996               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11997                                    ranges_by_label[lab_idx].end,
11998                                    NULL);
11999             }
12000         }
12001       else
12002         {
12003           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12004           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12005           fmt = start_fmt;
12006         }
12007     }
12008 }
12009
12010 /* Data structure containing information about input files.  */
12011 struct file_info
12012 {
12013   const char *path;     /* Complete file name.  */
12014   const char *fname;    /* File name part.  */
12015   int length;           /* Length of entire string.  */
12016   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
12017   int dir_idx;          /* Index in directory table.  */
12018 };
12019
12020 /* Data structure containing information about directories with source
12021    files.  */
12022 struct dir_info
12023 {
12024   const char *path;     /* Path including directory name.  */
12025   int length;           /* Path length.  */
12026   int prefix;           /* Index of directory entry which is a prefix.  */
12027   int count;            /* Number of files in this directory.  */
12028   int dir_idx;          /* Index of directory used as base.  */
12029 };
12030
12031 /* Callback function for file_info comparison.  We sort by looking at
12032    the directories in the path.  */
12033
12034 static int
12035 file_info_cmp (const void *p1, const void *p2)
12036 {
12037   const struct file_info *const s1 = (const struct file_info *) p1;
12038   const struct file_info *const s2 = (const struct file_info *) p2;
12039   const unsigned char *cp1;
12040   const unsigned char *cp2;
12041
12042   /* Take care of file names without directories.  We need to make sure that
12043      we return consistent values to qsort since some will get confused if
12044      we return the same value when identical operands are passed in opposite
12045      orders.  So if neither has a directory, return 0 and otherwise return
12046      1 or -1 depending on which one has the directory.  */
12047   if ((s1->path == s1->fname || s2->path == s2->fname))
12048     return (s2->path == s2->fname) - (s1->path == s1->fname);
12049
12050   cp1 = (const unsigned char *) s1->path;
12051   cp2 = (const unsigned char *) s2->path;
12052
12053   while (1)
12054     {
12055       ++cp1;
12056       ++cp2;
12057       /* Reached the end of the first path?  If so, handle like above.  */
12058       if ((cp1 == (const unsigned char *) s1->fname)
12059           || (cp2 == (const unsigned char *) s2->fname))
12060         return ((cp2 == (const unsigned char *) s2->fname)
12061                 - (cp1 == (const unsigned char *) s1->fname));
12062
12063       /* Character of current path component the same?  */
12064       else if (*cp1 != *cp2)
12065         return *cp1 - *cp2;
12066     }
12067 }
12068
12069 struct file_name_acquire_data
12070 {
12071   struct file_info *files;
12072   int used_files;
12073   int max_files;
12074 };
12075
12076 /* Traversal function for the hash table.  */
12077
12078 static int
12079 file_name_acquire (void ** slot, void *data)
12080 {
12081   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12082   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12083   struct file_info *fi;
12084   const char *f;
12085
12086   gcc_assert (fnad->max_files >= d->emitted_number);
12087
12088   if (! d->emitted_number)
12089     return 1;
12090
12091   gcc_assert (fnad->max_files != fnad->used_files);
12092
12093   fi = fnad->files + fnad->used_files++;
12094
12095   /* Skip all leading "./".  */
12096   f = d->filename;
12097   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12098     f += 2;
12099
12100   /* Create a new array entry.  */
12101   fi->path = f;
12102   fi->length = strlen (f);
12103   fi->file_idx = d;
12104
12105   /* Search for the file name part.  */
12106   f = strrchr (f, DIR_SEPARATOR);
12107 #if defined (DIR_SEPARATOR_2)
12108   {
12109     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12110
12111     if (g != NULL)
12112       {
12113         if (f == NULL || f < g)
12114           f = g;
12115       }
12116   }
12117 #endif
12118
12119   fi->fname = f == NULL ? fi->path : f + 1;
12120   return 1;
12121 }
12122
12123 /* Output the directory table and the file name table.  We try to minimize
12124    the total amount of memory needed.  A heuristic is used to avoid large
12125    slowdowns with many input files.  */
12126
12127 static void
12128 output_file_names (void)
12129 {
12130   struct file_name_acquire_data fnad;
12131   int numfiles;
12132   struct file_info *files;
12133   struct dir_info *dirs;
12134   int *saved;
12135   int *savehere;
12136   int *backmap;
12137   int ndirs;
12138   int idx_offset;
12139   int i;
12140
12141   if (!last_emitted_file)
12142     {
12143       dw2_asm_output_data (1, 0, "End directory table");
12144       dw2_asm_output_data (1, 0, "End file name table");
12145       return;
12146     }
12147
12148   numfiles = last_emitted_file->emitted_number;
12149
12150   /* Allocate the various arrays we need.  */
12151   files = XALLOCAVEC (struct file_info, numfiles);
12152   dirs = XALLOCAVEC (struct dir_info, numfiles);
12153
12154   fnad.files = files;
12155   fnad.used_files = 0;
12156   fnad.max_files = numfiles;
12157   htab_traverse (file_table, file_name_acquire, &fnad);
12158   gcc_assert (fnad.used_files == fnad.max_files);
12159
12160   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12161
12162   /* Find all the different directories used.  */
12163   dirs[0].path = files[0].path;
12164   dirs[0].length = files[0].fname - files[0].path;
12165   dirs[0].prefix = -1;
12166   dirs[0].count = 1;
12167   dirs[0].dir_idx = 0;
12168   files[0].dir_idx = 0;
12169   ndirs = 1;
12170
12171   for (i = 1; i < numfiles; i++)
12172     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12173         && memcmp (dirs[ndirs - 1].path, files[i].path,
12174                    dirs[ndirs - 1].length) == 0)
12175       {
12176         /* Same directory as last entry.  */
12177         files[i].dir_idx = ndirs - 1;
12178         ++dirs[ndirs - 1].count;
12179       }
12180     else
12181       {
12182         int j;
12183
12184         /* This is a new directory.  */
12185         dirs[ndirs].path = files[i].path;
12186         dirs[ndirs].length = files[i].fname - files[i].path;
12187         dirs[ndirs].count = 1;
12188         dirs[ndirs].dir_idx = ndirs;
12189         files[i].dir_idx = ndirs;
12190
12191         /* Search for a prefix.  */
12192         dirs[ndirs].prefix = -1;
12193         for (j = 0; j < ndirs; j++)
12194           if (dirs[j].length < dirs[ndirs].length
12195               && dirs[j].length > 1
12196               && (dirs[ndirs].prefix == -1
12197                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12198               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12199             dirs[ndirs].prefix = j;
12200
12201         ++ndirs;
12202       }
12203
12204   /* Now to the actual work.  We have to find a subset of the directories which
12205      allow expressing the file name using references to the directory table
12206      with the least amount of characters.  We do not do an exhaustive search
12207      where we would have to check out every combination of every single
12208      possible prefix.  Instead we use a heuristic which provides nearly optimal
12209      results in most cases and never is much off.  */
12210   saved = XALLOCAVEC (int, ndirs);
12211   savehere = XALLOCAVEC (int, ndirs);
12212
12213   memset (saved, '\0', ndirs * sizeof (saved[0]));
12214   for (i = 0; i < ndirs; i++)
12215     {
12216       int j;
12217       int total;
12218
12219       /* We can always save some space for the current directory.  But this
12220          does not mean it will be enough to justify adding the directory.  */
12221       savehere[i] = dirs[i].length;
12222       total = (savehere[i] - saved[i]) * dirs[i].count;
12223
12224       for (j = i + 1; j < ndirs; j++)
12225         {
12226           savehere[j] = 0;
12227           if (saved[j] < dirs[i].length)
12228             {
12229               /* Determine whether the dirs[i] path is a prefix of the
12230                  dirs[j] path.  */
12231               int k;
12232
12233               k = dirs[j].prefix;
12234               while (k != -1 && k != (int) i)
12235                 k = dirs[k].prefix;
12236
12237               if (k == (int) i)
12238                 {
12239                   /* Yes it is.  We can possibly save some memory by
12240                      writing the filenames in dirs[j] relative to
12241                      dirs[i].  */
12242                   savehere[j] = dirs[i].length;
12243                   total += (savehere[j] - saved[j]) * dirs[j].count;
12244                 }
12245             }
12246         }
12247
12248       /* Check whether we can save enough to justify adding the dirs[i]
12249          directory.  */
12250       if (total > dirs[i].length + 1)
12251         {
12252           /* It's worthwhile adding.  */
12253           for (j = i; j < ndirs; j++)
12254             if (savehere[j] > 0)
12255               {
12256                 /* Remember how much we saved for this directory so far.  */
12257                 saved[j] = savehere[j];
12258
12259                 /* Remember the prefix directory.  */
12260                 dirs[j].dir_idx = i;
12261               }
12262         }
12263     }
12264
12265   /* Emit the directory name table.  */
12266   idx_offset = dirs[0].length > 0 ? 1 : 0;
12267   for (i = 1 - idx_offset; i < ndirs; i++)
12268     dw2_asm_output_nstring (dirs[i].path,
12269                             dirs[i].length
12270                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12271                             "Directory Entry: %#x", i + idx_offset);
12272
12273   dw2_asm_output_data (1, 0, "End directory table");
12274
12275   /* We have to emit them in the order of emitted_number since that's
12276      used in the debug info generation.  To do this efficiently we
12277      generate a back-mapping of the indices first.  */
12278   backmap = XALLOCAVEC (int, numfiles);
12279   for (i = 0; i < numfiles; i++)
12280     backmap[files[i].file_idx->emitted_number - 1] = i;
12281
12282   /* Now write all the file names.  */
12283   for (i = 0; i < numfiles; i++)
12284     {
12285       int file_idx = backmap[i];
12286       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12287
12288 #ifdef VMS_DEBUGGING_INFO
12289 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12290
12291       /* Setting these fields can lead to debugger miscomparisons,
12292          but VMS Debug requires them to be set correctly.  */
12293
12294       int ver;
12295       long long cdt;
12296       long siz;
12297       int maxfilelen = strlen (files[file_idx].path)
12298                                + dirs[dir_idx].length
12299                                + MAX_VMS_VERSION_LEN + 1;
12300       char *filebuf = XALLOCAVEC (char, maxfilelen);
12301
12302       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12303       snprintf (filebuf, maxfilelen, "%s;%d",
12304                 files[file_idx].path + dirs[dir_idx].length, ver);
12305
12306       dw2_asm_output_nstring
12307         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12308
12309       /* Include directory index.  */
12310       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12311
12312       /* Modification time.  */
12313       dw2_asm_output_data_uleb128
12314         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12315           ? cdt : 0,
12316          NULL);
12317
12318       /* File length in bytes.  */
12319       dw2_asm_output_data_uleb128
12320         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12321           ? siz : 0,
12322          NULL);
12323 #else
12324       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12325                               "File Entry: %#x", (unsigned) i + 1);
12326
12327       /* Include directory index.  */
12328       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12329
12330       /* Modification time.  */
12331       dw2_asm_output_data_uleb128 (0, NULL);
12332
12333       /* File length in bytes.  */
12334       dw2_asm_output_data_uleb128 (0, NULL);
12335 #endif /* VMS_DEBUGGING_INFO */
12336     }
12337
12338   dw2_asm_output_data (1, 0, "End file name table");
12339 }
12340
12341
12342 /* Output the source line number correspondence information.  This
12343    information goes into the .debug_line section.  */
12344
12345 static void
12346 output_line_info (void)
12347 {
12348   char l1[20], l2[20], p1[20], p2[20];
12349   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12350   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12351   unsigned opc;
12352   unsigned n_op_args;
12353   unsigned long lt_index;
12354   unsigned long current_line;
12355   long line_offset;
12356   long line_delta;
12357   unsigned long current_file;
12358   unsigned long function;
12359   int ver = dwarf_version;
12360
12361   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12362   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12363   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12364   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12365
12366   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12367     dw2_asm_output_data (4, 0xffffffff,
12368       "Initial length escape value indicating 64-bit DWARF extension");
12369   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12370                         "Length of Source Line Info");
12371   ASM_OUTPUT_LABEL (asm_out_file, l1);
12372
12373   dw2_asm_output_data (2, ver, "DWARF Version");
12374   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12375   ASM_OUTPUT_LABEL (asm_out_file, p1);
12376
12377   /* Define the architecture-dependent minimum instruction length (in
12378    bytes).  In this implementation of DWARF, this field is used for
12379    information purposes only.  Since GCC generates assembly language,
12380    we have no a priori knowledge of how many instruction bytes are
12381    generated for each source line, and therefore can use only the
12382    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12383    commands.  Accordingly, we fix this as `1', which is "correct
12384    enough" for all architectures, and don't let the target override.  */
12385   dw2_asm_output_data (1, 1,
12386                        "Minimum Instruction Length");
12387
12388   if (ver >= 4)
12389     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12390                          "Maximum Operations Per Instruction");
12391   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12392                        "Default is_stmt_start flag");
12393   dw2_asm_output_data (1, DWARF_LINE_BASE,
12394                        "Line Base Value (Special Opcodes)");
12395   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12396                        "Line Range Value (Special Opcodes)");
12397   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12398                        "Special Opcode Base");
12399
12400   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12401     {
12402       switch (opc)
12403         {
12404         case DW_LNS_advance_pc:
12405         case DW_LNS_advance_line:
12406         case DW_LNS_set_file:
12407         case DW_LNS_set_column:
12408         case DW_LNS_fixed_advance_pc:
12409           n_op_args = 1;
12410           break;
12411         default:
12412           n_op_args = 0;
12413           break;
12414         }
12415
12416       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12417                            opc, n_op_args);
12418     }
12419
12420   /* Write out the information about the files we use.  */
12421   output_file_names ();
12422   ASM_OUTPUT_LABEL (asm_out_file, p2);
12423
12424   /* We used to set the address register to the first location in the text
12425      section here, but that didn't accomplish anything since we already
12426      have a line note for the opening brace of the first function.  */
12427
12428   /* Generate the line number to PC correspondence table, encoded as
12429      a series of state machine operations.  */
12430   current_file = 1;
12431   current_line = 1;
12432
12433   if (cfun && in_cold_section_p)
12434     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12435   else
12436     strcpy (prev_line_label, text_section_label);
12437   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12438     {
12439       dw_line_info_ref line_info = &line_info_table[lt_index];
12440
12441 #if 0
12442       /* Disable this optimization for now; GDB wants to see two line notes
12443          at the beginning of a function so it can find the end of the
12444          prologue.  */
12445
12446       /* Don't emit anything for redundant notes.  Just updating the
12447          address doesn't accomplish anything, because we already assume
12448          that anything after the last address is this line.  */
12449       if (line_info->dw_line_num == current_line
12450           && line_info->dw_file_num == current_file)
12451         continue;
12452 #endif
12453
12454       /* Emit debug info for the address of the current line.
12455
12456          Unfortunately, we have little choice here currently, and must always
12457          use the most general form.  GCC does not know the address delta
12458          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12459          attributes which will give an upper bound on the address range.  We
12460          could perhaps use length attributes to determine when it is safe to
12461          use DW_LNS_fixed_advance_pc.  */
12462
12463       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12464       if (0)
12465         {
12466           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12467           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12468                                "DW_LNS_fixed_advance_pc");
12469           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12470         }
12471       else
12472         {
12473           /* This can handle any delta.  This takes
12474              4+DWARF2_ADDR_SIZE bytes.  */
12475           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12476           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12477           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12478           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12479         }
12480
12481       strcpy (prev_line_label, line_label);
12482
12483       /* Emit debug info for the source file of the current line, if
12484          different from the previous line.  */
12485       if (line_info->dw_file_num != current_file)
12486         {
12487           current_file = line_info->dw_file_num;
12488           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12489           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12490         }
12491
12492       /* Emit debug info for the current line number, choosing the encoding
12493          that uses the least amount of space.  */
12494       if (line_info->dw_line_num != current_line)
12495         {
12496           line_offset = line_info->dw_line_num - current_line;
12497           line_delta = line_offset - DWARF_LINE_BASE;
12498           current_line = line_info->dw_line_num;
12499           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12500             /* This can handle deltas from -10 to 234, using the current
12501                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12502                takes 1 byte.  */
12503             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12504                                  "line %lu", current_line);
12505           else
12506             {
12507               /* This can handle any delta.  This takes at least 4 bytes,
12508                  depending on the value being encoded.  */
12509               dw2_asm_output_data (1, DW_LNS_advance_line,
12510                                    "advance to line %lu", current_line);
12511               dw2_asm_output_data_sleb128 (line_offset, NULL);
12512               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12513             }
12514         }
12515       else
12516         /* We still need to start a new row, so output a copy insn.  */
12517         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12518     }
12519
12520   /* Emit debug info for the address of the end of the function.  */
12521   if (0)
12522     {
12523       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12524                            "DW_LNS_fixed_advance_pc");
12525       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12526     }
12527   else
12528     {
12529       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12530       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12531       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12532       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12533     }
12534
12535   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12536   dw2_asm_output_data_uleb128 (1, NULL);
12537   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12538
12539   function = 0;
12540   current_file = 1;
12541   current_line = 1;
12542   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12543     {
12544       dw_separate_line_info_ref line_info
12545         = &separate_line_info_table[lt_index];
12546
12547 #if 0
12548       /* Don't emit anything for redundant notes.  */
12549       if (line_info->dw_line_num == current_line
12550           && line_info->dw_file_num == current_file
12551           && line_info->function == function)
12552         goto cont;
12553 #endif
12554
12555       /* Emit debug info for the address of the current line.  If this is
12556          a new function, or the first line of a function, then we need
12557          to handle it differently.  */
12558       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12559                                    lt_index);
12560       if (function != line_info->function)
12561         {
12562           function = line_info->function;
12563
12564           /* Set the address register to the first line in the function.  */
12565           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12566           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12567           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12568           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12569         }
12570       else
12571         {
12572           /* ??? See the DW_LNS_advance_pc comment above.  */
12573           if (0)
12574             {
12575               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12576                                    "DW_LNS_fixed_advance_pc");
12577               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12578             }
12579           else
12580             {
12581               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12582               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12583               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12584               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12585             }
12586         }
12587
12588       strcpy (prev_line_label, line_label);
12589
12590       /* Emit debug info for the source file of the current line, if
12591          different from the previous line.  */
12592       if (line_info->dw_file_num != current_file)
12593         {
12594           current_file = line_info->dw_file_num;
12595           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12596           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12597         }
12598
12599       /* Emit debug info for the current line number, choosing the encoding
12600          that uses the least amount of space.  */
12601       if (line_info->dw_line_num != current_line)
12602         {
12603           line_offset = line_info->dw_line_num - current_line;
12604           line_delta = line_offset - DWARF_LINE_BASE;
12605           current_line = line_info->dw_line_num;
12606           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12607             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12608                                  "line %lu", current_line);
12609           else
12610             {
12611               dw2_asm_output_data (1, DW_LNS_advance_line,
12612                                    "advance to line %lu", current_line);
12613               dw2_asm_output_data_sleb128 (line_offset, NULL);
12614               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12615             }
12616         }
12617       else
12618         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12619
12620 #if 0
12621     cont:
12622 #endif
12623
12624       lt_index++;
12625
12626       /* If we're done with a function, end its sequence.  */
12627       if (lt_index == separate_line_info_table_in_use
12628           || separate_line_info_table[lt_index].function != function)
12629         {
12630           current_file = 1;
12631           current_line = 1;
12632
12633           /* Emit debug info for the address of the end of the function.  */
12634           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12635           if (0)
12636             {
12637               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12638                                    "DW_LNS_fixed_advance_pc");
12639               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12640             }
12641           else
12642             {
12643               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12644               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12645               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12646               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12647             }
12648
12649           /* Output the marker for the end of this sequence.  */
12650           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12651           dw2_asm_output_data_uleb128 (1, NULL);
12652           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12653         }
12654     }
12655
12656   /* Output the marker for the end of the line number info.  */
12657   ASM_OUTPUT_LABEL (asm_out_file, l2);
12658 }
12659
12660 /* Return the size of the .debug_dcall table for the compilation unit.  */
12661
12662 static unsigned long
12663 size_of_dcall_table (void)
12664 {
12665   unsigned long size;
12666   unsigned int i;
12667   dcall_entry *p;
12668   tree last_poc_decl = NULL;
12669
12670   /* Header:  version + debug info section pointer + pointer size.  */
12671   size = 2 + DWARF_OFFSET_SIZE + 1;
12672
12673   /* Each entry:  code label + DIE offset.  */
12674   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12675     {
12676       gcc_assert (p->targ_die != NULL);
12677       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12678       if (p->poc_decl != last_poc_decl)
12679         {
12680           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12681           gcc_assert (poc_die);
12682           last_poc_decl = p->poc_decl;
12683           if (poc_die)
12684             size += (DWARF_OFFSET_SIZE
12685                      + size_of_uleb128 (poc_die->die_offset));
12686         }
12687       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12688     }
12689
12690   return size;
12691 }
12692
12693 /* Output the direct call table used to disambiguate PC values when
12694    identical function have been merged.  */
12695
12696 static void
12697 output_dcall_table (void)
12698 {
12699   unsigned i;
12700   unsigned long dcall_length = size_of_dcall_table ();
12701   dcall_entry *p;
12702   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12703   tree last_poc_decl = NULL;
12704
12705   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12706     dw2_asm_output_data (4, 0xffffffff,
12707       "Initial length escape value indicating 64-bit DWARF extension");
12708   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12709                        "Length of Direct Call Table");
12710   dw2_asm_output_data (2, 4, "Version number");
12711   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12712                          debug_info_section,
12713                          "Offset of Compilation Unit Info");
12714   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12715
12716   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, p)
12717     {
12718       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12719       if (p->poc_decl != last_poc_decl)
12720         {
12721           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12722           last_poc_decl = p->poc_decl;
12723           if (poc_die)
12724             {
12725               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12726               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12727                                            "Caller DIE offset");
12728             }
12729         }
12730       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12731       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12732       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12733                                    "Callee DIE offset");
12734     }
12735 }
12736 \f
12737 /* Return the size of the .debug_vcall table for the compilation unit.  */
12738
12739 static unsigned long
12740 size_of_vcall_table (void)
12741 {
12742   unsigned long size;
12743   unsigned int i;
12744   vcall_entry *p;
12745
12746   /* Header:  version + pointer size.  */
12747   size = 2 + 1;
12748
12749   /* Each entry:  code label + vtable slot index.  */
12750   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12751     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12752
12753   return size;
12754 }
12755
12756 /* Output the virtual call table used to disambiguate PC values when
12757    identical function have been merged.  */
12758
12759 static void
12760 output_vcall_table (void)
12761 {
12762   unsigned i;
12763   unsigned long vcall_length = size_of_vcall_table ();
12764   vcall_entry *p;
12765   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12766
12767   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12768     dw2_asm_output_data (4, 0xffffffff,
12769       "Initial length escape value indicating 64-bit DWARF extension");
12770   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12771                        "Length of Virtual Call Table");
12772   dw2_asm_output_data (2, 4, "Version number");
12773   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12774
12775   FOR_EACH_VEC_ELT (vcall_entry, vcall_table, i, p)
12776     {
12777       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12778       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12779       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12780     }
12781 }
12782 \f
12783 /* Given a pointer to a tree node for some base type, return a pointer to
12784    a DIE that describes the given type.
12785
12786    This routine must only be called for GCC type nodes that correspond to
12787    Dwarf base (fundamental) types.  */
12788
12789 static dw_die_ref
12790 base_type_die (tree type)
12791 {
12792   dw_die_ref base_type_result;
12793   enum dwarf_type encoding;
12794
12795   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12796     return 0;
12797
12798   /* If this is a subtype that should not be emitted as a subrange type,
12799      use the base type.  See subrange_type_for_debug_p.  */
12800   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12801     type = TREE_TYPE (type);
12802
12803   switch (TREE_CODE (type))
12804     {
12805     case INTEGER_TYPE:
12806       if ((dwarf_version >= 4 || !dwarf_strict)
12807           && TYPE_NAME (type)
12808           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12809           && DECL_IS_BUILTIN (TYPE_NAME (type))
12810           && DECL_NAME (TYPE_NAME (type)))
12811         {
12812           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12813           if (strcmp (name, "char16_t") == 0
12814               || strcmp (name, "char32_t") == 0)
12815             {
12816               encoding = DW_ATE_UTF;
12817               break;
12818             }
12819         }
12820       if (TYPE_STRING_FLAG (type))
12821         {
12822           if (TYPE_UNSIGNED (type))
12823             encoding = DW_ATE_unsigned_char;
12824           else
12825             encoding = DW_ATE_signed_char;
12826         }
12827       else if (TYPE_UNSIGNED (type))
12828         encoding = DW_ATE_unsigned;
12829       else
12830         encoding = DW_ATE_signed;
12831       break;
12832
12833     case REAL_TYPE:
12834       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12835         {
12836           if (dwarf_version >= 3 || !dwarf_strict)
12837             encoding = DW_ATE_decimal_float;
12838           else
12839             encoding = DW_ATE_lo_user;
12840         }
12841       else
12842         encoding = DW_ATE_float;
12843       break;
12844
12845     case FIXED_POINT_TYPE:
12846       if (!(dwarf_version >= 3 || !dwarf_strict))
12847         encoding = DW_ATE_lo_user;
12848       else if (TYPE_UNSIGNED (type))
12849         encoding = DW_ATE_unsigned_fixed;
12850       else
12851         encoding = DW_ATE_signed_fixed;
12852       break;
12853
12854       /* Dwarf2 doesn't know anything about complex ints, so use
12855          a user defined type for it.  */
12856     case COMPLEX_TYPE:
12857       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12858         encoding = DW_ATE_complex_float;
12859       else
12860         encoding = DW_ATE_lo_user;
12861       break;
12862
12863     case BOOLEAN_TYPE:
12864       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12865       encoding = DW_ATE_boolean;
12866       break;
12867
12868     default:
12869       /* No other TREE_CODEs are Dwarf fundamental types.  */
12870       gcc_unreachable ();
12871     }
12872
12873   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12874
12875   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12876                    int_size_in_bytes (type));
12877   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12878
12879   return base_type_result;
12880 }
12881
12882 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12883    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12884
12885 static inline int
12886 is_base_type (tree type)
12887 {
12888   switch (TREE_CODE (type))
12889     {
12890     case ERROR_MARK:
12891     case VOID_TYPE:
12892     case INTEGER_TYPE:
12893     case REAL_TYPE:
12894     case FIXED_POINT_TYPE:
12895     case COMPLEX_TYPE:
12896     case BOOLEAN_TYPE:
12897       return 1;
12898
12899     case ARRAY_TYPE:
12900     case RECORD_TYPE:
12901     case UNION_TYPE:
12902     case QUAL_UNION_TYPE:
12903     case ENUMERAL_TYPE:
12904     case FUNCTION_TYPE:
12905     case METHOD_TYPE:
12906     case POINTER_TYPE:
12907     case REFERENCE_TYPE:
12908     case NULLPTR_TYPE:
12909     case OFFSET_TYPE:
12910     case LANG_TYPE:
12911     case VECTOR_TYPE:
12912       return 0;
12913
12914     default:
12915       gcc_unreachable ();
12916     }
12917
12918   return 0;
12919 }
12920
12921 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12922    node, return the size in bits for the type if it is a constant, or else
12923    return the alignment for the type if the type's size is not constant, or
12924    else return BITS_PER_WORD if the type actually turns out to be an
12925    ERROR_MARK node.  */
12926
12927 static inline unsigned HOST_WIDE_INT
12928 simple_type_size_in_bits (const_tree type)
12929 {
12930   if (TREE_CODE (type) == ERROR_MARK)
12931     return BITS_PER_WORD;
12932   else if (TYPE_SIZE (type) == NULL_TREE)
12933     return 0;
12934   else if (host_integerp (TYPE_SIZE (type), 1))
12935     return tree_low_cst (TYPE_SIZE (type), 1);
12936   else
12937     return TYPE_ALIGN (type);
12938 }
12939
12940 /* Similarly, but return a double_int instead of UHWI.  */
12941
12942 static inline double_int
12943 double_int_type_size_in_bits (const_tree type)
12944 {
12945   if (TREE_CODE (type) == ERROR_MARK)
12946     return uhwi_to_double_int (BITS_PER_WORD);
12947   else if (TYPE_SIZE (type) == NULL_TREE)
12948     return double_int_zero;
12949   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12950     return tree_to_double_int (TYPE_SIZE (type));
12951   else
12952     return uhwi_to_double_int (TYPE_ALIGN (type));
12953 }
12954
12955 /*  Given a pointer to a tree node for a subrange type, return a pointer
12956     to a DIE that describes the given type.  */
12957
12958 static dw_die_ref
12959 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12960 {
12961   dw_die_ref subrange_die;
12962   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12963
12964   if (context_die == NULL)
12965     context_die = comp_unit_die ();
12966
12967   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12968
12969   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12970     {
12971       /* The size of the subrange type and its base type do not match,
12972          so we need to generate a size attribute for the subrange type.  */
12973       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12974     }
12975
12976   if (low)
12977     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12978   if (high)
12979     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12980
12981   return subrange_die;
12982 }
12983
12984 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12985    entry that chains various modifiers in front of the given type.  */
12986
12987 static dw_die_ref
12988 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12989                    dw_die_ref context_die)
12990 {
12991   enum tree_code code = TREE_CODE (type);
12992   dw_die_ref mod_type_die;
12993   dw_die_ref sub_die = NULL;
12994   tree item_type = NULL;
12995   tree qualified_type;
12996   tree name, low, high;
12997
12998   if (code == ERROR_MARK)
12999     return NULL;
13000
13001   /* See if we already have the appropriately qualified variant of
13002      this type.  */
13003   qualified_type
13004     = get_qualified_type (type,
13005                           ((is_const_type ? TYPE_QUAL_CONST : 0)
13006                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
13007
13008   if (qualified_type == sizetype
13009       && TYPE_NAME (qualified_type)
13010       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13011     {
13012       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13013
13014       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13015                            && TYPE_PRECISION (t)
13016                            == TYPE_PRECISION (qualified_type)
13017                            && TYPE_UNSIGNED (t)
13018                            == TYPE_UNSIGNED (qualified_type));
13019       qualified_type = t;
13020     }
13021
13022   /* If we do, then we can just use its DIE, if it exists.  */
13023   if (qualified_type)
13024     {
13025       mod_type_die = lookup_type_die (qualified_type);
13026       if (mod_type_die)
13027         return mod_type_die;
13028     }
13029
13030   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13031
13032   /* Handle C typedef types.  */
13033   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
13034       && !DECL_ARTIFICIAL (name))
13035     {
13036       tree dtype = TREE_TYPE (name);
13037
13038       if (qualified_type == dtype)
13039         {
13040           /* For a named type, use the typedef.  */
13041           gen_type_die (qualified_type, context_die);
13042           return lookup_type_die (qualified_type);
13043         }
13044       else if (is_const_type < TYPE_READONLY (dtype)
13045                || is_volatile_type < TYPE_VOLATILE (dtype)
13046                || (is_const_type <= TYPE_READONLY (dtype)
13047                    && is_volatile_type <= TYPE_VOLATILE (dtype)
13048                    && DECL_ORIGINAL_TYPE (name) != type))
13049         /* cv-unqualified version of named type.  Just use the unnamed
13050            type to which it refers.  */
13051         return modified_type_die (DECL_ORIGINAL_TYPE (name),
13052                                   is_const_type, is_volatile_type,
13053                                   context_die);
13054       /* Else cv-qualified version of named type; fall through.  */
13055     }
13056
13057   if (is_const_type
13058       /* If both is_const_type and is_volatile_type, prefer the path
13059          which leads to a qualified type.  */
13060       && (!is_volatile_type
13061           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
13062           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
13063     {
13064       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
13065       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
13066     }
13067   else if (is_volatile_type)
13068     {
13069       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
13070       sub_die = modified_type_die (type, is_const_type, 0, context_die);
13071     }
13072   else if (code == POINTER_TYPE)
13073     {
13074       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
13075       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13076                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13077       item_type = TREE_TYPE (type);
13078       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13079         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13080                          TYPE_ADDR_SPACE (item_type));
13081     }
13082   else if (code == REFERENCE_TYPE)
13083     {
13084       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13085         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
13086                                 type);
13087       else
13088         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
13089       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13090                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13091       item_type = TREE_TYPE (type);
13092       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13093         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13094                          TYPE_ADDR_SPACE (item_type));
13095     }
13096   else if (code == INTEGER_TYPE
13097            && TREE_TYPE (type) != NULL_TREE
13098            && subrange_type_for_debug_p (type, &low, &high))
13099     {
13100       mod_type_die = subrange_type_die (type, low, high, context_die);
13101       item_type = TREE_TYPE (type);
13102     }
13103   else if (is_base_type (type))
13104     mod_type_die = base_type_die (type);
13105   else
13106     {
13107       gen_type_die (type, context_die);
13108
13109       /* We have to get the type_main_variant here (and pass that to the
13110          `lookup_type_die' routine) because the ..._TYPE node we have
13111          might simply be a *copy* of some original type node (where the
13112          copy was created to help us keep track of typedef names) and
13113          that copy might have a different TYPE_UID from the original
13114          ..._TYPE node.  */
13115       if (TREE_CODE (type) != VECTOR_TYPE)
13116         return lookup_type_die (type_main_variant (type));
13117       else
13118         /* Vectors have the debugging information in the type,
13119            not the main variant.  */
13120         return lookup_type_die (type);
13121     }
13122
13123   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13124      don't output a DW_TAG_typedef, since there isn't one in the
13125      user's program; just attach a DW_AT_name to the type.
13126      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13127      if the base type already has the same name.  */
13128   if (name
13129       && ((TREE_CODE (name) != TYPE_DECL
13130            && (qualified_type == TYPE_MAIN_VARIANT (type)
13131                || (!is_const_type && !is_volatile_type)))
13132           || (TREE_CODE (name) == TYPE_DECL
13133               && TREE_TYPE (name) == qualified_type
13134               && DECL_NAME (name))))
13135     {
13136       if (TREE_CODE (name) == TYPE_DECL)
13137         /* Could just call add_name_and_src_coords_attributes here,
13138            but since this is a builtin type it doesn't have any
13139            useful source coordinates anyway.  */
13140         name = DECL_NAME (name);
13141       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13142     }
13143   /* This probably indicates a bug.  */
13144   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13145     add_name_attribute (mod_type_die, "__unknown__");
13146
13147   if (qualified_type)
13148     equate_type_number_to_die (qualified_type, mod_type_die);
13149
13150   if (item_type)
13151     /* We must do this after the equate_type_number_to_die call, in case
13152        this is a recursive type.  This ensures that the modified_type_die
13153        recursion will terminate even if the type is recursive.  Recursive
13154        types are possible in Ada.  */
13155     sub_die = modified_type_die (item_type,
13156                                  TYPE_READONLY (item_type),
13157                                  TYPE_VOLATILE (item_type),
13158                                  context_die);
13159
13160   if (sub_die != NULL)
13161     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13162
13163   return mod_type_die;
13164 }
13165
13166 /* Generate DIEs for the generic parameters of T.
13167    T must be either a generic type or a generic function.
13168    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13169
13170 static void
13171 gen_generic_params_dies (tree t)
13172 {
13173   tree parms, args;
13174   int parms_num, i;
13175   dw_die_ref die = NULL;
13176
13177   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13178     return;
13179
13180   if (TYPE_P (t))
13181     die = lookup_type_die (t);
13182   else if (DECL_P (t))
13183     die = lookup_decl_die (t);
13184
13185   gcc_assert (die);
13186
13187   parms = lang_hooks.get_innermost_generic_parms (t);
13188   if (!parms)
13189     /* T has no generic parameter. It means T is neither a generic type
13190        or function. End of story.  */
13191     return;
13192
13193   parms_num = TREE_VEC_LENGTH (parms);
13194   args = lang_hooks.get_innermost_generic_args (t);
13195   for (i = 0; i < parms_num; i++)
13196     {
13197       tree parm, arg, arg_pack_elems;
13198
13199       parm = TREE_VEC_ELT (parms, i);
13200       arg = TREE_VEC_ELT (args, i);
13201       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13202       gcc_assert (parm && TREE_VALUE (parm) && arg);
13203
13204       if (parm && TREE_VALUE (parm) && arg)
13205         {
13206           /* If PARM represents a template parameter pack,
13207              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13208              by DW_TAG_template_*_parameter DIEs for the argument
13209              pack elements of ARG. Note that ARG would then be
13210              an argument pack.  */
13211           if (arg_pack_elems)
13212             template_parameter_pack_die (TREE_VALUE (parm),
13213                                          arg_pack_elems,
13214                                          die);
13215           else
13216             generic_parameter_die (TREE_VALUE (parm), arg,
13217                                    true /* Emit DW_AT_name */, die);
13218         }
13219     }
13220 }
13221
13222 /* Create and return a DIE for PARM which should be
13223    the representation of a generic type parameter.
13224    For instance, in the C++ front end, PARM would be a template parameter.
13225    ARG is the argument to PARM.
13226    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13227    name of the PARM.
13228    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13229    as a child node.  */
13230
13231 static dw_die_ref
13232 generic_parameter_die (tree parm, tree arg,
13233                        bool emit_name_p,
13234                        dw_die_ref parent_die)
13235 {
13236   dw_die_ref tmpl_die = NULL;
13237   const char *name = NULL;
13238
13239   if (!parm || !DECL_NAME (parm) || !arg)
13240     return NULL;
13241
13242   /* We support non-type generic parameters and arguments,
13243      type generic parameters and arguments, as well as
13244      generic generic parameters (a.k.a. template template parameters in C++)
13245      and arguments.  */
13246   if (TREE_CODE (parm) == PARM_DECL)
13247     /* PARM is a nontype generic parameter  */
13248     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13249   else if (TREE_CODE (parm) == TYPE_DECL)
13250     /* PARM is a type generic parameter.  */
13251     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13252   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13253     /* PARM is a generic generic parameter.
13254        Its DIE is a GNU extension. It shall have a
13255        DW_AT_name attribute to represent the name of the template template
13256        parameter, and a DW_AT_GNU_template_name attribute to represent the
13257        name of the template template argument.  */
13258     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13259                         parent_die, parm);
13260   else
13261     gcc_unreachable ();
13262
13263   if (tmpl_die)
13264     {
13265       tree tmpl_type;
13266
13267       /* If PARM is a generic parameter pack, it means we are
13268          emitting debug info for a template argument pack element.
13269          In other terms, ARG is a template argument pack element.
13270          In that case, we don't emit any DW_AT_name attribute for
13271          the die.  */
13272       if (emit_name_p)
13273         {
13274           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13275           gcc_assert (name);
13276           add_AT_string (tmpl_die, DW_AT_name, name);
13277         }
13278
13279       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13280         {
13281           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13282              TMPL_DIE should have a child DW_AT_type attribute that is set
13283              to the type of the argument to PARM, which is ARG.
13284              If PARM is a type generic parameter, TMPL_DIE should have a
13285              child DW_AT_type that is set to ARG.  */
13286           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13287           add_type_attribute (tmpl_die, tmpl_type, 0,
13288                               TREE_THIS_VOLATILE (tmpl_type),
13289                               parent_die);
13290         }
13291       else
13292         {
13293           /* So TMPL_DIE is a DIE representing a
13294              a generic generic template parameter, a.k.a template template
13295              parameter in C++ and arg is a template.  */
13296
13297           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13298              to the name of the argument.  */
13299           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13300           if (name)
13301             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13302         }
13303
13304       if (TREE_CODE (parm) == PARM_DECL)
13305         /* So PARM is a non-type generic parameter.
13306            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13307            attribute of TMPL_DIE which value represents the value
13308            of ARG.
13309            We must be careful here:
13310            The value of ARG might reference some function decls.
13311            We might currently be emitting debug info for a generic
13312            type and types are emitted before function decls, we don't
13313            know if the function decls referenced by ARG will actually be
13314            emitted after cgraph computations.
13315            So must defer the generation of the DW_AT_const_value to
13316            after cgraph is ready.  */
13317         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13318     }
13319
13320   return tmpl_die;
13321 }
13322
13323 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13324    PARM_PACK must be a template parameter pack. The returned DIE
13325    will be child DIE of PARENT_DIE.  */
13326
13327 static dw_die_ref
13328 template_parameter_pack_die (tree parm_pack,
13329                              tree parm_pack_args,
13330                              dw_die_ref parent_die)
13331 {
13332   dw_die_ref die;
13333   int j;
13334
13335   gcc_assert (parent_die && parm_pack);
13336
13337   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13338   add_name_and_src_coords_attributes (die, parm_pack);
13339   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13340     generic_parameter_die (parm_pack,
13341                            TREE_VEC_ELT (parm_pack_args, j),
13342                            false /* Don't emit DW_AT_name */,
13343                            die);
13344   return die;
13345 }
13346
13347 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13348    an enumerated type.  */
13349
13350 static inline int
13351 type_is_enum (const_tree type)
13352 {
13353   return TREE_CODE (type) == ENUMERAL_TYPE;
13354 }
13355
13356 /* Return the DBX register number described by a given RTL node.  */
13357
13358 static unsigned int
13359 dbx_reg_number (const_rtx rtl)
13360 {
13361   unsigned regno = REGNO (rtl);
13362
13363   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13364
13365 #ifdef LEAF_REG_REMAP
13366   if (current_function_uses_only_leaf_regs)
13367     {
13368       int leaf_reg = LEAF_REG_REMAP (regno);
13369       if (leaf_reg != -1)
13370         regno = (unsigned) leaf_reg;
13371     }
13372 #endif
13373
13374   return DBX_REGISTER_NUMBER (regno);
13375 }
13376
13377 /* Optionally add a DW_OP_piece term to a location description expression.
13378    DW_OP_piece is only added if the location description expression already
13379    doesn't end with DW_OP_piece.  */
13380
13381 static void
13382 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13383 {
13384   dw_loc_descr_ref loc;
13385
13386   if (*list_head != NULL)
13387     {
13388       /* Find the end of the chain.  */
13389       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13390         ;
13391
13392       if (loc->dw_loc_opc != DW_OP_piece)
13393         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13394     }
13395 }
13396
13397 /* Return a location descriptor that designates a machine register or
13398    zero if there is none.  */
13399
13400 static dw_loc_descr_ref
13401 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13402 {
13403   rtx regs;
13404
13405   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13406     return 0;
13407
13408   /* We only use "frame base" when we're sure we're talking about the
13409      post-prologue local stack frame.  We do this by *not* running
13410      register elimination until this point, and recognizing the special
13411      argument pointer and soft frame pointer rtx's.
13412      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13413   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13414       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13415     {
13416       dw_loc_descr_ref result = NULL;
13417
13418       if (dwarf_version >= 4 || !dwarf_strict)
13419         {
13420           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13421           if (result)
13422             add_loc_descr (&result,
13423                            new_loc_descr (DW_OP_stack_value, 0, 0));
13424         }
13425       return result;
13426     }
13427
13428   regs = targetm.dwarf_register_span (rtl);
13429
13430   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13431     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13432   else
13433     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13434 }
13435
13436 /* Return a location descriptor that designates a machine register for
13437    a given hard register number.  */
13438
13439 static dw_loc_descr_ref
13440 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13441 {
13442   dw_loc_descr_ref reg_loc_descr;
13443
13444   if (regno <= 31)
13445     reg_loc_descr
13446       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13447   else
13448     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13449
13450   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13451     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13452
13453   return reg_loc_descr;
13454 }
13455
13456 /* Given an RTL of a register, return a location descriptor that
13457    designates a value that spans more than one register.  */
13458
13459 static dw_loc_descr_ref
13460 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13461                              enum var_init_status initialized)
13462 {
13463   int nregs, size, i;
13464   unsigned reg;
13465   dw_loc_descr_ref loc_result = NULL;
13466
13467   reg = REGNO (rtl);
13468 #ifdef LEAF_REG_REMAP
13469   if (current_function_uses_only_leaf_regs)
13470     {
13471       int leaf_reg = LEAF_REG_REMAP (reg);
13472       if (leaf_reg != -1)
13473         reg = (unsigned) leaf_reg;
13474     }
13475 #endif
13476   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13477   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13478
13479   /* Simple, contiguous registers.  */
13480   if (regs == NULL_RTX)
13481     {
13482       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13483
13484       loc_result = NULL;
13485       while (nregs--)
13486         {
13487           dw_loc_descr_ref t;
13488
13489           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13490                                       VAR_INIT_STATUS_INITIALIZED);
13491           add_loc_descr (&loc_result, t);
13492           add_loc_descr_op_piece (&loc_result, size);
13493           ++reg;
13494         }
13495       return loc_result;
13496     }
13497
13498   /* Now onto stupid register sets in non contiguous locations.  */
13499
13500   gcc_assert (GET_CODE (regs) == PARALLEL);
13501
13502   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13503   loc_result = NULL;
13504
13505   for (i = 0; i < XVECLEN (regs, 0); ++i)
13506     {
13507       dw_loc_descr_ref t;
13508
13509       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13510                                   VAR_INIT_STATUS_INITIALIZED);
13511       add_loc_descr (&loc_result, t);
13512       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13513       add_loc_descr_op_piece (&loc_result, size);
13514     }
13515
13516   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13517     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13518   return loc_result;
13519 }
13520
13521 /* Return a location descriptor that designates a constant.  */
13522
13523 static dw_loc_descr_ref
13524 int_loc_descriptor (HOST_WIDE_INT i)
13525 {
13526   enum dwarf_location_atom op;
13527
13528   /* Pick the smallest representation of a constant, rather than just
13529      defaulting to the LEB encoding.  */
13530   if (i >= 0)
13531     {
13532       if (i <= 31)
13533         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13534       else if (i <= 0xff)
13535         op = DW_OP_const1u;
13536       else if (i <= 0xffff)
13537         op = DW_OP_const2u;
13538       else if (HOST_BITS_PER_WIDE_INT == 32
13539                || i <= 0xffffffff)
13540         op = DW_OP_const4u;
13541       else
13542         op = DW_OP_constu;
13543     }
13544   else
13545     {
13546       if (i >= -0x80)
13547         op = DW_OP_const1s;
13548       else if (i >= -0x8000)
13549         op = DW_OP_const2s;
13550       else if (HOST_BITS_PER_WIDE_INT == 32
13551                || i >= -0x80000000)
13552         op = DW_OP_const4s;
13553       else
13554         op = DW_OP_consts;
13555     }
13556
13557   return new_loc_descr (op, i, 0);
13558 }
13559
13560 /* Return loc description representing "address" of integer value.
13561    This can appear only as toplevel expression.  */
13562
13563 static dw_loc_descr_ref
13564 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13565 {
13566   int litsize;
13567   dw_loc_descr_ref loc_result = NULL;
13568
13569   if (!(dwarf_version >= 4 || !dwarf_strict))
13570     return NULL;
13571
13572   if (i >= 0)
13573     {
13574       if (i <= 31)
13575         litsize = 1;
13576       else if (i <= 0xff)
13577         litsize = 2;
13578       else if (i <= 0xffff)
13579         litsize = 3;
13580       else if (HOST_BITS_PER_WIDE_INT == 32
13581                || i <= 0xffffffff)
13582         litsize = 5;
13583       else
13584         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13585     }
13586   else
13587     {
13588       if (i >= -0x80)
13589         litsize = 2;
13590       else if (i >= -0x8000)
13591         litsize = 3;
13592       else if (HOST_BITS_PER_WIDE_INT == 32
13593                || i >= -0x80000000)
13594         litsize = 5;
13595       else
13596         litsize = 1 + size_of_sleb128 (i);
13597     }
13598   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13599      is more compact.  For DW_OP_stack_value we need:
13600      litsize + 1 (DW_OP_stack_value)
13601      and for DW_OP_implicit_value:
13602      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13603   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13604     {
13605       loc_result = int_loc_descriptor (i);
13606       add_loc_descr (&loc_result,
13607                      new_loc_descr (DW_OP_stack_value, 0, 0));
13608       return loc_result;
13609     }
13610
13611   loc_result = new_loc_descr (DW_OP_implicit_value,
13612                               size, 0);
13613   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13614   loc_result->dw_loc_oprnd2.v.val_int = i;
13615   return loc_result;
13616 }
13617
13618 /* Return a location descriptor that designates a base+offset location.  */
13619
13620 static dw_loc_descr_ref
13621 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13622                  enum var_init_status initialized)
13623 {
13624   unsigned int regno;
13625   dw_loc_descr_ref result;
13626   dw_fde_ref fde = current_fde ();
13627
13628   /* We only use "frame base" when we're sure we're talking about the
13629      post-prologue local stack frame.  We do this by *not* running
13630      register elimination until this point, and recognizing the special
13631      argument pointer and soft frame pointer rtx's.  */
13632   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13633     {
13634       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13635
13636       if (elim != reg)
13637         {
13638           if (GET_CODE (elim) == PLUS)
13639             {
13640               offset += INTVAL (XEXP (elim, 1));
13641               elim = XEXP (elim, 0);
13642             }
13643           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13644                        && (elim == hard_frame_pointer_rtx
13645                            || elim == stack_pointer_rtx))
13646                       || elim == (frame_pointer_needed
13647                                   ? hard_frame_pointer_rtx
13648                                   : stack_pointer_rtx));
13649
13650           /* If drap register is used to align stack, use frame
13651              pointer + offset to access stack variables.  If stack
13652              is aligned without drap, use stack pointer + offset to
13653              access stack variables.  */
13654           if (crtl->stack_realign_tried
13655               && reg == frame_pointer_rtx)
13656             {
13657               int base_reg
13658                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13659                                       ? HARD_FRAME_POINTER_REGNUM
13660                                       : STACK_POINTER_REGNUM);
13661               return new_reg_loc_descr (base_reg, offset);
13662             }
13663
13664           offset += frame_pointer_fb_offset;
13665           return new_loc_descr (DW_OP_fbreg, offset, 0);
13666         }
13667     }
13668   else if (!optimize
13669            && fde
13670            && (fde->drap_reg == REGNO (reg)
13671                || fde->vdrap_reg == REGNO (reg)))
13672     {
13673       /* Use cfa+offset to represent the location of arguments passed
13674          on the stack when drap is used to align stack.
13675          Only do this when not optimizing, for optimized code var-tracking
13676          is supposed to track where the arguments live and the register
13677          used as vdrap or drap in some spot might be used for something
13678          else in other part of the routine.  */
13679       return new_loc_descr (DW_OP_fbreg, offset, 0);
13680     }
13681
13682   regno = dbx_reg_number (reg);
13683   if (regno <= 31)
13684     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13685                             offset, 0);
13686   else
13687     result = new_loc_descr (DW_OP_bregx, regno, offset);
13688
13689   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13690     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13691
13692   return result;
13693 }
13694
13695 /* Return true if this RTL expression describes a base+offset calculation.  */
13696
13697 static inline int
13698 is_based_loc (const_rtx rtl)
13699 {
13700   return (GET_CODE (rtl) == PLUS
13701           && ((REG_P (XEXP (rtl, 0))
13702                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13703                && CONST_INT_P (XEXP (rtl, 1)))));
13704 }
13705
13706 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13707    failed.  */
13708
13709 static dw_loc_descr_ref
13710 tls_mem_loc_descriptor (rtx mem)
13711 {
13712   tree base;
13713   dw_loc_descr_ref loc_result;
13714
13715   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13716     return NULL;
13717
13718   base = get_base_address (MEM_EXPR (mem));
13719   if (base == NULL
13720       || TREE_CODE (base) != VAR_DECL
13721       || !DECL_THREAD_LOCAL_P (base))
13722     return NULL;
13723
13724   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13725   if (loc_result == NULL)
13726     return NULL;
13727
13728   if (INTVAL (MEM_OFFSET (mem)))
13729     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13730
13731   return loc_result;
13732 }
13733
13734 /* Output debug info about reason why we failed to expand expression as dwarf
13735    expression.  */
13736
13737 static void
13738 expansion_failed (tree expr, rtx rtl, char const *reason)
13739 {
13740   if (dump_file && (dump_flags & TDF_DETAILS))
13741     {
13742       fprintf (dump_file, "Failed to expand as dwarf: ");
13743       if (expr)
13744         print_generic_expr (dump_file, expr, dump_flags);
13745       if (rtl)
13746         {
13747           fprintf (dump_file, "\n");
13748           print_rtl (dump_file, rtl);
13749         }
13750       fprintf (dump_file, "\nReason: %s\n", reason);
13751     }
13752 }
13753
13754 /* Helper function for const_ok_for_output, called either directly
13755    or via for_each_rtx.  */
13756
13757 static int
13758 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13759 {
13760   rtx rtl = *rtlp;
13761
13762   if (GET_CODE (rtl) == UNSPEC)
13763     {
13764       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13765          we can't express it in the debug info.  */
13766 #ifdef ENABLE_CHECKING
13767       /* Don't complain about TLS UNSPECs, those are just too hard to
13768          delegitimize.  */
13769       if (XVECLEN (rtl, 0) != 1
13770           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13771           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13772           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13773           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13774         inform (current_function_decl
13775                 ? DECL_SOURCE_LOCATION (current_function_decl)
13776                 : UNKNOWN_LOCATION,
13777                 "non-delegitimized UNSPEC %d found in variable location",
13778                 XINT (rtl, 1));
13779 #endif
13780       expansion_failed (NULL_TREE, rtl,
13781                         "UNSPEC hasn't been delegitimized.\n");
13782       return 1;
13783     }
13784
13785   if (GET_CODE (rtl) != SYMBOL_REF)
13786     return 0;
13787
13788   if (CONSTANT_POOL_ADDRESS_P (rtl))
13789     {
13790       bool marked;
13791       get_pool_constant_mark (rtl, &marked);
13792       /* If all references to this pool constant were optimized away,
13793          it was not output and thus we can't represent it.  */
13794       if (!marked)
13795         {
13796           expansion_failed (NULL_TREE, rtl,
13797                             "Constant was removed from constant pool.\n");
13798           return 1;
13799         }
13800     }
13801
13802   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13803     return 1;
13804
13805   /* Avoid references to external symbols in debug info, on several targets
13806      the linker might even refuse to link when linking a shared library,
13807      and in many other cases the relocations for .debug_info/.debug_loc are
13808      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13809      to be defined within the same shared library or executable are fine.  */
13810   if (SYMBOL_REF_EXTERNAL_P (rtl))
13811     {
13812       tree decl = SYMBOL_REF_DECL (rtl);
13813
13814       if (decl == NULL || !targetm.binds_local_p (decl))
13815         {
13816           expansion_failed (NULL_TREE, rtl,
13817                             "Symbol not defined in current TU.\n");
13818           return 1;
13819         }
13820     }
13821
13822   return 0;
13823 }
13824
13825 /* Return true if constant RTL can be emitted in DW_OP_addr or
13826    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13827    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13828
13829 static bool
13830 const_ok_for_output (rtx rtl)
13831 {
13832   if (GET_CODE (rtl) == SYMBOL_REF)
13833     return const_ok_for_output_1 (&rtl, NULL) == 0;
13834
13835   if (GET_CODE (rtl) == CONST)
13836     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13837
13838   return true;
13839 }
13840
13841 /* The following routine converts the RTL for a variable or parameter
13842    (resident in memory) into an equivalent Dwarf representation of a
13843    mechanism for getting the address of that same variable onto the top of a
13844    hypothetical "address evaluation" stack.
13845
13846    When creating memory location descriptors, we are effectively transforming
13847    the RTL for a memory-resident object into its Dwarf postfix expression
13848    equivalent.  This routine recursively descends an RTL tree, turning
13849    it into Dwarf postfix code as it goes.
13850
13851    MODE is the mode of the memory reference, needed to handle some
13852    autoincrement addressing modes.
13853
13854    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13855    location list for RTL.
13856
13857    Return 0 if we can't represent the location.  */
13858
13859 static dw_loc_descr_ref
13860 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13861                     enum var_init_status initialized)
13862 {
13863   dw_loc_descr_ref mem_loc_result = NULL;
13864   enum dwarf_location_atom op;
13865   dw_loc_descr_ref op0, op1;
13866
13867   /* Note that for a dynamically sized array, the location we will generate a
13868      description of here will be the lowest numbered location which is
13869      actually within the array.  That's *not* necessarily the same as the
13870      zeroth element of the array.  */
13871
13872   rtl = targetm.delegitimize_address (rtl);
13873
13874   switch (GET_CODE (rtl))
13875     {
13876     case POST_INC:
13877     case POST_DEC:
13878     case POST_MODIFY:
13879       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13880
13881     case SUBREG:
13882       /* The case of a subreg may arise when we have a local (register)
13883          variable or a formal (register) parameter which doesn't quite fill
13884          up an entire register.  For now, just assume that it is
13885          legitimate to make the Dwarf info refer to the whole register which
13886          contains the given subreg.  */
13887       if (!subreg_lowpart_p (rtl))
13888         break;
13889       rtl = SUBREG_REG (rtl);
13890       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13891         break;
13892       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13893         break;
13894       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13895       break;
13896
13897     case REG:
13898       /* Whenever a register number forms a part of the description of the
13899          method for calculating the (dynamic) address of a memory resident
13900          object, DWARF rules require the register number be referred to as
13901          a "base register".  This distinction is not based in any way upon
13902          what category of register the hardware believes the given register
13903          belongs to.  This is strictly DWARF terminology we're dealing with
13904          here. Note that in cases where the location of a memory-resident
13905          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13906          OP_CONST (0)) the actual DWARF location descriptor that we generate
13907          may just be OP_BASEREG (basereg).  This may look deceptively like
13908          the object in question was allocated to a register (rather than in
13909          memory) so DWARF consumers need to be aware of the subtle
13910          distinction between OP_REG and OP_BASEREG.  */
13911       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13912         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13913       else if (stack_realign_drap
13914                && crtl->drap_reg
13915                && crtl->args.internal_arg_pointer == rtl
13916                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13917         {
13918           /* If RTL is internal_arg_pointer, which has been optimized
13919              out, use DRAP instead.  */
13920           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13921                                             VAR_INIT_STATUS_INITIALIZED);
13922         }
13923       break;
13924
13925     case SIGN_EXTEND:
13926     case ZERO_EXTEND:
13927       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13928                                 VAR_INIT_STATUS_INITIALIZED);
13929       if (op0 == 0)
13930         break;
13931       else
13932         {
13933           int shift = DWARF2_ADDR_SIZE
13934                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13935           shift *= BITS_PER_UNIT;
13936           if (GET_CODE (rtl) == SIGN_EXTEND)
13937             op = DW_OP_shra;
13938           else
13939             op = DW_OP_shr;
13940           mem_loc_result = op0;
13941           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13942           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13943           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13944           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13945         }
13946       break;
13947
13948     case MEM:
13949       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13950                                            VAR_INIT_STATUS_INITIALIZED);
13951       if (mem_loc_result == NULL)
13952         mem_loc_result = tls_mem_loc_descriptor (rtl);
13953       if (mem_loc_result != 0)
13954         {
13955           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13956             {
13957               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13958               return 0;
13959             }
13960           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13961             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13962           else
13963             add_loc_descr (&mem_loc_result,
13964                            new_loc_descr (DW_OP_deref_size,
13965                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13966         }
13967       else
13968         {
13969           rtx new_rtl = avoid_constant_pool_reference (rtl);
13970           if (new_rtl != rtl)
13971             return mem_loc_descriptor (new_rtl, mode, initialized);
13972         }
13973       break;
13974
13975     case LO_SUM:
13976          rtl = XEXP (rtl, 1);
13977
13978       /* ... fall through ...  */
13979
13980     case LABEL_REF:
13981       /* Some ports can transform a symbol ref into a label ref, because
13982          the symbol ref is too far away and has to be dumped into a constant
13983          pool.  */
13984     case CONST:
13985     case SYMBOL_REF:
13986       if (GET_CODE (rtl) == SYMBOL_REF
13987           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13988         {
13989           dw_loc_descr_ref temp;
13990
13991           /* If this is not defined, we have no way to emit the data.  */
13992           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13993             break;
13994
13995           /* We used to emit DW_OP_addr here, but that's wrong, since
13996              DW_OP_addr should be relocated by the debug info consumer,
13997              while DW_OP_GNU_push_tls_address operand should not.  */
13998           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13999                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
14000           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
14001           temp->dw_loc_oprnd1.v.val_addr = rtl;
14002           temp->dtprel = true;
14003
14004           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
14005           add_loc_descr (&mem_loc_result, temp);
14006
14007           break;
14008         }
14009
14010       if (!const_ok_for_output (rtl))
14011         break;
14012
14013     symref:
14014       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14015       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14016       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14017       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14018       break;
14019
14020     case CONCAT:
14021     case CONCATN:
14022     case VAR_LOCATION:
14023     case DEBUG_IMPLICIT_PTR:
14024       expansion_failed (NULL_TREE, rtl,
14025                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14026       return 0;
14027
14028     case ENTRY_VALUE:
14029       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
14030       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14031       if (REG_P (XEXP (rtl, 0)))
14032         mem_loc_result->dw_loc_oprnd1.v.val_loc
14033           = one_reg_loc_descriptor (dbx_reg_number (XEXP (rtl, 0)),
14034                                     VAR_INIT_STATUS_INITIALIZED);
14035       else if (MEM_P (XEXP (rtl, 0)) && REG_P (XEXP (XEXP (rtl, 0), 0)))
14036         {
14037           dw_loc_descr_ref ref
14038             = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14039                                   VAR_INIT_STATUS_INITIALIZED);
14040           if (ref == NULL)
14041             return NULL;
14042           mem_loc_result->dw_loc_oprnd1.v.val_loc = ref;
14043         }
14044       else
14045         gcc_unreachable ();
14046       return mem_loc_result;
14047
14048     case PRE_MODIFY:
14049       /* Extract the PLUS expression nested inside and fall into
14050          PLUS code below.  */
14051       rtl = XEXP (rtl, 1);
14052       goto plus;
14053
14054     case PRE_INC:
14055     case PRE_DEC:
14056       /* Turn these into a PLUS expression and fall into the PLUS code
14057          below.  */
14058       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
14059                           GEN_INT (GET_CODE (rtl) == PRE_INC
14060                                    ? GET_MODE_UNIT_SIZE (mode)
14061                                    : -GET_MODE_UNIT_SIZE (mode)));
14062
14063       /* ... fall through ...  */
14064
14065     case PLUS:
14066     plus:
14067       if (is_based_loc (rtl))
14068         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14069                                           INTVAL (XEXP (rtl, 1)),
14070                                           VAR_INIT_STATUS_INITIALIZED);
14071       else
14072         {
14073           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
14074                                                VAR_INIT_STATUS_INITIALIZED);
14075           if (mem_loc_result == 0)
14076             break;
14077
14078           if (CONST_INT_P (XEXP (rtl, 1)))
14079             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14080           else
14081             {
14082               dw_loc_descr_ref mem_loc_result2
14083                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14084                                       VAR_INIT_STATUS_INITIALIZED);
14085               if (mem_loc_result2 == 0)
14086                 break;
14087               add_loc_descr (&mem_loc_result, mem_loc_result2);
14088               add_loc_descr (&mem_loc_result,
14089                              new_loc_descr (DW_OP_plus, 0, 0));
14090             }
14091         }
14092       break;
14093
14094     /* If a pseudo-reg is optimized away, it is possible for it to
14095        be replaced with a MEM containing a multiply or shift.  */
14096     case MINUS:
14097       op = DW_OP_minus;
14098       goto do_binop;
14099
14100     case MULT:
14101       op = DW_OP_mul;
14102       goto do_binop;
14103
14104     case DIV:
14105       op = DW_OP_div;
14106       goto do_binop;
14107
14108     case UMOD:
14109       op = DW_OP_mod;
14110       goto do_binop;
14111
14112     case ASHIFT:
14113       op = DW_OP_shl;
14114       goto do_binop;
14115
14116     case ASHIFTRT:
14117       op = DW_OP_shra;
14118       goto do_binop;
14119
14120     case LSHIFTRT:
14121       op = DW_OP_shr;
14122       goto do_binop;
14123
14124     case AND:
14125       op = DW_OP_and;
14126       goto do_binop;
14127
14128     case IOR:
14129       op = DW_OP_or;
14130       goto do_binop;
14131
14132     case XOR:
14133       op = DW_OP_xor;
14134       goto do_binop;
14135
14136     do_binop:
14137       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14138                                 VAR_INIT_STATUS_INITIALIZED);
14139       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14140                                 VAR_INIT_STATUS_INITIALIZED);
14141
14142       if (op0 == 0 || op1 == 0)
14143         break;
14144
14145       mem_loc_result = op0;
14146       add_loc_descr (&mem_loc_result, op1);
14147       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14148       break;
14149
14150     case MOD:
14151       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14152                                 VAR_INIT_STATUS_INITIALIZED);
14153       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14154                                 VAR_INIT_STATUS_INITIALIZED);
14155
14156       if (op0 == 0 || op1 == 0)
14157         break;
14158
14159       mem_loc_result = op0;
14160       add_loc_descr (&mem_loc_result, op1);
14161       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14162       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14163       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14164       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14165       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14166       break;
14167
14168     case NOT:
14169       op = DW_OP_not;
14170       goto do_unop;
14171
14172     case ABS:
14173       op = DW_OP_abs;
14174       goto do_unop;
14175
14176     case NEG:
14177       op = DW_OP_neg;
14178       goto do_unop;
14179
14180     do_unop:
14181       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14182                                 VAR_INIT_STATUS_INITIALIZED);
14183
14184       if (op0 == 0)
14185         break;
14186
14187       mem_loc_result = op0;
14188       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14189       break;
14190
14191     case CONST_INT:
14192       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14193       break;
14194
14195     case EQ:
14196       op = DW_OP_eq;
14197       goto do_scompare;
14198
14199     case GE:
14200       op = DW_OP_ge;
14201       goto do_scompare;
14202
14203     case GT:
14204       op = DW_OP_gt;
14205       goto do_scompare;
14206
14207     case LE:
14208       op = DW_OP_le;
14209       goto do_scompare;
14210
14211     case LT:
14212       op = DW_OP_lt;
14213       goto do_scompare;
14214
14215     case NE:
14216       op = DW_OP_ne;
14217       goto do_scompare;
14218
14219     do_scompare:
14220       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14221           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14222         break;
14223       else
14224         {
14225           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14226
14227           if (op_mode == VOIDmode)
14228             op_mode = GET_MODE (XEXP (rtl, 1));
14229           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14230             break;
14231
14232           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14233                                     VAR_INIT_STATUS_INITIALIZED);
14234           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14235                                     VAR_INIT_STATUS_INITIALIZED);
14236
14237           if (op0 == 0 || op1 == 0)
14238             break;
14239
14240           if (op_mode != VOIDmode
14241               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14242             {
14243               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14244               shift *= BITS_PER_UNIT;
14245               /* For eq/ne, if the operands are known to be zero-extended,
14246                  there is no need to do the fancy shifting up.  */
14247               if (op == DW_OP_eq || op == DW_OP_ne)
14248                 {
14249                   dw_loc_descr_ref last0, last1;
14250                   for (last0 = op0;
14251                        last0->dw_loc_next != NULL;
14252                        last0 = last0->dw_loc_next)
14253                     ;
14254                   for (last1 = op1;
14255                        last1->dw_loc_next != NULL;
14256                        last1 = last1->dw_loc_next)
14257                     ;
14258                   /* deref_size zero extends, and for constants we can check
14259                      whether they are zero extended or not.  */
14260                   if (((last0->dw_loc_opc == DW_OP_deref_size
14261                         && last0->dw_loc_oprnd1.v.val_int
14262                            <= GET_MODE_SIZE (op_mode))
14263                        || (CONST_INT_P (XEXP (rtl, 0))
14264                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14265                                == (INTVAL (XEXP (rtl, 0))
14266                                    & GET_MODE_MASK (op_mode))))
14267                       && ((last1->dw_loc_opc == DW_OP_deref_size
14268                            && last1->dw_loc_oprnd1.v.val_int
14269                               <= GET_MODE_SIZE (op_mode))
14270                           || (CONST_INT_P (XEXP (rtl, 1))
14271                               && (unsigned HOST_WIDE_INT)
14272                                  INTVAL (XEXP (rtl, 1))
14273                                  == (INTVAL (XEXP (rtl, 1))
14274                                      & GET_MODE_MASK (op_mode)))))
14275                     goto do_compare;
14276                 }
14277               add_loc_descr (&op0, int_loc_descriptor (shift));
14278               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14279               if (CONST_INT_P (XEXP (rtl, 1)))
14280                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14281               else
14282                 {
14283                   add_loc_descr (&op1, int_loc_descriptor (shift));
14284                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14285                 }
14286             }
14287         }
14288
14289     do_compare:
14290       mem_loc_result = op0;
14291       add_loc_descr (&mem_loc_result, op1);
14292       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14293       if (STORE_FLAG_VALUE != 1)
14294         {
14295           add_loc_descr (&mem_loc_result,
14296                          int_loc_descriptor (STORE_FLAG_VALUE));
14297           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14298         }
14299       break;
14300
14301     case GEU:
14302       op = DW_OP_ge;
14303       goto do_ucompare;
14304
14305     case GTU:
14306       op = DW_OP_gt;
14307       goto do_ucompare;
14308
14309     case LEU:
14310       op = DW_OP_le;
14311       goto do_ucompare;
14312
14313     case LTU:
14314       op = DW_OP_lt;
14315       goto do_ucompare;
14316
14317     do_ucompare:
14318       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14319           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
14320         break;
14321       else
14322         {
14323           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14324
14325           if (op_mode == VOIDmode)
14326             op_mode = GET_MODE (XEXP (rtl, 1));
14327           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14328             break;
14329
14330           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14331                                     VAR_INIT_STATUS_INITIALIZED);
14332           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14333                                     VAR_INIT_STATUS_INITIALIZED);
14334
14335           if (op0 == 0 || op1 == 0)
14336             break;
14337
14338           if (op_mode != VOIDmode
14339               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14340             {
14341               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14342               dw_loc_descr_ref last0, last1;
14343               for (last0 = op0;
14344                    last0->dw_loc_next != NULL;
14345                    last0 = last0->dw_loc_next)
14346                 ;
14347               for (last1 = op1;
14348                    last1->dw_loc_next != NULL;
14349                    last1 = last1->dw_loc_next)
14350                 ;
14351               if (CONST_INT_P (XEXP (rtl, 0)))
14352                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14353               /* deref_size zero extends, so no need to mask it again.  */
14354               else if (last0->dw_loc_opc != DW_OP_deref_size
14355                        || last0->dw_loc_oprnd1.v.val_int
14356                           > GET_MODE_SIZE (op_mode))
14357                 {
14358                   add_loc_descr (&op0, int_loc_descriptor (mask));
14359                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14360                 }
14361               if (CONST_INT_P (XEXP (rtl, 1)))
14362                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14363               /* deref_size zero extends, so no need to mask it again.  */
14364               else if (last1->dw_loc_opc != DW_OP_deref_size
14365                        || last1->dw_loc_oprnd1.v.val_int
14366                           > GET_MODE_SIZE (op_mode))
14367                 {
14368                   add_loc_descr (&op1, int_loc_descriptor (mask));
14369                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14370                 }
14371             }
14372           else
14373             {
14374               HOST_WIDE_INT bias = 1;
14375               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14376               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14377               if (CONST_INT_P (XEXP (rtl, 1)))
14378                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14379                                           + INTVAL (XEXP (rtl, 1)));
14380               else
14381                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14382                                                     bias, 0));
14383             }
14384         }
14385       goto do_compare;
14386
14387     case SMIN:
14388     case SMAX:
14389     case UMIN:
14390     case UMAX:
14391       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
14392           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
14393           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
14394         break;
14395
14396       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14397                                 VAR_INIT_STATUS_INITIALIZED);
14398       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14399                                 VAR_INIT_STATUS_INITIALIZED);
14400
14401       if (op0 == 0 || op1 == 0)
14402         break;
14403
14404       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14405       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14406       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14407       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14408         {
14409           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14410             {
14411               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14412               add_loc_descr (&op0, int_loc_descriptor (mask));
14413               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14414               add_loc_descr (&op1, int_loc_descriptor (mask));
14415               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14416             }
14417           else
14418             {
14419               HOST_WIDE_INT bias = 1;
14420               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14421               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14422               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14423             }
14424         }
14425       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14426         {
14427           int shift = DWARF2_ADDR_SIZE
14428                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14429           shift *= BITS_PER_UNIT;
14430           add_loc_descr (&op0, int_loc_descriptor (shift));
14431           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14432           add_loc_descr (&op1, int_loc_descriptor (shift));
14433           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14434         }
14435
14436       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14437         op = DW_OP_lt;
14438       else
14439         op = DW_OP_gt;
14440       mem_loc_result = op0;
14441       add_loc_descr (&mem_loc_result, op1);
14442       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14443       {
14444         dw_loc_descr_ref bra_node, drop_node;
14445
14446         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14447         add_loc_descr (&mem_loc_result, bra_node);
14448         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14449         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14450         add_loc_descr (&mem_loc_result, drop_node);
14451         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14452         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14453       }
14454       break;
14455
14456     case ZERO_EXTRACT:
14457     case SIGN_EXTRACT:
14458       if (CONST_INT_P (XEXP (rtl, 1))
14459           && CONST_INT_P (XEXP (rtl, 2))
14460           && ((unsigned) INTVAL (XEXP (rtl, 1))
14461               + (unsigned) INTVAL (XEXP (rtl, 2))
14462               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14463           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14464           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14465         {
14466           int shift, size;
14467           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14468                                     VAR_INIT_STATUS_INITIALIZED);
14469           if (op0 == 0)
14470             break;
14471           if (GET_CODE (rtl) == SIGN_EXTRACT)
14472             op = DW_OP_shra;
14473           else
14474             op = DW_OP_shr;
14475           mem_loc_result = op0;
14476           size = INTVAL (XEXP (rtl, 1));
14477           shift = INTVAL (XEXP (rtl, 2));
14478           if (BITS_BIG_ENDIAN)
14479             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14480                     - shift - size;
14481           if (shift + size != (int) DWARF2_ADDR_SIZE)
14482             {
14483               add_loc_descr (&mem_loc_result,
14484                              int_loc_descriptor (DWARF2_ADDR_SIZE
14485                                                  - shift - size));
14486               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14487             }
14488           if (size != (int) DWARF2_ADDR_SIZE)
14489             {
14490               add_loc_descr (&mem_loc_result,
14491                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14492               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14493             }
14494         }
14495       break;
14496
14497     case IF_THEN_ELSE:
14498       {
14499         dw_loc_descr_ref op2, bra_node, drop_node;
14500         op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14501                                   VAR_INIT_STATUS_INITIALIZED);
14502         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14503                                   VAR_INIT_STATUS_INITIALIZED);
14504         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode,
14505                                   VAR_INIT_STATUS_INITIALIZED);
14506         if (op0 == NULL || op1 == NULL || op2 == NULL)
14507           break;
14508
14509         mem_loc_result = op1;
14510         add_loc_descr (&mem_loc_result, op2);
14511         add_loc_descr (&mem_loc_result, op0);
14512         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14513         add_loc_descr (&mem_loc_result, bra_node);
14514         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14515         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14516         add_loc_descr (&mem_loc_result, drop_node);
14517         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14518         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14519       }
14520       break;
14521
14522     case COMPARE:
14523     case ROTATE:
14524     case ROTATERT:
14525     case TRUNCATE:
14526       /* In theory, we could implement the above.  */
14527       /* DWARF cannot represent the unsigned compare operations
14528          natively.  */
14529     case SS_MULT:
14530     case US_MULT:
14531     case SS_DIV:
14532     case US_DIV:
14533     case SS_PLUS:
14534     case US_PLUS:
14535     case SS_MINUS:
14536     case US_MINUS:
14537     case SS_NEG:
14538     case US_NEG:
14539     case SS_ABS:
14540     case SS_ASHIFT:
14541     case US_ASHIFT:
14542     case SS_TRUNCATE:
14543     case US_TRUNCATE:
14544     case UDIV:
14545     case UNORDERED:
14546     case ORDERED:
14547     case UNEQ:
14548     case UNGE:
14549     case UNGT:
14550     case UNLE:
14551     case UNLT:
14552     case LTGT:
14553     case FLOAT_EXTEND:
14554     case FLOAT_TRUNCATE:
14555     case FLOAT:
14556     case UNSIGNED_FLOAT:
14557     case FIX:
14558     case UNSIGNED_FIX:
14559     case FRACT_CONVERT:
14560     case UNSIGNED_FRACT_CONVERT:
14561     case SAT_FRACT:
14562     case UNSIGNED_SAT_FRACT:
14563     case SQRT:
14564     case BSWAP:
14565     case FFS:
14566     case CLZ:
14567     case CTZ:
14568     case POPCOUNT:
14569     case PARITY:
14570     case ASM_OPERANDS:
14571     case VEC_MERGE:
14572     case VEC_SELECT:
14573     case VEC_CONCAT:
14574     case VEC_DUPLICATE:
14575     case UNSPEC:
14576     case HIGH:
14577       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14578          can't express it in the debug info.  This can happen e.g. with some
14579          TLS UNSPECs.  */
14580       break;
14581
14582     case CONST_STRING:
14583       resolve_one_addr (&rtl, NULL);
14584       goto symref;
14585
14586     default:
14587 #ifdef ENABLE_CHECKING
14588       print_rtl (stderr, rtl);
14589       gcc_unreachable ();
14590 #else
14591       break;
14592 #endif
14593     }
14594
14595   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14596     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14597
14598   return mem_loc_result;
14599 }
14600
14601 /* Return a descriptor that describes the concatenation of two locations.
14602    This is typically a complex variable.  */
14603
14604 static dw_loc_descr_ref
14605 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14606 {
14607   dw_loc_descr_ref cc_loc_result = NULL;
14608   dw_loc_descr_ref x0_ref
14609     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14610   dw_loc_descr_ref x1_ref
14611     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14612
14613   if (x0_ref == 0 || x1_ref == 0)
14614     return 0;
14615
14616   cc_loc_result = x0_ref;
14617   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14618
14619   add_loc_descr (&cc_loc_result, x1_ref);
14620   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14621
14622   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14623     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14624
14625   return cc_loc_result;
14626 }
14627
14628 /* Return a descriptor that describes the concatenation of N
14629    locations.  */
14630
14631 static dw_loc_descr_ref
14632 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14633 {
14634   unsigned int i;
14635   dw_loc_descr_ref cc_loc_result = NULL;
14636   unsigned int n = XVECLEN (concatn, 0);
14637
14638   for (i = 0; i < n; ++i)
14639     {
14640       dw_loc_descr_ref ref;
14641       rtx x = XVECEXP (concatn, 0, i);
14642
14643       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14644       if (ref == NULL)
14645         return NULL;
14646
14647       add_loc_descr (&cc_loc_result, ref);
14648       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14649     }
14650
14651   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14652     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14653
14654   return cc_loc_result;
14655 }
14656
14657 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14658    for DEBUG_IMPLICIT_PTR RTL.  */
14659
14660 static dw_loc_descr_ref
14661 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14662 {
14663   dw_loc_descr_ref ret;
14664   dw_die_ref ref;
14665
14666   if (dwarf_strict)
14667     return NULL;
14668   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14669               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14670               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14671   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14672   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14673   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14674   if (ref)
14675     {
14676       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14677       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14678       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14679     }
14680   else
14681     {
14682       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14683       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14684     }
14685   return ret;
14686 }
14687
14688 /* Output a proper Dwarf location descriptor for a variable or parameter
14689    which is either allocated in a register or in a memory location.  For a
14690    register, we just generate an OP_REG and the register number.  For a
14691    memory location we provide a Dwarf postfix expression describing how to
14692    generate the (dynamic) address of the object onto the address stack.
14693
14694    MODE is mode of the decl if this loc_descriptor is going to be used in
14695    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14696    allowed, VOIDmode otherwise.
14697
14698    If we don't know how to describe it, return 0.  */
14699
14700 static dw_loc_descr_ref
14701 loc_descriptor (rtx rtl, enum machine_mode mode,
14702                 enum var_init_status initialized)
14703 {
14704   dw_loc_descr_ref loc_result = NULL;
14705
14706   switch (GET_CODE (rtl))
14707     {
14708     case SUBREG:
14709       /* The case of a subreg may arise when we have a local (register)
14710          variable or a formal (register) parameter which doesn't quite fill
14711          up an entire register.  For now, just assume that it is
14712          legitimate to make the Dwarf info refer to the whole register which
14713          contains the given subreg.  */
14714       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14715       break;
14716
14717     case REG:
14718       loc_result = reg_loc_descriptor (rtl, initialized);
14719       break;
14720
14721     case MEM:
14722       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14723                                        initialized);
14724       if (loc_result == NULL)
14725         loc_result = tls_mem_loc_descriptor (rtl);
14726       if (loc_result == NULL)
14727         {
14728           rtx new_rtl = avoid_constant_pool_reference (rtl);
14729           if (new_rtl != rtl)
14730             loc_result = loc_descriptor (new_rtl, mode, initialized);
14731         }
14732       break;
14733
14734     case CONCAT:
14735       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14736                                           initialized);
14737       break;
14738
14739     case CONCATN:
14740       loc_result = concatn_loc_descriptor (rtl, initialized);
14741       break;
14742
14743     case VAR_LOCATION:
14744       /* Single part.  */
14745       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14746         {
14747           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14748           if (GET_CODE (loc) == EXPR_LIST)
14749             loc = XEXP (loc, 0);
14750           loc_result = loc_descriptor (loc, mode, initialized);
14751           break;
14752         }
14753
14754       rtl = XEXP (rtl, 1);
14755       /* FALLTHRU */
14756
14757     case PARALLEL:
14758       {
14759         rtvec par_elems = XVEC (rtl, 0);
14760         int num_elem = GET_NUM_ELEM (par_elems);
14761         enum machine_mode mode;
14762         int i;
14763
14764         /* Create the first one, so we have something to add to.  */
14765         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14766                                      VOIDmode, initialized);
14767         if (loc_result == NULL)
14768           return NULL;
14769         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14770         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14771         for (i = 1; i < num_elem; i++)
14772           {
14773             dw_loc_descr_ref temp;
14774
14775             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14776                                    VOIDmode, initialized);
14777             if (temp == NULL)
14778               return NULL;
14779             add_loc_descr (&loc_result, temp);
14780             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14781             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14782           }
14783       }
14784       break;
14785
14786     case CONST_INT:
14787       if (mode != VOIDmode && mode != BLKmode)
14788         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14789                                                     INTVAL (rtl));
14790       break;
14791
14792     case CONST_DOUBLE:
14793       if (mode == VOIDmode)
14794         mode = GET_MODE (rtl);
14795
14796       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14797         {
14798           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14799
14800           /* Note that a CONST_DOUBLE rtx could represent either an integer
14801              or a floating-point constant.  A CONST_DOUBLE is used whenever
14802              the constant requires more than one word in order to be
14803              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14804           loc_result = new_loc_descr (DW_OP_implicit_value,
14805                                       GET_MODE_SIZE (mode), 0);
14806           if (SCALAR_FLOAT_MODE_P (mode))
14807             {
14808               unsigned int length = GET_MODE_SIZE (mode);
14809               unsigned char *array
14810                   = (unsigned char*) ggc_alloc_atomic (length);
14811
14812               insert_float (rtl, array);
14813               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14814               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14815               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14816               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14817             }
14818           else
14819             {
14820               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14821               loc_result->dw_loc_oprnd2.v.val_double
14822                 = rtx_to_double_int (rtl);
14823             }
14824         }
14825       break;
14826
14827     case CONST_VECTOR:
14828       if (mode == VOIDmode)
14829         mode = GET_MODE (rtl);
14830
14831       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14832         {
14833           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14834           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14835           unsigned char *array = (unsigned char *)
14836             ggc_alloc_atomic (length * elt_size);
14837           unsigned int i;
14838           unsigned char *p;
14839
14840           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14841           switch (GET_MODE_CLASS (mode))
14842             {
14843             case MODE_VECTOR_INT:
14844               for (i = 0, p = array; i < length; i++, p += elt_size)
14845                 {
14846                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14847                   double_int val = rtx_to_double_int (elt);
14848
14849                   if (elt_size <= sizeof (HOST_WIDE_INT))
14850                     insert_int (double_int_to_shwi (val), elt_size, p);
14851                   else
14852                     {
14853                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14854                       insert_double (val, p);
14855                     }
14856                 }
14857               break;
14858
14859             case MODE_VECTOR_FLOAT:
14860               for (i = 0, p = array; i < length; i++, p += elt_size)
14861                 {
14862                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14863                   insert_float (elt, p);
14864                 }
14865               break;
14866
14867             default:
14868               gcc_unreachable ();
14869             }
14870
14871           loc_result = new_loc_descr (DW_OP_implicit_value,
14872                                       length * elt_size, 0);
14873           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14874           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14875           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14876           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14877         }
14878       break;
14879
14880     case CONST:
14881       if (mode == VOIDmode
14882           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14883           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14884           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14885         {
14886           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14887           break;
14888         }
14889       /* FALLTHROUGH */
14890     case SYMBOL_REF:
14891       if (!const_ok_for_output (rtl))
14892         break;
14893     case LABEL_REF:
14894       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14895           && (dwarf_version >= 4 || !dwarf_strict))
14896         {
14897           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14898           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14899           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14900           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14901           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14902         }
14903       break;
14904
14905     case DEBUG_IMPLICIT_PTR:
14906       loc_result = implicit_ptr_descriptor (rtl, 0);
14907       break;
14908
14909     case PLUS:
14910       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
14911           && CONST_INT_P (XEXP (rtl, 1)))
14912         {
14913           loc_result
14914             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
14915           break;
14916         }
14917       /* FALLTHRU */
14918     default:
14919       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14920           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14921           && (dwarf_version >= 4 || !dwarf_strict))
14922         {
14923           /* Value expression.  */
14924           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14925           if (loc_result)
14926             add_loc_descr (&loc_result,
14927                            new_loc_descr (DW_OP_stack_value, 0, 0));
14928         }
14929       break;
14930     }
14931
14932   return loc_result;
14933 }
14934
14935 /* We need to figure out what section we should use as the base for the
14936    address ranges where a given location is valid.
14937    1. If this particular DECL has a section associated with it, use that.
14938    2. If this function has a section associated with it, use that.
14939    3. Otherwise, use the text section.
14940    XXX: If you split a variable across multiple sections, we won't notice.  */
14941
14942 static const char *
14943 secname_for_decl (const_tree decl)
14944 {
14945   const char *secname;
14946
14947   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14948     {
14949       tree sectree = DECL_SECTION_NAME (decl);
14950       secname = TREE_STRING_POINTER (sectree);
14951     }
14952   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14953     {
14954       tree sectree = DECL_SECTION_NAME (current_function_decl);
14955       secname = TREE_STRING_POINTER (sectree);
14956     }
14957   else if (cfun && in_cold_section_p)
14958     secname = crtl->subsections.cold_section_label;
14959   else
14960     secname = text_section_label;
14961
14962   return secname;
14963 }
14964
14965 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14966
14967 static bool
14968 decl_by_reference_p (tree decl)
14969 {
14970   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14971            || TREE_CODE (decl) == VAR_DECL)
14972           && DECL_BY_REFERENCE (decl));
14973 }
14974
14975 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14976    for VARLOC.  */
14977
14978 static dw_loc_descr_ref
14979 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14980                enum var_init_status initialized)
14981 {
14982   int have_address = 0;
14983   dw_loc_descr_ref descr;
14984   enum machine_mode mode;
14985
14986   if (want_address != 2)
14987     {
14988       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14989       /* Single part.  */
14990       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14991         {
14992           varloc = PAT_VAR_LOCATION_LOC (varloc);
14993           if (GET_CODE (varloc) == EXPR_LIST)
14994             varloc = XEXP (varloc, 0);
14995           mode = GET_MODE (varloc);
14996           if (MEM_P (varloc))
14997             {
14998               rtx addr = XEXP (varloc, 0);
14999               descr = mem_loc_descriptor (addr, mode, initialized);
15000               if (descr)
15001                 have_address = 1;
15002               else
15003                 {
15004                   rtx x = avoid_constant_pool_reference (varloc);
15005                   if (x != varloc)
15006                     descr = mem_loc_descriptor (x, mode, initialized);
15007                 }
15008             }
15009           else
15010             descr = mem_loc_descriptor (varloc, mode, initialized);
15011         }
15012       else
15013         return 0;
15014     }
15015   else
15016     {
15017       if (GET_CODE (varloc) == VAR_LOCATION)
15018         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15019       else
15020         mode = DECL_MODE (loc);
15021       descr = loc_descriptor (varloc, mode, initialized);
15022       have_address = 1;
15023     }
15024
15025   if (!descr)
15026     return 0;
15027
15028   if (want_address == 2 && !have_address
15029       && (dwarf_version >= 4 || !dwarf_strict))
15030     {
15031       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15032         {
15033           expansion_failed (loc, NULL_RTX,
15034                             "DWARF address size mismatch");
15035           return 0;
15036         }
15037       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15038       have_address = 1;
15039     }
15040   /* Show if we can't fill the request for an address.  */
15041   if (want_address && !have_address)
15042     {
15043       expansion_failed (loc, NULL_RTX,
15044                         "Want address and only have value");
15045       return 0;
15046     }
15047
15048   /* If we've got an address and don't want one, dereference.  */
15049   if (!want_address && have_address)
15050     {
15051       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15052       enum dwarf_location_atom op;
15053
15054       if (size > DWARF2_ADDR_SIZE || size == -1)
15055         {
15056           expansion_failed (loc, NULL_RTX,
15057                             "DWARF address size mismatch");
15058           return 0;
15059         }
15060       else if (size == DWARF2_ADDR_SIZE)
15061         op = DW_OP_deref;
15062       else
15063         op = DW_OP_deref_size;
15064
15065       add_loc_descr (&descr, new_loc_descr (op, size, 0));
15066     }
15067
15068   return descr;
15069 }
15070
15071 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15072    if it is not possible.  */
15073
15074 static dw_loc_descr_ref
15075 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15076 {
15077   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15078     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15079   else if (dwarf_version >= 3 || !dwarf_strict)
15080     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15081   else
15082     return NULL;
15083 }
15084
15085 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15086    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
15087
15088 static dw_loc_descr_ref
15089 dw_sra_loc_expr (tree decl, rtx loc)
15090 {
15091   rtx p;
15092   unsigned int padsize = 0;
15093   dw_loc_descr_ref descr, *descr_tail;
15094   unsigned HOST_WIDE_INT decl_size;
15095   rtx varloc;
15096   enum var_init_status initialized;
15097
15098   if (DECL_SIZE (decl) == NULL
15099       || !host_integerp (DECL_SIZE (decl), 1))
15100     return NULL;
15101
15102   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
15103   descr = NULL;
15104   descr_tail = &descr;
15105
15106   for (p = loc; p; p = XEXP (p, 1))
15107     {
15108       unsigned int bitsize = decl_piece_bitsize (p);
15109       rtx loc_note = *decl_piece_varloc_ptr (p);
15110       dw_loc_descr_ref cur_descr;
15111       dw_loc_descr_ref *tail, last = NULL;
15112       unsigned int opsize = 0;
15113
15114       if (loc_note == NULL_RTX
15115           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15116         {
15117           padsize += bitsize;
15118           continue;
15119         }
15120       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15121       varloc = NOTE_VAR_LOCATION (loc_note);
15122       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15123       if (cur_descr == NULL)
15124         {
15125           padsize += bitsize;
15126           continue;
15127         }
15128
15129       /* Check that cur_descr either doesn't use
15130          DW_OP_*piece operations, or their sum is equal
15131          to bitsize.  Otherwise we can't embed it.  */
15132       for (tail = &cur_descr; *tail != NULL;
15133            tail = &(*tail)->dw_loc_next)
15134         if ((*tail)->dw_loc_opc == DW_OP_piece)
15135           {
15136             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
15137                       * BITS_PER_UNIT;
15138             last = *tail;
15139           }
15140         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
15141           {
15142             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
15143             last = *tail;
15144           }
15145
15146       if (last != NULL && opsize != bitsize)
15147         {
15148           padsize += bitsize;
15149           continue;
15150         }
15151
15152       /* If there is a hole, add DW_OP_*piece after empty DWARF
15153          expression, which means that those bits are optimized out.  */
15154       if (padsize)
15155         {
15156           if (padsize > decl_size)
15157             return NULL;
15158           decl_size -= padsize;
15159           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15160           if (*descr_tail == NULL)
15161             return NULL;
15162           descr_tail = &(*descr_tail)->dw_loc_next;
15163           padsize = 0;
15164         }
15165       *descr_tail = cur_descr;
15166       descr_tail = tail;
15167       if (bitsize > decl_size)
15168         return NULL;
15169       decl_size -= bitsize;
15170       if (last == NULL)
15171         {
15172           HOST_WIDE_INT offset = 0;
15173           if (GET_CODE (varloc) == VAR_LOCATION
15174               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15175             {
15176               varloc = PAT_VAR_LOCATION_LOC (varloc);
15177               if (GET_CODE (varloc) == EXPR_LIST)
15178                 varloc = XEXP (varloc, 0);
15179             }
15180           do 
15181             {
15182               if (GET_CODE (varloc) == CONST
15183                   || GET_CODE (varloc) == SIGN_EXTEND
15184                   || GET_CODE (varloc) == ZERO_EXTEND)
15185                 varloc = XEXP (varloc, 0);
15186               else if (GET_CODE (varloc) == SUBREG)
15187                 varloc = SUBREG_REG (varloc);
15188               else
15189                 break;
15190             }
15191           while (1);
15192           /* DW_OP_bit_size offset should be zero for register
15193              or implicit location descriptions and empty location
15194              descriptions, but for memory addresses needs big endian
15195              adjustment.  */
15196           if (MEM_P (varloc))
15197             {
15198               unsigned HOST_WIDE_INT memsize
15199                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15200               if (memsize != bitsize)
15201                 {
15202                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15203                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15204                     return NULL;
15205                   if (memsize < bitsize)
15206                     return NULL;
15207                   if (BITS_BIG_ENDIAN)
15208                     offset = memsize - bitsize;
15209                 }
15210             }
15211
15212           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15213           if (*descr_tail == NULL)
15214             return NULL;
15215           descr_tail = &(*descr_tail)->dw_loc_next;
15216         }
15217     }
15218
15219   /* If there were any non-empty expressions, add padding till the end of
15220      the decl.  */
15221   if (descr != NULL && decl_size != 0)
15222     {
15223       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15224       if (*descr_tail == NULL)
15225         return NULL;
15226     }
15227   return descr;
15228 }
15229
15230 /* Return the dwarf representation of the location list LOC_LIST of
15231    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15232    function.  */
15233
15234 static dw_loc_list_ref
15235 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15236 {
15237   const char *endname, *secname;
15238   rtx varloc;
15239   enum var_init_status initialized;
15240   struct var_loc_node *node;
15241   dw_loc_descr_ref descr;
15242   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15243   dw_loc_list_ref list = NULL;
15244   dw_loc_list_ref *listp = &list;
15245
15246   /* Now that we know what section we are using for a base,
15247      actually construct the list of locations.
15248      The first location information is what is passed to the
15249      function that creates the location list, and the remaining
15250      locations just get added on to that list.
15251      Note that we only know the start address for a location
15252      (IE location changes), so to build the range, we use
15253      the range [current location start, next location start].
15254      This means we have to special case the last node, and generate
15255      a range of [last location start, end of function label].  */
15256
15257   secname = secname_for_decl (decl);
15258
15259   for (node = loc_list->first; node; node = node->next)
15260     if (GET_CODE (node->loc) == EXPR_LIST
15261         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15262       {
15263         if (GET_CODE (node->loc) == EXPR_LIST)
15264           {
15265             /* This requires DW_OP_{,bit_}piece, which is not usable
15266                inside DWARF expressions.  */
15267             if (want_address != 2)
15268               continue;
15269             descr = dw_sra_loc_expr (decl, node->loc);
15270             if (descr == NULL)
15271               continue;
15272           }
15273         else
15274           {
15275             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15276             varloc = NOTE_VAR_LOCATION (node->loc);
15277             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15278           }
15279         if (descr)
15280           {
15281             /* The variable has a location between NODE->LABEL and
15282                NODE->NEXT->LABEL.  */
15283             if (node->next)
15284               endname = node->next->label;
15285             /* If the variable has a location at the last label
15286                it keeps its location until the end of function.  */
15287             else if (!current_function_decl)
15288               endname = text_end_label;
15289             else
15290               {
15291                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15292                                              current_function_funcdef_no);
15293                 endname = ggc_strdup (label_id);
15294               }
15295
15296             *listp = new_loc_list (descr, node->label, endname, secname);
15297             listp = &(*listp)->dw_loc_next;
15298           }
15299       }
15300
15301   /* Try to avoid the overhead of a location list emitting a location
15302      expression instead, but only if we didn't have more than one
15303      location entry in the first place.  If some entries were not
15304      representable, we don't want to pretend a single entry that was
15305      applies to the entire scope in which the variable is
15306      available.  */
15307   if (list && loc_list->first->next)
15308     gen_llsym (list);
15309
15310   return list;
15311 }
15312
15313 /* Return if the loc_list has only single element and thus can be represented
15314    as location description.   */
15315
15316 static bool
15317 single_element_loc_list_p (dw_loc_list_ref list)
15318 {
15319   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15320   return !list->ll_symbol;
15321 }
15322
15323 /* To each location in list LIST add loc descr REF.  */
15324
15325 static void
15326 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15327 {
15328   dw_loc_descr_ref copy;
15329   add_loc_descr (&list->expr, ref);
15330   list = list->dw_loc_next;
15331   while (list)
15332     {
15333       copy = ggc_alloc_dw_loc_descr_node ();
15334       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15335       add_loc_descr (&list->expr, copy);
15336       while (copy->dw_loc_next)
15337         {
15338           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15339           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15340           copy->dw_loc_next = new_copy;
15341           copy = new_copy;
15342         }
15343       list = list->dw_loc_next;
15344     }
15345 }
15346
15347 /* Given two lists RET and LIST
15348    produce location list that is result of adding expression in LIST
15349    to expression in RET on each possition in program.
15350    Might be destructive on both RET and LIST.
15351
15352    TODO: We handle only simple cases of RET or LIST having at most one
15353    element. General case would inolve sorting the lists in program order
15354    and merging them that will need some additional work.
15355    Adding that will improve quality of debug info especially for SRA-ed
15356    structures.  */
15357
15358 static void
15359 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15360 {
15361   if (!list)
15362     return;
15363   if (!*ret)
15364     {
15365       *ret = list;
15366       return;
15367     }
15368   if (!list->dw_loc_next)
15369     {
15370       add_loc_descr_to_each (*ret, list->expr);
15371       return;
15372     }
15373   if (!(*ret)->dw_loc_next)
15374     {
15375       add_loc_descr_to_each (list, (*ret)->expr);
15376       *ret = list;
15377       return;
15378     }
15379   expansion_failed (NULL_TREE, NULL_RTX,
15380                     "Don't know how to merge two non-trivial"
15381                     " location lists.\n");
15382   *ret = NULL;
15383   return;
15384 }
15385
15386 /* LOC is constant expression.  Try a luck, look it up in constant
15387    pool and return its loc_descr of its address.  */
15388
15389 static dw_loc_descr_ref
15390 cst_pool_loc_descr (tree loc)
15391 {
15392   /* Get an RTL for this, if something has been emitted.  */
15393   rtx rtl = lookup_constant_def (loc);
15394   enum machine_mode mode;
15395
15396   if (!rtl || !MEM_P (rtl))
15397     {
15398       gcc_assert (!rtl);
15399       return 0;
15400     }
15401   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15402
15403   /* TODO: We might get more coverage if we was actually delaying expansion
15404      of all expressions till end of compilation when constant pools are fully
15405      populated.  */
15406   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15407     {
15408       expansion_failed (loc, NULL_RTX,
15409                         "CST value in contant pool but not marked.");
15410       return 0;
15411     }
15412   mode = GET_MODE (rtl);
15413   rtl = XEXP (rtl, 0);
15414   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15415 }
15416
15417 /* Return dw_loc_list representing address of addr_expr LOC
15418    by looking for innder INDIRECT_REF expression and turing it
15419    into simple arithmetics.  */
15420
15421 static dw_loc_list_ref
15422 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15423 {
15424   tree obj, offset;
15425   HOST_WIDE_INT bitsize, bitpos, bytepos;
15426   enum machine_mode mode;
15427   int volatilep;
15428   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15429   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15430
15431   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15432                              &bitsize, &bitpos, &offset, &mode,
15433                              &unsignedp, &volatilep, false);
15434   STRIP_NOPS (obj);
15435   if (bitpos % BITS_PER_UNIT)
15436     {
15437       expansion_failed (loc, NULL_RTX, "bitfield access");
15438       return 0;
15439     }
15440   if (!INDIRECT_REF_P (obj))
15441     {
15442       expansion_failed (obj,
15443                         NULL_RTX, "no indirect ref in inner refrence");
15444       return 0;
15445     }
15446   if (!offset && !bitpos)
15447     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15448   else if (toplev
15449            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15450            && (dwarf_version >= 4 || !dwarf_strict))
15451     {
15452       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15453       if (!list_ret)
15454         return 0;
15455       if (offset)
15456         {
15457           /* Variable offset.  */
15458           list_ret1 = loc_list_from_tree (offset, 0);
15459           if (list_ret1 == 0)
15460             return 0;
15461           add_loc_list (&list_ret, list_ret1);
15462           if (!list_ret)
15463             return 0;
15464           add_loc_descr_to_each (list_ret,
15465                                  new_loc_descr (DW_OP_plus, 0, 0));
15466         }
15467       bytepos = bitpos / BITS_PER_UNIT;
15468       if (bytepos > 0)
15469         add_loc_descr_to_each (list_ret,
15470                                new_loc_descr (DW_OP_plus_uconst,
15471                                               bytepos, 0));
15472       else if (bytepos < 0)
15473         loc_list_plus_const (list_ret, bytepos);
15474       add_loc_descr_to_each (list_ret,
15475                              new_loc_descr (DW_OP_stack_value, 0, 0));
15476     }
15477   return list_ret;
15478 }
15479
15480
15481 /* Generate Dwarf location list representing LOC.
15482    If WANT_ADDRESS is false, expression computing LOC will be computed
15483    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15484    if WANT_ADDRESS is 2, expression computing address useable in location
15485      will be returned (i.e. DW_OP_reg can be used
15486      to refer to register values).  */
15487
15488 static dw_loc_list_ref
15489 loc_list_from_tree (tree loc, int want_address)
15490 {
15491   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15492   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15493   int have_address = 0;
15494   enum dwarf_location_atom op;
15495
15496   /* ??? Most of the time we do not take proper care for sign/zero
15497      extending the values properly.  Hopefully this won't be a real
15498      problem...  */
15499
15500   switch (TREE_CODE (loc))
15501     {
15502     case ERROR_MARK:
15503       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15504       return 0;
15505
15506     case PLACEHOLDER_EXPR:
15507       /* This case involves extracting fields from an object to determine the
15508          position of other fields.  We don't try to encode this here.  The
15509          only user of this is Ada, which encodes the needed information using
15510          the names of types.  */
15511       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15512       return 0;
15513
15514     case CALL_EXPR:
15515       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15516       /* There are no opcodes for these operations.  */
15517       return 0;
15518
15519     case PREINCREMENT_EXPR:
15520     case PREDECREMENT_EXPR:
15521     case POSTINCREMENT_EXPR:
15522     case POSTDECREMENT_EXPR:
15523       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15524       /* There are no opcodes for these operations.  */
15525       return 0;
15526
15527     case ADDR_EXPR:
15528       /* If we already want an address, see if there is INDIRECT_REF inside
15529          e.g. for &this->field.  */
15530       if (want_address)
15531         {
15532           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15533                        (loc, want_address == 2);
15534           if (list_ret)
15535             have_address = 1;
15536           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15537                    && (ret = cst_pool_loc_descr (loc)))
15538             have_address = 1;
15539         }
15540         /* Otherwise, process the argument and look for the address.  */
15541       if (!list_ret && !ret)
15542         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15543       else
15544         {
15545           if (want_address)
15546             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15547           return NULL;
15548         }
15549       break;
15550
15551     case VAR_DECL:
15552       if (DECL_THREAD_LOCAL_P (loc))
15553         {
15554           rtx rtl;
15555           enum dwarf_location_atom first_op;
15556           enum dwarf_location_atom second_op;
15557           bool dtprel = false;
15558
15559           if (targetm.have_tls)
15560             {
15561               /* If this is not defined, we have no way to emit the
15562                  data.  */
15563               if (!targetm.asm_out.output_dwarf_dtprel)
15564                 return 0;
15565
15566                /* The way DW_OP_GNU_push_tls_address is specified, we
15567                   can only look up addresses of objects in the current
15568                   module.  We used DW_OP_addr as first op, but that's
15569                   wrong, because DW_OP_addr is relocated by the debug
15570                   info consumer, while DW_OP_GNU_push_tls_address
15571                   operand shouldn't be.  */
15572               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15573                 return 0;
15574               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15575               dtprel = true;
15576               second_op = DW_OP_GNU_push_tls_address;
15577             }
15578           else
15579             {
15580               if (!targetm.emutls.debug_form_tls_address
15581                   || !(dwarf_version >= 3 || !dwarf_strict))
15582                 return 0;
15583               /* We stuffed the control variable into the DECL_VALUE_EXPR
15584                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15585                  no longer appear in gimple code.  We used the control
15586                  variable in specific so that we could pick it up here.  */
15587               loc = DECL_VALUE_EXPR (loc);
15588               first_op = DW_OP_addr;
15589               second_op = DW_OP_form_tls_address;
15590             }
15591
15592           rtl = rtl_for_decl_location (loc);
15593           if (rtl == NULL_RTX)
15594             return 0;
15595
15596           if (!MEM_P (rtl))
15597             return 0;
15598           rtl = XEXP (rtl, 0);
15599           if (! CONSTANT_P (rtl))
15600             return 0;
15601
15602           ret = new_loc_descr (first_op, 0, 0);
15603           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15604           ret->dw_loc_oprnd1.v.val_addr = rtl;
15605           ret->dtprel = dtprel;
15606
15607           ret1 = new_loc_descr (second_op, 0, 0);
15608           add_loc_descr (&ret, ret1);
15609
15610           have_address = 1;
15611           break;
15612         }
15613       /* FALLTHRU */
15614
15615     case PARM_DECL:
15616     case RESULT_DECL:
15617       if (DECL_HAS_VALUE_EXPR_P (loc))
15618         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15619                                    want_address);
15620       /* FALLTHRU */
15621
15622     case FUNCTION_DECL:
15623       {
15624         rtx rtl;
15625         var_loc_list *loc_list = lookup_decl_loc (loc);
15626
15627         if (loc_list && loc_list->first)
15628           {
15629             list_ret = dw_loc_list (loc_list, loc, want_address);
15630             have_address = want_address != 0;
15631             break;
15632           }
15633         rtl = rtl_for_decl_location (loc);
15634         if (rtl == NULL_RTX)
15635           {
15636             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15637             return 0;
15638           }
15639         else if (CONST_INT_P (rtl))
15640           {
15641             HOST_WIDE_INT val = INTVAL (rtl);
15642             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15643               val &= GET_MODE_MASK (DECL_MODE (loc));
15644             ret = int_loc_descriptor (val);
15645           }
15646         else if (GET_CODE (rtl) == CONST_STRING)
15647           {
15648             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15649             return 0;
15650           }
15651         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15652           {
15653             ret = new_loc_descr (DW_OP_addr, 0, 0);
15654             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15655             ret->dw_loc_oprnd1.v.val_addr = rtl;
15656           }
15657         else
15658           {
15659             enum machine_mode mode;
15660
15661             /* Certain constructs can only be represented at top-level.  */
15662             if (want_address == 2)
15663               {
15664                 ret = loc_descriptor (rtl, VOIDmode,
15665                                       VAR_INIT_STATUS_INITIALIZED);
15666                 have_address = 1;
15667               }
15668             else
15669               {
15670                 mode = GET_MODE (rtl);
15671                 if (MEM_P (rtl))
15672                   {
15673                     rtl = XEXP (rtl, 0);
15674                     have_address = 1;
15675                   }
15676                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15677               }
15678             if (!ret)
15679               expansion_failed (loc, rtl,
15680                                 "failed to produce loc descriptor for rtl");
15681           }
15682       }
15683       break;
15684
15685     case MEM_REF:
15686       /* ??? FIXME.  */
15687       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15688         return 0;
15689       /* Fallthru.  */
15690     case INDIRECT_REF:
15691       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15692       have_address = 1;
15693       break;
15694
15695     case COMPOUND_EXPR:
15696       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15697
15698     CASE_CONVERT:
15699     case VIEW_CONVERT_EXPR:
15700     case SAVE_EXPR:
15701     case MODIFY_EXPR:
15702       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15703
15704     case COMPONENT_REF:
15705     case BIT_FIELD_REF:
15706     case ARRAY_REF:
15707     case ARRAY_RANGE_REF:
15708     case REALPART_EXPR:
15709     case IMAGPART_EXPR:
15710       {
15711         tree obj, offset;
15712         HOST_WIDE_INT bitsize, bitpos, bytepos;
15713         enum machine_mode mode;
15714         int volatilep;
15715         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15716
15717         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15718                                    &unsignedp, &volatilep, false);
15719
15720         gcc_assert (obj != loc);
15721
15722         list_ret = loc_list_from_tree (obj,
15723                                        want_address == 2
15724                                        && !bitpos && !offset ? 2 : 1);
15725         /* TODO: We can extract value of the small expression via shifting even
15726            for nonzero bitpos.  */
15727         if (list_ret == 0)
15728           return 0;
15729         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15730           {
15731             expansion_failed (loc, NULL_RTX,
15732                               "bitfield access");
15733             return 0;
15734           }
15735
15736         if (offset != NULL_TREE)
15737           {
15738             /* Variable offset.  */
15739             list_ret1 = loc_list_from_tree (offset, 0);
15740             if (list_ret1 == 0)
15741               return 0;
15742             add_loc_list (&list_ret, list_ret1);
15743             if (!list_ret)
15744               return 0;
15745             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15746           }
15747
15748         bytepos = bitpos / BITS_PER_UNIT;
15749         if (bytepos > 0)
15750           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15751         else if (bytepos < 0)
15752           loc_list_plus_const (list_ret, bytepos);
15753
15754         have_address = 1;
15755         break;
15756       }
15757
15758     case INTEGER_CST:
15759       if ((want_address || !host_integerp (loc, 0))
15760           && (ret = cst_pool_loc_descr (loc)))
15761         have_address = 1;
15762       else if (want_address == 2
15763                && host_integerp (loc, 0)
15764                && (ret = address_of_int_loc_descriptor
15765                            (int_size_in_bytes (TREE_TYPE (loc)),
15766                             tree_low_cst (loc, 0))))
15767         have_address = 1;
15768       else if (host_integerp (loc, 0))
15769         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15770       else
15771         {
15772           expansion_failed (loc, NULL_RTX,
15773                             "Integer operand is not host integer");
15774           return 0;
15775         }
15776       break;
15777
15778     case CONSTRUCTOR:
15779     case REAL_CST:
15780     case STRING_CST:
15781     case COMPLEX_CST:
15782       if ((ret = cst_pool_loc_descr (loc)))
15783         have_address = 1;
15784       else
15785       /* We can construct small constants here using int_loc_descriptor.  */
15786         expansion_failed (loc, NULL_RTX,
15787                           "constructor or constant not in constant pool");
15788       break;
15789
15790     case TRUTH_AND_EXPR:
15791     case TRUTH_ANDIF_EXPR:
15792     case BIT_AND_EXPR:
15793       op = DW_OP_and;
15794       goto do_binop;
15795
15796     case TRUTH_XOR_EXPR:
15797     case BIT_XOR_EXPR:
15798       op = DW_OP_xor;
15799       goto do_binop;
15800
15801     case TRUTH_OR_EXPR:
15802     case TRUTH_ORIF_EXPR:
15803     case BIT_IOR_EXPR:
15804       op = DW_OP_or;
15805       goto do_binop;
15806
15807     case FLOOR_DIV_EXPR:
15808     case CEIL_DIV_EXPR:
15809     case ROUND_DIV_EXPR:
15810     case TRUNC_DIV_EXPR:
15811       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15812         return 0;
15813       op = DW_OP_div;
15814       goto do_binop;
15815
15816     case MINUS_EXPR:
15817       op = DW_OP_minus;
15818       goto do_binop;
15819
15820     case FLOOR_MOD_EXPR:
15821     case CEIL_MOD_EXPR:
15822     case ROUND_MOD_EXPR:
15823     case TRUNC_MOD_EXPR:
15824       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15825         {
15826           op = DW_OP_mod;
15827           goto do_binop;
15828         }
15829       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15830       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15831       if (list_ret == 0 || list_ret1 == 0)
15832         return 0;
15833
15834       add_loc_list (&list_ret, list_ret1);
15835       if (list_ret == 0)
15836         return 0;
15837       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15838       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15839       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15840       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15841       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15842       break;
15843
15844     case MULT_EXPR:
15845       op = DW_OP_mul;
15846       goto do_binop;
15847
15848     case LSHIFT_EXPR:
15849       op = DW_OP_shl;
15850       goto do_binop;
15851
15852     case RSHIFT_EXPR:
15853       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15854       goto do_binop;
15855
15856     case POINTER_PLUS_EXPR:
15857     case PLUS_EXPR:
15858       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15859         {
15860           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15861           if (list_ret == 0)
15862             return 0;
15863
15864           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15865           break;
15866         }
15867
15868       op = DW_OP_plus;
15869       goto do_binop;
15870
15871     case LE_EXPR:
15872       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15873         return 0;
15874
15875       op = DW_OP_le;
15876       goto do_binop;
15877
15878     case GE_EXPR:
15879       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15880         return 0;
15881
15882       op = DW_OP_ge;
15883       goto do_binop;
15884
15885     case LT_EXPR:
15886       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15887         return 0;
15888
15889       op = DW_OP_lt;
15890       goto do_binop;
15891
15892     case GT_EXPR:
15893       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15894         return 0;
15895
15896       op = DW_OP_gt;
15897       goto do_binop;
15898
15899     case EQ_EXPR:
15900       op = DW_OP_eq;
15901       goto do_binop;
15902
15903     case NE_EXPR:
15904       op = DW_OP_ne;
15905       goto do_binop;
15906
15907     do_binop:
15908       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15909       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15910       if (list_ret == 0 || list_ret1 == 0)
15911         return 0;
15912
15913       add_loc_list (&list_ret, list_ret1);
15914       if (list_ret == 0)
15915         return 0;
15916       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15917       break;
15918
15919     case TRUTH_NOT_EXPR:
15920     case BIT_NOT_EXPR:
15921       op = DW_OP_not;
15922       goto do_unop;
15923
15924     case ABS_EXPR:
15925       op = DW_OP_abs;
15926       goto do_unop;
15927
15928     case NEGATE_EXPR:
15929       op = DW_OP_neg;
15930       goto do_unop;
15931
15932     do_unop:
15933       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15934       if (list_ret == 0)
15935         return 0;
15936
15937       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15938       break;
15939
15940     case MIN_EXPR:
15941     case MAX_EXPR:
15942       {
15943         const enum tree_code code =
15944           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15945
15946         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15947                       build2 (code, integer_type_node,
15948                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15949                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15950       }
15951
15952       /* ... fall through ...  */
15953
15954     case COND_EXPR:
15955       {
15956         dw_loc_descr_ref lhs
15957           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15958         dw_loc_list_ref rhs
15959           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15960         dw_loc_descr_ref bra_node, jump_node, tmp;
15961
15962         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15963         if (list_ret == 0 || lhs == 0 || rhs == 0)
15964           return 0;
15965
15966         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15967         add_loc_descr_to_each (list_ret, bra_node);
15968
15969         add_loc_list (&list_ret, rhs);
15970         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15971         add_loc_descr_to_each (list_ret, jump_node);
15972
15973         add_loc_descr_to_each (list_ret, lhs);
15974         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15975         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15976
15977         /* ??? Need a node to point the skip at.  Use a nop.  */
15978         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15979         add_loc_descr_to_each (list_ret, tmp);
15980         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15981         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15982       }
15983       break;
15984
15985     case FIX_TRUNC_EXPR:
15986       return 0;
15987
15988     default:
15989       /* Leave front-end specific codes as simply unknown.  This comes
15990          up, for instance, with the C STMT_EXPR.  */
15991       if ((unsigned int) TREE_CODE (loc)
15992           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15993         {
15994           expansion_failed (loc, NULL_RTX,
15995                             "language specific tree node");
15996           return 0;
15997         }
15998
15999 #ifdef ENABLE_CHECKING
16000       /* Otherwise this is a generic code; we should just lists all of
16001          these explicitly.  We forgot one.  */
16002       gcc_unreachable ();
16003 #else
16004       /* In a release build, we want to degrade gracefully: better to
16005          generate incomplete debugging information than to crash.  */
16006       return NULL;
16007 #endif
16008     }
16009
16010   if (!ret && !list_ret)
16011     return 0;
16012
16013   if (want_address == 2 && !have_address
16014       && (dwarf_version >= 4 || !dwarf_strict))
16015     {
16016       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16017         {
16018           expansion_failed (loc, NULL_RTX,
16019                             "DWARF address size mismatch");
16020           return 0;
16021         }
16022       if (ret)
16023         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16024       else
16025         add_loc_descr_to_each (list_ret,
16026                                new_loc_descr (DW_OP_stack_value, 0, 0));
16027       have_address = 1;
16028     }
16029   /* Show if we can't fill the request for an address.  */
16030   if (want_address && !have_address)
16031     {
16032       expansion_failed (loc, NULL_RTX,
16033                         "Want address and only have value");
16034       return 0;
16035     }
16036
16037   gcc_assert (!ret || !list_ret);
16038
16039   /* If we've got an address and don't want one, dereference.  */
16040   if (!want_address && have_address)
16041     {
16042       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16043
16044       if (size > DWARF2_ADDR_SIZE || size == -1)
16045         {
16046           expansion_failed (loc, NULL_RTX,
16047                             "DWARF address size mismatch");
16048           return 0;
16049         }
16050       else if (size == DWARF2_ADDR_SIZE)
16051         op = DW_OP_deref;
16052       else
16053         op = DW_OP_deref_size;
16054
16055       if (ret)
16056         add_loc_descr (&ret, new_loc_descr (op, size, 0));
16057       else
16058         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16059     }
16060   if (ret)
16061     list_ret = new_loc_list (ret, NULL, NULL, NULL);
16062
16063   return list_ret;
16064 }
16065
16066 /* Same as above but return only single location expression.  */
16067 static dw_loc_descr_ref
16068 loc_descriptor_from_tree (tree loc, int want_address)
16069 {
16070   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
16071   if (!ret)
16072     return NULL;
16073   if (ret->dw_loc_next)
16074     {
16075       expansion_failed (loc, NULL_RTX,
16076                         "Location list where only loc descriptor needed");
16077       return NULL;
16078     }
16079   return ret->expr;
16080 }
16081
16082 /* Given a value, round it up to the lowest multiple of `boundary'
16083    which is not less than the value itself.  */
16084
16085 static inline HOST_WIDE_INT
16086 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16087 {
16088   return (((value + boundary - 1) / boundary) * boundary);
16089 }
16090
16091 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16092    pointer to the declared type for the relevant field variable, or return
16093    `integer_type_node' if the given node turns out to be an
16094    ERROR_MARK node.  */
16095
16096 static inline tree
16097 field_type (const_tree decl)
16098 {
16099   tree type;
16100
16101   if (TREE_CODE (decl) == ERROR_MARK)
16102     return integer_type_node;
16103
16104   type = DECL_BIT_FIELD_TYPE (decl);
16105   if (type == NULL_TREE)
16106     type = TREE_TYPE (decl);
16107
16108   return type;
16109 }
16110
16111 /* Given a pointer to a tree node, return the alignment in bits for
16112    it, or else return BITS_PER_WORD if the node actually turns out to
16113    be an ERROR_MARK node.  */
16114
16115 static inline unsigned
16116 simple_type_align_in_bits (const_tree type)
16117 {
16118   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16119 }
16120
16121 static inline unsigned
16122 simple_decl_align_in_bits (const_tree decl)
16123 {
16124   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16125 }
16126
16127 /* Return the result of rounding T up to ALIGN.  */
16128
16129 static inline double_int
16130 round_up_to_align (double_int t, unsigned int align)
16131 {
16132   double_int alignd = uhwi_to_double_int (align);
16133   t = double_int_add (t, alignd);
16134   t = double_int_add (t, double_int_minus_one);
16135   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
16136   t = double_int_mul (t, alignd);
16137   return t;
16138 }
16139
16140 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
16141    lowest addressed byte of the "containing object" for the given FIELD_DECL,
16142    or return 0 if we are unable to determine what that offset is, either
16143    because the argument turns out to be a pointer to an ERROR_MARK node, or
16144    because the offset is actually variable.  (We can't handle the latter case
16145    just yet).  */
16146
16147 static HOST_WIDE_INT
16148 field_byte_offset (const_tree decl)
16149 {
16150   double_int object_offset_in_bits;
16151   double_int object_offset_in_bytes;
16152   double_int bitpos_int;
16153
16154   if (TREE_CODE (decl) == ERROR_MARK)
16155     return 0;
16156
16157   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16158
16159   /* We cannot yet cope with fields whose positions are variable, so
16160      for now, when we see such things, we simply return 0.  Someday, we may
16161      be able to handle such cases, but it will be damn difficult.  */
16162   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16163     return 0;
16164
16165   bitpos_int = tree_to_double_int (bit_position (decl));
16166
16167 #ifdef PCC_BITFIELD_TYPE_MATTERS
16168   if (PCC_BITFIELD_TYPE_MATTERS)
16169     {
16170       tree type;
16171       tree field_size_tree;
16172       double_int deepest_bitpos;
16173       double_int field_size_in_bits;
16174       unsigned int type_align_in_bits;
16175       unsigned int decl_align_in_bits;
16176       double_int type_size_in_bits;
16177
16178       type = field_type (decl);
16179       type_size_in_bits = double_int_type_size_in_bits (type);
16180       type_align_in_bits = simple_type_align_in_bits (type);
16181
16182       field_size_tree = DECL_SIZE (decl);
16183
16184       /* The size could be unspecified if there was an error, or for
16185          a flexible array member.  */
16186       if (!field_size_tree)
16187         field_size_tree = bitsize_zero_node;
16188
16189       /* If the size of the field is not constant, use the type size.  */
16190       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16191         field_size_in_bits = tree_to_double_int (field_size_tree);
16192       else
16193         field_size_in_bits = type_size_in_bits;
16194
16195       decl_align_in_bits = simple_decl_align_in_bits (decl);
16196
16197       /* The GCC front-end doesn't make any attempt to keep track of the
16198          starting bit offset (relative to the start of the containing
16199          structure type) of the hypothetical "containing object" for a
16200          bit-field.  Thus, when computing the byte offset value for the
16201          start of the "containing object" of a bit-field, we must deduce
16202          this information on our own. This can be rather tricky to do in
16203          some cases.  For example, handling the following structure type
16204          definition when compiling for an i386/i486 target (which only
16205          aligns long long's to 32-bit boundaries) can be very tricky:
16206
16207          struct S { int field1; long long field2:31; };
16208
16209          Fortunately, there is a simple rule-of-thumb which can be used
16210          in such cases.  When compiling for an i386/i486, GCC will
16211          allocate 8 bytes for the structure shown above.  It decides to
16212          do this based upon one simple rule for bit-field allocation.
16213          GCC allocates each "containing object" for each bit-field at
16214          the first (i.e. lowest addressed) legitimate alignment boundary
16215          (based upon the required minimum alignment for the declared
16216          type of the field) which it can possibly use, subject to the
16217          condition that there is still enough available space remaining
16218          in the containing object (when allocated at the selected point)
16219          to fully accommodate all of the bits of the bit-field itself.
16220
16221          This simple rule makes it obvious why GCC allocates 8 bytes for
16222          each object of the structure type shown above.  When looking
16223          for a place to allocate the "containing object" for `field2',
16224          the compiler simply tries to allocate a 64-bit "containing
16225          object" at each successive 32-bit boundary (starting at zero)
16226          until it finds a place to allocate that 64- bit field such that
16227          at least 31 contiguous (and previously unallocated) bits remain
16228          within that selected 64 bit field.  (As it turns out, for the
16229          example above, the compiler finds it is OK to allocate the
16230          "containing object" 64-bit field at bit-offset zero within the
16231          structure type.)
16232
16233          Here we attempt to work backwards from the limited set of facts
16234          we're given, and we try to deduce from those facts, where GCC
16235          must have believed that the containing object started (within
16236          the structure type). The value we deduce is then used (by the
16237          callers of this routine) to generate DW_AT_location and
16238          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16239          the case of DW_AT_location, regular fields as well).  */
16240
16241       /* Figure out the bit-distance from the start of the structure to
16242          the "deepest" bit of the bit-field.  */
16243       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16244
16245       /* This is the tricky part.  Use some fancy footwork to deduce
16246          where the lowest addressed bit of the containing object must
16247          be.  */
16248       object_offset_in_bits
16249         = double_int_sub (deepest_bitpos, type_size_in_bits);
16250
16251       /* Round up to type_align by default.  This works best for
16252          bitfields.  */
16253       object_offset_in_bits
16254         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16255
16256       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16257         {
16258           object_offset_in_bits
16259             = double_int_sub (deepest_bitpos, type_size_in_bits);
16260
16261           /* Round up to decl_align instead.  */
16262           object_offset_in_bits
16263             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16264         }
16265     }
16266   else
16267 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16268     object_offset_in_bits = bitpos_int;
16269
16270   object_offset_in_bytes
16271     = double_int_div (object_offset_in_bits,
16272                       uhwi_to_double_int (BITS_PER_UNIT), true,
16273                       TRUNC_DIV_EXPR);
16274   return double_int_to_shwi (object_offset_in_bytes);
16275 }
16276 \f
16277 /* The following routines define various Dwarf attributes and any data
16278    associated with them.  */
16279
16280 /* Add a location description attribute value to a DIE.
16281
16282    This emits location attributes suitable for whole variables and
16283    whole parameters.  Note that the location attributes for struct fields are
16284    generated by the routine `data_member_location_attribute' below.  */
16285
16286 static inline void
16287 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16288                              dw_loc_list_ref descr)
16289 {
16290   if (descr == 0)
16291     return;
16292   if (single_element_loc_list_p (descr))
16293     add_AT_loc (die, attr_kind, descr->expr);
16294   else
16295     add_AT_loc_list (die, attr_kind, descr);
16296 }
16297
16298 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16299
16300 static void
16301 add_accessibility_attribute (dw_die_ref die, tree decl)
16302 {
16303   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16304      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16305      the default has always been DW_ACCESS_public.  */
16306   if (TREE_PROTECTED (decl))
16307     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16308   else if (TREE_PRIVATE (decl))
16309     {
16310       if (dwarf_version == 2
16311           || die->die_parent == NULL
16312           || die->die_parent->die_tag != DW_TAG_class_type)
16313         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16314     }
16315   else if (dwarf_version > 2
16316            && die->die_parent
16317            && die->die_parent->die_tag == DW_TAG_class_type)
16318     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16319 }
16320
16321 /* Attach the specialized form of location attribute used for data members of
16322    struct and union types.  In the special case of a FIELD_DECL node which
16323    represents a bit-field, the "offset" part of this special location
16324    descriptor must indicate the distance in bytes from the lowest-addressed
16325    byte of the containing struct or union type to the lowest-addressed byte of
16326    the "containing object" for the bit-field.  (See the `field_byte_offset'
16327    function above).
16328
16329    For any given bit-field, the "containing object" is a hypothetical object
16330    (of some integral or enum type) within which the given bit-field lives.  The
16331    type of this hypothetical "containing object" is always the same as the
16332    declared type of the individual bit-field itself (for GCC anyway... the
16333    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16334    bytes) of the hypothetical "containing object" which will be given in the
16335    DW_AT_byte_size attribute for this bit-field.  (See the
16336    `byte_size_attribute' function below.)  It is also used when calculating the
16337    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16338    function below.)  */
16339
16340 static void
16341 add_data_member_location_attribute (dw_die_ref die, tree decl)
16342 {
16343   HOST_WIDE_INT offset;
16344   dw_loc_descr_ref loc_descr = 0;
16345
16346   if (TREE_CODE (decl) == TREE_BINFO)
16347     {
16348       /* We're working on the TAG_inheritance for a base class.  */
16349       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16350         {
16351           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16352              aren't at a fixed offset from all (sub)objects of the same
16353              type.  We need to extract the appropriate offset from our
16354              vtable.  The following dwarf expression means
16355
16356                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16357
16358              This is specific to the V3 ABI, of course.  */
16359
16360           dw_loc_descr_ref tmp;
16361
16362           /* Make a copy of the object address.  */
16363           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16364           add_loc_descr (&loc_descr, tmp);
16365
16366           /* Extract the vtable address.  */
16367           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16368           add_loc_descr (&loc_descr, tmp);
16369
16370           /* Calculate the address of the offset.  */
16371           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16372           gcc_assert (offset < 0);
16373
16374           tmp = int_loc_descriptor (-offset);
16375           add_loc_descr (&loc_descr, tmp);
16376           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16377           add_loc_descr (&loc_descr, tmp);
16378
16379           /* Extract the offset.  */
16380           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16381           add_loc_descr (&loc_descr, tmp);
16382
16383           /* Add it to the object address.  */
16384           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16385           add_loc_descr (&loc_descr, tmp);
16386         }
16387       else
16388         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16389     }
16390   else
16391     offset = field_byte_offset (decl);
16392
16393   if (! loc_descr)
16394     {
16395       if (dwarf_version > 2)
16396         {
16397           /* Don't need to output a location expression, just the constant. */
16398           if (offset < 0)
16399             add_AT_int (die, DW_AT_data_member_location, offset);
16400           else
16401             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16402           return;
16403         }
16404       else
16405         {
16406           enum dwarf_location_atom op;
16407
16408           /* The DWARF2 standard says that we should assume that the structure
16409              address is already on the stack, so we can specify a structure
16410              field address by using DW_OP_plus_uconst.  */
16411
16412 #ifdef MIPS_DEBUGGING_INFO
16413           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16414              operator correctly.  It works only if we leave the offset on the
16415              stack.  */
16416           op = DW_OP_constu;
16417 #else
16418           op = DW_OP_plus_uconst;
16419 #endif
16420
16421           loc_descr = new_loc_descr (op, offset, 0);
16422         }
16423     }
16424
16425   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16426 }
16427
16428 /* Writes integer values to dw_vec_const array.  */
16429
16430 static void
16431 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16432 {
16433   while (size != 0)
16434     {
16435       *dest++ = val & 0xff;
16436       val >>= 8;
16437       --size;
16438     }
16439 }
16440
16441 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16442
16443 static HOST_WIDE_INT
16444 extract_int (const unsigned char *src, unsigned int size)
16445 {
16446   HOST_WIDE_INT val = 0;
16447
16448   src += size;
16449   while (size != 0)
16450     {
16451       val <<= 8;
16452       val |= *--src & 0xff;
16453       --size;
16454     }
16455   return val;
16456 }
16457
16458 /* Writes double_int values to dw_vec_const array.  */
16459
16460 static void
16461 insert_double (double_int val, unsigned char *dest)
16462 {
16463   unsigned char *p0 = dest;
16464   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16465
16466   if (WORDS_BIG_ENDIAN)
16467     {
16468       p0 = p1;
16469       p1 = dest;
16470     }
16471
16472   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16473   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16474 }
16475
16476 /* Writes floating point values to dw_vec_const array.  */
16477
16478 static void
16479 insert_float (const_rtx rtl, unsigned char *array)
16480 {
16481   REAL_VALUE_TYPE rv;
16482   long val[4];
16483   int i;
16484
16485   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16486   real_to_target (val, &rv, GET_MODE (rtl));
16487
16488   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16489   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16490     {
16491       insert_int (val[i], 4, array);
16492       array += 4;
16493     }
16494 }
16495
16496 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16497    does not have a "location" either in memory or in a register.  These
16498    things can arise in GNU C when a constant is passed as an actual parameter
16499    to an inlined function.  They can also arise in C++ where declared
16500    constants do not necessarily get memory "homes".  */
16501
16502 static bool
16503 add_const_value_attribute (dw_die_ref die, rtx rtl)
16504 {
16505   switch (GET_CODE (rtl))
16506     {
16507     case CONST_INT:
16508       {
16509         HOST_WIDE_INT val = INTVAL (rtl);
16510
16511         if (val < 0)
16512           add_AT_int (die, DW_AT_const_value, val);
16513         else
16514           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16515       }
16516       return true;
16517
16518     case CONST_DOUBLE:
16519       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16520          floating-point constant.  A CONST_DOUBLE is used whenever the
16521          constant requires more than one word in order to be adequately
16522          represented.  */
16523       {
16524         enum machine_mode mode = GET_MODE (rtl);
16525
16526         if (SCALAR_FLOAT_MODE_P (mode))
16527           {
16528             unsigned int length = GET_MODE_SIZE (mode);
16529             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16530
16531             insert_float (rtl, array);
16532             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16533           }
16534         else
16535           add_AT_double (die, DW_AT_const_value,
16536                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16537       }
16538       return true;
16539
16540     case CONST_VECTOR:
16541       {
16542         enum machine_mode mode = GET_MODE (rtl);
16543         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16544         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16545         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16546           (length * elt_size);
16547         unsigned int i;
16548         unsigned char *p;
16549
16550         switch (GET_MODE_CLASS (mode))
16551           {
16552           case MODE_VECTOR_INT:
16553             for (i = 0, p = array; i < length; i++, p += elt_size)
16554               {
16555                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16556                 double_int val = rtx_to_double_int (elt);
16557
16558                 if (elt_size <= sizeof (HOST_WIDE_INT))
16559                   insert_int (double_int_to_shwi (val), elt_size, p);
16560                 else
16561                   {
16562                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16563                     insert_double (val, p);
16564                   }
16565               }
16566             break;
16567
16568           case MODE_VECTOR_FLOAT:
16569             for (i = 0, p = array; i < length; i++, p += elt_size)
16570               {
16571                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16572                 insert_float (elt, p);
16573               }
16574             break;
16575
16576           default:
16577             gcc_unreachable ();
16578           }
16579
16580         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16581       }
16582       return true;
16583
16584     case CONST_STRING:
16585       if (dwarf_version >= 4 || !dwarf_strict)
16586         {
16587           dw_loc_descr_ref loc_result;
16588           resolve_one_addr (&rtl, NULL);
16589         rtl_addr:
16590           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16591           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16592           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16593           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16594           add_AT_loc (die, DW_AT_location, loc_result);
16595           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16596           return true;
16597         }
16598       return false;
16599
16600     case CONST:
16601       if (CONSTANT_P (XEXP (rtl, 0)))
16602         return add_const_value_attribute (die, XEXP (rtl, 0));
16603       /* FALLTHROUGH */
16604     case SYMBOL_REF:
16605       if (!const_ok_for_output (rtl))
16606         return false;
16607     case LABEL_REF:
16608       if (dwarf_version >= 4 || !dwarf_strict)
16609         goto rtl_addr;
16610       return false;
16611
16612     case PLUS:
16613       /* In cases where an inlined instance of an inline function is passed
16614          the address of an `auto' variable (which is local to the caller) we
16615          can get a situation where the DECL_RTL of the artificial local
16616          variable (for the inlining) which acts as a stand-in for the
16617          corresponding formal parameter (of the inline function) will look
16618          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16619          exactly a compile-time constant expression, but it isn't the address
16620          of the (artificial) local variable either.  Rather, it represents the
16621          *value* which the artificial local variable always has during its
16622          lifetime.  We currently have no way to represent such quasi-constant
16623          values in Dwarf, so for now we just punt and generate nothing.  */
16624       return false;
16625
16626     case HIGH:
16627     case CONST_FIXED:
16628       return false;
16629
16630     case MEM:
16631       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16632           && MEM_READONLY_P (rtl)
16633           && GET_MODE (rtl) == BLKmode)
16634         {
16635           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16636           return true;
16637         }
16638       return false;
16639
16640     default:
16641       /* No other kinds of rtx should be possible here.  */
16642       gcc_unreachable ();
16643     }
16644   return false;
16645 }
16646
16647 /* Determine whether the evaluation of EXPR references any variables
16648    or functions which aren't otherwise used (and therefore may not be
16649    output).  */
16650 static tree
16651 reference_to_unused (tree * tp, int * walk_subtrees,
16652                      void * data ATTRIBUTE_UNUSED)
16653 {
16654   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16655     *walk_subtrees = 0;
16656
16657   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16658       && ! TREE_ASM_WRITTEN (*tp))
16659     return *tp;
16660   /* ???  The C++ FE emits debug information for using decls, so
16661      putting gcc_unreachable here falls over.  See PR31899.  For now
16662      be conservative.  */
16663   else if (!cgraph_global_info_ready
16664            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16665     return *tp;
16666   else if (TREE_CODE (*tp) == VAR_DECL)
16667     {
16668       struct varpool_node *node = varpool_get_node (*tp);
16669       if (!node || !node->needed)
16670         return *tp;
16671     }
16672   else if (TREE_CODE (*tp) == FUNCTION_DECL
16673            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16674     {
16675       /* The call graph machinery must have finished analyzing,
16676          optimizing and gimplifying the CU by now.
16677          So if *TP has no call graph node associated
16678          to it, it means *TP will not be emitted.  */
16679       if (!cgraph_get_node (*tp))
16680         return *tp;
16681     }
16682   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16683     return *tp;
16684
16685   return NULL_TREE;
16686 }
16687
16688 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16689    for use in a later add_const_value_attribute call.  */
16690
16691 static rtx
16692 rtl_for_decl_init (tree init, tree type)
16693 {
16694   rtx rtl = NULL_RTX;
16695
16696   STRIP_NOPS (init);
16697
16698   /* If a variable is initialized with a string constant without embedded
16699      zeros, build CONST_STRING.  */
16700   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16701     {
16702       tree enttype = TREE_TYPE (type);
16703       tree domain = TYPE_DOMAIN (type);
16704       enum machine_mode mode = TYPE_MODE (enttype);
16705
16706       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16707           && domain
16708           && integer_zerop (TYPE_MIN_VALUE (domain))
16709           && compare_tree_int (TYPE_MAX_VALUE (domain),
16710                                TREE_STRING_LENGTH (init) - 1) == 0
16711           && ((size_t) TREE_STRING_LENGTH (init)
16712               == strlen (TREE_STRING_POINTER (init)) + 1))
16713         {
16714           rtl = gen_rtx_CONST_STRING (VOIDmode,
16715                                       ggc_strdup (TREE_STRING_POINTER (init)));
16716           rtl = gen_rtx_MEM (BLKmode, rtl);
16717           MEM_READONLY_P (rtl) = 1;
16718         }
16719     }
16720   /* Other aggregates, and complex values, could be represented using
16721      CONCAT: FIXME!  */
16722   else if (AGGREGATE_TYPE_P (type)
16723            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
16724                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
16725            || TREE_CODE (type) == COMPLEX_TYPE)
16726     ;
16727   /* Vectors only work if their mode is supported by the target.
16728      FIXME: generic vectors ought to work too.  */
16729   else if (TREE_CODE (type) == VECTOR_TYPE
16730            && !VECTOR_MODE_P (TYPE_MODE (type)))
16731     ;
16732   /* If the initializer is something that we know will expand into an
16733      immediate RTL constant, expand it now.  We must be careful not to
16734      reference variables which won't be output.  */
16735   else if (initializer_constant_valid_p (init, type)
16736            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16737     {
16738       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16739          possible.  */
16740       if (TREE_CODE (type) == VECTOR_TYPE)
16741         switch (TREE_CODE (init))
16742           {
16743           case VECTOR_CST:
16744             break;
16745           case CONSTRUCTOR:
16746             if (TREE_CONSTANT (init))
16747               {
16748                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16749                 bool constant_p = true;
16750                 tree value;
16751                 unsigned HOST_WIDE_INT ix;
16752
16753                 /* Even when ctor is constant, it might contain non-*_CST
16754                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16755                    belong into VECTOR_CST nodes.  */
16756                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16757                   if (!CONSTANT_CLASS_P (value))
16758                     {
16759                       constant_p = false;
16760                       break;
16761                     }
16762
16763                 if (constant_p)
16764                   {
16765                     init = build_vector_from_ctor (type, elts);
16766                     break;
16767                   }
16768               }
16769             /* FALLTHRU */
16770
16771           default:
16772             return NULL;
16773           }
16774
16775       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16776
16777       /* If expand_expr returns a MEM, it wasn't immediate.  */
16778       gcc_assert (!rtl || !MEM_P (rtl));
16779     }
16780
16781   return rtl;
16782 }
16783
16784 /* Generate RTL for the variable DECL to represent its location.  */
16785
16786 static rtx
16787 rtl_for_decl_location (tree decl)
16788 {
16789   rtx rtl;
16790
16791   /* Here we have to decide where we are going to say the parameter "lives"
16792      (as far as the debugger is concerned).  We only have a couple of
16793      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16794
16795      DECL_RTL normally indicates where the parameter lives during most of the
16796      activation of the function.  If optimization is enabled however, this
16797      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16798      that the parameter doesn't really live anywhere (as far as the code
16799      generation parts of GCC are concerned) during most of the function's
16800      activation.  That will happen (for example) if the parameter is never
16801      referenced within the function.
16802
16803      We could just generate a location descriptor here for all non-NULL
16804      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16805      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16806      where DECL_RTL is NULL or is a pseudo-reg.
16807
16808      Note however that we can only get away with using DECL_INCOMING_RTL as
16809      a backup substitute for DECL_RTL in certain limited cases.  In cases
16810      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16811      we can be sure that the parameter was passed using the same type as it is
16812      declared to have within the function, and that its DECL_INCOMING_RTL
16813      points us to a place where a value of that type is passed.
16814
16815      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16816      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16817      because in these cases DECL_INCOMING_RTL points us to a value of some
16818      type which is *different* from the type of the parameter itself.  Thus,
16819      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16820      such cases, the debugger would end up (for example) trying to fetch a
16821      `float' from a place which actually contains the first part of a
16822      `double'.  That would lead to really incorrect and confusing
16823      output at debug-time.
16824
16825      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16826      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16827      are a couple of exceptions however.  On little-endian machines we can
16828      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16829      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16830      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16831      when (on a little-endian machine) a non-prototyped function has a
16832      parameter declared to be of type `short' or `char'.  In such cases,
16833      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16834      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16835      passed `int' value.  If the debugger then uses that address to fetch
16836      a `short' or a `char' (on a little-endian machine) the result will be
16837      the correct data, so we allow for such exceptional cases below.
16838
16839      Note that our goal here is to describe the place where the given formal
16840      parameter lives during most of the function's activation (i.e. between the
16841      end of the prologue and the start of the epilogue).  We'll do that as best
16842      as we can. Note however that if the given formal parameter is modified
16843      sometime during the execution of the function, then a stack backtrace (at
16844      debug-time) will show the function as having been called with the *new*
16845      value rather than the value which was originally passed in.  This happens
16846      rarely enough that it is not a major problem, but it *is* a problem, and
16847      I'd like to fix it.
16848
16849      A future version of dwarf2out.c may generate two additional attributes for
16850      any given DW_TAG_formal_parameter DIE which will describe the "passed
16851      type" and the "passed location" for the given formal parameter in addition
16852      to the attributes we now generate to indicate the "declared type" and the
16853      "active location" for each parameter.  This additional set of attributes
16854      could be used by debuggers for stack backtraces. Separately, note that
16855      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16856      This happens (for example) for inlined-instances of inline function formal
16857      parameters which are never referenced.  This really shouldn't be
16858      happening.  All PARM_DECL nodes should get valid non-NULL
16859      DECL_INCOMING_RTL values.  FIXME.  */
16860
16861   /* Use DECL_RTL as the "location" unless we find something better.  */
16862   rtl = DECL_RTL_IF_SET (decl);
16863
16864   /* When generating abstract instances, ignore everything except
16865      constants, symbols living in memory, and symbols living in
16866      fixed registers.  */
16867   if (! reload_completed)
16868     {
16869       if (rtl
16870           && (CONSTANT_P (rtl)
16871               || (MEM_P (rtl)
16872                   && CONSTANT_P (XEXP (rtl, 0)))
16873               || (REG_P (rtl)
16874                   && TREE_CODE (decl) == VAR_DECL
16875                   && TREE_STATIC (decl))))
16876         {
16877           rtl = targetm.delegitimize_address (rtl);
16878           return rtl;
16879         }
16880       rtl = NULL_RTX;
16881     }
16882   else if (TREE_CODE (decl) == PARM_DECL)
16883     {
16884       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16885         {
16886           tree declared_type = TREE_TYPE (decl);
16887           tree passed_type = DECL_ARG_TYPE (decl);
16888           enum machine_mode dmode = TYPE_MODE (declared_type);
16889           enum machine_mode pmode = TYPE_MODE (passed_type);
16890
16891           /* This decl represents a formal parameter which was optimized out.
16892              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16893              all cases where (rtl == NULL_RTX) just below.  */
16894           if (dmode == pmode)
16895             rtl = DECL_INCOMING_RTL (decl);
16896           else if (SCALAR_INT_MODE_P (dmode)
16897                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16898                    && DECL_INCOMING_RTL (decl))
16899             {
16900               rtx inc = DECL_INCOMING_RTL (decl);
16901               if (REG_P (inc))
16902                 rtl = inc;
16903               else if (MEM_P (inc))
16904                 {
16905                   if (BYTES_BIG_ENDIAN)
16906                     rtl = adjust_address_nv (inc, dmode,
16907                                              GET_MODE_SIZE (pmode)
16908                                              - GET_MODE_SIZE (dmode));
16909                   else
16910                     rtl = inc;
16911                 }
16912             }
16913         }
16914
16915       /* If the parm was passed in registers, but lives on the stack, then
16916          make a big endian correction if the mode of the type of the
16917          parameter is not the same as the mode of the rtl.  */
16918       /* ??? This is the same series of checks that are made in dbxout.c before
16919          we reach the big endian correction code there.  It isn't clear if all
16920          of these checks are necessary here, but keeping them all is the safe
16921          thing to do.  */
16922       else if (MEM_P (rtl)
16923                && XEXP (rtl, 0) != const0_rtx
16924                && ! CONSTANT_P (XEXP (rtl, 0))
16925                /* Not passed in memory.  */
16926                && !MEM_P (DECL_INCOMING_RTL (decl))
16927                /* Not passed by invisible reference.  */
16928                && (!REG_P (XEXP (rtl, 0))
16929                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16930                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16931 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
16932                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16933 #endif
16934                      )
16935                /* Big endian correction check.  */
16936                && BYTES_BIG_ENDIAN
16937                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16938                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16939                    < UNITS_PER_WORD))
16940         {
16941           int offset = (UNITS_PER_WORD
16942                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16943
16944           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16945                              plus_constant (XEXP (rtl, 0), offset));
16946         }
16947     }
16948   else if (TREE_CODE (decl) == VAR_DECL
16949            && rtl
16950            && MEM_P (rtl)
16951            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16952            && BYTES_BIG_ENDIAN)
16953     {
16954       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16955       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16956
16957       /* If a variable is declared "register" yet is smaller than
16958          a register, then if we store the variable to memory, it
16959          looks like we're storing a register-sized value, when in
16960          fact we are not.  We need to adjust the offset of the
16961          storage location to reflect the actual value's bytes,
16962          else gdb will not be able to display it.  */
16963       if (rsize > dsize)
16964         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16965                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16966     }
16967
16968   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16969      and will have been substituted directly into all expressions that use it.
16970      C does not have such a concept, but C++ and other languages do.  */
16971   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16972     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16973
16974   if (rtl)
16975     rtl = targetm.delegitimize_address (rtl);
16976
16977   /* If we don't look past the constant pool, we risk emitting a
16978      reference to a constant pool entry that isn't referenced from
16979      code, and thus is not emitted.  */
16980   if (rtl)
16981     rtl = avoid_constant_pool_reference (rtl);
16982
16983   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16984      in the current CU, resolve_addr will remove the expression referencing
16985      it.  */
16986   if (rtl == NULL_RTX
16987       && TREE_CODE (decl) == VAR_DECL
16988       && !DECL_EXTERNAL (decl)
16989       && TREE_STATIC (decl)
16990       && DECL_NAME (decl)
16991       && !DECL_HARD_REGISTER (decl)
16992       && DECL_MODE (decl) != VOIDmode)
16993     {
16994       rtl = make_decl_rtl_for_debug (decl);
16995       if (!MEM_P (rtl)
16996           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16997           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16998         rtl = NULL_RTX;
16999     }
17000
17001   return rtl;
17002 }
17003
17004 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17005    returned.  If so, the decl for the COMMON block is returned, and the
17006    value is the offset into the common block for the symbol.  */
17007
17008 static tree
17009 fortran_common (tree decl, HOST_WIDE_INT *value)
17010 {
17011   tree val_expr, cvar;
17012   enum machine_mode mode;
17013   HOST_WIDE_INT bitsize, bitpos;
17014   tree offset;
17015   int volatilep = 0, unsignedp = 0;
17016
17017   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17018      it does not have a value (the offset into the common area), or if it
17019      is thread local (as opposed to global) then it isn't common, and shouldn't
17020      be handled as such.  */
17021   if (TREE_CODE (decl) != VAR_DECL
17022       || !TREE_STATIC (decl)
17023       || !DECL_HAS_VALUE_EXPR_P (decl)
17024       || !is_fortran ())
17025     return NULL_TREE;
17026
17027   val_expr = DECL_VALUE_EXPR (decl);
17028   if (TREE_CODE (val_expr) != COMPONENT_REF)
17029     return NULL_TREE;
17030
17031   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
17032                               &mode, &unsignedp, &volatilep, true);
17033
17034   if (cvar == NULL_TREE
17035       || TREE_CODE (cvar) != VAR_DECL
17036       || DECL_ARTIFICIAL (cvar)
17037       || !TREE_PUBLIC (cvar))
17038     return NULL_TREE;
17039
17040   *value = 0;
17041   if (offset != NULL)
17042     {
17043       if (!host_integerp (offset, 0))
17044         return NULL_TREE;
17045       *value = tree_low_cst (offset, 0);
17046     }
17047   if (bitpos != 0)
17048     *value += bitpos / BITS_PER_UNIT;
17049
17050   return cvar;
17051 }
17052
17053 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17054    data attribute for a variable or a parameter.  We generate the
17055    DW_AT_const_value attribute only in those cases where the given variable
17056    or parameter does not have a true "location" either in memory or in a
17057    register.  This can happen (for example) when a constant is passed as an
17058    actual argument in a call to an inline function.  (It's possible that
17059    these things can crop up in other ways also.)  Note that one type of
17060    constant value which can be passed into an inlined function is a constant
17061    pointer.  This can happen for example if an actual argument in an inlined
17062    function call evaluates to a compile-time constant address.  */
17063
17064 static bool
17065 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
17066                                        enum dwarf_attribute attr)
17067 {
17068   rtx rtl;
17069   dw_loc_list_ref list;
17070   var_loc_list *loc_list;
17071
17072   if (TREE_CODE (decl) == ERROR_MARK)
17073     return false;
17074
17075   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17076               || TREE_CODE (decl) == RESULT_DECL);
17077
17078   /* Try to get some constant RTL for this decl, and use that as the value of
17079      the location.  */
17080
17081   rtl = rtl_for_decl_location (decl);
17082   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17083       && add_const_value_attribute (die, rtl))
17084     return true;
17085
17086   /* See if we have single element location list that is equivalent to
17087      a constant value.  That way we are better to use add_const_value_attribute
17088      rather than expanding constant value equivalent.  */
17089   loc_list = lookup_decl_loc (decl);
17090   if (loc_list
17091       && loc_list->first
17092       && loc_list->first->next == NULL
17093       && NOTE_P (loc_list->first->loc)
17094       && NOTE_VAR_LOCATION (loc_list->first->loc)
17095       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17096     {
17097       struct var_loc_node *node;
17098
17099       node = loc_list->first;
17100       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17101       if (GET_CODE (rtl) == EXPR_LIST)
17102         rtl = XEXP (rtl, 0);
17103       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17104           && add_const_value_attribute (die, rtl))
17105          return true;
17106     }
17107   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
17108   if (list)
17109     {
17110       add_AT_location_description (die, attr, list);
17111       return true;
17112     }
17113   /* None of that worked, so it must not really have a location;
17114      try adding a constant value attribute from the DECL_INITIAL.  */
17115   return tree_add_const_value_attribute_for_decl (die, decl);
17116 }
17117
17118 /* Add VARIABLE and DIE into deferred locations list.  */
17119
17120 static void
17121 defer_location (tree variable, dw_die_ref die)
17122 {
17123   deferred_locations entry;
17124   entry.variable = variable;
17125   entry.die = die;
17126   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
17127 }
17128
17129 /* Helper function for tree_add_const_value_attribute.  Natively encode
17130    initializer INIT into an array.  Return true if successful.  */
17131
17132 static bool
17133 native_encode_initializer (tree init, unsigned char *array, int size)
17134 {
17135   tree type;
17136
17137   if (init == NULL_TREE)
17138     return false;
17139
17140   STRIP_NOPS (init);
17141   switch (TREE_CODE (init))
17142     {
17143     case STRING_CST:
17144       type = TREE_TYPE (init);
17145       if (TREE_CODE (type) == ARRAY_TYPE)
17146         {
17147           tree enttype = TREE_TYPE (type);
17148           enum machine_mode mode = TYPE_MODE (enttype);
17149
17150           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17151             return false;
17152           if (int_size_in_bytes (type) != size)
17153             return false;
17154           if (size > TREE_STRING_LENGTH (init))
17155             {
17156               memcpy (array, TREE_STRING_POINTER (init),
17157                       TREE_STRING_LENGTH (init));
17158               memset (array + TREE_STRING_LENGTH (init),
17159                       '\0', size - TREE_STRING_LENGTH (init));
17160             }
17161           else
17162             memcpy (array, TREE_STRING_POINTER (init), size);
17163           return true;
17164         }
17165       return false;
17166     case CONSTRUCTOR:
17167       type = TREE_TYPE (init);
17168       if (int_size_in_bytes (type) != size)
17169         return false;
17170       if (TREE_CODE (type) == ARRAY_TYPE)
17171         {
17172           HOST_WIDE_INT min_index;
17173           unsigned HOST_WIDE_INT cnt;
17174           int curpos = 0, fieldsize;
17175           constructor_elt *ce;
17176
17177           if (TYPE_DOMAIN (type) == NULL_TREE
17178               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17179             return false;
17180
17181           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17182           if (fieldsize <= 0)
17183             return false;
17184
17185           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17186           memset (array, '\0', size);
17187           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17188             {
17189               tree val = ce->value;
17190               tree index = ce->index;
17191               int pos = curpos;
17192               if (index && TREE_CODE (index) == RANGE_EXPR)
17193                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17194                       * fieldsize;
17195               else if (index)
17196                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17197
17198               if (val)
17199                 {
17200                   STRIP_NOPS (val);
17201                   if (!native_encode_initializer (val, array + pos, fieldsize))
17202                     return false;
17203                 }
17204               curpos = pos + fieldsize;
17205               if (index && TREE_CODE (index) == RANGE_EXPR)
17206                 {
17207                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17208                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17209                   while (count > 0)
17210                     {
17211                       if (val)
17212                         memcpy (array + curpos, array + pos, fieldsize);
17213                       curpos += fieldsize;
17214                     }
17215                 }
17216               gcc_assert (curpos <= size);
17217             }
17218           return true;
17219         }
17220       else if (TREE_CODE (type) == RECORD_TYPE
17221                || TREE_CODE (type) == UNION_TYPE)
17222         {
17223           tree field = NULL_TREE;
17224           unsigned HOST_WIDE_INT cnt;
17225           constructor_elt *ce;
17226
17227           if (int_size_in_bytes (type) != size)
17228             return false;
17229
17230           if (TREE_CODE (type) == RECORD_TYPE)
17231             field = TYPE_FIELDS (type);
17232
17233           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17234             {
17235               tree val = ce->value;
17236               int pos, fieldsize;
17237
17238               if (ce->index != 0)
17239                 field = ce->index;
17240
17241               if (val)
17242                 STRIP_NOPS (val);
17243
17244               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17245                 return false;
17246
17247               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17248                   && TYPE_DOMAIN (TREE_TYPE (field))
17249                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17250                 return false;
17251               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17252                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17253                 return false;
17254               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17255               pos = int_byte_position (field);
17256               gcc_assert (pos + fieldsize <= size);
17257               if (val
17258                   && !native_encode_initializer (val, array + pos, fieldsize))
17259                 return false;
17260             }
17261           return true;
17262         }
17263       return false;
17264     case VIEW_CONVERT_EXPR:
17265     case NON_LVALUE_EXPR:
17266       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17267     default:
17268       return native_encode_expr (init, array, size) == size;
17269     }
17270 }
17271
17272 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17273    attribute is the const value T.  */
17274
17275 static bool
17276 tree_add_const_value_attribute (dw_die_ref die, tree t)
17277 {
17278   tree init;
17279   tree type = TREE_TYPE (t);
17280   rtx rtl;
17281
17282   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17283     return false;
17284
17285   init = t;
17286   gcc_assert (!DECL_P (init));
17287
17288   rtl = rtl_for_decl_init (init, type);
17289   if (rtl)
17290     return add_const_value_attribute (die, rtl);
17291   /* If the host and target are sane, try harder.  */
17292   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17293            && initializer_constant_valid_p (init, type))
17294     {
17295       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17296       if (size > 0 && (int) size == size)
17297         {
17298           unsigned char *array = (unsigned char *)
17299             ggc_alloc_cleared_atomic (size);
17300
17301           if (native_encode_initializer (init, array, size))
17302             {
17303               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17304               return true;
17305             }
17306         }
17307     }
17308   return false;
17309 }
17310
17311 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17312    attribute is the const value of T, where T is an integral constant
17313    variable with static storage duration
17314    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17315
17316 static bool
17317 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17318 {
17319
17320   if (!decl
17321       || (TREE_CODE (decl) != VAR_DECL
17322           && TREE_CODE (decl) != CONST_DECL))
17323     return false;
17324
17325     if (TREE_READONLY (decl)
17326         && ! TREE_THIS_VOLATILE (decl)
17327         && DECL_INITIAL (decl))
17328       /* OK */;
17329     else
17330       return false;
17331
17332   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17333   if (get_AT (var_die, DW_AT_const_value))
17334     return false;
17335
17336   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17337 }
17338
17339 /* Convert the CFI instructions for the current function into a
17340    location list.  This is used for DW_AT_frame_base when we targeting
17341    a dwarf2 consumer that does not support the dwarf3
17342    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17343    expressions.  */
17344
17345 static dw_loc_list_ref
17346 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17347 {
17348   dw_fde_ref fde;
17349   dw_loc_list_ref list, *list_tail;
17350   dw_cfi_ref cfi;
17351   dw_cfa_location last_cfa, next_cfa;
17352   const char *start_label, *last_label, *section;
17353   dw_cfa_location remember;
17354
17355   fde = current_fde ();
17356   gcc_assert (fde != NULL);
17357
17358   section = secname_for_decl (current_function_decl);
17359   list_tail = &list;
17360   list = NULL;
17361
17362   memset (&next_cfa, 0, sizeof (next_cfa));
17363   next_cfa.reg = INVALID_REGNUM;
17364   remember = next_cfa;
17365
17366   start_label = fde->dw_fde_begin;
17367
17368   /* ??? Bald assumption that the CIE opcode list does not contain
17369      advance opcodes.  */
17370   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
17371     lookup_cfa_1 (cfi, &next_cfa, &remember);
17372
17373   last_cfa = next_cfa;
17374   last_label = start_label;
17375
17376   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
17377     switch (cfi->dw_cfi_opc)
17378       {
17379       case DW_CFA_set_loc:
17380       case DW_CFA_advance_loc1:
17381       case DW_CFA_advance_loc2:
17382       case DW_CFA_advance_loc4:
17383         if (!cfa_equal_p (&last_cfa, &next_cfa))
17384           {
17385             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17386                                        start_label, last_label, section);
17387
17388             list_tail = &(*list_tail)->dw_loc_next;
17389             last_cfa = next_cfa;
17390             start_label = last_label;
17391           }
17392         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17393         break;
17394
17395       case DW_CFA_advance_loc:
17396         /* The encoding is complex enough that we should never emit this.  */
17397         gcc_unreachable ();
17398
17399       default:
17400         lookup_cfa_1 (cfi, &next_cfa, &remember);
17401         break;
17402       }
17403
17404   if (!cfa_equal_p (&last_cfa, &next_cfa))
17405     {
17406       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17407                                  start_label, last_label, section);
17408       list_tail = &(*list_tail)->dw_loc_next;
17409       start_label = last_label;
17410     }
17411
17412   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17413                              start_label, fde->dw_fde_end, section);
17414
17415   if (list && list->dw_loc_next)
17416     gen_llsym (list);
17417
17418   return list;
17419 }
17420
17421 /* Compute a displacement from the "steady-state frame pointer" to the
17422    frame base (often the same as the CFA), and store it in
17423    frame_pointer_fb_offset.  OFFSET is added to the displacement
17424    before the latter is negated.  */
17425
17426 static void
17427 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17428 {
17429   rtx reg, elim;
17430
17431 #ifdef FRAME_POINTER_CFA_OFFSET
17432   reg = frame_pointer_rtx;
17433   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17434 #else
17435   reg = arg_pointer_rtx;
17436   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17437 #endif
17438
17439   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17440   if (GET_CODE (elim) == PLUS)
17441     {
17442       offset += INTVAL (XEXP (elim, 1));
17443       elim = XEXP (elim, 0);
17444     }
17445
17446   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17447                && (elim == hard_frame_pointer_rtx
17448                    || elim == stack_pointer_rtx))
17449               || elim == (frame_pointer_needed
17450                           ? hard_frame_pointer_rtx
17451                           : stack_pointer_rtx));
17452
17453   frame_pointer_fb_offset = -offset;
17454 }
17455
17456 /* Generate a DW_AT_name attribute given some string value to be included as
17457    the value of the attribute.  */
17458
17459 static void
17460 add_name_attribute (dw_die_ref die, const char *name_string)
17461 {
17462   if (name_string != NULL && *name_string != 0)
17463     {
17464       if (demangle_name_func)
17465         name_string = (*demangle_name_func) (name_string);
17466
17467       add_AT_string (die, DW_AT_name, name_string);
17468     }
17469 }
17470
17471 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17472
17473 static void
17474 add_comp_dir_attribute (dw_die_ref die)
17475 {
17476   const char *wd = get_src_pwd ();
17477   char *wd1;
17478
17479   if (wd == NULL)
17480     return;
17481
17482   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17483     {
17484       int wdlen;
17485
17486       wdlen = strlen (wd);
17487       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17488       strcpy (wd1, wd);
17489       wd1 [wdlen] = DIR_SEPARATOR;
17490       wd1 [wdlen + 1] = 0;
17491       wd = wd1;
17492     }
17493
17494     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17495 }
17496
17497 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17498    default.  */
17499
17500 static int
17501 lower_bound_default (void)
17502 {
17503   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17504     {
17505     case DW_LANG_C:
17506     case DW_LANG_C89:
17507     case DW_LANG_C99:
17508     case DW_LANG_C_plus_plus:
17509     case DW_LANG_ObjC:
17510     case DW_LANG_ObjC_plus_plus:
17511     case DW_LANG_Java:
17512       return 0;
17513     case DW_LANG_Fortran77:
17514     case DW_LANG_Fortran90:
17515     case DW_LANG_Fortran95:
17516       return 1;
17517     case DW_LANG_UPC:
17518     case DW_LANG_D:
17519     case DW_LANG_Python:
17520       return dwarf_version >= 4 ? 0 : -1;
17521     case DW_LANG_Ada95:
17522     case DW_LANG_Ada83:
17523     case DW_LANG_Cobol74:
17524     case DW_LANG_Cobol85:
17525     case DW_LANG_Pascal83:
17526     case DW_LANG_Modula2:
17527     case DW_LANG_PLI:
17528       return dwarf_version >= 4 ? 1 : -1;
17529     default:
17530       return -1;
17531     }
17532 }
17533
17534 /* Given a tree node describing an array bound (either lower or upper) output
17535    a representation for that bound.  */
17536
17537 static void
17538 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17539 {
17540   switch (TREE_CODE (bound))
17541     {
17542     case ERROR_MARK:
17543       return;
17544
17545     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17546     case INTEGER_CST:
17547       {
17548         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17549         int dflt;
17550
17551         /* Use the default if possible.  */
17552         if (bound_attr == DW_AT_lower_bound
17553             && host_integerp (bound, 0)
17554             && (dflt = lower_bound_default ()) != -1
17555             && tree_low_cst (bound, 0) == dflt)
17556           ;
17557
17558         /* Otherwise represent the bound as an unsigned value with the
17559            precision of its type.  The precision and signedness of the
17560            type will be necessary to re-interpret it unambiguously.  */
17561         else if (prec < HOST_BITS_PER_WIDE_INT)
17562           {
17563             unsigned HOST_WIDE_INT mask
17564               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17565             add_AT_unsigned (subrange_die, bound_attr,
17566                              TREE_INT_CST_LOW (bound) & mask);
17567           }
17568         else if (prec == HOST_BITS_PER_WIDE_INT
17569                  || TREE_INT_CST_HIGH (bound) == 0)
17570           add_AT_unsigned (subrange_die, bound_attr,
17571                            TREE_INT_CST_LOW (bound));
17572         else
17573           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17574                          TREE_INT_CST_LOW (bound));
17575       }
17576       break;
17577
17578     CASE_CONVERT:
17579     case VIEW_CONVERT_EXPR:
17580       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17581       break;
17582
17583     case SAVE_EXPR:
17584       break;
17585
17586     case VAR_DECL:
17587     case PARM_DECL:
17588     case RESULT_DECL:
17589       {
17590         dw_die_ref decl_die = lookup_decl_die (bound);
17591
17592         /* ??? Can this happen, or should the variable have been bound
17593            first?  Probably it can, since I imagine that we try to create
17594            the types of parameters in the order in which they exist in
17595            the list, and won't have created a forward reference to a
17596            later parameter.  */
17597         if (decl_die != NULL)
17598           {
17599             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17600             break;
17601           }
17602       }
17603       /* FALLTHRU */
17604
17605     default:
17606       {
17607         /* Otherwise try to create a stack operation procedure to
17608            evaluate the value of the array bound.  */
17609
17610         dw_die_ref ctx, decl_die;
17611         dw_loc_list_ref list;
17612
17613         list = loc_list_from_tree (bound, 2);
17614         if (list == NULL || single_element_loc_list_p (list))
17615           {
17616             /* If DW_AT_*bound is not a reference nor constant, it is
17617                a DWARF expression rather than location description.
17618                For that loc_list_from_tree (bound, 0) is needed.
17619                If that fails to give a single element list,
17620                fall back to outputting this as a reference anyway.  */
17621             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17622             if (list2 && single_element_loc_list_p (list2))
17623               {
17624                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17625                 break;
17626               }
17627           }
17628         if (list == NULL)
17629           break;
17630
17631         if (current_function_decl == 0)
17632           ctx = comp_unit_die ();
17633         else
17634           ctx = lookup_decl_die (current_function_decl);
17635
17636         decl_die = new_die (DW_TAG_variable, ctx, bound);
17637         add_AT_flag (decl_die, DW_AT_artificial, 1);
17638         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17639         add_AT_location_description (decl_die, DW_AT_location, list);
17640         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17641         break;
17642       }
17643     }
17644 }
17645
17646 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17647    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17648    Note that the block of subscript information for an array type also
17649    includes information about the element type of the given array type.  */
17650
17651 static void
17652 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17653 {
17654   unsigned dimension_number;
17655   tree lower, upper;
17656   dw_die_ref subrange_die;
17657
17658   for (dimension_number = 0;
17659        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17660        type = TREE_TYPE (type), dimension_number++)
17661     {
17662       tree domain = TYPE_DOMAIN (type);
17663
17664       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17665         break;
17666
17667       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17668          and (in GNU C only) variable bounds.  Handle all three forms
17669          here.  */
17670       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17671       if (domain)
17672         {
17673           /* We have an array type with specified bounds.  */
17674           lower = TYPE_MIN_VALUE (domain);
17675           upper = TYPE_MAX_VALUE (domain);
17676
17677           /* Define the index type.  */
17678           if (TREE_TYPE (domain))
17679             {
17680               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17681                  TREE_TYPE field.  We can't emit debug info for this
17682                  because it is an unnamed integral type.  */
17683               if (TREE_CODE (domain) == INTEGER_TYPE
17684                   && TYPE_NAME (domain) == NULL_TREE
17685                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17686                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17687                 ;
17688               else
17689                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17690                                     type_die);
17691             }
17692
17693           /* ??? If upper is NULL, the array has unspecified length,
17694              but it does have a lower bound.  This happens with Fortran
17695                dimension arr(N:*)
17696              Since the debugger is definitely going to need to know N
17697              to produce useful results, go ahead and output the lower
17698              bound solo, and hope the debugger can cope.  */
17699
17700           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17701           if (upper)
17702             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17703         }
17704
17705       /* Otherwise we have an array type with an unspecified length.  The
17706          DWARF-2 spec does not say how to handle this; let's just leave out the
17707          bounds.  */
17708     }
17709 }
17710
17711 static void
17712 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17713 {
17714   unsigned size;
17715
17716   switch (TREE_CODE (tree_node))
17717     {
17718     case ERROR_MARK:
17719       size = 0;
17720       break;
17721     case ENUMERAL_TYPE:
17722     case RECORD_TYPE:
17723     case UNION_TYPE:
17724     case QUAL_UNION_TYPE:
17725       size = int_size_in_bytes (tree_node);
17726       break;
17727     case FIELD_DECL:
17728       /* For a data member of a struct or union, the DW_AT_byte_size is
17729          generally given as the number of bytes normally allocated for an
17730          object of the *declared* type of the member itself.  This is true
17731          even for bit-fields.  */
17732       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17733       break;
17734     default:
17735       gcc_unreachable ();
17736     }
17737
17738   /* Note that `size' might be -1 when we get to this point.  If it is, that
17739      indicates that the byte size of the entity in question is variable.  We
17740      have no good way of expressing this fact in Dwarf at the present time,
17741      so just let the -1 pass on through.  */
17742   add_AT_unsigned (die, DW_AT_byte_size, size);
17743 }
17744
17745 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17746    which specifies the distance in bits from the highest order bit of the
17747    "containing object" for the bit-field to the highest order bit of the
17748    bit-field itself.
17749
17750    For any given bit-field, the "containing object" is a hypothetical object
17751    (of some integral or enum type) within which the given bit-field lives.  The
17752    type of this hypothetical "containing object" is always the same as the
17753    declared type of the individual bit-field itself.  The determination of the
17754    exact location of the "containing object" for a bit-field is rather
17755    complicated.  It's handled by the `field_byte_offset' function (above).
17756
17757    Note that it is the size (in bytes) of the hypothetical "containing object"
17758    which will be given in the DW_AT_byte_size attribute for this bit-field.
17759    (See `byte_size_attribute' above).  */
17760
17761 static inline void
17762 add_bit_offset_attribute (dw_die_ref die, tree decl)
17763 {
17764   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17765   tree type = DECL_BIT_FIELD_TYPE (decl);
17766   HOST_WIDE_INT bitpos_int;
17767   HOST_WIDE_INT highest_order_object_bit_offset;
17768   HOST_WIDE_INT highest_order_field_bit_offset;
17769   HOST_WIDE_INT bit_offset;
17770
17771   /* Must be a field and a bit field.  */
17772   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17773
17774   /* We can't yet handle bit-fields whose offsets are variable, so if we
17775      encounter such things, just return without generating any attribute
17776      whatsoever.  Likewise for variable or too large size.  */
17777   if (! host_integerp (bit_position (decl), 0)
17778       || ! host_integerp (DECL_SIZE (decl), 1))
17779     return;
17780
17781   bitpos_int = int_bit_position (decl);
17782
17783   /* Note that the bit offset is always the distance (in bits) from the
17784      highest-order bit of the "containing object" to the highest-order bit of
17785      the bit-field itself.  Since the "high-order end" of any object or field
17786      is different on big-endian and little-endian machines, the computation
17787      below must take account of these differences.  */
17788   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17789   highest_order_field_bit_offset = bitpos_int;
17790
17791   if (! BYTES_BIG_ENDIAN)
17792     {
17793       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17794       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17795     }
17796
17797   bit_offset
17798     = (! BYTES_BIG_ENDIAN
17799        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17800        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17801
17802   if (bit_offset < 0)
17803     add_AT_int (die, DW_AT_bit_offset, bit_offset);
17804   else
17805     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
17806 }
17807
17808 /* For a FIELD_DECL node which represents a bit field, output an attribute
17809    which specifies the length in bits of the given field.  */
17810
17811 static inline void
17812 add_bit_size_attribute (dw_die_ref die, tree decl)
17813 {
17814   /* Must be a field and a bit field.  */
17815   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17816               && DECL_BIT_FIELD_TYPE (decl));
17817
17818   if (host_integerp (DECL_SIZE (decl), 1))
17819     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17820 }
17821
17822 /* If the compiled language is ANSI C, then add a 'prototyped'
17823    attribute, if arg types are given for the parameters of a function.  */
17824
17825 static inline void
17826 add_prototyped_attribute (dw_die_ref die, tree func_type)
17827 {
17828   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
17829       && prototype_p (func_type))
17830     add_AT_flag (die, DW_AT_prototyped, 1);
17831 }
17832
17833 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17834    by looking in either the type declaration or object declaration
17835    equate table.  */
17836
17837 static inline dw_die_ref
17838 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17839 {
17840   dw_die_ref origin_die = NULL;
17841
17842   if (TREE_CODE (origin) != FUNCTION_DECL)
17843     {
17844       /* We may have gotten separated from the block for the inlined
17845          function, if we're in an exception handler or some such; make
17846          sure that the abstract function has been written out.
17847
17848          Doing this for nested functions is wrong, however; functions are
17849          distinct units, and our context might not even be inline.  */
17850       tree fn = origin;
17851
17852       if (TYPE_P (fn))
17853         fn = TYPE_STUB_DECL (fn);
17854
17855       fn = decl_function_context (fn);
17856       if (fn)
17857         dwarf2out_abstract_function (fn);
17858     }
17859
17860   if (DECL_P (origin))
17861     origin_die = lookup_decl_die (origin);
17862   else if (TYPE_P (origin))
17863     origin_die = lookup_type_die (origin);
17864
17865   /* XXX: Functions that are never lowered don't always have correct block
17866      trees (in the case of java, they simply have no block tree, in some other
17867      languages).  For these functions, there is nothing we can really do to
17868      output correct debug info for inlined functions in all cases.  Rather
17869      than die, we'll just produce deficient debug info now, in that we will
17870      have variables without a proper abstract origin.  In the future, when all
17871      functions are lowered, we should re-add a gcc_assert (origin_die)
17872      here.  */
17873
17874   if (origin_die)
17875     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17876   return origin_die;
17877 }
17878
17879 /* We do not currently support the pure_virtual attribute.  */
17880
17881 static inline void
17882 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17883 {
17884   if (DECL_VINDEX (func_decl))
17885     {
17886       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17887
17888       if (host_integerp (DECL_VINDEX (func_decl), 0))
17889         add_AT_loc (die, DW_AT_vtable_elem_location,
17890                     new_loc_descr (DW_OP_constu,
17891                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17892                                    0));
17893
17894       /* GNU extension: Record what type this method came from originally.  */
17895       if (debug_info_level > DINFO_LEVEL_TERSE
17896           && DECL_CONTEXT (func_decl))
17897         add_AT_die_ref (die, DW_AT_containing_type,
17898                         lookup_type_die (DECL_CONTEXT (func_decl)));
17899     }
17900 }
17901 \f
17902 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17903    given decl.  This used to be a vendor extension until after DWARF 4
17904    standardized it.  */
17905
17906 static void
17907 add_linkage_attr (dw_die_ref die, tree decl)
17908 {
17909   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17910
17911   /* Mimic what assemble_name_raw does with a leading '*'.  */
17912   if (name[0] == '*')
17913     name = &name[1];
17914
17915   if (dwarf_version >= 4)
17916     add_AT_string (die, DW_AT_linkage_name, name);
17917   else
17918     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17919 }
17920
17921 /* Add source coordinate attributes for the given decl.  */
17922
17923 static void
17924 add_src_coords_attributes (dw_die_ref die, tree decl)
17925 {
17926   expanded_location s;
17927
17928   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
17929     return;
17930   s = expand_location (DECL_SOURCE_LOCATION (decl));
17931   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17932   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17933 }
17934
17935 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17936
17937 static void
17938 add_linkage_name (dw_die_ref die, tree decl)
17939 {
17940   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17941        && TREE_PUBLIC (decl)
17942        && !DECL_ABSTRACT (decl)
17943        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17944        && die->die_tag != DW_TAG_member)
17945     {
17946       /* Defer until we have an assembler name set.  */
17947       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17948         {
17949           limbo_die_node *asm_name;
17950
17951           asm_name = ggc_alloc_cleared_limbo_die_node ();
17952           asm_name->die = die;
17953           asm_name->created_for = decl;
17954           asm_name->next = deferred_asm_name;
17955           deferred_asm_name = asm_name;
17956         }
17957       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17958         add_linkage_attr (die, decl);
17959     }
17960 }
17961
17962 /* Add a DW_AT_name attribute and source coordinate attribute for the
17963    given decl, but only if it actually has a name.  */
17964
17965 static void
17966 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17967 {
17968   tree decl_name;
17969
17970   decl_name = DECL_NAME (decl);
17971   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17972     {
17973       const char *name = dwarf2_name (decl, 0);
17974       if (name)
17975         add_name_attribute (die, name);
17976       if (! DECL_ARTIFICIAL (decl))
17977         add_src_coords_attributes (die, decl);
17978
17979       add_linkage_name (die, decl);
17980     }
17981
17982 #ifdef VMS_DEBUGGING_INFO
17983   /* Get the function's name, as described by its RTL.  This may be different
17984      from the DECL_NAME name used in the source file.  */
17985   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17986     {
17987       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17988                    XEXP (DECL_RTL (decl), 0));
17989       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17990     }
17991 #endif /* VMS_DEBUGGING_INFO */
17992 }
17993
17994 #ifdef VMS_DEBUGGING_INFO
17995 /* Output the debug main pointer die for VMS */
17996
17997 void
17998 dwarf2out_vms_debug_main_pointer (void)
17999 {
18000   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18001   dw_die_ref die;
18002
18003   /* Allocate the VMS debug main subprogram die.  */
18004   die = ggc_alloc_cleared_die_node ();
18005   die->die_tag = DW_TAG_subprogram;
18006   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18007   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18008                                current_function_funcdef_no);
18009   add_AT_lbl_id (die, DW_AT_entry_pc, label);
18010
18011   /* Make it the first child of comp_unit_die ().  */
18012   die->die_parent = comp_unit_die ();
18013   if (comp_unit_die ()->die_child)
18014     {
18015       die->die_sib = comp_unit_die ()->die_child->die_sib;
18016       comp_unit_die ()->die_child->die_sib = die;
18017     }
18018   else
18019     {
18020       die->die_sib = die;
18021       comp_unit_die ()->die_child = die;
18022     }
18023 }
18024 #endif /* VMS_DEBUGGING_INFO */
18025
18026 /* Push a new declaration scope.  */
18027
18028 static void
18029 push_decl_scope (tree scope)
18030 {
18031   VEC_safe_push (tree, gc, decl_scope_table, scope);
18032 }
18033
18034 /* Pop a declaration scope.  */
18035
18036 static inline void
18037 pop_decl_scope (void)
18038 {
18039   VEC_pop (tree, decl_scope_table);
18040 }
18041
18042 /* Return the DIE for the scope that immediately contains this type.
18043    Non-named types get global scope.  Named types nested in other
18044    types get their containing scope if it's open, or global scope
18045    otherwise.  All other types (i.e. function-local named types) get
18046    the current active scope.  */
18047
18048 static dw_die_ref
18049 scope_die_for (tree t, dw_die_ref context_die)
18050 {
18051   dw_die_ref scope_die = NULL;
18052   tree containing_scope;
18053   int i;
18054
18055   /* Non-types always go in the current scope.  */
18056   gcc_assert (TYPE_P (t));
18057
18058   containing_scope = TYPE_CONTEXT (t);
18059
18060   /* Use the containing namespace if it was passed in (for a declaration).  */
18061   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18062     {
18063       if (context_die == lookup_decl_die (containing_scope))
18064         /* OK */;
18065       else
18066         containing_scope = NULL_TREE;
18067     }
18068
18069   /* Ignore function type "scopes" from the C frontend.  They mean that
18070      a tagged type is local to a parmlist of a function declarator, but
18071      that isn't useful to DWARF.  */
18072   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18073     containing_scope = NULL_TREE;
18074
18075   if (SCOPE_FILE_SCOPE_P (containing_scope))
18076     scope_die = comp_unit_die ();
18077   else if (TYPE_P (containing_scope))
18078     {
18079       /* For types, we can just look up the appropriate DIE.  But
18080          first we check to see if we're in the middle of emitting it
18081          so we know where the new DIE should go.  */
18082       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
18083         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
18084           break;
18085
18086       if (i < 0)
18087         {
18088           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
18089                       || TREE_ASM_WRITTEN (containing_scope));
18090           /*We are not in the middle of emitting the type
18091             CONTAINING_SCOPE. Let's see if it's emitted already.  */
18092           scope_die = lookup_type_die (containing_scope);
18093
18094           /* If none of the current dies are suitable, we get file scope.  */
18095           if (scope_die == NULL)
18096             scope_die = comp_unit_die ();
18097         }
18098       else
18099         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18100     }
18101   else
18102     scope_die = context_die;
18103
18104   return scope_die;
18105 }
18106
18107 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18108
18109 static inline int
18110 local_scope_p (dw_die_ref context_die)
18111 {
18112   for (; context_die; context_die = context_die->die_parent)
18113     if (context_die->die_tag == DW_TAG_inlined_subroutine
18114         || context_die->die_tag == DW_TAG_subprogram)
18115       return 1;
18116
18117   return 0;
18118 }
18119
18120 /* Returns nonzero if CONTEXT_DIE is a class.  */
18121
18122 static inline int
18123 class_scope_p (dw_die_ref context_die)
18124 {
18125   return (context_die
18126           && (context_die->die_tag == DW_TAG_structure_type
18127               || context_die->die_tag == DW_TAG_class_type
18128               || context_die->die_tag == DW_TAG_interface_type
18129               || context_die->die_tag == DW_TAG_union_type));
18130 }
18131
18132 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18133    whether or not to treat a DIE in this context as a declaration.  */
18134
18135 static inline int
18136 class_or_namespace_scope_p (dw_die_ref context_die)
18137 {
18138   return (class_scope_p (context_die)
18139           || (context_die && context_die->die_tag == DW_TAG_namespace));
18140 }
18141
18142 /* Many forms of DIEs require a "type description" attribute.  This
18143    routine locates the proper "type descriptor" die for the type given
18144    by 'type', and adds a DW_AT_type attribute below the given die.  */
18145
18146 static void
18147 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18148                     int decl_volatile, dw_die_ref context_die)
18149 {
18150   enum tree_code code  = TREE_CODE (type);
18151   dw_die_ref type_die  = NULL;
18152
18153   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18154      or fixed-point type, use the inner type.  This is because we have no
18155      support for unnamed types in base_type_die.  This can happen if this is
18156      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18157   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18158       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18159     type = TREE_TYPE (type), code = TREE_CODE (type);
18160
18161   if (code == ERROR_MARK
18162       /* Handle a special case.  For functions whose return type is void, we
18163          generate *no* type attribute.  (Note that no object may have type
18164          `void', so this only applies to function return types).  */
18165       || code == VOID_TYPE)
18166     return;
18167
18168   type_die = modified_type_die (type,
18169                                 decl_const || TYPE_READONLY (type),
18170                                 decl_volatile || TYPE_VOLATILE (type),
18171                                 context_die);
18172
18173   if (type_die != NULL)
18174     add_AT_die_ref (object_die, DW_AT_type, type_die);
18175 }
18176
18177 /* Given an object die, add the calling convention attribute for the
18178    function call type.  */
18179 static void
18180 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18181 {
18182   enum dwarf_calling_convention value = DW_CC_normal;
18183
18184   value = ((enum dwarf_calling_convention)
18185            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18186
18187   if (is_fortran ()
18188       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18189     {
18190       /* DWARF 2 doesn't provide a way to identify a program's source-level
18191         entry point.  DW_AT_calling_convention attributes are only meant
18192         to describe functions' calling conventions.  However, lacking a
18193         better way to signal the Fortran main program, we used this for 
18194         a long time, following existing custom.  Now, DWARF 4 has 
18195         DW_AT_main_subprogram, which we add below, but some tools still
18196         rely on the old way, which we thus keep.  */
18197       value = DW_CC_program;
18198
18199       if (dwarf_version >= 4 || !dwarf_strict)
18200         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18201     }
18202
18203   /* Only add the attribute if the backend requests it, and
18204      is not DW_CC_normal.  */
18205   if (value && (value != DW_CC_normal))
18206     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18207 }
18208
18209 /* Given a tree pointer to a struct, class, union, or enum type node, return
18210    a pointer to the (string) tag name for the given type, or zero if the type
18211    was declared without a tag.  */
18212
18213 static const char *
18214 type_tag (const_tree type)
18215 {
18216   const char *name = 0;
18217
18218   if (TYPE_NAME (type) != 0)
18219     {
18220       tree t = 0;
18221
18222       /* Find the IDENTIFIER_NODE for the type name.  */
18223       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18224           && !TYPE_NAMELESS (type))
18225         t = TYPE_NAME (type);
18226
18227       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18228          a TYPE_DECL node, regardless of whether or not a `typedef' was
18229          involved.  */
18230       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18231                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18232         {
18233           /* We want to be extra verbose.  Don't call dwarf_name if
18234              DECL_NAME isn't set.  The default hook for decl_printable_name
18235              doesn't like that, and in this context it's correct to return
18236              0, instead of "<anonymous>" or the like.  */
18237           if (DECL_NAME (TYPE_NAME (type))
18238               && !DECL_NAMELESS (TYPE_NAME (type)))
18239             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18240         }
18241
18242       /* Now get the name as a string, or invent one.  */
18243       if (!name && t != 0)
18244         name = IDENTIFIER_POINTER (t);
18245     }
18246
18247   return (name == 0 || *name == '\0') ? 0 : name;
18248 }
18249
18250 /* Return the type associated with a data member, make a special check
18251    for bit field types.  */
18252
18253 static inline tree
18254 member_declared_type (const_tree member)
18255 {
18256   return (DECL_BIT_FIELD_TYPE (member)
18257           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18258 }
18259
18260 /* Get the decl's label, as described by its RTL. This may be different
18261    from the DECL_NAME name used in the source file.  */
18262
18263 #if 0
18264 static const char *
18265 decl_start_label (tree decl)
18266 {
18267   rtx x;
18268   const char *fnname;
18269
18270   x = DECL_RTL (decl);
18271   gcc_assert (MEM_P (x));
18272
18273   x = XEXP (x, 0);
18274   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18275
18276   fnname = XSTR (x, 0);
18277   return fnname;
18278 }
18279 #endif
18280 \f
18281 /* These routines generate the internal representation of the DIE's for
18282    the compilation unit.  Debugging information is collected by walking
18283    the declaration trees passed in from dwarf2out_decl().  */
18284
18285 static void
18286 gen_array_type_die (tree type, dw_die_ref context_die)
18287 {
18288   dw_die_ref scope_die = scope_die_for (type, context_die);
18289   dw_die_ref array_die;
18290
18291   /* GNU compilers represent multidimensional array types as sequences of one
18292      dimensional array types whose element types are themselves array types.
18293      We sometimes squish that down to a single array_type DIE with multiple
18294      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18295      say that we are allowed to do this kind of compression in C, because
18296      there is no difference between an array of arrays and a multidimensional
18297      array.  We don't do this for Ada to remain as close as possible to the
18298      actual representation, which is especially important against the language
18299      flexibilty wrt arrays of variable size.  */
18300
18301   bool collapse_nested_arrays = !is_ada ();
18302   tree element_type;
18303
18304   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18305      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18306   if (TYPE_STRING_FLAG (type)
18307       && TREE_CODE (type) == ARRAY_TYPE
18308       && is_fortran ()
18309       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18310     {
18311       HOST_WIDE_INT size;
18312
18313       array_die = new_die (DW_TAG_string_type, scope_die, type);
18314       add_name_attribute (array_die, type_tag (type));
18315       equate_type_number_to_die (type, array_die);
18316       size = int_size_in_bytes (type);
18317       if (size >= 0)
18318         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18319       else if (TYPE_DOMAIN (type) != NULL_TREE
18320                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18321                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18322         {
18323           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18324           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18325
18326           size = int_size_in_bytes (TREE_TYPE (szdecl));
18327           if (loc && size > 0)
18328             {
18329               add_AT_location_description (array_die, DW_AT_string_length, loc);
18330               if (size != DWARF2_ADDR_SIZE)
18331                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18332             }
18333         }
18334       return;
18335     }
18336
18337   /* ??? The SGI dwarf reader fails for array of array of enum types
18338      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18339      array type comes before the outer array type.  We thus call gen_type_die
18340      before we new_die and must prevent nested array types collapsing for this
18341      target.  */
18342
18343 #ifdef MIPS_DEBUGGING_INFO
18344   gen_type_die (TREE_TYPE (type), context_die);
18345   collapse_nested_arrays = false;
18346 #endif
18347
18348   array_die = new_die (DW_TAG_array_type, scope_die, type);
18349   add_name_attribute (array_die, type_tag (type));
18350   equate_type_number_to_die (type, array_die);
18351
18352   if (TREE_CODE (type) == VECTOR_TYPE)
18353     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18354
18355   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18356   if (is_fortran ()
18357       && TREE_CODE (type) == ARRAY_TYPE
18358       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18359       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18360     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18361
18362 #if 0
18363   /* We default the array ordering.  SDB will probably do
18364      the right things even if DW_AT_ordering is not present.  It's not even
18365      an issue until we start to get into multidimensional arrays anyway.  If
18366      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18367      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18368      and when we find out that we need to put these in, we will only do so
18369      for multidimensional arrays.  */
18370   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18371 #endif
18372
18373 #ifdef MIPS_DEBUGGING_INFO
18374   /* The SGI compilers handle arrays of unknown bound by setting
18375      AT_declaration and not emitting any subrange DIEs.  */
18376   if (TREE_CODE (type) == ARRAY_TYPE
18377       && ! TYPE_DOMAIN (type))
18378     add_AT_flag (array_die, DW_AT_declaration, 1);
18379   else
18380 #endif
18381   if (TREE_CODE (type) == VECTOR_TYPE)
18382     {
18383       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18384       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18385       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18386       add_bound_info (subrange_die, DW_AT_upper_bound,
18387                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18388     }
18389   else
18390     add_subscript_info (array_die, type, collapse_nested_arrays);
18391
18392   /* Add representation of the type of the elements of this array type and
18393      emit the corresponding DIE if we haven't done it already.  */
18394   element_type = TREE_TYPE (type);
18395   if (collapse_nested_arrays)
18396     while (TREE_CODE (element_type) == ARRAY_TYPE)
18397       {
18398         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18399           break;
18400         element_type = TREE_TYPE (element_type);
18401       }
18402
18403 #ifndef MIPS_DEBUGGING_INFO
18404   gen_type_die (element_type, context_die);
18405 #endif
18406
18407   add_type_attribute (array_die, element_type, 0, 0, context_die);
18408
18409   if (get_AT (array_die, DW_AT_name))
18410     add_pubtype (type, array_die);
18411 }
18412
18413 static dw_loc_descr_ref
18414 descr_info_loc (tree val, tree base_decl)
18415 {
18416   HOST_WIDE_INT size;
18417   dw_loc_descr_ref loc, loc2;
18418   enum dwarf_location_atom op;
18419
18420   if (val == base_decl)
18421     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18422
18423   switch (TREE_CODE (val))
18424     {
18425     CASE_CONVERT:
18426       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18427     case VAR_DECL:
18428       return loc_descriptor_from_tree (val, 0);
18429     case INTEGER_CST:
18430       if (host_integerp (val, 0))
18431         return int_loc_descriptor (tree_low_cst (val, 0));
18432       break;
18433     case INDIRECT_REF:
18434       size = int_size_in_bytes (TREE_TYPE (val));
18435       if (size < 0)
18436         break;
18437       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18438       if (!loc)
18439         break;
18440       if (size == DWARF2_ADDR_SIZE)
18441         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18442       else
18443         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18444       return loc;
18445     case POINTER_PLUS_EXPR:
18446     case PLUS_EXPR:
18447       if (host_integerp (TREE_OPERAND (val, 1), 1)
18448           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18449              < 16384)
18450         {
18451           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18452           if (!loc)
18453             break;
18454           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18455         }
18456       else
18457         {
18458           op = DW_OP_plus;
18459         do_binop:
18460           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18461           if (!loc)
18462             break;
18463           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18464           if (!loc2)
18465             break;
18466           add_loc_descr (&loc, loc2);
18467           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18468         }
18469       return loc;
18470     case MINUS_EXPR:
18471       op = DW_OP_minus;
18472       goto do_binop;
18473     case MULT_EXPR:
18474       op = DW_OP_mul;
18475       goto do_binop;
18476     case EQ_EXPR:
18477       op = DW_OP_eq;
18478       goto do_binop;
18479     case NE_EXPR:
18480       op = DW_OP_ne;
18481       goto do_binop;
18482     default:
18483       break;
18484     }
18485   return NULL;
18486 }
18487
18488 static void
18489 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18490                       tree val, tree base_decl)
18491 {
18492   dw_loc_descr_ref loc;
18493
18494   if (host_integerp (val, 0))
18495     {
18496       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18497       return;
18498     }
18499
18500   loc = descr_info_loc (val, base_decl);
18501   if (!loc)
18502     return;
18503
18504   add_AT_loc (die, attr, loc);
18505 }
18506
18507 /* This routine generates DIE for array with hidden descriptor, details
18508    are filled into *info by a langhook.  */
18509
18510 static void
18511 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18512                           dw_die_ref context_die)
18513 {
18514   dw_die_ref scope_die = scope_die_for (type, context_die);
18515   dw_die_ref array_die;
18516   int dim;
18517
18518   array_die = new_die (DW_TAG_array_type, scope_die, type);
18519   add_name_attribute (array_die, type_tag (type));
18520   equate_type_number_to_die (type, array_die);
18521
18522   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18523   if (is_fortran ()
18524       && info->ndimensions >= 2)
18525     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18526
18527   if (info->data_location)
18528     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18529                           info->base_decl);
18530   if (info->associated)
18531     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18532                           info->base_decl);
18533   if (info->allocated)
18534     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18535                           info->base_decl);
18536
18537   for (dim = 0; dim < info->ndimensions; dim++)
18538     {
18539       dw_die_ref subrange_die
18540         = new_die (DW_TAG_subrange_type, array_die, NULL);
18541
18542       if (info->dimen[dim].lower_bound)
18543         {
18544           /* If it is the default value, omit it.  */
18545           int dflt;
18546
18547           if (host_integerp (info->dimen[dim].lower_bound, 0)
18548               && (dflt = lower_bound_default ()) != -1
18549               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18550             ;
18551           else
18552             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18553                                   info->dimen[dim].lower_bound,
18554                                   info->base_decl);
18555         }
18556       if (info->dimen[dim].upper_bound)
18557         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18558                               info->dimen[dim].upper_bound,
18559                               info->base_decl);
18560       if (info->dimen[dim].stride)
18561         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18562                               info->dimen[dim].stride,
18563                               info->base_decl);
18564     }
18565
18566   gen_type_die (info->element_type, context_die);
18567   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18568
18569   if (get_AT (array_die, DW_AT_name))
18570     add_pubtype (type, array_die);
18571 }
18572
18573 #if 0
18574 static void
18575 gen_entry_point_die (tree decl, dw_die_ref context_die)
18576 {
18577   tree origin = decl_ultimate_origin (decl);
18578   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18579
18580   if (origin != NULL)
18581     add_abstract_origin_attribute (decl_die, origin);
18582   else
18583     {
18584       add_name_and_src_coords_attributes (decl_die, decl);
18585       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18586                           0, 0, context_die);
18587     }
18588
18589   if (DECL_ABSTRACT (decl))
18590     equate_decl_number_to_die (decl, decl_die);
18591   else
18592     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18593 }
18594 #endif
18595
18596 /* Walk through the list of incomplete types again, trying once more to
18597    emit full debugging info for them.  */
18598
18599 static void
18600 retry_incomplete_types (void)
18601 {
18602   int i;
18603
18604   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18605     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18606                                   DINFO_USAGE_DIR_USE))
18607       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
18608 }
18609
18610 /* Determine what tag to use for a record type.  */
18611
18612 static enum dwarf_tag
18613 record_type_tag (tree type)
18614 {
18615   if (! lang_hooks.types.classify_record)
18616     return DW_TAG_structure_type;
18617
18618   switch (lang_hooks.types.classify_record (type))
18619     {
18620     case RECORD_IS_STRUCT:
18621       return DW_TAG_structure_type;
18622
18623     case RECORD_IS_CLASS:
18624       return DW_TAG_class_type;
18625
18626     case RECORD_IS_INTERFACE:
18627       if (dwarf_version >= 3 || !dwarf_strict)
18628         return DW_TAG_interface_type;
18629       return DW_TAG_structure_type;
18630
18631     default:
18632       gcc_unreachable ();
18633     }
18634 }
18635
18636 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18637    include all of the information about the enumeration values also. Each
18638    enumerated type name/value is listed as a child of the enumerated type
18639    DIE.  */
18640
18641 static dw_die_ref
18642 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18643 {
18644   dw_die_ref type_die = lookup_type_die (type);
18645
18646   if (type_die == NULL)
18647     {
18648       type_die = new_die (DW_TAG_enumeration_type,
18649                           scope_die_for (type, context_die), type);
18650       equate_type_number_to_die (type, type_die);
18651       add_name_attribute (type_die, type_tag (type));
18652       if (dwarf_version >= 4 || !dwarf_strict)
18653         {
18654           if (ENUM_IS_SCOPED (type))
18655             add_AT_flag (type_die, DW_AT_enum_class, 1);
18656           if (ENUM_IS_OPAQUE (type))
18657             add_AT_flag (type_die, DW_AT_declaration, 1);
18658         }
18659     }
18660   else if (! TYPE_SIZE (type))
18661     return type_die;
18662   else
18663     remove_AT (type_die, DW_AT_declaration);
18664
18665   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18666      given enum type is incomplete, do not generate the DW_AT_byte_size
18667      attribute or the DW_AT_element_list attribute.  */
18668   if (TYPE_SIZE (type))
18669     {
18670       tree link;
18671
18672       TREE_ASM_WRITTEN (type) = 1;
18673       add_byte_size_attribute (type_die, type);
18674       if (TYPE_STUB_DECL (type) != NULL_TREE)
18675         {
18676           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18677           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18678         }
18679
18680       /* If the first reference to this type was as the return type of an
18681          inline function, then it may not have a parent.  Fix this now.  */
18682       if (type_die->die_parent == NULL)
18683         add_child_die (scope_die_for (type, context_die), type_die);
18684
18685       for (link = TYPE_VALUES (type);
18686            link != NULL; link = TREE_CHAIN (link))
18687         {
18688           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18689           tree value = TREE_VALUE (link);
18690
18691           add_name_attribute (enum_die,
18692                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18693
18694           if (TREE_CODE (value) == CONST_DECL)
18695             value = DECL_INITIAL (value);
18696
18697           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18698             /* DWARF2 does not provide a way of indicating whether or
18699                not enumeration constants are signed or unsigned.  GDB
18700                always assumes the values are signed, so we output all
18701                values as if they were signed.  That means that
18702                enumeration constants with very large unsigned values
18703                will appear to have negative values in the debugger.  */
18704             add_AT_int (enum_die, DW_AT_const_value,
18705                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18706         }
18707     }
18708   else
18709     add_AT_flag (type_die, DW_AT_declaration, 1);
18710
18711   if (get_AT (type_die, DW_AT_name))
18712     add_pubtype (type, type_die);
18713
18714   return type_die;
18715 }
18716
18717 /* Generate a DIE to represent either a real live formal parameter decl or to
18718    represent just the type of some formal parameter position in some function
18719    type.
18720
18721    Note that this routine is a bit unusual because its argument may be a
18722    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18723    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18724    node.  If it's the former then this function is being called to output a
18725    DIE to represent a formal parameter object (or some inlining thereof).  If
18726    it's the latter, then this function is only being called to output a
18727    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18728    argument type of some subprogram type.
18729    If EMIT_NAME_P is true, name and source coordinate attributes
18730    are emitted.  */
18731
18732 static dw_die_ref
18733 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18734                           dw_die_ref context_die)
18735 {
18736   tree node_or_origin = node ? node : origin;
18737   tree ultimate_origin;
18738   dw_die_ref parm_die
18739     = new_die (DW_TAG_formal_parameter, context_die, node);
18740
18741   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18742     {
18743     case tcc_declaration:
18744       ultimate_origin = decl_ultimate_origin (node_or_origin);
18745       if (node || ultimate_origin)
18746         origin = ultimate_origin;
18747       if (origin != NULL)
18748         add_abstract_origin_attribute (parm_die, origin);
18749       else if (emit_name_p)
18750         add_name_and_src_coords_attributes (parm_die, node);
18751       if (origin == NULL
18752           || (! DECL_ABSTRACT (node_or_origin)
18753               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18754                                            decl_function_context
18755                                                             (node_or_origin))))
18756         {
18757           tree type = TREE_TYPE (node_or_origin);
18758           if (decl_by_reference_p (node_or_origin))
18759             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18760                                 context_die);
18761           else
18762             add_type_attribute (parm_die, type,
18763                                 TREE_READONLY (node_or_origin),
18764                                 TREE_THIS_VOLATILE (node_or_origin),
18765                                 context_die);
18766         }
18767       if (origin == NULL && DECL_ARTIFICIAL (node))
18768         add_AT_flag (parm_die, DW_AT_artificial, 1);
18769
18770       if (node && node != origin)
18771         equate_decl_number_to_die (node, parm_die);
18772       if (! DECL_ABSTRACT (node_or_origin))
18773         add_location_or_const_value_attribute (parm_die, node_or_origin,
18774                                                DW_AT_location);
18775
18776       break;
18777
18778     case tcc_type:
18779       /* We were called with some kind of a ..._TYPE node.  */
18780       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18781       break;
18782
18783     default:
18784       gcc_unreachable ();
18785     }
18786
18787   return parm_die;
18788 }
18789
18790 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18791    children DW_TAG_formal_parameter DIEs representing the arguments of the
18792    parameter pack.
18793
18794    PARM_PACK must be a function parameter pack.
18795    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18796    must point to the subsequent arguments of the function PACK_ARG belongs to.
18797    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18798    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18799    following the last one for which a DIE was generated.  */
18800
18801 static dw_die_ref
18802 gen_formal_parameter_pack_die  (tree parm_pack,
18803                                 tree pack_arg,
18804                                 dw_die_ref subr_die,
18805                                 tree *next_arg)
18806 {
18807   tree arg;
18808   dw_die_ref parm_pack_die;
18809
18810   gcc_assert (parm_pack
18811               && lang_hooks.function_parameter_pack_p (parm_pack)
18812               && subr_die);
18813
18814   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18815   add_src_coords_attributes (parm_pack_die, parm_pack);
18816
18817   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18818     {
18819       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18820                                                                  parm_pack))
18821         break;
18822       gen_formal_parameter_die (arg, NULL,
18823                                 false /* Don't emit name attribute.  */,
18824                                 parm_pack_die);
18825     }
18826   if (next_arg)
18827     *next_arg = arg;
18828   return parm_pack_die;
18829 }
18830
18831 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18832    at the end of an (ANSI prototyped) formal parameters list.  */
18833
18834 static void
18835 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18836 {
18837   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18838 }
18839
18840 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18841    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18842    parameters as specified in some function type specification (except for
18843    those which appear as part of a function *definition*).  */
18844
18845 static void
18846 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18847 {
18848   tree link;
18849   tree formal_type = NULL;
18850   tree first_parm_type;
18851   tree arg;
18852
18853   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18854     {
18855       arg = DECL_ARGUMENTS (function_or_method_type);
18856       function_or_method_type = TREE_TYPE (function_or_method_type);
18857     }
18858   else
18859     arg = NULL_TREE;
18860
18861   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18862
18863   /* Make our first pass over the list of formal parameter types and output a
18864      DW_TAG_formal_parameter DIE for each one.  */
18865   for (link = first_parm_type; link; )
18866     {
18867       dw_die_ref parm_die;
18868
18869       formal_type = TREE_VALUE (link);
18870       if (formal_type == void_type_node)
18871         break;
18872
18873       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18874       parm_die = gen_formal_parameter_die (formal_type, NULL,
18875                                            true /* Emit name attribute.  */,
18876                                            context_die);
18877       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18878           && link == first_parm_type)
18879         {
18880           add_AT_flag (parm_die, DW_AT_artificial, 1);
18881           if (dwarf_version >= 3 || !dwarf_strict)
18882             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18883         }
18884       else if (arg && DECL_ARTIFICIAL (arg))
18885         add_AT_flag (parm_die, DW_AT_artificial, 1);
18886
18887       link = TREE_CHAIN (link);
18888       if (arg)
18889         arg = DECL_CHAIN (arg);
18890     }
18891
18892   /* If this function type has an ellipsis, add a
18893      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18894   if (formal_type != void_type_node)
18895     gen_unspecified_parameters_die (function_or_method_type, context_die);
18896
18897   /* Make our second (and final) pass over the list of formal parameter types
18898      and output DIEs to represent those types (as necessary).  */
18899   for (link = TYPE_ARG_TYPES (function_or_method_type);
18900        link && TREE_VALUE (link);
18901        link = TREE_CHAIN (link))
18902     gen_type_die (TREE_VALUE (link), context_die);
18903 }
18904
18905 /* We want to generate the DIE for TYPE so that we can generate the
18906    die for MEMBER, which has been defined; we will need to refer back
18907    to the member declaration nested within TYPE.  If we're trying to
18908    generate minimal debug info for TYPE, processing TYPE won't do the
18909    trick; we need to attach the member declaration by hand.  */
18910
18911 static void
18912 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18913 {
18914   gen_type_die (type, context_die);
18915
18916   /* If we're trying to avoid duplicate debug info, we may not have
18917      emitted the member decl for this function.  Emit it now.  */
18918   if (TYPE_STUB_DECL (type)
18919       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18920       && ! lookup_decl_die (member))
18921     {
18922       dw_die_ref type_die;
18923       gcc_assert (!decl_ultimate_origin (member));
18924
18925       push_decl_scope (type);
18926       type_die = lookup_type_die_strip_naming_typedef (type);
18927       if (TREE_CODE (member) == FUNCTION_DECL)
18928         gen_subprogram_die (member, type_die);
18929       else if (TREE_CODE (member) == FIELD_DECL)
18930         {
18931           /* Ignore the nameless fields that are used to skip bits but handle
18932              C++ anonymous unions and structs.  */
18933           if (DECL_NAME (member) != NULL_TREE
18934               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18935               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18936             {
18937               gen_type_die (member_declared_type (member), type_die);
18938               gen_field_die (member, type_die);
18939             }
18940         }
18941       else
18942         gen_variable_die (member, NULL_TREE, type_die);
18943
18944       pop_decl_scope ();
18945     }
18946 }
18947
18948 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18949    may later generate inlined and/or out-of-line instances of.  */
18950
18951 static void
18952 dwarf2out_abstract_function (tree decl)
18953 {
18954   dw_die_ref old_die;
18955   tree save_fn;
18956   tree context;
18957   int was_abstract;
18958   htab_t old_decl_loc_table;
18959   int old_call_site_count, old_tail_call_site_count;
18960   struct call_arg_loc_node *old_call_arg_locations;
18961
18962   /* Make sure we have the actual abstract inline, not a clone.  */
18963   decl = DECL_ORIGIN (decl);
18964
18965   old_die = lookup_decl_die (decl);
18966   if (old_die && get_AT (old_die, DW_AT_inline))
18967     /* We've already generated the abstract instance.  */
18968     return;
18969
18970   /* We can be called while recursively when seeing block defining inlined subroutine
18971      DIE.  Be sure to not clobber the outer location table nor use it or we would
18972      get locations in abstract instantces.  */
18973   old_decl_loc_table = decl_loc_table;
18974   decl_loc_table = NULL;
18975   old_call_arg_locations = call_arg_locations;
18976   call_arg_locations = NULL;
18977   old_call_site_count = call_site_count;
18978   call_site_count = -1;
18979   old_tail_call_site_count = tail_call_site_count;
18980   tail_call_site_count = -1;
18981
18982   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18983      we don't get confused by DECL_ABSTRACT.  */
18984   if (debug_info_level > DINFO_LEVEL_TERSE)
18985     {
18986       context = decl_class_context (decl);
18987       if (context)
18988         gen_type_die_for_member
18989           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
18990     }
18991
18992   /* Pretend we've just finished compiling this function.  */
18993   save_fn = current_function_decl;
18994   current_function_decl = decl;
18995   push_cfun (DECL_STRUCT_FUNCTION (decl));
18996
18997   was_abstract = DECL_ABSTRACT (decl);
18998   set_decl_abstract_flags (decl, 1);
18999   dwarf2out_decl (decl);
19000   if (! was_abstract)
19001     set_decl_abstract_flags (decl, 0);
19002
19003   current_function_decl = save_fn;
19004   decl_loc_table = old_decl_loc_table;
19005   call_arg_locations = old_call_arg_locations;
19006   call_site_count = old_call_site_count;
19007   tail_call_site_count = old_tail_call_site_count;
19008   pop_cfun ();
19009 }
19010
19011 /* Helper function of premark_used_types() which gets called through
19012    htab_traverse.
19013
19014    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19015    marked as unused by prune_unused_types.  */
19016
19017 static int
19018 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
19019 {
19020   tree type;
19021   dw_die_ref die;
19022
19023   type = (tree) *slot;
19024   die = lookup_type_die (type);
19025   if (die != NULL)
19026     die->die_perennial_p = 1;
19027   return 1;
19028 }
19029
19030 /* Helper function of premark_types_used_by_global_vars which gets called
19031    through htab_traverse.
19032
19033    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19034    marked as unused by prune_unused_types. The DIE of the type is marked
19035    only if the global variable using the type will actually be emitted.  */
19036
19037 static int
19038 premark_types_used_by_global_vars_helper (void **slot,
19039                                           void *data ATTRIBUTE_UNUSED)
19040 {
19041   struct types_used_by_vars_entry *entry;
19042   dw_die_ref die;
19043
19044   entry = (struct types_used_by_vars_entry *) *slot;
19045   gcc_assert (entry->type != NULL
19046               && entry->var_decl != NULL);
19047   die = lookup_type_die (entry->type);
19048   if (die)
19049     {
19050       /* Ask cgraph if the global variable really is to be emitted.
19051          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
19052       struct varpool_node *node = varpool_get_node (entry->var_decl);
19053       if (node && node->needed)
19054         {
19055           die->die_perennial_p = 1;
19056           /* Keep the parent DIEs as well.  */
19057           while ((die = die->die_parent) && die->die_perennial_p == 0)
19058             die->die_perennial_p = 1;
19059         }
19060     }
19061   return 1;
19062 }
19063
19064 /* Mark all members of used_types_hash as perennial.  */
19065
19066 static void
19067 premark_used_types (void)
19068 {
19069   if (cfun && cfun->used_types_hash)
19070     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
19071 }
19072
19073 /* Mark all members of types_used_by_vars_entry as perennial.  */
19074
19075 static void
19076 premark_types_used_by_global_vars (void)
19077 {
19078   if (types_used_by_vars_hash)
19079     htab_traverse (types_used_by_vars_hash,
19080                    premark_types_used_by_global_vars_helper, NULL);
19081 }
19082
19083 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
19084    for CA_LOC call arg loc node.  */
19085
19086 static dw_die_ref
19087 gen_call_site_die (tree decl, dw_die_ref subr_die,
19088                    struct call_arg_loc_node *ca_loc)
19089 {
19090   dw_die_ref stmt_die = NULL, die;
19091   tree block = ca_loc->block;
19092
19093   while (block
19094          && block != DECL_INITIAL (decl)
19095          && TREE_CODE (block) == BLOCK)
19096     {
19097       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
19098         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
19099       if (stmt_die)
19100         break;
19101       block = BLOCK_SUPERCONTEXT (block);
19102     }
19103   if (stmt_die == NULL)
19104     stmt_die = subr_die;
19105   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
19106   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
19107   if (ca_loc->tail_call_p)
19108     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
19109   if (ca_loc->symbol_ref)
19110     {
19111       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
19112       if (tdie)
19113         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
19114       else
19115         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
19116     }
19117   return die;
19118 }
19119
19120 /* Generate a DIE to represent a declared function (either file-scope or
19121    block-local).  */
19122
19123 static void
19124 gen_subprogram_die (tree decl, dw_die_ref context_die)
19125 {
19126   tree origin = decl_ultimate_origin (decl);
19127   dw_die_ref subr_die;
19128   tree outer_scope;
19129   dw_die_ref old_die = lookup_decl_die (decl);
19130   int declaration = (current_function_decl != decl
19131                      || class_or_namespace_scope_p (context_die));
19132
19133   premark_used_types ();
19134
19135   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19136      started to generate the abstract instance of an inline, decided to output
19137      its containing class, and proceeded to emit the declaration of the inline
19138      from the member list for the class.  If so, DECLARATION takes priority;
19139      we'll get back to the abstract instance when done with the class.  */
19140
19141   /* The class-scope declaration DIE must be the primary DIE.  */
19142   if (origin && declaration && class_or_namespace_scope_p (context_die))
19143     {
19144       origin = NULL;
19145       gcc_assert (!old_die);
19146     }
19147
19148   /* Now that the C++ front end lazily declares artificial member fns, we
19149      might need to retrofit the declaration into its class.  */
19150   if (!declaration && !origin && !old_die
19151       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19152       && !class_or_namespace_scope_p (context_die)
19153       && debug_info_level > DINFO_LEVEL_TERSE)
19154     old_die = force_decl_die (decl);
19155
19156   if (origin != NULL)
19157     {
19158       gcc_assert (!declaration || local_scope_p (context_die));
19159
19160       /* Fixup die_parent for the abstract instance of a nested
19161          inline function.  */
19162       if (old_die && old_die->die_parent == NULL)
19163         add_child_die (context_die, old_die);
19164
19165       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19166       add_abstract_origin_attribute (subr_die, origin);
19167     }
19168   else if (old_die)
19169     {
19170       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19171       struct dwarf_file_data * file_index = lookup_filename (s.file);
19172
19173       if (!get_AT_flag (old_die, DW_AT_declaration)
19174           /* We can have a normal definition following an inline one in the
19175              case of redefinition of GNU C extern inlines.
19176              It seems reasonable to use AT_specification in this case.  */
19177           && !get_AT (old_die, DW_AT_inline))
19178         {
19179           /* Detect and ignore this case, where we are trying to output
19180              something we have already output.  */
19181           return;
19182         }
19183
19184       /* If the definition comes from the same place as the declaration,
19185          maybe use the old DIE.  We always want the DIE for this function
19186          that has the *_pc attributes to be under comp_unit_die so the
19187          debugger can find it.  We also need to do this for abstract
19188          instances of inlines, since the spec requires the out-of-line copy
19189          to have the same parent.  For local class methods, this doesn't
19190          apply; we just use the old DIE.  */
19191       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19192           && (DECL_ARTIFICIAL (decl)
19193               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19194                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19195                       == (unsigned) s.line))))
19196         {
19197           subr_die = old_die;
19198
19199           /* Clear out the declaration attribute and the formal parameters.
19200              Do not remove all children, because it is possible that this
19201              declaration die was forced using force_decl_die(). In such
19202              cases die that forced declaration die (e.g. TAG_imported_module)
19203              is one of the children that we do not want to remove.  */
19204           remove_AT (subr_die, DW_AT_declaration);
19205           remove_AT (subr_die, DW_AT_object_pointer);
19206           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19207         }
19208       else
19209         {
19210           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19211           add_AT_specification (subr_die, old_die);
19212           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19213             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19214           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19215             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19216         }
19217     }
19218   else
19219     {
19220       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19221
19222       if (TREE_PUBLIC (decl))
19223         add_AT_flag (subr_die, DW_AT_external, 1);
19224
19225       add_name_and_src_coords_attributes (subr_die, decl);
19226       if (debug_info_level > DINFO_LEVEL_TERSE)
19227         {
19228           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19229           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19230                               0, 0, context_die);
19231         }
19232
19233       add_pure_or_virtual_attribute (subr_die, decl);
19234       if (DECL_ARTIFICIAL (decl))
19235         add_AT_flag (subr_die, DW_AT_artificial, 1);
19236
19237       add_accessibility_attribute (subr_die, decl);
19238     }
19239
19240   if (declaration)
19241     {
19242       if (!old_die || !get_AT (old_die, DW_AT_inline))
19243         {
19244           add_AT_flag (subr_die, DW_AT_declaration, 1);
19245
19246           /* If this is an explicit function declaration then generate
19247              a DW_AT_explicit attribute.  */
19248           if (lang_hooks.decls.function_decl_explicit_p (decl)
19249               && (dwarf_version >= 3 || !dwarf_strict))
19250             add_AT_flag (subr_die, DW_AT_explicit, 1);
19251
19252           /* The first time we see a member function, it is in the context of
19253              the class to which it belongs.  We make sure of this by emitting
19254              the class first.  The next time is the definition, which is
19255              handled above.  The two may come from the same source text.
19256
19257              Note that force_decl_die() forces function declaration die. It is
19258              later reused to represent definition.  */
19259           equate_decl_number_to_die (decl, subr_die);
19260         }
19261     }
19262   else if (DECL_ABSTRACT (decl))
19263     {
19264       if (DECL_DECLARED_INLINE_P (decl))
19265         {
19266           if (cgraph_function_possibly_inlined_p (decl))
19267             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19268           else
19269             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19270         }
19271       else
19272         {
19273           if (cgraph_function_possibly_inlined_p (decl))
19274             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19275           else
19276             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19277         }
19278
19279       if (DECL_DECLARED_INLINE_P (decl)
19280           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19281         add_AT_flag (subr_die, DW_AT_artificial, 1);
19282
19283       equate_decl_number_to_die (decl, subr_die);
19284     }
19285   else if (!DECL_EXTERNAL (decl))
19286     {
19287       HOST_WIDE_INT cfa_fb_offset;
19288
19289       if (!old_die || !get_AT (old_die, DW_AT_inline))
19290         equate_decl_number_to_die (decl, subr_die);
19291
19292       if (!flag_reorder_blocks_and_partition)
19293         {
19294           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19295           if (fde->dw_fde_begin)
19296             {
19297               /* We have already generated the labels.  */
19298               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19299               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19300             }
19301           else
19302             {
19303               /* Create start/end labels and add the range.  */
19304               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19305               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19306                                            current_function_funcdef_no);
19307               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19308               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19309                                            current_function_funcdef_no);
19310               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19311             }
19312
19313 #if VMS_DEBUGGING_INFO
19314       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19315          Section 2.3 Prologue and Epilogue Attributes:
19316          When a breakpoint is set on entry to a function, it is generally
19317          desirable for execution to be suspended, not on the very first
19318          instruction of the function, but rather at a point after the
19319          function's frame has been set up, after any language defined local
19320          declaration processing has been completed, and before execution of
19321          the first statement of the function begins. Debuggers generally
19322          cannot properly determine where this point is.  Similarly for a
19323          breakpoint set on exit from a function. The prologue and epilogue
19324          attributes allow a compiler to communicate the location(s) to use.  */
19325
19326       {
19327         if (fde->dw_fde_vms_end_prologue)
19328           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19329             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19330
19331         if (fde->dw_fde_vms_begin_epilogue)
19332           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19333             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19334       }
19335 #endif
19336
19337           add_pubname (decl, subr_die);
19338           add_arange (decl, subr_die);
19339         }
19340       else
19341         {  /* Generate pubnames entries for the split function code
19342               ranges.  */
19343           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19344
19345           if (fde->dw_fde_switched_sections)
19346             {
19347               if (dwarf_version >= 3 || !dwarf_strict)
19348                 {
19349                   /* We should use ranges for non-contiguous code section 
19350                      addresses.  Use the actual code range for the initial
19351                      section, since the HOT/COLD labels might precede an 
19352                      alignment offset.  */
19353                   bool range_list_added = false;
19354                   if (fde->in_std_section)
19355                     {
19356                       add_ranges_by_labels (subr_die,
19357                                             fde->dw_fde_begin,
19358                                             fde->dw_fde_end,
19359                                             &range_list_added);
19360                       add_ranges_by_labels (subr_die,
19361                                             fde->dw_fde_unlikely_section_label,
19362                                             fde->dw_fde_unlikely_section_end_label,
19363                                             &range_list_added);
19364                     }
19365                   else
19366                     {
19367                       add_ranges_by_labels (subr_die,
19368                                             fde->dw_fde_begin,
19369                                             fde->dw_fde_end,
19370                                             &range_list_added);
19371                       add_ranges_by_labels (subr_die,
19372                                             fde->dw_fde_hot_section_label,
19373                                             fde->dw_fde_hot_section_end_label,
19374                                             &range_list_added);
19375                     }
19376                   add_pubname (decl, subr_die);
19377                   if (range_list_added)
19378                     add_ranges (NULL);
19379                 }
19380               else
19381                 {
19382                   /* There is no real support in DW2 for this .. so we make
19383                      a work-around.  First, emit the pub name for the segment
19384                      containing the function label.  Then make and emit a
19385                      simplified subprogram DIE for the second segment with the
19386                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19387                      linkage name for the second die so that gdb will find both
19388                      sections when given "b foo".  */
19389                   const char *name = NULL;
19390                   tree decl_name = DECL_NAME (decl);
19391                   dw_die_ref seg_die;
19392
19393                   /* Do the 'primary' section.   */
19394                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19395                                  fde->dw_fde_begin);
19396                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19397                                  fde->dw_fde_end);
19398                   /* Add it.   */
19399                   add_pubname (decl, subr_die);
19400                   add_arange (decl, subr_die);
19401
19402                   /* Build a minimal DIE for the secondary section.  */
19403                   seg_die = new_die (DW_TAG_subprogram,
19404                                      subr_die->die_parent, decl);
19405
19406                   if (TREE_PUBLIC (decl))
19407                     add_AT_flag (seg_die, DW_AT_external, 1);
19408
19409                   if (decl_name != NULL 
19410                       && IDENTIFIER_POINTER (decl_name) != NULL)
19411                     {
19412                       name = dwarf2_name (decl, 1);
19413                       if (! DECL_ARTIFICIAL (decl))
19414                         add_src_coords_attributes (seg_die, decl);
19415
19416                       add_linkage_name (seg_die, decl);
19417                     }
19418                   gcc_assert (name!=NULL);
19419                   add_pure_or_virtual_attribute (seg_die, decl);
19420                   if (DECL_ARTIFICIAL (decl))
19421                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19422
19423                   if (fde->in_std_section)
19424                     {
19425                       name = concat ("__cold_sect_of_", name, NULL); 
19426                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19427                                      fde->dw_fde_unlikely_section_label);
19428                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19429                                      fde->dw_fde_unlikely_section_end_label); 
19430                     }
19431                   else 
19432                     {
19433                       name = concat ("__hot_sect_of_", name, NULL); 
19434                       add_AT_lbl_id (seg_die, DW_AT_low_pc,
19435                                      fde->dw_fde_hot_section_label);
19436                       add_AT_lbl_id (seg_die, DW_AT_high_pc,
19437                                      fde->dw_fde_hot_section_end_label); 
19438                     }
19439                   add_name_attribute (seg_die, name);
19440                   add_pubname_string (name, seg_die);
19441                   add_arange (decl, seg_die);
19442                 }
19443             }
19444           else
19445             {
19446               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19447               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19448               add_pubname (decl, subr_die);
19449               add_arange (decl, subr_die);
19450             }
19451         }
19452
19453 #ifdef MIPS_DEBUGGING_INFO
19454       /* Add a reference to the FDE for this routine.  */
19455       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19456 #endif
19457
19458       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19459
19460       /* We define the "frame base" as the function's CFA.  This is more
19461          convenient for several reasons: (1) It's stable across the prologue
19462          and epilogue, which makes it better than just a frame pointer,
19463          (2) With dwarf3, there exists a one-byte encoding that allows us
19464          to reference the .debug_frame data by proxy, but failing that,
19465          (3) We can at least reuse the code inspection and interpretation
19466          code that determines the CFA position at various points in the
19467          function.  */
19468       if (dwarf_version >= 3)
19469         {
19470           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19471           add_AT_loc (subr_die, DW_AT_frame_base, op);
19472         }
19473       else
19474         {
19475           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19476           if (list->dw_loc_next)
19477             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19478           else
19479             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19480         }
19481
19482       /* Compute a displacement from the "steady-state frame pointer" to
19483          the CFA.  The former is what all stack slots and argument slots
19484          will reference in the rtl; the later is what we've told the
19485          debugger about.  We'll need to adjust all frame_base references
19486          by this displacement.  */
19487       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19488
19489       if (cfun->static_chain_decl)
19490         add_AT_location_description (subr_die, DW_AT_static_link,
19491                  loc_list_from_tree (cfun->static_chain_decl, 2));
19492     }
19493
19494   /* Generate child dies for template paramaters.  */
19495   if (debug_info_level > DINFO_LEVEL_TERSE)
19496     gen_generic_params_dies (decl);
19497
19498   /* Now output descriptions of the arguments for this function. This gets
19499      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19500      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19501      `...' at the end of the formal parameter list.  In order to find out if
19502      there was a trailing ellipsis or not, we must instead look at the type
19503      associated with the FUNCTION_DECL.  This will be a node of type
19504      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19505      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19506      an ellipsis at the end.  */
19507
19508   /* In the case where we are describing a mere function declaration, all we
19509      need to do here (and all we *can* do here) is to describe the *types* of
19510      its formal parameters.  */
19511   if (debug_info_level <= DINFO_LEVEL_TERSE)
19512     ;
19513   else if (declaration)
19514     gen_formal_types_die (decl, subr_die);
19515   else
19516     {
19517       /* Generate DIEs to represent all known formal parameters.  */
19518       tree parm = DECL_ARGUMENTS (decl);
19519       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19520       tree generic_decl_parm = generic_decl
19521                                 ? DECL_ARGUMENTS (generic_decl)
19522                                 : NULL;
19523
19524       /* Now we want to walk the list of parameters of the function and
19525          emit their relevant DIEs.
19526
19527          We consider the case of DECL being an instance of a generic function
19528          as well as it being a normal function.
19529
19530          If DECL is an instance of a generic function we walk the
19531          parameters of the generic function declaration _and_ the parameters of
19532          DECL itself. This is useful because we want to emit specific DIEs for
19533          function parameter packs and those are declared as part of the
19534          generic function declaration. In that particular case,
19535          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19536          That DIE has children DIEs representing the set of arguments
19537          of the pack. Note that the set of pack arguments can be empty.
19538          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19539          children DIE.
19540
19541          Otherwise, we just consider the parameters of DECL.  */
19542       while (generic_decl_parm || parm)
19543         {
19544           if (generic_decl_parm
19545               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19546             gen_formal_parameter_pack_die (generic_decl_parm,
19547                                            parm, subr_die,
19548                                            &parm);
19549           else if (parm)
19550             {
19551               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19552
19553               if (parm == DECL_ARGUMENTS (decl)
19554                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19555                   && parm_die
19556                   && (dwarf_version >= 3 || !dwarf_strict))
19557                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19558
19559               parm = DECL_CHAIN (parm);
19560             }
19561
19562           if (generic_decl_parm)
19563             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
19564         }
19565
19566       /* Decide whether we need an unspecified_parameters DIE at the end.
19567          There are 2 more cases to do this for: 1) the ansi ... declaration -
19568          this is detectable when the end of the arg list is not a
19569          void_type_node 2) an unprototyped function declaration (not a
19570          definition).  This just means that we have no info about the
19571          parameters at all.  */
19572       if (prototype_p (TREE_TYPE (decl)))
19573         {
19574           /* This is the prototyped case, check for....  */
19575           if (stdarg_p (TREE_TYPE (decl)))
19576             gen_unspecified_parameters_die (decl, subr_die);
19577         }
19578       else if (DECL_INITIAL (decl) == NULL_TREE)
19579         gen_unspecified_parameters_die (decl, subr_die);
19580     }
19581
19582   /* Output Dwarf info for all of the stuff within the body of the function
19583      (if it has one - it may be just a declaration).  */
19584   outer_scope = DECL_INITIAL (decl);
19585
19586   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
19587      a function.  This BLOCK actually represents the outermost binding contour
19588      for the function, i.e. the contour in which the function's formal
19589      parameters and labels get declared. Curiously, it appears that the front
19590      end doesn't actually put the PARM_DECL nodes for the current function onto
19591      the BLOCK_VARS list for this outer scope, but are strung off of the
19592      DECL_ARGUMENTS list for the function instead.
19593
19594      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
19595      the LABEL_DECL nodes for the function however, and we output DWARF info
19596      for those in decls_for_scope.  Just within the `outer_scope' there will be
19597      a BLOCK node representing the function's outermost pair of curly braces,
19598      and any blocks used for the base and member initializers of a C++
19599      constructor function.  */
19600   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
19601     {
19602       int call_site_note_count = 0;
19603       int tail_call_site_note_count = 0;
19604
19605       /* Emit a DW_TAG_variable DIE for a named return value.  */
19606       if (DECL_NAME (DECL_RESULT (decl)))
19607         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
19608
19609       current_function_has_inlines = 0;
19610       decls_for_scope (outer_scope, subr_die, 0);
19611
19612       if (call_arg_locations)
19613         {
19614           struct call_arg_loc_node *ca_loc;
19615           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
19616             {
19617               dw_die_ref die = NULL;
19618               rtx tloc = NULL_RTX;
19619               rtx arg, next_arg;
19620
19621               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
19622                    arg; arg = next_arg)
19623                 {
19624                   dw_loc_descr_ref reg, val;
19625                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
19626                   dw_die_ref cdie;
19627
19628                   next_arg = XEXP (arg, 1);
19629                   if (REG_P (XEXP (XEXP (arg, 0), 0))
19630                       && next_arg
19631                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
19632                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
19633                       && REGNO (XEXP (XEXP (arg, 0), 0))
19634                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
19635                     next_arg = XEXP (next_arg, 1);
19636                   if (mode == VOIDmode)
19637                     mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
19638                   if (GET_MODE_CLASS (mode) != MODE_INT
19639                       || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
19640                     continue;
19641                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
19642                     {
19643                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
19644                       tloc = XEXP (XEXP (arg, 0), 1);
19645                       continue;
19646                     }
19647                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
19648                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
19649                                               VAR_INIT_STATUS_INITIALIZED);
19650                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
19651                     reg = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 0),
19652                                                           0), 0), mode,
19653                                               VAR_INIT_STATUS_INITIALIZED);
19654                   else
19655                     continue;
19656                   if (reg == NULL)
19657                     continue;
19658                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), VOIDmode,
19659                                             VAR_INIT_STATUS_INITIALIZED);
19660                   if (val == NULL)
19661                     continue;
19662                   if (die == NULL)
19663                     die = gen_call_site_die (decl, subr_die, ca_loc);
19664                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
19665                                   NULL_TREE);           
19666                   add_AT_loc (cdie, DW_AT_location, reg);
19667                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
19668                   if (next_arg != XEXP (arg, 1))
19669                     {
19670                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
19671                                                             0), 1), VOIDmode,
19672                                                 VAR_INIT_STATUS_INITIALIZED);
19673                       if (val != NULL)
19674                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
19675                     }
19676                 }
19677               if (die == NULL
19678                   && (ca_loc->symbol_ref || tloc))
19679                 die = gen_call_site_die (decl, subr_die, ca_loc);
19680               if (die != NULL && tloc != NULL_RTX)
19681                 {
19682                   dw_loc_descr_ref tval
19683                     = mem_loc_descriptor (tloc, VOIDmode,
19684                                           VAR_INIT_STATUS_INITIALIZED);
19685                   if (tval)
19686                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
19687                 }
19688               if (die != NULL)
19689                 {
19690                   call_site_note_count++;
19691                   if (ca_loc->tail_call_p)
19692                     tail_call_site_note_count++;
19693                 }
19694             }
19695           call_arg_locations = NULL;
19696           call_arg_loc_last = NULL;
19697         }
19698       if (tail_call_site_count >= 0
19699           && tail_call_site_count == tail_call_site_note_count)
19700         {
19701           if (call_site_count >= 0
19702               && call_site_count == call_site_note_count)
19703             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
19704           else
19705             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
19706         }
19707       call_site_count = -1;
19708       tail_call_site_count = -1;
19709     }
19710   /* Add the calling convention attribute if requested.  */
19711   add_calling_convention_attribute (subr_die, decl);
19712
19713 }
19714
19715 /* Returns a hash value for X (which really is a die_struct).  */
19716
19717 static hashval_t
19718 common_block_die_table_hash (const void *x)
19719 {
19720   const_dw_die_ref d = (const_dw_die_ref) x;
19721   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
19722 }
19723
19724 /* Return nonzero if decl_id and die_parent of die_struct X is the same
19725    as decl_id and die_parent of die_struct Y.  */
19726
19727 static int
19728 common_block_die_table_eq (const void *x, const void *y)
19729 {
19730   const_dw_die_ref d = (const_dw_die_ref) x;
19731   const_dw_die_ref e = (const_dw_die_ref) y;
19732   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
19733 }
19734
19735 /* Generate a DIE to represent a declared data object.
19736    Either DECL or ORIGIN must be non-null.  */
19737
19738 static void
19739 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
19740 {
19741   HOST_WIDE_INT off;
19742   tree com_decl;
19743   tree decl_or_origin = decl ? decl : origin;
19744   tree ultimate_origin;
19745   dw_die_ref var_die;
19746   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19747   dw_die_ref origin_die;
19748   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19749                       || class_or_namespace_scope_p (context_die));
19750   bool specialization_p = false;
19751
19752   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19753   if (decl || ultimate_origin)
19754     origin = ultimate_origin;
19755   com_decl = fortran_common (decl_or_origin, &off);
19756
19757   /* Symbol in common gets emitted as a child of the common block, in the form
19758      of a data member.  */
19759   if (com_decl)
19760     {
19761       dw_die_ref com_die;
19762       dw_loc_list_ref loc;
19763       die_node com_die_arg;
19764
19765       var_die = lookup_decl_die (decl_or_origin);
19766       if (var_die)
19767         {
19768           if (get_AT (var_die, DW_AT_location) == NULL)
19769             {
19770               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19771               if (loc)
19772                 {
19773                   if (off)
19774                     {
19775                       /* Optimize the common case.  */
19776                       if (single_element_loc_list_p (loc)
19777                           && loc->expr->dw_loc_opc == DW_OP_addr
19778                           && loc->expr->dw_loc_next == NULL
19779                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19780                              == SYMBOL_REF)
19781                         loc->expr->dw_loc_oprnd1.v.val_addr
19782                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19783                         else
19784                           loc_list_plus_const (loc, off);
19785                     }
19786                   add_AT_location_description (var_die, DW_AT_location, loc);
19787                   remove_AT (var_die, DW_AT_declaration);
19788                 }
19789             }
19790           return;
19791         }
19792
19793       if (common_block_die_table == NULL)
19794         common_block_die_table
19795           = htab_create_ggc (10, common_block_die_table_hash,
19796                              common_block_die_table_eq, NULL);
19797
19798       com_die_arg.decl_id = DECL_UID (com_decl);
19799       com_die_arg.die_parent = context_die;
19800       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19801       loc = loc_list_from_tree (com_decl, 2);
19802       if (com_die == NULL)
19803         {
19804           const char *cnam
19805             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19806           void **slot;
19807
19808           com_die = new_die (DW_TAG_common_block, context_die, decl);
19809           add_name_and_src_coords_attributes (com_die, com_decl);
19810           if (loc)
19811             {
19812               add_AT_location_description (com_die, DW_AT_location, loc);
19813               /* Avoid sharing the same loc descriptor between
19814                  DW_TAG_common_block and DW_TAG_variable.  */
19815               loc = loc_list_from_tree (com_decl, 2);
19816             }
19817           else if (DECL_EXTERNAL (decl))
19818             add_AT_flag (com_die, DW_AT_declaration, 1);
19819           add_pubname_string (cnam, com_die); /* ??? needed? */
19820           com_die->decl_id = DECL_UID (com_decl);
19821           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19822           *slot = (void *) com_die;
19823         }
19824       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19825         {
19826           add_AT_location_description (com_die, DW_AT_location, loc);
19827           loc = loc_list_from_tree (com_decl, 2);
19828           remove_AT (com_die, DW_AT_declaration);
19829         }
19830       var_die = new_die (DW_TAG_variable, com_die, decl);
19831       add_name_and_src_coords_attributes (var_die, decl);
19832       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19833                           TREE_THIS_VOLATILE (decl), context_die);
19834       add_AT_flag (var_die, DW_AT_external, 1);
19835       if (loc)
19836         {
19837           if (off)
19838             {
19839               /* Optimize the common case.  */
19840               if (single_element_loc_list_p (loc)
19841                   && loc->expr->dw_loc_opc == DW_OP_addr
19842                   && loc->expr->dw_loc_next == NULL
19843                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19844                 loc->expr->dw_loc_oprnd1.v.val_addr
19845                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19846               else
19847                 loc_list_plus_const (loc, off);
19848             }
19849           add_AT_location_description (var_die, DW_AT_location, loc);
19850         }
19851       else if (DECL_EXTERNAL (decl))
19852         add_AT_flag (var_die, DW_AT_declaration, 1);
19853       equate_decl_number_to_die (decl, var_die);
19854       return;
19855     }
19856
19857   /* If the compiler emitted a definition for the DECL declaration
19858      and if we already emitted a DIE for it, don't emit a second
19859      DIE for it again. Allow re-declarations of DECLs that are
19860      inside functions, though.  */
19861   if (old_die && declaration && !local_scope_p (context_die))
19862     return;
19863
19864   /* For static data members, the declaration in the class is supposed
19865      to have DW_TAG_member tag; the specification should still be
19866      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19867   if (declaration && class_scope_p (context_die))
19868     var_die = new_die (DW_TAG_member, context_die, decl);
19869   else
19870     var_die = new_die (DW_TAG_variable, context_die, decl);
19871
19872   origin_die = NULL;
19873   if (origin != NULL)
19874     origin_die = add_abstract_origin_attribute (var_die, origin);
19875
19876   /* Loop unrolling can create multiple blocks that refer to the same
19877      static variable, so we must test for the DW_AT_declaration flag.
19878
19879      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19880      copy decls and set the DECL_ABSTRACT flag on them instead of
19881      sharing them.
19882
19883      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19884
19885      ??? The declare_in_namespace support causes us to get two DIEs for one
19886      variable, both of which are declarations.  We want to avoid considering
19887      one to be a specification, so we must test that this DIE is not a
19888      declaration.  */
19889   else if (old_die && TREE_STATIC (decl) && ! declaration
19890            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19891     {
19892       /* This is a definition of a C++ class level static.  */
19893       add_AT_specification (var_die, old_die);
19894       specialization_p = true;
19895       if (DECL_NAME (decl))
19896         {
19897           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19898           struct dwarf_file_data * file_index = lookup_filename (s.file);
19899
19900           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19901             add_AT_file (var_die, DW_AT_decl_file, file_index);
19902
19903           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19904             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19905
19906           if (old_die->die_tag == DW_TAG_member)
19907             add_linkage_name (var_die, decl);
19908         }
19909     }
19910   else
19911     add_name_and_src_coords_attributes (var_die, decl);
19912
19913   if ((origin == NULL && !specialization_p)
19914       || (origin != NULL
19915           && !DECL_ABSTRACT (decl_or_origin)
19916           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19917                                        decl_function_context
19918                                                         (decl_or_origin))))
19919     {
19920       tree type = TREE_TYPE (decl_or_origin);
19921
19922       if (decl_by_reference_p (decl_or_origin))
19923         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19924       else
19925         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19926                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19927     }
19928
19929   if (origin == NULL && !specialization_p)
19930     {
19931       if (TREE_PUBLIC (decl))
19932         add_AT_flag (var_die, DW_AT_external, 1);
19933
19934       if (DECL_ARTIFICIAL (decl))
19935         add_AT_flag (var_die, DW_AT_artificial, 1);
19936
19937       add_accessibility_attribute (var_die, decl);
19938     }
19939
19940   if (declaration)
19941     add_AT_flag (var_die, DW_AT_declaration, 1);
19942
19943   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
19944     equate_decl_number_to_die (decl, var_die);
19945
19946   if (! declaration
19947       && (! DECL_ABSTRACT (decl_or_origin)
19948           /* Local static vars are shared between all clones/inlines,
19949              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19950              already set.  */
19951           || (TREE_CODE (decl_or_origin) == VAR_DECL
19952               && TREE_STATIC (decl_or_origin)
19953               && DECL_RTL_SET_P (decl_or_origin)))
19954       /* When abstract origin already has DW_AT_location attribute, no need
19955          to add it again.  */
19956       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19957     {
19958       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19959           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19960         defer_location (decl_or_origin, var_die);
19961       else
19962         add_location_or_const_value_attribute (var_die,
19963                                                decl_or_origin,
19964                                                DW_AT_location);
19965       add_pubname (decl_or_origin, var_die);
19966     }
19967   else
19968     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19969 }
19970
19971 /* Generate a DIE to represent a named constant.  */
19972
19973 static void
19974 gen_const_die (tree decl, dw_die_ref context_die)
19975 {
19976   dw_die_ref const_die;
19977   tree type = TREE_TYPE (decl);
19978
19979   const_die = new_die (DW_TAG_constant, context_die, decl);
19980   add_name_and_src_coords_attributes (const_die, decl);
19981   add_type_attribute (const_die, type, 1, 0, context_die);
19982   if (TREE_PUBLIC (decl))
19983     add_AT_flag (const_die, DW_AT_external, 1);
19984   if (DECL_ARTIFICIAL (decl))
19985     add_AT_flag (const_die, DW_AT_artificial, 1);
19986   tree_add_const_value_attribute_for_decl (const_die, decl);
19987 }
19988
19989 /* Generate a DIE to represent a label identifier.  */
19990
19991 static void
19992 gen_label_die (tree decl, dw_die_ref context_die)
19993 {
19994   tree origin = decl_ultimate_origin (decl);
19995   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19996   rtx insn;
19997   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19998
19999   if (origin != NULL)
20000     add_abstract_origin_attribute (lbl_die, origin);
20001   else
20002     add_name_and_src_coords_attributes (lbl_die, decl);
20003
20004   if (DECL_ABSTRACT (decl))
20005     equate_decl_number_to_die (decl, lbl_die);
20006   else
20007     {
20008       insn = DECL_RTL_IF_SET (decl);
20009
20010       /* Deleted labels are programmer specified labels which have been
20011          eliminated because of various optimizations.  We still emit them
20012          here so that it is possible to put breakpoints on them.  */
20013       if (insn
20014           && (LABEL_P (insn)
20015               || ((NOTE_P (insn)
20016                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
20017         {
20018           /* When optimization is enabled (via -O) some parts of the compiler
20019              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
20020              represent source-level labels which were explicitly declared by
20021              the user.  This really shouldn't be happening though, so catch
20022              it if it ever does happen.  */
20023           gcc_assert (!INSN_DELETED_P (insn));
20024
20025           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
20026           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
20027         }
20028     }
20029 }
20030
20031 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
20032    attributes to the DIE for a block STMT, to describe where the inlined
20033    function was called from.  This is similar to add_src_coords_attributes.  */
20034
20035 static inline void
20036 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
20037 {
20038   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
20039
20040   if (dwarf_version >= 3 || !dwarf_strict)
20041     {
20042       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
20043       add_AT_unsigned (die, DW_AT_call_line, s.line);
20044     }
20045 }
20046
20047
20048 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
20049    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
20050
20051 static inline void
20052 add_high_low_attributes (tree stmt, dw_die_ref die)
20053 {
20054   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20055
20056   if (BLOCK_FRAGMENT_CHAIN (stmt)
20057       && (dwarf_version >= 3 || !dwarf_strict))
20058     {
20059       tree chain;
20060
20061       if (inlined_function_outer_scope_p (stmt))
20062         {
20063           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20064                                        BLOCK_NUMBER (stmt));
20065           add_AT_lbl_id (die, DW_AT_entry_pc, label);
20066         }
20067
20068       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
20069
20070       chain = BLOCK_FRAGMENT_CHAIN (stmt);
20071       do
20072         {
20073           add_ranges (chain);
20074           chain = BLOCK_FRAGMENT_CHAIN (chain);
20075         }
20076       while (chain);
20077       add_ranges (NULL);
20078     }
20079   else
20080     {
20081       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20082                                    BLOCK_NUMBER (stmt));
20083       add_AT_lbl_id (die, DW_AT_low_pc, label);
20084       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
20085                                    BLOCK_NUMBER (stmt));
20086       add_AT_lbl_id (die, DW_AT_high_pc, label);
20087     }
20088 }
20089
20090 /* Generate a DIE for a lexical block.  */
20091
20092 static void
20093 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
20094 {
20095   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
20096
20097   if (call_arg_locations)
20098     {
20099       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20100         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20101                                BLOCK_NUMBER (stmt) + 1);
20102       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
20103     }
20104
20105   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
20106     add_high_low_attributes (stmt, stmt_die);
20107
20108   decls_for_scope (stmt, stmt_die, depth);
20109 }
20110
20111 /* Generate a DIE for an inlined subprogram.  */
20112
20113 static void
20114 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
20115 {
20116   tree decl;
20117
20118   /* The instance of function that is effectively being inlined shall not
20119      be abstract.  */
20120   gcc_assert (! BLOCK_ABSTRACT (stmt));
20121
20122   decl = block_ultimate_origin (stmt);
20123
20124   /* Emit info for the abstract instance first, if we haven't yet.  We
20125      must emit this even if the block is abstract, otherwise when we
20126      emit the block below (or elsewhere), we may end up trying to emit
20127      a die whose origin die hasn't been emitted, and crashing.  */
20128   dwarf2out_abstract_function (decl);
20129
20130   if (! BLOCK_ABSTRACT (stmt))
20131     {
20132       dw_die_ref subr_die
20133         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20134
20135       if (call_arg_locations)
20136         {
20137           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20138             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20139                                    BLOCK_NUMBER (stmt) + 1);
20140           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20141         }
20142       add_abstract_origin_attribute (subr_die, decl);
20143       if (TREE_ASM_WRITTEN (stmt))
20144         add_high_low_attributes (stmt, subr_die);
20145       add_call_src_coords_attributes (stmt, subr_die);
20146
20147       decls_for_scope (stmt, subr_die, depth);
20148       current_function_has_inlines = 1;
20149     }
20150 }
20151
20152 /* Generate a DIE for a field in a record, or structure.  */
20153
20154 static void
20155 gen_field_die (tree decl, dw_die_ref context_die)
20156 {
20157   dw_die_ref decl_die;
20158
20159   if (TREE_TYPE (decl) == error_mark_node)
20160     return;
20161
20162   decl_die = new_die (DW_TAG_member, context_die, decl);
20163   add_name_and_src_coords_attributes (decl_die, decl);
20164   add_type_attribute (decl_die, member_declared_type (decl),
20165                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20166                       context_die);
20167
20168   if (DECL_BIT_FIELD_TYPE (decl))
20169     {
20170       add_byte_size_attribute (decl_die, decl);
20171       add_bit_size_attribute (decl_die, decl);
20172       add_bit_offset_attribute (decl_die, decl);
20173     }
20174
20175   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20176     add_data_member_location_attribute (decl_die, decl);
20177
20178   if (DECL_ARTIFICIAL (decl))
20179     add_AT_flag (decl_die, DW_AT_artificial, 1);
20180
20181   add_accessibility_attribute (decl_die, decl);
20182
20183   /* Equate decl number to die, so that we can look up this decl later on.  */
20184   equate_decl_number_to_die (decl, decl_die);
20185 }
20186
20187 #if 0
20188 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20189    Use modified_type_die instead.
20190    We keep this code here just in case these types of DIEs may be needed to
20191    represent certain things in other languages (e.g. Pascal) someday.  */
20192
20193 static void
20194 gen_pointer_type_die (tree type, dw_die_ref context_die)
20195 {
20196   dw_die_ref ptr_die
20197     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20198
20199   equate_type_number_to_die (type, ptr_die);
20200   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20201   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20202 }
20203
20204 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20205    Use modified_type_die instead.
20206    We keep this code here just in case these types of DIEs may be needed to
20207    represent certain things in other languages (e.g. Pascal) someday.  */
20208
20209 static void
20210 gen_reference_type_die (tree type, dw_die_ref context_die)
20211 {
20212   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20213
20214   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20215     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20216   else
20217     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20218
20219   equate_type_number_to_die (type, ref_die);
20220   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20221   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20222 }
20223 #endif
20224
20225 /* Generate a DIE for a pointer to a member type.  */
20226
20227 static void
20228 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20229 {
20230   dw_die_ref ptr_die
20231     = new_die (DW_TAG_ptr_to_member_type,
20232                scope_die_for (type, context_die), type);
20233
20234   equate_type_number_to_die (type, ptr_die);
20235   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20236                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20237   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20238 }
20239
20240 /* Generate the DIE for the compilation unit.  */
20241
20242 static dw_die_ref
20243 gen_compile_unit_die (const char *filename)
20244 {
20245   dw_die_ref die;
20246   char producer[250];
20247   const char *language_string = lang_hooks.name;
20248   int language;
20249
20250   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20251
20252   if (filename)
20253     {
20254       add_name_attribute (die, filename);
20255       /* Don't add cwd for <built-in>.  */
20256       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20257         add_comp_dir_attribute (die);
20258     }
20259
20260   sprintf (producer, "%s %s", language_string, version_string);
20261
20262 #ifdef MIPS_DEBUGGING_INFO
20263   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20264      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20265      not appear in the producer string, the debugger reaches the conclusion
20266      that the object file is stripped and has no debugging information.
20267      To get the MIPS/SGI debugger to believe that there is debugging
20268      information in the object file, we add a -g to the producer string.  */
20269   if (debug_info_level > DINFO_LEVEL_TERSE)
20270     strcat (producer, " -g");
20271 #endif
20272
20273   add_AT_string (die, DW_AT_producer, producer);
20274
20275   /* If our producer is LTO try to figure out a common language to use
20276      from the global list of translation units.  */
20277   if (strcmp (language_string, "GNU GIMPLE") == 0)
20278     {
20279       unsigned i;
20280       tree t;
20281       const char *common_lang = NULL;
20282
20283       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20284         {
20285           if (!TRANSLATION_UNIT_LANGUAGE (t))
20286             continue;
20287           if (!common_lang)
20288             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20289           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20290             ;
20291           else if (strncmp (common_lang, "GNU C", 5) == 0
20292                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20293             /* Mixing C and C++ is ok, use C++ in that case.  */
20294             common_lang = "GNU C++";
20295           else
20296             {
20297               /* Fall back to C.  */
20298               common_lang = NULL;
20299               break;
20300             }
20301         }
20302
20303       if (common_lang)
20304         language_string = common_lang;
20305     }
20306
20307   language = DW_LANG_C89;
20308   if (strcmp (language_string, "GNU C++") == 0)
20309     language = DW_LANG_C_plus_plus;
20310   else if (strcmp (language_string, "GNU F77") == 0)
20311     language = DW_LANG_Fortran77;
20312   else if (strcmp (language_string, "GNU Pascal") == 0)
20313     language = DW_LANG_Pascal83;
20314   else if (dwarf_version >= 3 || !dwarf_strict)
20315     {
20316       if (strcmp (language_string, "GNU Ada") == 0)
20317         language = DW_LANG_Ada95;
20318       else if (strcmp (language_string, "GNU Fortran") == 0)
20319         language = DW_LANG_Fortran95;
20320       else if (strcmp (language_string, "GNU Java") == 0)
20321         language = DW_LANG_Java;
20322       else if (strcmp (language_string, "GNU Objective-C") == 0)
20323         language = DW_LANG_ObjC;
20324       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20325         language = DW_LANG_ObjC_plus_plus;
20326     }
20327
20328   add_AT_unsigned (die, DW_AT_language, language);
20329
20330   switch (language)
20331     {
20332     case DW_LANG_Fortran77:
20333     case DW_LANG_Fortran90:
20334     case DW_LANG_Fortran95:
20335       /* Fortran has case insensitive identifiers and the front-end
20336          lowercases everything.  */
20337       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20338       break;
20339     default:
20340       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20341       break;
20342     }
20343   return die;
20344 }
20345
20346 /* Generate the DIE for a base class.  */
20347
20348 static void
20349 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20350 {
20351   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20352
20353   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20354   add_data_member_location_attribute (die, binfo);
20355
20356   if (BINFO_VIRTUAL_P (binfo))
20357     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20358
20359   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20360      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20361      the default has always been DW_ACCESS_private.  */
20362   if (access == access_public_node)
20363     {
20364       if (dwarf_version == 2
20365           || context_die->die_tag == DW_TAG_class_type)
20366       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20367     }
20368   else if (access == access_protected_node)
20369     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20370   else if (dwarf_version > 2
20371            && context_die->die_tag != DW_TAG_class_type)
20372     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20373 }
20374
20375 /* Generate a DIE for a class member.  */
20376
20377 static void
20378 gen_member_die (tree type, dw_die_ref context_die)
20379 {
20380   tree member;
20381   tree binfo = TYPE_BINFO (type);
20382   dw_die_ref child;
20383
20384   /* If this is not an incomplete type, output descriptions of each of its
20385      members. Note that as we output the DIEs necessary to represent the
20386      members of this record or union type, we will also be trying to output
20387      DIEs to represent the *types* of those members. However the `type'
20388      function (above) will specifically avoid generating type DIEs for member
20389      types *within* the list of member DIEs for this (containing) type except
20390      for those types (of members) which are explicitly marked as also being
20391      members of this (containing) type themselves.  The g++ front- end can
20392      force any given type to be treated as a member of some other (containing)
20393      type by setting the TYPE_CONTEXT of the given (member) type to point to
20394      the TREE node representing the appropriate (containing) type.  */
20395
20396   /* First output info about the base classes.  */
20397   if (binfo)
20398     {
20399       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20400       int i;
20401       tree base;
20402
20403       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20404         gen_inheritance_die (base,
20405                              (accesses ? VEC_index (tree, accesses, i)
20406                               : access_public_node), context_die);
20407     }
20408
20409   /* Now output info about the data members and type members.  */
20410   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20411     {
20412       /* If we thought we were generating minimal debug info for TYPE
20413          and then changed our minds, some of the member declarations
20414          may have already been defined.  Don't define them again, but
20415          do put them in the right order.  */
20416
20417       child = lookup_decl_die (member);
20418       if (child)
20419         splice_child_die (context_die, child);
20420       else
20421         gen_decl_die (member, NULL, context_die);
20422     }
20423
20424   /* Now output info about the function members (if any).  */
20425   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20426     {
20427       /* Don't include clones in the member list.  */
20428       if (DECL_ABSTRACT_ORIGIN (member))
20429         continue;
20430
20431       child = lookup_decl_die (member);
20432       if (child)
20433         splice_child_die (context_die, child);
20434       else
20435         gen_decl_die (member, NULL, context_die);
20436     }
20437 }
20438
20439 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20440    is set, we pretend that the type was never defined, so we only get the
20441    member DIEs needed by later specification DIEs.  */
20442
20443 static void
20444 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20445                                 enum debug_info_usage usage)
20446 {
20447   dw_die_ref type_die = lookup_type_die (type);
20448   dw_die_ref scope_die = 0;
20449   int nested = 0;
20450   int complete = (TYPE_SIZE (type)
20451                   && (! TYPE_STUB_DECL (type)
20452                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20453   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20454   complete = complete && should_emit_struct_debug (type, usage);
20455
20456   if (type_die && ! complete)
20457     return;
20458
20459   if (TYPE_CONTEXT (type) != NULL_TREE
20460       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20461           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20462     nested = 1;
20463
20464   scope_die = scope_die_for (type, context_die);
20465
20466   if (! type_die || (nested && is_cu_die (scope_die)))
20467     /* First occurrence of type or toplevel definition of nested class.  */
20468     {
20469       dw_die_ref old_die = type_die;
20470
20471       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20472                           ? record_type_tag (type) : DW_TAG_union_type,
20473                           scope_die, type);
20474       equate_type_number_to_die (type, type_die);
20475       if (old_die)
20476         add_AT_specification (type_die, old_die);
20477       else
20478         add_name_attribute (type_die, type_tag (type));
20479     }
20480   else
20481     remove_AT (type_die, DW_AT_declaration);
20482
20483   /* Generate child dies for template paramaters.  */
20484   if (debug_info_level > DINFO_LEVEL_TERSE
20485       && COMPLETE_TYPE_P (type))
20486     schedule_generic_params_dies_gen (type);
20487
20488   /* If this type has been completed, then give it a byte_size attribute and
20489      then give a list of members.  */
20490   if (complete && !ns_decl)
20491     {
20492       /* Prevent infinite recursion in cases where the type of some member of
20493          this type is expressed in terms of this type itself.  */
20494       TREE_ASM_WRITTEN (type) = 1;
20495       add_byte_size_attribute (type_die, type);
20496       if (TYPE_STUB_DECL (type) != NULL_TREE)
20497         {
20498           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20499           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20500         }
20501
20502       /* If the first reference to this type was as the return type of an
20503          inline function, then it may not have a parent.  Fix this now.  */
20504       if (type_die->die_parent == NULL)
20505         add_child_die (scope_die, type_die);
20506
20507       push_decl_scope (type);
20508       gen_member_die (type, type_die);
20509       pop_decl_scope ();
20510
20511       /* GNU extension: Record what type our vtable lives in.  */
20512       if (TYPE_VFIELD (type))
20513         {
20514           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20515
20516           gen_type_die (vtype, context_die);
20517           add_AT_die_ref (type_die, DW_AT_containing_type,
20518                           lookup_type_die (vtype));
20519         }
20520     }
20521   else
20522     {
20523       add_AT_flag (type_die, DW_AT_declaration, 1);
20524
20525       /* We don't need to do this for function-local types.  */
20526       if (TYPE_STUB_DECL (type)
20527           && ! decl_function_context (TYPE_STUB_DECL (type)))
20528         VEC_safe_push (tree, gc, incomplete_types, type);
20529     }
20530
20531   if (get_AT (type_die, DW_AT_name))
20532     add_pubtype (type, type_die);
20533 }
20534
20535 /* Generate a DIE for a subroutine _type_.  */
20536
20537 static void
20538 gen_subroutine_type_die (tree type, dw_die_ref context_die)
20539 {
20540   tree return_type = TREE_TYPE (type);
20541   dw_die_ref subr_die
20542     = new_die (DW_TAG_subroutine_type,
20543                scope_die_for (type, context_die), type);
20544
20545   equate_type_number_to_die (type, subr_die);
20546   add_prototyped_attribute (subr_die, type);
20547   add_type_attribute (subr_die, return_type, 0, 0, context_die);
20548   gen_formal_types_die (type, subr_die);
20549
20550   if (get_AT (subr_die, DW_AT_name))
20551     add_pubtype (type, subr_die);
20552 }
20553
20554 /* Generate a DIE for a type definition.  */
20555
20556 static void
20557 gen_typedef_die (tree decl, dw_die_ref context_die)
20558 {
20559   dw_die_ref type_die;
20560   tree origin;
20561
20562   if (TREE_ASM_WRITTEN (decl))
20563     return;
20564
20565   TREE_ASM_WRITTEN (decl) = 1;
20566   type_die = new_die (DW_TAG_typedef, context_die, decl);
20567   origin = decl_ultimate_origin (decl);
20568   if (origin != NULL)
20569     add_abstract_origin_attribute (type_die, origin);
20570   else
20571     {
20572       tree type;
20573
20574       add_name_and_src_coords_attributes (type_die, decl);
20575       if (DECL_ORIGINAL_TYPE (decl))
20576         {
20577           type = DECL_ORIGINAL_TYPE (decl);
20578
20579           gcc_assert (type != TREE_TYPE (decl));
20580           equate_type_number_to_die (TREE_TYPE (decl), type_die);
20581         }
20582       else
20583         {
20584           type = TREE_TYPE (decl);
20585
20586           if (is_naming_typedef_decl (TYPE_NAME (type)))
20587             {
20588               /* Here, we are in the case of decl being a typedef naming
20589                  an anonymous type, e.g:
20590                      typedef struct {...} foo;
20591                  In that case TREE_TYPE (decl) is not a typedef variant
20592                  type and TYPE_NAME of the anonymous type is set to the
20593                  TYPE_DECL of the typedef. This construct is emitted by
20594                  the C++ FE.
20595
20596                  TYPE is the anonymous struct named by the typedef
20597                  DECL. As we need the DW_AT_type attribute of the
20598                  DW_TAG_typedef to point to the DIE of TYPE, let's
20599                  generate that DIE right away. add_type_attribute
20600                  called below will then pick (via lookup_type_die) that
20601                  anonymous struct DIE.  */
20602               if (!TREE_ASM_WRITTEN (type))
20603                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
20604             }
20605         }
20606
20607       add_type_attribute (type_die, type, TREE_READONLY (decl),
20608                           TREE_THIS_VOLATILE (decl), context_die);
20609
20610       if (is_naming_typedef_decl (decl))
20611         /* We want that all subsequent calls to lookup_type_die with
20612            TYPE in argument yield the DW_TAG_typedef we have just
20613            created.  */
20614         equate_type_number_to_die (type, type_die);
20615
20616       add_accessibility_attribute (type_die, decl);
20617     }
20618
20619   if (DECL_ABSTRACT (decl))
20620     equate_decl_number_to_die (decl, type_die);
20621
20622   if (get_AT (type_die, DW_AT_name))
20623     add_pubtype (decl, type_die);
20624 }
20625
20626 /* Generate a DIE for a struct, class, enum or union type.  */
20627
20628 static void
20629 gen_tagged_type_die (tree type,
20630                      dw_die_ref context_die,
20631                      enum debug_info_usage usage)
20632 {
20633   int need_pop;
20634
20635   if (type == NULL_TREE
20636       || !is_tagged_type (type))
20637     return;
20638
20639   /* If this is a nested type whose containing class hasn't been written
20640      out yet, writing it out will cover this one, too.  This does not apply
20641      to instantiations of member class templates; they need to be added to
20642      the containing class as they are generated.  FIXME: This hurts the
20643      idea of combining type decls from multiple TUs, since we can't predict
20644      what set of template instantiations we'll get.  */
20645   if (TYPE_CONTEXT (type)
20646       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20647       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
20648     {
20649       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
20650
20651       if (TREE_ASM_WRITTEN (type))
20652         return;
20653
20654       /* If that failed, attach ourselves to the stub.  */
20655       push_decl_scope (TYPE_CONTEXT (type));
20656       context_die = lookup_type_die (TYPE_CONTEXT (type));
20657       need_pop = 1;
20658     }
20659   else if (TYPE_CONTEXT (type) != NULL_TREE
20660            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
20661     {
20662       /* If this type is local to a function that hasn't been written
20663          out yet, use a NULL context for now; it will be fixed up in
20664          decls_for_scope.  */
20665       context_die = lookup_decl_die (TYPE_CONTEXT (type));
20666       /* A declaration DIE doesn't count; nested types need to go in the
20667          specification.  */
20668       if (context_die && is_declaration_die (context_die))
20669         context_die = NULL;
20670       need_pop = 0;
20671     }
20672   else
20673     {
20674       context_die = declare_in_namespace (type, context_die);
20675       need_pop = 0;
20676     }
20677
20678   if (TREE_CODE (type) == ENUMERAL_TYPE)
20679     {
20680       /* This might have been written out by the call to
20681          declare_in_namespace.  */
20682       if (!TREE_ASM_WRITTEN (type))
20683         gen_enumeration_type_die (type, context_die);
20684     }
20685   else
20686     gen_struct_or_union_type_die (type, context_die, usage);
20687
20688   if (need_pop)
20689     pop_decl_scope ();
20690
20691   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
20692      it up if it is ever completed.  gen_*_type_die will set it for us
20693      when appropriate.  */
20694 }
20695
20696 /* Generate a type description DIE.  */
20697
20698 static void
20699 gen_type_die_with_usage (tree type, dw_die_ref context_die,
20700                          enum debug_info_usage usage)
20701 {
20702   struct array_descr_info info;
20703
20704   if (type == NULL_TREE || type == error_mark_node)
20705     return;
20706
20707   if (TYPE_NAME (type) != NULL_TREE
20708       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
20709       && is_redundant_typedef (TYPE_NAME (type))
20710       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
20711     /* The DECL of this type is a typedef we don't want to emit debug
20712        info for but we want debug info for its underlying typedef.
20713        This can happen for e.g, the injected-class-name of a C++
20714        type.  */
20715     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
20716
20717   /* If TYPE is a typedef type variant, let's generate debug info
20718      for the parent typedef which TYPE is a type of.  */
20719   if (typedef_variant_p (type))
20720     {
20721       if (TREE_ASM_WRITTEN (type))
20722         return;
20723
20724       /* Prevent broken recursion; we can't hand off to the same type.  */
20725       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
20726
20727       /* Use the DIE of the containing namespace as the parent DIE of
20728          the type description DIE we want to generate.  */
20729       if (DECL_CONTEXT (TYPE_NAME (type))
20730           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20731         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20732
20733       TREE_ASM_WRITTEN (type) = 1;
20734
20735       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20736       return;
20737     }
20738
20739   /* If type is an anonymous tagged type named by a typedef, let's
20740      generate debug info for the typedef.  */
20741   if (is_naming_typedef_decl (TYPE_NAME (type)))
20742     {
20743       /* Use the DIE of the containing namespace as the parent DIE of
20744          the type description DIE we want to generate.  */
20745       if (DECL_CONTEXT (TYPE_NAME (type))
20746           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
20747         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
20748       
20749       gen_decl_die (TYPE_NAME (type), NULL, context_die);
20750       return;
20751     }
20752
20753   /* If this is an array type with hidden descriptor, handle it first.  */
20754   if (!TREE_ASM_WRITTEN (type)
20755       && lang_hooks.types.get_array_descr_info
20756       && lang_hooks.types.get_array_descr_info (type, &info)
20757       && (dwarf_version >= 3 || !dwarf_strict))
20758     {
20759       gen_descr_array_type_die (type, &info, context_die);
20760       TREE_ASM_WRITTEN (type) = 1;
20761       return;
20762     }
20763
20764   /* We are going to output a DIE to represent the unqualified version
20765      of this type (i.e. without any const or volatile qualifiers) so
20766      get the main variant (i.e. the unqualified version) of this type
20767      now.  (Vectors are special because the debugging info is in the
20768      cloned type itself).  */
20769   if (TREE_CODE (type) != VECTOR_TYPE)
20770     type = type_main_variant (type);
20771
20772   if (TREE_ASM_WRITTEN (type))
20773     return;
20774
20775   switch (TREE_CODE (type))
20776     {
20777     case ERROR_MARK:
20778       break;
20779
20780     case POINTER_TYPE:
20781     case REFERENCE_TYPE:
20782       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
20783          ensures that the gen_type_die recursion will terminate even if the
20784          type is recursive.  Recursive types are possible in Ada.  */
20785       /* ??? We could perhaps do this for all types before the switch
20786          statement.  */
20787       TREE_ASM_WRITTEN (type) = 1;
20788
20789       /* For these types, all that is required is that we output a DIE (or a
20790          set of DIEs) to represent the "basis" type.  */
20791       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20792                                 DINFO_USAGE_IND_USE);
20793       break;
20794
20795     case OFFSET_TYPE:
20796       /* This code is used for C++ pointer-to-data-member types.
20797          Output a description of the relevant class type.  */
20798       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
20799                                         DINFO_USAGE_IND_USE);
20800
20801       /* Output a description of the type of the object pointed to.  */
20802       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20803                                         DINFO_USAGE_IND_USE);
20804
20805       /* Now output a DIE to represent this pointer-to-data-member type
20806          itself.  */
20807       gen_ptr_to_mbr_type_die (type, context_die);
20808       break;
20809
20810     case FUNCTION_TYPE:
20811       /* Force out return type (in case it wasn't forced out already).  */
20812       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20813                                         DINFO_USAGE_DIR_USE);
20814       gen_subroutine_type_die (type, context_die);
20815       break;
20816
20817     case METHOD_TYPE:
20818       /* Force out return type (in case it wasn't forced out already).  */
20819       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20820                                         DINFO_USAGE_DIR_USE);
20821       gen_subroutine_type_die (type, context_die);
20822       break;
20823
20824     case ARRAY_TYPE:
20825       gen_array_type_die (type, context_die);
20826       break;
20827
20828     case VECTOR_TYPE:
20829       gen_array_type_die (type, context_die);
20830       break;
20831
20832     case ENUMERAL_TYPE:
20833     case RECORD_TYPE:
20834     case UNION_TYPE:
20835     case QUAL_UNION_TYPE:
20836       gen_tagged_type_die (type, context_die, usage);
20837       return;
20838
20839     case VOID_TYPE:
20840     case INTEGER_TYPE:
20841     case REAL_TYPE:
20842     case FIXED_POINT_TYPE:
20843     case COMPLEX_TYPE:
20844     case BOOLEAN_TYPE:
20845       /* No DIEs needed for fundamental types.  */
20846       break;
20847
20848     case NULLPTR_TYPE:
20849     case LANG_TYPE:
20850       /* Just use DW_TAG_unspecified_type.  */
20851       {
20852         dw_die_ref type_die = lookup_type_die (type);
20853         if (type_die == NULL)
20854           {
20855             tree name = TYPE_NAME (type);
20856             if (TREE_CODE (name) == TYPE_DECL)
20857               name = DECL_NAME (name);
20858             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
20859             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20860             equate_type_number_to_die (type, type_die);
20861           }
20862       }
20863       break;
20864
20865     default:
20866       gcc_unreachable ();
20867     }
20868
20869   TREE_ASM_WRITTEN (type) = 1;
20870 }
20871
20872 static void
20873 gen_type_die (tree type, dw_die_ref context_die)
20874 {
20875   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20876 }
20877
20878 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20879    things which are local to the given block.  */
20880
20881 static void
20882 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20883 {
20884   int must_output_die = 0;
20885   bool inlined_func;
20886
20887   /* Ignore blocks that are NULL.  */
20888   if (stmt == NULL_TREE)
20889     return;
20890
20891   inlined_func = inlined_function_outer_scope_p (stmt);
20892
20893   /* If the block is one fragment of a non-contiguous block, do not
20894      process the variables, since they will have been done by the
20895      origin block.  Do process subblocks.  */
20896   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20897     {
20898       tree sub;
20899
20900       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20901         gen_block_die (sub, context_die, depth + 1);
20902
20903       return;
20904     }
20905
20906   /* Determine if we need to output any Dwarf DIEs at all to represent this
20907      block.  */
20908   if (inlined_func)
20909     /* The outer scopes for inlinings *must* always be represented.  We
20910        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20911     must_output_die = 1;
20912   else
20913     {
20914       /* Determine if this block directly contains any "significant"
20915          local declarations which we will need to output DIEs for.  */
20916       if (debug_info_level > DINFO_LEVEL_TERSE)
20917         /* We are not in terse mode so *any* local declaration counts
20918            as being a "significant" one.  */
20919         must_output_die = ((BLOCK_VARS (stmt) != NULL
20920                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20921                            && (TREE_USED (stmt)
20922                                || TREE_ASM_WRITTEN (stmt)
20923                                || BLOCK_ABSTRACT (stmt)));
20924       else if ((TREE_USED (stmt)
20925                 || TREE_ASM_WRITTEN (stmt)
20926                 || BLOCK_ABSTRACT (stmt))
20927                && !dwarf2out_ignore_block (stmt))
20928         must_output_die = 1;
20929     }
20930
20931   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20932      DIE for any block which contains no significant local declarations at
20933      all.  Rather, in such cases we just call `decls_for_scope' so that any
20934      needed Dwarf info for any sub-blocks will get properly generated. Note
20935      that in terse mode, our definition of what constitutes a "significant"
20936      local declaration gets restricted to include only inlined function
20937      instances and local (nested) function definitions.  */
20938   if (must_output_die)
20939     {
20940       if (inlined_func)
20941         {
20942           /* If STMT block is abstract, that means we have been called
20943              indirectly from dwarf2out_abstract_function.
20944              That function rightfully marks the descendent blocks (of
20945              the abstract function it is dealing with) as being abstract,
20946              precisely to prevent us from emitting any
20947              DW_TAG_inlined_subroutine DIE as a descendent
20948              of an abstract function instance. So in that case, we should
20949              not call gen_inlined_subroutine_die.
20950
20951              Later though, when cgraph asks dwarf2out to emit info
20952              for the concrete instance of the function decl into which
20953              the concrete instance of STMT got inlined, the later will lead
20954              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20955           if (! BLOCK_ABSTRACT (stmt))
20956             gen_inlined_subroutine_die (stmt, context_die, depth);
20957         }
20958       else
20959         gen_lexical_block_die (stmt, context_die, depth);
20960     }
20961   else
20962     decls_for_scope (stmt, context_die, depth);
20963 }
20964
20965 /* Process variable DECL (or variable with origin ORIGIN) within
20966    block STMT and add it to CONTEXT_DIE.  */
20967 static void
20968 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20969 {
20970   dw_die_ref die;
20971   tree decl_or_origin = decl ? decl : origin;
20972
20973   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20974     die = lookup_decl_die (decl_or_origin);
20975   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20976            && TYPE_DECL_IS_STUB (decl_or_origin))
20977     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20978   else
20979     die = NULL;
20980
20981   if (die != NULL && die->die_parent == NULL)
20982     add_child_die (context_die, die);
20983   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20984     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20985                                          stmt, context_die);
20986   else
20987     gen_decl_die (decl, origin, context_die);
20988 }
20989
20990 /* Generate all of the decls declared within a given scope and (recursively)
20991    all of its sub-blocks.  */
20992
20993 static void
20994 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20995 {
20996   tree decl;
20997   unsigned int i;
20998   tree subblocks;
20999
21000   /* Ignore NULL blocks.  */
21001   if (stmt == NULL_TREE)
21002     return;
21003
21004   /* Output the DIEs to represent all of the data objects and typedefs
21005      declared directly within this block but not within any nested
21006      sub-blocks.  Also, nested function and tag DIEs have been
21007      generated with a parent of NULL; fix that up now.  */
21008   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21009     process_scope_var (stmt, decl, NULL_TREE, context_die);
21010   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21011     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21012                        context_die);
21013
21014   /* If we're at -g1, we're not interested in subblocks.  */
21015   if (debug_info_level <= DINFO_LEVEL_TERSE)
21016     return;
21017
21018   /* Output the DIEs to represent all sub-blocks (and the items declared
21019      therein) of this block.  */
21020   for (subblocks = BLOCK_SUBBLOCKS (stmt);
21021        subblocks != NULL;
21022        subblocks = BLOCK_CHAIN (subblocks))
21023     gen_block_die (subblocks, context_die, depth + 1);
21024 }
21025
21026 /* Is this a typedef we can avoid emitting?  */
21027
21028 static inline int
21029 is_redundant_typedef (const_tree decl)
21030 {
21031   if (TYPE_DECL_IS_STUB (decl))
21032     return 1;
21033
21034   if (DECL_ARTIFICIAL (decl)
21035       && DECL_CONTEXT (decl)
21036       && is_tagged_type (DECL_CONTEXT (decl))
21037       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21038       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21039     /* Also ignore the artificial member typedef for the class name.  */
21040     return 1;
21041
21042   return 0;
21043 }
21044
21045 /* Return TRUE if TYPE is a typedef that names a type for linkage
21046    purposes. This kind of typedefs is produced by the C++ FE for
21047    constructs like:
21048
21049    typedef struct {...} foo;
21050
21051    In that case, there is no typedef variant type produced for foo.
21052    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21053    struct type.  */
21054
21055 static bool
21056 is_naming_typedef_decl (const_tree decl)
21057 {
21058   if (decl == NULL_TREE
21059       || TREE_CODE (decl) != TYPE_DECL
21060       || !is_tagged_type (TREE_TYPE (decl))
21061       || DECL_IS_BUILTIN (decl)
21062       || is_redundant_typedef (decl)
21063       /* It looks like Ada produces TYPE_DECLs that are very similar
21064          to C++ naming typedefs but that have different
21065          semantics. Let's be specific to c++ for now.  */
21066       || !is_cxx ())
21067     return FALSE;
21068
21069   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21070           && TYPE_NAME (TREE_TYPE (decl)) == decl
21071           && (TYPE_STUB_DECL (TREE_TYPE (decl))
21072               != TYPE_NAME (TREE_TYPE (decl))));
21073 }
21074
21075 /* Returns the DIE for a context.  */
21076
21077 static inline dw_die_ref
21078 get_context_die (tree context)
21079 {
21080   if (context)
21081     {
21082       /* Find die that represents this context.  */
21083       if (TYPE_P (context))
21084         return force_type_die (TYPE_MAIN_VARIANT (context));
21085       else
21086         return force_decl_die (context);
21087     }
21088   return comp_unit_die ();
21089 }
21090
21091 /* Returns the DIE for decl.  A DIE will always be returned.  */
21092
21093 static dw_die_ref
21094 force_decl_die (tree decl)
21095 {
21096   dw_die_ref decl_die;
21097   unsigned saved_external_flag;
21098   tree save_fn = NULL_TREE;
21099   decl_die = lookup_decl_die (decl);
21100   if (!decl_die)
21101     {
21102       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21103
21104       decl_die = lookup_decl_die (decl);
21105       if (decl_die)
21106         return decl_die;
21107
21108       switch (TREE_CODE (decl))
21109         {
21110         case FUNCTION_DECL:
21111           /* Clear current_function_decl, so that gen_subprogram_die thinks
21112              that this is a declaration. At this point, we just want to force
21113              declaration die.  */
21114           save_fn = current_function_decl;
21115           current_function_decl = NULL_TREE;
21116           gen_subprogram_die (decl, context_die);
21117           current_function_decl = save_fn;
21118           break;
21119
21120         case VAR_DECL:
21121           /* Set external flag to force declaration die. Restore it after
21122            gen_decl_die() call.  */
21123           saved_external_flag = DECL_EXTERNAL (decl);
21124           DECL_EXTERNAL (decl) = 1;
21125           gen_decl_die (decl, NULL, context_die);
21126           DECL_EXTERNAL (decl) = saved_external_flag;
21127           break;
21128
21129         case NAMESPACE_DECL:
21130           if (dwarf_version >= 3 || !dwarf_strict)
21131             dwarf2out_decl (decl);
21132           else
21133             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21134             decl_die = comp_unit_die ();
21135           break;
21136
21137         case TRANSLATION_UNIT_DECL:
21138           decl_die = comp_unit_die ();
21139           break;
21140
21141         default:
21142           gcc_unreachable ();
21143         }
21144
21145       /* We should be able to find the DIE now.  */
21146       if (!decl_die)
21147         decl_die = lookup_decl_die (decl);
21148       gcc_assert (decl_die);
21149     }
21150
21151   return decl_die;
21152 }
21153
21154 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21155    always returned.  */
21156
21157 static dw_die_ref
21158 force_type_die (tree type)
21159 {
21160   dw_die_ref type_die;
21161
21162   type_die = lookup_type_die (type);
21163   if (!type_die)
21164     {
21165       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21166
21167       type_die = modified_type_die (type, TYPE_READONLY (type),
21168                                     TYPE_VOLATILE (type), context_die);
21169       gcc_assert (type_die);
21170     }
21171   return type_die;
21172 }
21173
21174 /* Force out any required namespaces to be able to output DECL,
21175    and return the new context_die for it, if it's changed.  */
21176
21177 static dw_die_ref
21178 setup_namespace_context (tree thing, dw_die_ref context_die)
21179 {
21180   tree context = (DECL_P (thing)
21181                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21182   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21183     /* Force out the namespace.  */
21184     context_die = force_decl_die (context);
21185
21186   return context_die;
21187 }
21188
21189 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21190    type) within its namespace, if appropriate.
21191
21192    For compatibility with older debuggers, namespace DIEs only contain
21193    declarations; all definitions are emitted at CU scope.  */
21194
21195 static dw_die_ref
21196 declare_in_namespace (tree thing, dw_die_ref context_die)
21197 {
21198   dw_die_ref ns_context;
21199
21200   if (debug_info_level <= DINFO_LEVEL_TERSE)
21201     return context_die;
21202
21203   /* If this decl is from an inlined function, then don't try to emit it in its
21204      namespace, as we will get confused.  It would have already been emitted
21205      when the abstract instance of the inline function was emitted anyways.  */
21206   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21207     return context_die;
21208
21209   ns_context = setup_namespace_context (thing, context_die);
21210
21211   if (ns_context != context_die)
21212     {
21213       if (is_fortran ())
21214         return ns_context;
21215       if (DECL_P (thing))
21216         gen_decl_die (thing, NULL, ns_context);
21217       else
21218         gen_type_die (thing, ns_context);
21219     }
21220   return context_die;
21221 }
21222
21223 /* Generate a DIE for a namespace or namespace alias.  */
21224
21225 static void
21226 gen_namespace_die (tree decl, dw_die_ref context_die)
21227 {
21228   dw_die_ref namespace_die;
21229
21230   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21231      they are an alias of.  */
21232   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21233     {
21234       /* Output a real namespace or module.  */
21235       context_die = setup_namespace_context (decl, comp_unit_die ());
21236       namespace_die = new_die (is_fortran ()
21237                                ? DW_TAG_module : DW_TAG_namespace,
21238                                context_die, decl);
21239       /* For Fortran modules defined in different CU don't add src coords.  */
21240       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21241         {
21242           const char *name = dwarf2_name (decl, 0);
21243           if (name)
21244             add_name_attribute (namespace_die, name);
21245         }
21246       else
21247         add_name_and_src_coords_attributes (namespace_die, decl);
21248       if (DECL_EXTERNAL (decl))
21249         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21250       equate_decl_number_to_die (decl, namespace_die);
21251     }
21252   else
21253     {
21254       /* Output a namespace alias.  */
21255
21256       /* Force out the namespace we are an alias of, if necessary.  */
21257       dw_die_ref origin_die
21258         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21259
21260       if (DECL_FILE_SCOPE_P (decl)
21261           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21262         context_die = setup_namespace_context (decl, comp_unit_die ());
21263       /* Now create the namespace alias DIE.  */
21264       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21265       add_name_and_src_coords_attributes (namespace_die, decl);
21266       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21267       equate_decl_number_to_die (decl, namespace_die);
21268     }
21269 }
21270
21271 /* Generate Dwarf debug information for a decl described by DECL.
21272    The return value is currently only meaningful for PARM_DECLs,
21273    for all other decls it returns NULL.  */
21274
21275 static dw_die_ref
21276 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21277 {
21278   tree decl_or_origin = decl ? decl : origin;
21279   tree class_origin = NULL, ultimate_origin;
21280
21281   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21282     return NULL;
21283
21284   switch (TREE_CODE (decl_or_origin))
21285     {
21286     case ERROR_MARK:
21287       break;
21288
21289     case CONST_DECL:
21290       if (!is_fortran () && !is_ada ())
21291         {
21292           /* The individual enumerators of an enum type get output when we output
21293              the Dwarf representation of the relevant enum type itself.  */
21294           break;
21295         }
21296
21297       /* Emit its type.  */
21298       gen_type_die (TREE_TYPE (decl), context_die);
21299
21300       /* And its containing namespace.  */
21301       context_die = declare_in_namespace (decl, context_die);
21302
21303       gen_const_die (decl, context_die);
21304       break;
21305
21306     case FUNCTION_DECL:
21307       /* Don't output any DIEs to represent mere function declarations,
21308          unless they are class members or explicit block externs.  */
21309       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21310           && DECL_FILE_SCOPE_P (decl_or_origin)
21311           && (current_function_decl == NULL_TREE
21312               || DECL_ARTIFICIAL (decl_or_origin)))
21313         break;
21314
21315 #if 0
21316       /* FIXME */
21317       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21318          on local redeclarations of global functions.  That seems broken.  */
21319       if (current_function_decl != decl)
21320         /* This is only a declaration.  */;
21321 #endif
21322
21323       /* If we're emitting a clone, emit info for the abstract instance.  */
21324       if (origin || DECL_ORIGIN (decl) != decl)
21325         dwarf2out_abstract_function (origin
21326                                      ? DECL_ORIGIN (origin)
21327                                      : DECL_ABSTRACT_ORIGIN (decl));
21328
21329       /* If we're emitting an out-of-line copy of an inline function,
21330          emit info for the abstract instance and set up to refer to it.  */
21331       else if (cgraph_function_possibly_inlined_p (decl)
21332                && ! DECL_ABSTRACT (decl)
21333                && ! class_or_namespace_scope_p (context_die)
21334                /* dwarf2out_abstract_function won't emit a die if this is just
21335                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21336                   that case, because that works only if we have a die.  */
21337                && DECL_INITIAL (decl) != NULL_TREE)
21338         {
21339           dwarf2out_abstract_function (decl);
21340           set_decl_origin_self (decl);
21341         }
21342
21343       /* Otherwise we're emitting the primary DIE for this decl.  */
21344       else if (debug_info_level > DINFO_LEVEL_TERSE)
21345         {
21346           /* Before we describe the FUNCTION_DECL itself, make sure that we
21347              have its containing type.  */
21348           if (!origin)
21349             origin = decl_class_context (decl);
21350           if (origin != NULL_TREE)
21351             gen_type_die (origin, context_die);
21352
21353           /* And its return type.  */
21354           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21355
21356           /* And its virtual context.  */
21357           if (DECL_VINDEX (decl) != NULL_TREE)
21358             gen_type_die (DECL_CONTEXT (decl), context_die);
21359
21360           /* Make sure we have a member DIE for decl.  */
21361           if (origin != NULL_TREE)
21362             gen_type_die_for_member (origin, decl, context_die);
21363
21364           /* And its containing namespace.  */
21365           context_die = declare_in_namespace (decl, context_die);
21366         }
21367
21368       /* Now output a DIE to represent the function itself.  */
21369       if (decl)
21370         gen_subprogram_die (decl, context_die);
21371       break;
21372
21373     case TYPE_DECL:
21374       /* If we are in terse mode, don't generate any DIEs to represent any
21375          actual typedefs.  */
21376       if (debug_info_level <= DINFO_LEVEL_TERSE)
21377         break;
21378
21379       /* In the special case of a TYPE_DECL node representing the declaration
21380          of some type tag, if the given TYPE_DECL is marked as having been
21381          instantiated from some other (original) TYPE_DECL node (e.g. one which
21382          was generated within the original definition of an inline function) we
21383          used to generate a special (abbreviated) DW_TAG_structure_type,
21384          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21385          should be actually referencing those DIEs, as variable DIEs with that
21386          type would be emitted already in the abstract origin, so it was always
21387          removed during unused type prunning.  Don't add anything in this
21388          case.  */
21389       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21390         break;
21391
21392       if (is_redundant_typedef (decl))
21393         gen_type_die (TREE_TYPE (decl), context_die);
21394       else
21395         /* Output a DIE to represent the typedef itself.  */
21396         gen_typedef_die (decl, context_die);
21397       break;
21398
21399     case LABEL_DECL:
21400       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21401         gen_label_die (decl, context_die);
21402       break;
21403
21404     case VAR_DECL:
21405     case RESULT_DECL:
21406       /* If we are in terse mode, don't generate any DIEs to represent any
21407          variable declarations or definitions.  */
21408       if (debug_info_level <= DINFO_LEVEL_TERSE)
21409         break;
21410
21411       /* Output any DIEs that are needed to specify the type of this data
21412          object.  */
21413       if (decl_by_reference_p (decl_or_origin))
21414         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21415       else
21416         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21417
21418       /* And its containing type.  */
21419       class_origin = decl_class_context (decl_or_origin);
21420       if (class_origin != NULL_TREE)
21421         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21422
21423       /* And its containing namespace.  */
21424       context_die = declare_in_namespace (decl_or_origin, context_die);
21425
21426       /* Now output the DIE to represent the data object itself.  This gets
21427          complicated because of the possibility that the VAR_DECL really
21428          represents an inlined instance of a formal parameter for an inline
21429          function.  */
21430       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21431       if (ultimate_origin != NULL_TREE
21432           && TREE_CODE (ultimate_origin) == PARM_DECL)
21433         gen_formal_parameter_die (decl, origin,
21434                                   true /* Emit name attribute.  */,
21435                                   context_die);
21436       else
21437         gen_variable_die (decl, origin, context_die);
21438       break;
21439
21440     case FIELD_DECL:
21441       /* Ignore the nameless fields that are used to skip bits but handle C++
21442          anonymous unions and structs.  */
21443       if (DECL_NAME (decl) != NULL_TREE
21444           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21445           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21446         {
21447           gen_type_die (member_declared_type (decl), context_die);
21448           gen_field_die (decl, context_die);
21449         }
21450       break;
21451
21452     case PARM_DECL:
21453       if (DECL_BY_REFERENCE (decl_or_origin))
21454         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21455       else
21456         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21457       return gen_formal_parameter_die (decl, origin,
21458                                        true /* Emit name attribute.  */,
21459                                        context_die);
21460
21461     case NAMESPACE_DECL:
21462     case IMPORTED_DECL:
21463       if (dwarf_version >= 3 || !dwarf_strict)
21464         gen_namespace_die (decl, context_die);
21465       break;
21466
21467     default:
21468       /* Probably some frontend-internal decl.  Assume we don't care.  */
21469       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21470       break;
21471     }
21472
21473   return NULL;
21474 }
21475 \f
21476 /* Output debug information for global decl DECL.  Called from toplev.c after
21477    compilation proper has finished.  */
21478
21479 static void
21480 dwarf2out_global_decl (tree decl)
21481 {
21482   /* Output DWARF2 information for file-scope tentative data object
21483      declarations, file-scope (extern) function declarations (which
21484      had no corresponding body) and file-scope tagged type declarations
21485      and definitions which have not yet been forced out.  */
21486   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21487     dwarf2out_decl (decl);
21488 }
21489
21490 /* Output debug information for type decl DECL.  Called from toplev.c
21491    and from language front ends (to record built-in types).  */
21492 static void
21493 dwarf2out_type_decl (tree decl, int local)
21494 {
21495   if (!local)
21496     dwarf2out_decl (decl);
21497 }
21498
21499 /* Output debug information for imported module or decl DECL.
21500    NAME is non-NULL name in the lexical block if the decl has been renamed.
21501    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21502    that DECL belongs to.
21503    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21504 static void
21505 dwarf2out_imported_module_or_decl_1 (tree decl,
21506                                      tree name,
21507                                      tree lexical_block,
21508                                      dw_die_ref lexical_block_die)
21509 {
21510   expanded_location xloc;
21511   dw_die_ref imported_die = NULL;
21512   dw_die_ref at_import_die;
21513
21514   if (TREE_CODE (decl) == IMPORTED_DECL)
21515     {
21516       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
21517       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
21518       gcc_assert (decl);
21519     }
21520   else
21521     xloc = expand_location (input_location);
21522
21523   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
21524     {
21525       at_import_die = force_type_die (TREE_TYPE (decl));
21526       /* For namespace N { typedef void T; } using N::T; base_type_die
21527          returns NULL, but DW_TAG_imported_declaration requires
21528          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
21529       if (!at_import_die)
21530         {
21531           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
21532           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
21533           at_import_die = lookup_type_die (TREE_TYPE (decl));
21534           gcc_assert (at_import_die);
21535         }
21536     }
21537   else
21538     {
21539       at_import_die = lookup_decl_die (decl);
21540       if (!at_import_die)
21541         {
21542           /* If we're trying to avoid duplicate debug info, we may not have
21543              emitted the member decl for this field.  Emit it now.  */
21544           if (TREE_CODE (decl) == FIELD_DECL)
21545             {
21546               tree type = DECL_CONTEXT (decl);
21547
21548               if (TYPE_CONTEXT (type)
21549                   && TYPE_P (TYPE_CONTEXT (type))
21550                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
21551                                                 DINFO_USAGE_DIR_USE))
21552                 return;
21553               gen_type_die_for_member (type, decl,
21554                                        get_context_die (TYPE_CONTEXT (type)));
21555             }
21556           at_import_die = force_decl_die (decl);
21557         }
21558     }
21559
21560   if (TREE_CODE (decl) == NAMESPACE_DECL)
21561     {
21562       if (dwarf_version >= 3 || !dwarf_strict)
21563         imported_die = new_die (DW_TAG_imported_module,
21564                                 lexical_block_die,
21565                                 lexical_block);
21566       else
21567         return;
21568     }
21569   else
21570     imported_die = new_die (DW_TAG_imported_declaration,
21571                             lexical_block_die,
21572                             lexical_block);
21573
21574   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
21575   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
21576   if (name)
21577     add_AT_string (imported_die, DW_AT_name,
21578                    IDENTIFIER_POINTER (name));
21579   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
21580 }
21581
21582 /* Output debug information for imported module or decl DECL.
21583    NAME is non-NULL name in context if the decl has been renamed.
21584    CHILD is true if decl is one of the renamed decls as part of
21585    importing whole module.  */
21586
21587 static void
21588 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
21589                                    bool child)
21590 {
21591   /* dw_die_ref at_import_die;  */
21592   dw_die_ref scope_die;
21593
21594   if (debug_info_level <= DINFO_LEVEL_TERSE)
21595     return;
21596
21597   gcc_assert (decl);
21598
21599   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
21600      We need decl DIE for reference and scope die. First, get DIE for the decl
21601      itself.  */
21602
21603   /* Get the scope die for decl context. Use comp_unit_die for global module
21604      or decl. If die is not found for non globals, force new die.  */
21605   if (context
21606       && TYPE_P (context)
21607       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
21608     return;
21609
21610   if (!(dwarf_version >= 3 || !dwarf_strict))
21611     return;
21612
21613   scope_die = get_context_die (context);
21614
21615   if (child)
21616     {
21617       gcc_assert (scope_die->die_child);
21618       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
21619       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
21620       scope_die = scope_die->die_child;
21621     }
21622
21623   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
21624   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
21625
21626 }
21627
21628 /* Write the debugging output for DECL.  */
21629
21630 void
21631 dwarf2out_decl (tree decl)
21632 {
21633   dw_die_ref context_die = comp_unit_die ();
21634
21635   switch (TREE_CODE (decl))
21636     {
21637     case ERROR_MARK:
21638       return;
21639
21640     case FUNCTION_DECL:
21641       /* What we would really like to do here is to filter out all mere
21642          file-scope declarations of file-scope functions which are never
21643          referenced later within this translation unit (and keep all of ones
21644          that *are* referenced later on) but we aren't clairvoyant, so we have
21645          no idea which functions will be referenced in the future (i.e. later
21646          on within the current translation unit). So here we just ignore all
21647          file-scope function declarations which are not also definitions.  If
21648          and when the debugger needs to know something about these functions,
21649          it will have to hunt around and find the DWARF information associated
21650          with the definition of the function.
21651
21652          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
21653          nodes represent definitions and which ones represent mere
21654          declarations.  We have to check DECL_INITIAL instead. That's because
21655          the C front-end supports some weird semantics for "extern inline"
21656          function definitions.  These can get inlined within the current
21657          translation unit (and thus, we need to generate Dwarf info for their
21658          abstract instances so that the Dwarf info for the concrete inlined
21659          instances can have something to refer to) but the compiler never
21660          generates any out-of-lines instances of such things (despite the fact
21661          that they *are* definitions).
21662
21663          The important point is that the C front-end marks these "extern
21664          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
21665          them anyway. Note that the C++ front-end also plays some similar games
21666          for inline function definitions appearing within include files which
21667          also contain `#pragma interface' pragmas.  */
21668       if (DECL_INITIAL (decl) == NULL_TREE)
21669         return;
21670
21671       /* If we're a nested function, initially use a parent of NULL; if we're
21672          a plain function, this will be fixed up in decls_for_scope.  If
21673          we're a method, it will be ignored, since we already have a DIE.  */
21674       if (decl_function_context (decl)
21675           /* But if we're in terse mode, we don't care about scope.  */
21676           && debug_info_level > DINFO_LEVEL_TERSE)
21677         context_die = NULL;
21678       break;
21679
21680     case VAR_DECL:
21681       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
21682          declaration and if the declaration was never even referenced from
21683          within this entire compilation unit.  We suppress these DIEs in
21684          order to save space in the .debug section (by eliminating entries
21685          which are probably useless).  Note that we must not suppress
21686          block-local extern declarations (whether used or not) because that
21687          would screw-up the debugger's name lookup mechanism and cause it to
21688          miss things which really ought to be in scope at a given point.  */
21689       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
21690         return;
21691
21692       /* For local statics lookup proper context die.  */
21693       if (TREE_STATIC (decl) && decl_function_context (decl))
21694         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21695
21696       /* If we are in terse mode, don't generate any DIEs to represent any
21697          variable declarations or definitions.  */
21698       if (debug_info_level <= DINFO_LEVEL_TERSE)
21699         return;
21700       break;
21701
21702     case CONST_DECL:
21703       if (debug_info_level <= DINFO_LEVEL_TERSE)
21704         return;
21705       if (!is_fortran () && !is_ada ())
21706         return;
21707       if (TREE_STATIC (decl) && decl_function_context (decl))
21708         context_die = lookup_decl_die (DECL_CONTEXT (decl));
21709       break;
21710
21711     case NAMESPACE_DECL:
21712     case IMPORTED_DECL:
21713       if (debug_info_level <= DINFO_LEVEL_TERSE)
21714         return;
21715       if (lookup_decl_die (decl) != NULL)
21716         return;
21717       break;
21718
21719     case TYPE_DECL:
21720       /* Don't emit stubs for types unless they are needed by other DIEs.  */
21721       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
21722         return;
21723
21724       /* Don't bother trying to generate any DIEs to represent any of the
21725          normal built-in types for the language we are compiling.  */
21726       if (DECL_IS_BUILTIN (decl))
21727         return;
21728
21729       /* If we are in terse mode, don't generate any DIEs for types.  */
21730       if (debug_info_level <= DINFO_LEVEL_TERSE)
21731         return;
21732
21733       /* If we're a function-scope tag, initially use a parent of NULL;
21734          this will be fixed up in decls_for_scope.  */
21735       if (decl_function_context (decl))
21736         context_die = NULL;
21737
21738       break;
21739
21740     default:
21741       return;
21742     }
21743
21744   gen_decl_die (decl, NULL, context_die);
21745 }
21746
21747 /* Write the debugging output for DECL.  */
21748
21749 static void
21750 dwarf2out_function_decl (tree decl)
21751 {
21752   dwarf2out_decl (decl);
21753   call_arg_locations = NULL;
21754   call_arg_loc_last = NULL;
21755   call_site_count = -1;
21756   tail_call_site_count = -1;
21757   VEC_free (dw_die_ref, heap, block_map);
21758   htab_empty (decl_loc_table);
21759 }
21760
21761 /* Output a marker (i.e. a label) for the beginning of the generated code for
21762    a lexical block.  */
21763
21764 static void
21765 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
21766                        unsigned int blocknum)
21767 {
21768   switch_to_section (current_function_section ());
21769   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
21770 }
21771
21772 /* Output a marker (i.e. a label) for the end of the generated code for a
21773    lexical block.  */
21774
21775 static void
21776 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
21777 {
21778   switch_to_section (current_function_section ());
21779   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
21780 }
21781
21782 /* Returns nonzero if it is appropriate not to emit any debugging
21783    information for BLOCK, because it doesn't contain any instructions.
21784
21785    Don't allow this for blocks with nested functions or local classes
21786    as we would end up with orphans, and in the presence of scheduling
21787    we may end up calling them anyway.  */
21788
21789 static bool
21790 dwarf2out_ignore_block (const_tree block)
21791 {
21792   tree decl;
21793   unsigned int i;
21794
21795   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
21796     if (TREE_CODE (decl) == FUNCTION_DECL
21797         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21798       return 0;
21799   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
21800     {
21801       decl = BLOCK_NONLOCALIZED_VAR (block, i);
21802       if (TREE_CODE (decl) == FUNCTION_DECL
21803           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
21804       return 0;
21805     }
21806
21807   return 1;
21808 }
21809
21810 /* Hash table routines for file_hash.  */
21811
21812 static int
21813 file_table_eq (const void *p1_p, const void *p2_p)
21814 {
21815   const struct dwarf_file_data *const p1 =
21816     (const struct dwarf_file_data *) p1_p;
21817   const char *const p2 = (const char *) p2_p;
21818   return filename_cmp (p1->filename, p2) == 0;
21819 }
21820
21821 static hashval_t
21822 file_table_hash (const void *p_p)
21823 {
21824   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21825   return htab_hash_string (p->filename);
21826 }
21827
21828 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21829    dwarf2out.c) and return its "index".  The index of each (known) filename is
21830    just a unique number which is associated with only that one filename.  We
21831    need such numbers for the sake of generating labels (in the .debug_sfnames
21832    section) and references to those files numbers (in the .debug_srcinfo
21833    and.debug_macinfo sections).  If the filename given as an argument is not
21834    found in our current list, add it to the list and assign it the next
21835    available unique index number.  In order to speed up searches, we remember
21836    the index of the filename was looked up last.  This handles the majority of
21837    all searches.  */
21838
21839 static struct dwarf_file_data *
21840 lookup_filename (const char *file_name)
21841 {
21842   void ** slot;
21843   struct dwarf_file_data * created;
21844
21845   /* Check to see if the file name that was searched on the previous
21846      call matches this file name.  If so, return the index.  */
21847   if (file_table_last_lookup
21848       && (file_name == file_table_last_lookup->filename
21849           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
21850     return file_table_last_lookup;
21851
21852   /* Didn't match the previous lookup, search the table.  */
21853   slot = htab_find_slot_with_hash (file_table, file_name,
21854                                    htab_hash_string (file_name), INSERT);
21855   if (*slot)
21856     return (struct dwarf_file_data *) *slot;
21857
21858   created = ggc_alloc_dwarf_file_data ();
21859   created->filename = file_name;
21860   created->emitted_number = 0;
21861   *slot = created;
21862   return created;
21863 }
21864
21865 /* If the assembler will construct the file table, then translate the compiler
21866    internal file table number into the assembler file table number, and emit
21867    a .file directive if we haven't already emitted one yet.  The file table
21868    numbers are different because we prune debug info for unused variables and
21869    types, which may include filenames.  */
21870
21871 static int
21872 maybe_emit_file (struct dwarf_file_data * fd)
21873 {
21874   if (! fd->emitted_number)
21875     {
21876       if (last_emitted_file)
21877         fd->emitted_number = last_emitted_file->emitted_number + 1;
21878       else
21879         fd->emitted_number = 1;
21880       last_emitted_file = fd;
21881
21882       if (DWARF2_ASM_LINE_DEBUG_INFO)
21883         {
21884           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21885           output_quoted_string (asm_out_file,
21886                                 remap_debug_filename (fd->filename));
21887           fputc ('\n', asm_out_file);
21888         }
21889     }
21890
21891   return fd->emitted_number;
21892 }
21893
21894 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21895    That generation should happen after function debug info has been
21896    generated. The value of the attribute is the constant value of ARG.  */
21897
21898 static void
21899 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21900 {
21901   die_arg_entry entry;
21902
21903   if (!die || !arg)
21904     return;
21905
21906   if (!tmpl_value_parm_die_table)
21907     tmpl_value_parm_die_table
21908       = VEC_alloc (die_arg_entry, gc, 32);
21909
21910   entry.die = die;
21911   entry.arg = arg;
21912   VEC_safe_push (die_arg_entry, gc,
21913                  tmpl_value_parm_die_table,
21914                  &entry);
21915 }
21916
21917 /* Return TRUE if T is an instance of generic type, FALSE
21918    otherwise.  */
21919
21920 static bool
21921 generic_type_p (tree t)
21922 {
21923   if (t == NULL_TREE || !TYPE_P (t))
21924     return false;
21925   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
21926 }
21927
21928 /* Schedule the generation of the generic parameter dies for the
21929   instance of generic type T. The proper generation itself is later
21930   done by gen_scheduled_generic_parms_dies. */
21931
21932 static void
21933 schedule_generic_params_dies_gen (tree t)
21934 {
21935   if (!generic_type_p (t))
21936     return;
21937
21938   if (generic_type_instances == NULL)
21939     generic_type_instances = VEC_alloc (tree, gc, 256);
21940
21941   VEC_safe_push (tree, gc, generic_type_instances, t);
21942 }
21943
21944 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21945    by append_entry_to_tmpl_value_parm_die_table. This function must
21946    be called after function DIEs have been generated.  */
21947
21948 static void
21949 gen_remaining_tmpl_value_param_die_attribute (void)
21950 {
21951   if (tmpl_value_parm_die_table)
21952     {
21953       unsigned i;
21954       die_arg_entry *e;
21955
21956       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
21957         tree_add_const_value_attribute (e->die, e->arg);
21958     }
21959 }
21960
21961 /* Generate generic parameters DIEs for instances of generic types
21962    that have been previously scheduled by
21963    schedule_generic_params_dies_gen. This function must be called
21964    after all the types of the CU have been laid out.  */
21965
21966 static void
21967 gen_scheduled_generic_parms_dies (void)
21968 {
21969   unsigned i;
21970   tree t;
21971
21972   if (generic_type_instances == NULL)
21973     return;
21974   
21975   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
21976     gen_generic_params_dies (t);
21977 }
21978
21979
21980 /* Replace DW_AT_name for the decl with name.  */
21981
21982 static void
21983 dwarf2out_set_name (tree decl, tree name)
21984 {
21985   dw_die_ref die;
21986   dw_attr_ref attr;
21987   const char *dname;
21988
21989   die = TYPE_SYMTAB_DIE (decl);
21990   if (!die)
21991     return;
21992
21993   dname = dwarf2_name (name, 0);
21994   if (!dname)
21995     return;
21996
21997   attr = get_AT (die, DW_AT_name);
21998   if (attr)
21999     {
22000       struct indirect_string_node *node;
22001
22002       node = find_AT_string (dname);
22003       /* replace the string.  */
22004       attr->dw_attr_val.v.val_str = node;
22005     }
22006
22007   else
22008     add_name_attribute (die, dname);
22009 }
22010
22011 /* Called by the final INSN scan whenever we see a direct function call.
22012    Make an entry into the direct call table, recording the point of call
22013    and a reference to the target function's debug entry.  */
22014
22015 static void
22016 dwarf2out_direct_call (tree targ)
22017 {
22018   dcall_entry e;
22019   tree origin = decl_ultimate_origin (targ);
22020
22021   /* If this is a clone, use the abstract origin as the target.  */
22022   if (origin)
22023     targ = origin;
22024
22025   e.poc_label_num = poc_label_num++;
22026   e.poc_decl = current_function_decl;
22027   e.targ_die = force_decl_die (targ);
22028   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
22029
22030   /* Drop a label at the return point to mark the point of call.  */
22031   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
22032 }
22033
22034 /* Returns a hash value for X (which really is a struct vcall_insn).  */
22035
22036 static hashval_t
22037 vcall_insn_table_hash (const void *x)
22038 {
22039   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
22040 }
22041
22042 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
22043    insnd_uid of *Y.  */
22044
22045 static int
22046 vcall_insn_table_eq (const void *x, const void *y)
22047 {
22048   return (((const struct vcall_insn *) x)->insn_uid
22049           == ((const struct vcall_insn *) y)->insn_uid);
22050 }
22051
22052 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
22053
22054 static void
22055 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
22056 {
22057   struct vcall_insn *item = ggc_alloc_vcall_insn ();
22058   struct vcall_insn **slot;
22059
22060   gcc_assert (item);
22061   item->insn_uid = insn_uid;
22062   item->vtable_slot = vtable_slot;
22063   slot = (struct vcall_insn **)
22064       htab_find_slot_with_hash (vcall_insn_table, &item,
22065                                 (hashval_t) insn_uid, INSERT);
22066   *slot = item;
22067 }
22068
22069 /* Return the VTABLE_SLOT associated with INSN_UID.  */
22070
22071 static unsigned int
22072 lookup_vcall_insn (unsigned int insn_uid)
22073 {
22074   struct vcall_insn item;
22075   struct vcall_insn *p;
22076
22077   item.insn_uid = insn_uid;
22078   item.vtable_slot = 0;
22079   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
22080                                                  (void *) &item,
22081                                                  (hashval_t) insn_uid);
22082   if (p == NULL)
22083     return (unsigned int) -1;
22084   return p->vtable_slot;
22085 }
22086
22087
22088 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
22089    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
22090    is the vtable slot index that we will need to put in the virtual call
22091    table later.  */
22092
22093 static void
22094 dwarf2out_virtual_call_token (tree addr, int insn_uid)
22095 {
22096   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
22097     {
22098       tree token = OBJ_TYPE_REF_TOKEN (addr);
22099       if (TREE_CODE (token) == INTEGER_CST)
22100         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
22101     }
22102 }
22103
22104 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
22105    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
22106    with NEW_INSN.  */
22107
22108 static void
22109 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
22110 {
22111   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
22112
22113   if (vtable_slot != (unsigned int) -1)
22114     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
22115 }
22116
22117 /* Called by the final INSN scan whenever we see a virtual function call.
22118    Make an entry into the virtual call table, recording the point of call
22119    and the slot index of the vtable entry used to call the virtual member
22120    function.  The slot index was associated with the INSN_UID during the
22121    lowering to RTL.  */
22122
22123 static void
22124 dwarf2out_virtual_call (int insn_uid)
22125 {
22126   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
22127   vcall_entry e;
22128
22129   if (vtable_slot == (unsigned int) -1)
22130     return;
22131
22132   e.poc_label_num = poc_label_num++;
22133   e.vtable_slot = vtable_slot;
22134   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
22135
22136   /* Drop a label at the return point to mark the point of call.  */
22137   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
22138 }
22139
22140 /* Called by the final INSN scan whenever we see a var location.  We
22141    use it to drop labels in the right places, and throw the location in
22142    our lookup table.  */
22143
22144 static void
22145 dwarf2out_var_location (rtx loc_note)
22146 {
22147   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22148   struct var_loc_node *newloc;
22149   rtx next_real;
22150   static const char *last_label;
22151   static const char *last_postcall_label;
22152   static bool last_in_cold_section_p;
22153   tree decl;
22154   bool var_loc_p;
22155
22156   if (!NOTE_P (loc_note))
22157     {
22158       if (CALL_P (loc_note))
22159         {
22160           call_site_count++;
22161           if (SIBLING_CALL_P (loc_note))
22162             tail_call_site_count++;
22163         }
22164       return;
22165     }
22166
22167   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22168   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22169     return;
22170
22171   next_real = next_real_insn (loc_note);
22172
22173   /* If there are no instructions which would be affected by this note,
22174      don't do anything.  */
22175   if (var_loc_p
22176       && next_real == NULL_RTX
22177       && !NOTE_DURING_CALL_P (loc_note))
22178     return;
22179
22180   if (next_real == NULL_RTX)
22181     next_real = get_last_insn ();
22182
22183   /* If there were any real insns between note we processed last time
22184      and this note (or if it is the first note), clear
22185      last_{,postcall_}label so that they are not reused this time.  */
22186   if (last_var_location_insn == NULL_RTX
22187       || last_var_location_insn != next_real
22188       || last_in_cold_section_p != in_cold_section_p)
22189     {
22190       last_label = NULL;
22191       last_postcall_label = NULL;
22192     }
22193
22194   if (var_loc_p)
22195     {
22196       decl = NOTE_VAR_LOCATION_DECL (loc_note);
22197       newloc = add_var_loc_to_decl (decl, loc_note,
22198                                     NOTE_DURING_CALL_P (loc_note)
22199                                     ? last_postcall_label : last_label);
22200       if (newloc == NULL)
22201         return;
22202     }
22203   else
22204     {
22205       decl = NULL_TREE;
22206       newloc = NULL;
22207     }
22208
22209   /* If there were no real insns between note we processed last time
22210      and this note, use the label we emitted last time.  Otherwise
22211      create a new label and emit it.  */
22212   if (last_label == NULL)
22213     {
22214       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22215       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22216       loclabel_num++;
22217       last_label = ggc_strdup (loclabel);
22218     }
22219
22220   if (!var_loc_p)
22221     {
22222       struct call_arg_loc_node *ca_loc
22223         = ggc_alloc_cleared_call_arg_loc_node ();
22224       rtx prev = prev_real_insn (loc_note), x;
22225       ca_loc->call_arg_loc_note = loc_note;
22226       ca_loc->next = NULL;
22227       ca_loc->label = last_label;
22228       gcc_assert (prev
22229                   && (CALL_P (prev)
22230                       || (NONJUMP_INSN_P (prev)
22231                           && GET_CODE (PATTERN (prev)) == SEQUENCE
22232                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22233       if (!CALL_P (prev))
22234         prev = XVECEXP (PATTERN (prev), 0, 0);
22235       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22236       x = PATTERN (prev);
22237       if (GET_CODE (x) == PARALLEL)
22238         x = XVECEXP (x, 0, 0);
22239       if (GET_CODE (x) == SET)
22240         x = SET_SRC (x);
22241       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22242         {
22243           x = XEXP (XEXP (x, 0), 0);
22244           if (GET_CODE (x) == SYMBOL_REF
22245               && SYMBOL_REF_DECL (x)
22246               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22247             ca_loc->symbol_ref = x;
22248         }
22249       ca_loc->block = insn_scope (prev);
22250       if (call_arg_locations)
22251         call_arg_loc_last->next = ca_loc;
22252       else
22253         call_arg_locations = ca_loc;
22254       call_arg_loc_last = ca_loc;
22255     }
22256   else if (!NOTE_DURING_CALL_P (loc_note))
22257     newloc->label = last_label;
22258   else
22259     {
22260       if (!last_postcall_label)
22261         {
22262           sprintf (loclabel, "%s-1", last_label);
22263           last_postcall_label = ggc_strdup (loclabel);
22264         }
22265       newloc->label = last_postcall_label;
22266     }
22267
22268   last_var_location_insn = next_real;
22269   last_in_cold_section_p = in_cold_section_p;
22270 }
22271
22272 /* We need to reset the locations at the beginning of each
22273    function. We can't do this in the end_function hook, because the
22274    declarations that use the locations won't have been output when
22275    that hook is called.  Also compute have_multiple_function_sections here.  */
22276
22277 static void
22278 dwarf2out_begin_function (tree fun)
22279 {
22280   if (function_section (fun) != text_section)
22281     have_multiple_function_sections = true;
22282   else if (flag_reorder_blocks_and_partition && !cold_text_section)
22283     {
22284       gcc_assert (current_function_decl == fun);
22285       cold_text_section = unlikely_text_section ();
22286       switch_to_section (cold_text_section);
22287       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22288       switch_to_section (current_function_section ());
22289     }
22290
22291   dwarf2out_note_section_used ();
22292   call_site_count = 0;
22293   tail_call_site_count = 0;
22294 }
22295
22296 /* Output a label to mark the beginning of a source code line entry
22297    and record information relating to this source line, in
22298    'line_info_table' for later output of the .debug_line section.  */
22299
22300 static void
22301 dwarf2out_source_line (unsigned int line, const char *filename,
22302                        int discriminator, bool is_stmt)
22303 {
22304   static bool last_is_stmt = true;
22305
22306   if (debug_info_level >= DINFO_LEVEL_NORMAL
22307       && line != 0)
22308     {
22309       int file_num = maybe_emit_file (lookup_filename (filename));
22310
22311       switch_to_section (current_function_section ());
22312
22313       /* If requested, emit something human-readable.  */
22314       if (flag_debug_asm)
22315         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
22316                  filename, line);
22317
22318       if (DWARF2_ASM_LINE_DEBUG_INFO)
22319         {
22320           /* Emit the .loc directive understood by GNU as.  */
22321           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22322           if (is_stmt != last_is_stmt)
22323             {
22324               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22325               last_is_stmt = is_stmt;
22326             }
22327           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22328             fprintf (asm_out_file, " discriminator %d", discriminator);
22329           fputc ('\n', asm_out_file);
22330
22331           /* Indicate that line number info exists.  */
22332           line_info_table_in_use++;
22333         }
22334       else if (function_section (current_function_decl) != text_section)
22335         {
22336           dw_separate_line_info_ref line_info;
22337           targetm.asm_out.internal_label (asm_out_file,
22338                                           SEPARATE_LINE_CODE_LABEL,
22339                                           separate_line_info_table_in_use);
22340
22341           /* Expand the line info table if necessary.  */
22342           if (separate_line_info_table_in_use
22343               == separate_line_info_table_allocated)
22344             {
22345               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22346               separate_line_info_table
22347                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
22348                                  separate_line_info_table,
22349                                  separate_line_info_table_allocated);
22350               memset (separate_line_info_table
22351                        + separate_line_info_table_in_use,
22352                       0,
22353                       (LINE_INFO_TABLE_INCREMENT
22354                        * sizeof (dw_separate_line_info_entry)));
22355             }
22356
22357           /* Add the new entry at the end of the line_info_table.  */
22358           line_info
22359             = &separate_line_info_table[separate_line_info_table_in_use++];
22360           line_info->dw_file_num = file_num;
22361           line_info->dw_line_num = line;
22362           line_info->function = current_function_funcdef_no;
22363         }
22364       else
22365         {
22366           dw_line_info_ref line_info;
22367
22368           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
22369                                      line_info_table_in_use);
22370
22371           /* Expand the line info table if necessary.  */
22372           if (line_info_table_in_use == line_info_table_allocated)
22373             {
22374               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
22375               line_info_table
22376                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
22377                                  line_info_table_allocated);
22378               memset (line_info_table + line_info_table_in_use, 0,
22379                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
22380             }
22381
22382           /* Add the new entry at the end of the line_info_table.  */
22383           line_info = &line_info_table[line_info_table_in_use++];
22384           line_info->dw_file_num = file_num;
22385           line_info->dw_line_num = line;
22386         }
22387     }
22388 }
22389
22390 /* Record the beginning of a new source file.  */
22391
22392 static void
22393 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22394 {
22395   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22396     {
22397       /* Record the beginning of the file for break_out_includes.  */
22398       dw_die_ref bincl_die;
22399
22400       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22401       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22402     }
22403
22404   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22405     {
22406       macinfo_entry e;
22407       e.code = DW_MACINFO_start_file;
22408       e.lineno = lineno;
22409       e.info = xstrdup (filename);
22410       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22411     }
22412 }
22413
22414 /* Record the end of a source file.  */
22415
22416 static void
22417 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22418 {
22419   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22420     /* Record the end of the file for break_out_includes.  */
22421     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22422
22423   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22424     {
22425       macinfo_entry e;
22426       e.code = DW_MACINFO_end_file;
22427       e.lineno = lineno;
22428       e.info = NULL;
22429       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22430     }
22431 }
22432
22433 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22434    the tail part of the directive line, i.e. the part which is past the
22435    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22436
22437 static void
22438 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22439                   const char *buffer ATTRIBUTE_UNUSED)
22440 {
22441   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22442     {
22443       macinfo_entry e;
22444       e.code = DW_MACINFO_define;
22445       e.lineno = lineno;
22446       e.info = xstrdup (buffer);;
22447       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22448     }
22449 }
22450
22451 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22452    the tail part of the directive line, i.e. the part which is past the
22453    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22454
22455 static void
22456 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22457                  const char *buffer ATTRIBUTE_UNUSED)
22458 {
22459   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22460     {
22461       macinfo_entry e;
22462       e.code = DW_MACINFO_undef;
22463       e.lineno = lineno;
22464       e.info = xstrdup (buffer);;
22465       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22466     }
22467 }
22468
22469 static void
22470 output_macinfo (void)
22471 {
22472   unsigned i;
22473   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22474   macinfo_entry *ref;
22475
22476   if (! length)
22477     return;
22478
22479   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22480     {
22481       switch (ref->code)
22482         {
22483           case DW_MACINFO_start_file:
22484             {
22485               int file_num = maybe_emit_file (lookup_filename (ref->info));
22486               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22487               dw2_asm_output_data_uleb128 
22488                         (ref->lineno, "Included from line number %lu", 
22489                                                 (unsigned long)ref->lineno);
22490               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22491             }
22492             break;
22493           case DW_MACINFO_end_file:
22494             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22495             break;
22496           case DW_MACINFO_define:
22497             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22498             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22499                                                 (unsigned long)ref->lineno);
22500             dw2_asm_output_nstring (ref->info, -1, "The macro");
22501             break;
22502           case DW_MACINFO_undef:
22503             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22504             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22505                                                 (unsigned long)ref->lineno);
22506             dw2_asm_output_nstring (ref->info, -1, "The macro");
22507             break;
22508           default:
22509            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22510              ASM_COMMENT_START, (unsigned long)ref->code);
22511           break;
22512         }
22513     }
22514 }
22515
22516 /* Set up for Dwarf output at the start of compilation.  */
22517
22518 static void
22519 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22520 {
22521   /* Allocate the file_table.  */
22522   file_table = htab_create_ggc (50, file_table_hash,
22523                                 file_table_eq, NULL);
22524
22525   /* Allocate the decl_die_table.  */
22526   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22527                                     decl_die_table_eq, NULL);
22528
22529   /* Allocate the decl_loc_table.  */
22530   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22531                                     decl_loc_table_eq, NULL);
22532
22533   /* Allocate the initial hunk of the decl_scope_table.  */
22534   decl_scope_table = VEC_alloc (tree, gc, 256);
22535
22536   /* Allocate the initial hunk of the abbrev_die_table.  */
22537   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22538     (ABBREV_DIE_TABLE_INCREMENT);
22539   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22540   /* Zero-th entry is allocated, but unused.  */
22541   abbrev_die_table_in_use = 1;
22542
22543   /* Allocate the initial hunk of the line_info_table.  */
22544   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
22545     (LINE_INFO_TABLE_INCREMENT);
22546   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
22547
22548   /* Zero-th entry is allocated, but unused.  */
22549   line_info_table_in_use = 1;
22550
22551   /* Allocate the pubtypes and pubnames vectors.  */
22552   pubname_table = VEC_alloc (pubname_entry, gc, 32);
22553   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
22554
22555   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
22556   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
22557                                       vcall_insn_table_eq, NULL);
22558
22559   incomplete_types = VEC_alloc (tree, gc, 64);
22560
22561   used_rtx_array = VEC_alloc (rtx, gc, 32);
22562
22563   debug_info_section = get_section (DEBUG_INFO_SECTION,
22564                                     SECTION_DEBUG, NULL);
22565   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
22566                                       SECTION_DEBUG, NULL);
22567   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
22568                                        SECTION_DEBUG, NULL);
22569   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
22570                                        SECTION_DEBUG, NULL);
22571   debug_line_section = get_section (DEBUG_LINE_SECTION,
22572                                     SECTION_DEBUG, NULL);
22573   debug_loc_section = get_section (DEBUG_LOC_SECTION,
22574                                    SECTION_DEBUG, NULL);
22575   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
22576                                         SECTION_DEBUG, NULL);
22577   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
22578                                         SECTION_DEBUG, NULL);
22579   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
22580                                      SECTION_DEBUG, NULL);
22581   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
22582                                      SECTION_DEBUG, NULL);
22583   debug_str_section = get_section (DEBUG_STR_SECTION,
22584                                    DEBUG_STR_SECTION_FLAGS, NULL);
22585   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
22586                                       SECTION_DEBUG, NULL);
22587   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
22588                                      SECTION_DEBUG, NULL);
22589
22590   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
22591   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
22592                                DEBUG_ABBREV_SECTION_LABEL, 0);
22593   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
22594   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
22595                                COLD_TEXT_SECTION_LABEL, 0);
22596   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
22597
22598   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
22599                                DEBUG_INFO_SECTION_LABEL, 0);
22600   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
22601                                DEBUG_LINE_SECTION_LABEL, 0);
22602   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
22603                                DEBUG_RANGES_SECTION_LABEL, 0);
22604   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
22605                                DEBUG_MACINFO_SECTION_LABEL, 0);
22606
22607   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22608     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
22609
22610   switch_to_section (text_section);
22611   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
22612 }
22613
22614 /* Called before cgraph_optimize starts outputtting functions, variables
22615    and toplevel asms into assembly.  */
22616
22617 static void
22618 dwarf2out_assembly_start (void)
22619 {
22620   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
22621       && dwarf2out_do_cfi_asm ()
22622       && (!(flag_unwind_tables || flag_exceptions)
22623           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
22624     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
22625 }
22626
22627 /* A helper function for dwarf2out_finish called through
22628    htab_traverse.  Emit one queued .debug_str string.  */
22629
22630 static int
22631 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22632 {
22633   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22634
22635   if (node->label && node->refcount)
22636     {
22637       switch_to_section (debug_str_section);
22638       ASM_OUTPUT_LABEL (asm_out_file, node->label);
22639       assemble_string (node->str, strlen (node->str) + 1);
22640     }
22641
22642   return 1;
22643 }
22644
22645 #if ENABLE_ASSERT_CHECKING
22646 /* Verify that all marks are clear.  */
22647
22648 static void
22649 verify_marks_clear (dw_die_ref die)
22650 {
22651   dw_die_ref c;
22652
22653   gcc_assert (! die->die_mark);
22654   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
22655 }
22656 #endif /* ENABLE_ASSERT_CHECKING */
22657
22658 /* Clear the marks for a die and its children.
22659    Be cool if the mark isn't set.  */
22660
22661 static void
22662 prune_unmark_dies (dw_die_ref die)
22663 {
22664   dw_die_ref c;
22665
22666   if (die->die_mark)
22667     die->die_mark = 0;
22668   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
22669 }
22670
22671 /* Given DIE that we're marking as used, find any other dies
22672    it references as attributes and mark them as used.  */
22673
22674 static void
22675 prune_unused_types_walk_attribs (dw_die_ref die)
22676 {
22677   dw_attr_ref a;
22678   unsigned ix;
22679
22680   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22681     {
22682       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
22683         {
22684           /* A reference to another DIE.
22685              Make sure that it will get emitted.
22686              If it was broken out into a comdat group, don't follow it.  */
22687           if (dwarf_version < 4
22688               || a->dw_attr == DW_AT_specification
22689               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
22690             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
22691         }
22692       /* Set the string's refcount to 0 so that prune_unused_types_mark
22693          accounts properly for it.  */
22694       if (AT_class (a) == dw_val_class_str)
22695         a->dw_attr_val.v.val_str->refcount = 0;
22696     }
22697 }
22698
22699 /* Mark the generic parameters and arguments children DIEs of DIE.  */
22700
22701 static void
22702 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
22703 {
22704   dw_die_ref c;
22705
22706   if (die == NULL || die->die_child == NULL)
22707     return;
22708   c = die->die_child;
22709   do
22710     {
22711       switch (c->die_tag)
22712         {
22713         case DW_TAG_template_type_param:
22714         case DW_TAG_template_value_param:
22715         case DW_TAG_GNU_template_template_param:
22716         case DW_TAG_GNU_template_parameter_pack:
22717           prune_unused_types_mark (c, 1);
22718           break;
22719         default:
22720           break;
22721         }
22722       c = c->die_sib;
22723     } while (c && c != die->die_child);
22724 }
22725
22726 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22727    to DIE's children.  */
22728
22729 static void
22730 prune_unused_types_mark (dw_die_ref die, int dokids)
22731 {
22732   dw_die_ref c;
22733
22734   if (die->die_mark == 0)
22735     {
22736       /* We haven't done this node yet.  Mark it as used.  */
22737       die->die_mark = 1;
22738       /* If this is the DIE of a generic type instantiation,
22739          mark the children DIEs that describe its generic parms and
22740          args.  */
22741       prune_unused_types_mark_generic_parms_dies (die);
22742
22743       /* We also have to mark its parents as used.
22744          (But we don't want to mark our parents' kids due to this.)  */
22745       if (die->die_parent)
22746         prune_unused_types_mark (die->die_parent, 0);
22747
22748       /* Mark any referenced nodes.  */
22749       prune_unused_types_walk_attribs (die);
22750
22751       /* If this node is a specification,
22752          also mark the definition, if it exists.  */
22753       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22754         prune_unused_types_mark (die->die_definition, 1);
22755     }
22756
22757   if (dokids && die->die_mark != 2)
22758     {
22759       /* We need to walk the children, but haven't done so yet.
22760          Remember that we've walked the kids.  */
22761       die->die_mark = 2;
22762
22763       /* If this is an array type, we need to make sure our
22764          kids get marked, even if they're types.  If we're
22765          breaking out types into comdat sections, do this
22766          for all type definitions.  */
22767       if (die->die_tag == DW_TAG_array_type
22768           || (dwarf_version >= 4
22769               && is_type_die (die) && ! is_declaration_die (die)))
22770         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22771       else
22772         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22773     }
22774 }
22775
22776 /* For local classes, look if any static member functions were emitted
22777    and if so, mark them.  */
22778
22779 static void
22780 prune_unused_types_walk_local_classes (dw_die_ref die)
22781 {
22782   dw_die_ref c;
22783
22784   if (die->die_mark == 2)
22785     return;
22786
22787   switch (die->die_tag)
22788     {
22789     case DW_TAG_structure_type:
22790     case DW_TAG_union_type:
22791     case DW_TAG_class_type:
22792       break;
22793
22794     case DW_TAG_subprogram:
22795       if (!get_AT_flag (die, DW_AT_declaration)
22796           || die->die_definition != NULL)
22797         prune_unused_types_mark (die, 1);
22798       return;
22799
22800     default:
22801       return;
22802     }
22803
22804   /* Mark children.  */
22805   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22806 }
22807
22808 /* Walk the tree DIE and mark types that we actually use.  */
22809
22810 static void
22811 prune_unused_types_walk (dw_die_ref die)
22812 {
22813   dw_die_ref c;
22814
22815   /* Don't do anything if this node is already marked and
22816      children have been marked as well.  */
22817   if (die->die_mark == 2)
22818     return;
22819
22820   switch (die->die_tag)
22821     {
22822     case DW_TAG_structure_type:
22823     case DW_TAG_union_type:
22824     case DW_TAG_class_type:
22825       if (die->die_perennial_p)
22826         break;
22827
22828       for (c = die->die_parent; c; c = c->die_parent)
22829         if (c->die_tag == DW_TAG_subprogram)
22830           break;
22831
22832       /* Finding used static member functions inside of classes
22833          is needed just for local classes, because for other classes
22834          static member function DIEs with DW_AT_specification
22835          are emitted outside of the DW_TAG_*_type.  If we ever change
22836          it, we'd need to call this even for non-local classes.  */
22837       if (c)
22838         prune_unused_types_walk_local_classes (die);
22839
22840       /* It's a type node --- don't mark it.  */
22841       return;
22842
22843     case DW_TAG_const_type:
22844     case DW_TAG_packed_type:
22845     case DW_TAG_pointer_type:
22846     case DW_TAG_reference_type:
22847     case DW_TAG_rvalue_reference_type:
22848     case DW_TAG_volatile_type:
22849     case DW_TAG_typedef:
22850     case DW_TAG_array_type:
22851     case DW_TAG_interface_type:
22852     case DW_TAG_friend:
22853     case DW_TAG_variant_part:
22854     case DW_TAG_enumeration_type:
22855     case DW_TAG_subroutine_type:
22856     case DW_TAG_string_type:
22857     case DW_TAG_set_type:
22858     case DW_TAG_subrange_type:
22859     case DW_TAG_ptr_to_member_type:
22860     case DW_TAG_file_type:
22861       if (die->die_perennial_p)
22862         break;
22863
22864       /* It's a type node --- don't mark it.  */
22865       return;
22866
22867     default:
22868       /* Mark everything else.  */
22869       break;
22870   }
22871
22872   if (die->die_mark == 0)
22873     {
22874       die->die_mark = 1;
22875
22876       /* Now, mark any dies referenced from here.  */
22877       prune_unused_types_walk_attribs (die);
22878     }
22879
22880   die->die_mark = 2;
22881
22882   /* Mark children.  */
22883   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22884 }
22885
22886 /* Increment the string counts on strings referred to from DIE's
22887    attributes.  */
22888
22889 static void
22890 prune_unused_types_update_strings (dw_die_ref die)
22891 {
22892   dw_attr_ref a;
22893   unsigned ix;
22894
22895   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22896     if (AT_class (a) == dw_val_class_str)
22897       {
22898         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22899         s->refcount++;
22900         /* Avoid unnecessarily putting strings that are used less than
22901            twice in the hash table.  */
22902         if (s->refcount
22903             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22904           {
22905             void ** slot;
22906             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22907                                              htab_hash_string (s->str),
22908                                              INSERT);
22909             gcc_assert (*slot == NULL);
22910             *slot = s;
22911           }
22912       }
22913 }
22914
22915 /* Remove from the tree DIE any dies that aren't marked.  */
22916
22917 static void
22918 prune_unused_types_prune (dw_die_ref die)
22919 {
22920   dw_die_ref c;
22921
22922   gcc_assert (die->die_mark);
22923   prune_unused_types_update_strings (die);
22924
22925   if (! die->die_child)
22926     return;
22927
22928   c = die->die_child;
22929   do {
22930     dw_die_ref prev = c;
22931     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22932       if (c == die->die_child)
22933         {
22934           /* No marked children between 'prev' and the end of the list.  */
22935           if (prev == c)
22936             /* No marked children at all.  */
22937             die->die_child = NULL;
22938           else
22939             {
22940               prev->die_sib = c->die_sib;
22941               die->die_child = prev;
22942             }
22943           return;
22944         }
22945
22946     if (c != prev->die_sib)
22947       prev->die_sib = c;
22948     prune_unused_types_prune (c);
22949   } while (c != die->die_child);
22950 }
22951
22952 /* A helper function for dwarf2out_finish called through
22953    htab_traverse.  Clear .debug_str strings that we haven't already
22954    decided to emit.  */
22955
22956 static int
22957 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
22958 {
22959   struct indirect_string_node *node = (struct indirect_string_node *) *h;
22960
22961   if (!node->label || !node->refcount)
22962     htab_clear_slot (debug_str_hash, h);
22963
22964   return 1;
22965 }
22966
22967 /* Remove dies representing declarations that we never use.  */
22968
22969 static void
22970 prune_unused_types (void)
22971 {
22972   unsigned int i;
22973   limbo_die_node *node;
22974   comdat_type_node *ctnode;
22975   pubname_ref pub;
22976   dcall_entry *dcall;
22977
22978 #if ENABLE_ASSERT_CHECKING
22979   /* All the marks should already be clear.  */
22980   verify_marks_clear (comp_unit_die ());
22981   for (node = limbo_die_list; node; node = node->next)
22982     verify_marks_clear (node->die);
22983   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22984     verify_marks_clear (ctnode->root_die);
22985 #endif /* ENABLE_ASSERT_CHECKING */
22986
22987   /* Mark types that are used in global variables.  */
22988   premark_types_used_by_global_vars ();
22989
22990   /* Set the mark on nodes that are actually used.  */
22991   prune_unused_types_walk (comp_unit_die ());
22992   for (node = limbo_die_list; node; node = node->next)
22993     prune_unused_types_walk (node->die);
22994   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22995     {
22996       prune_unused_types_walk (ctnode->root_die);
22997       prune_unused_types_mark (ctnode->type_die, 1);
22998     }
22999
23000   /* Also set the mark on nodes referenced from the
23001      pubname_table or arange_table.  */
23002   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
23003     prune_unused_types_mark (pub->die, 1);
23004   for (i = 0; i < arange_table_in_use; i++)
23005     prune_unused_types_mark (arange_table[i], 1);
23006
23007   /* Mark nodes referenced from the direct call table.  */
23008   FOR_EACH_VEC_ELT (dcall_entry, dcall_table, i, dcall)
23009     prune_unused_types_mark (dcall->targ_die, 1);
23010
23011   /* Get rid of nodes that aren't marked; and update the string counts.  */
23012   if (debug_str_hash && debug_str_hash_forced)
23013     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
23014   else if (debug_str_hash)
23015     htab_empty (debug_str_hash);
23016   prune_unused_types_prune (comp_unit_die ());
23017   for (node = limbo_die_list; node; node = node->next)
23018     prune_unused_types_prune (node->die);
23019   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23020     prune_unused_types_prune (ctnode->root_die);
23021
23022   /* Leave the marks clear.  */
23023   prune_unmark_dies (comp_unit_die ());
23024   for (node = limbo_die_list; node; node = node->next)
23025     prune_unmark_dies (node->die);
23026   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23027     prune_unmark_dies (ctnode->root_die);
23028 }
23029
23030 /* Set the parameter to true if there are any relative pathnames in
23031    the file table.  */
23032 static int
23033 file_table_relative_p (void ** slot, void *param)
23034 {
23035   bool *p = (bool *) param;
23036   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
23037   if (!IS_ABSOLUTE_PATH (d->filename))
23038     {
23039       *p = true;
23040       return 0;
23041     }
23042   return 1;
23043 }
23044
23045 /* Routines to manipulate hash table of comdat type units.  */
23046
23047 static hashval_t
23048 htab_ct_hash (const void *of)
23049 {
23050   hashval_t h;
23051   const comdat_type_node *const type_node = (const comdat_type_node *) of;
23052
23053   memcpy (&h, type_node->signature, sizeof (h));
23054   return h;
23055 }
23056
23057 static int
23058 htab_ct_eq (const void *of1, const void *of2)
23059 {
23060   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
23061   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
23062
23063   return (! memcmp (type_node_1->signature, type_node_2->signature,
23064                     DWARF_TYPE_SIGNATURE_SIZE));
23065 }
23066
23067 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23068    to the location it would have been added, should we know its
23069    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23070    probably improve compactness of debug info, removing equivalent
23071    abbrevs, and hide any differences caused by deferring the
23072    computation of the assembler name, triggered by e.g. PCH.  */
23073
23074 static inline void
23075 move_linkage_attr (dw_die_ref die)
23076 {
23077   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
23078   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
23079
23080   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23081               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23082
23083   while (--ix > 0)
23084     {
23085       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
23086
23087       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23088         break;
23089     }
23090
23091   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
23092     {
23093       VEC_pop (dw_attr_node, die->die_attr);
23094       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
23095     }
23096 }
23097
23098 /* Helper function for resolve_addr, attempt to resolve
23099    one CONST_STRING, return non-zero if not successful.  Similarly verify that
23100    SYMBOL_REFs refer to variables emitted in the current CU.  */
23101
23102 static int
23103 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
23104 {
23105   rtx rtl = *addr;
23106
23107   if (GET_CODE (rtl) == CONST_STRING)
23108     {
23109       size_t len = strlen (XSTR (rtl, 0)) + 1;
23110       tree t = build_string (len, XSTR (rtl, 0));
23111       tree tlen = build_int_cst (NULL_TREE, len - 1);
23112       TREE_TYPE (t)
23113         = build_array_type (char_type_node, build_index_type (tlen));
23114       rtl = lookup_constant_def (t);
23115       if (!rtl || !MEM_P (rtl))
23116         return 1;
23117       rtl = XEXP (rtl, 0);
23118       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
23119       *addr = rtl;
23120       return 0;
23121     }
23122
23123   if (GET_CODE (rtl) == SYMBOL_REF
23124       && SYMBOL_REF_DECL (rtl))
23125     {
23126       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23127         {
23128           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23129             return 1;
23130         }
23131       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23132         return 1;
23133     }
23134
23135   if (GET_CODE (rtl) == CONST
23136       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23137     return 1;
23138
23139   return 0;
23140 }
23141
23142 /* Helper function for resolve_addr, handle one location
23143    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23144    the location list couldn't be resolved.  */
23145
23146 static bool
23147 resolve_addr_in_expr (dw_loc_descr_ref loc)
23148 {
23149   for (; loc; loc = loc->dw_loc_next)
23150     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
23151          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23152         || (loc->dw_loc_opc == DW_OP_implicit_value
23153             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23154             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
23155       return false;
23156     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
23157              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23158       {
23159         dw_die_ref ref
23160           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23161         if (ref == NULL)
23162           return false;
23163         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23164         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23165         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23166       }
23167   return true;
23168 }
23169
23170 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23171    an address in .rodata section if the string literal is emitted there,
23172    or remove the containing location list or replace DW_AT_const_value
23173    with DW_AT_location and empty location expression, if it isn't found
23174    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23175    to something that has been emitted in the current CU.  */
23176
23177 static void
23178 resolve_addr (dw_die_ref die)
23179 {
23180   dw_die_ref c;
23181   dw_attr_ref a;
23182   dw_loc_list_ref *curr;
23183   unsigned ix;
23184
23185   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23186     switch (AT_class (a))
23187       {
23188       case dw_val_class_loc_list:
23189         curr = AT_loc_list_ptr (a);
23190         while (*curr)
23191           {
23192             if (!resolve_addr_in_expr ((*curr)->expr))
23193               {
23194                 dw_loc_list_ref next = (*curr)->dw_loc_next;
23195                 if (next && (*curr)->ll_symbol)
23196                   {
23197                     gcc_assert (!next->ll_symbol);
23198                     next->ll_symbol = (*curr)->ll_symbol;
23199                   }
23200                 *curr = next;
23201               }
23202             else
23203               curr = &(*curr)->dw_loc_next;
23204           }
23205         if (!AT_loc_list (a))
23206           {
23207             remove_AT (die, a->dw_attr);
23208             ix--;
23209           }
23210         break;
23211       case dw_val_class_loc:
23212         if (!resolve_addr_in_expr (AT_loc (a)))
23213           {
23214             remove_AT (die, a->dw_attr);
23215             ix--;
23216           }
23217         break;
23218       case dw_val_class_addr:
23219         if (a->dw_attr == DW_AT_const_value
23220             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23221           {
23222             remove_AT (die, a->dw_attr);
23223             ix--;
23224           }
23225         if (die->die_tag == DW_TAG_GNU_call_site
23226             && a->dw_attr == DW_AT_abstract_origin)
23227           {
23228             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23229             dw_die_ref tdie = lookup_decl_die (tdecl);
23230             if (tdie == NULL && DECL_EXTERNAL (tdecl))
23231               {
23232                 force_decl_die (tdecl);
23233                 tdie = lookup_decl_die (tdecl);
23234               }
23235             if (tdie)
23236               {
23237                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23238                 a->dw_attr_val.v.val_die_ref.die = tdie;
23239                 a->dw_attr_val.v.val_die_ref.external = 0;
23240               }
23241             else
23242               {
23243                 remove_AT (die, a->dw_attr);
23244                 ix--;
23245               }
23246           }
23247         break;
23248       default:
23249         break;
23250       }
23251
23252   FOR_EACH_CHILD (die, c, resolve_addr (c));
23253 }
23254 \f
23255 /* Helper routines for optimize_location_lists.
23256    This pass tries to share identical local lists in .debug_loc
23257    section.  */
23258
23259 /* Iteratively hash operands of LOC opcode.  */
23260
23261 static inline hashval_t
23262 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23263 {
23264   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23265   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23266
23267   switch (loc->dw_loc_opc)
23268     {
23269     case DW_OP_const4u:
23270     case DW_OP_const8u:
23271       if (loc->dtprel)
23272         goto hash_addr;
23273       /* FALLTHRU */
23274     case DW_OP_const1u:
23275     case DW_OP_const1s:
23276     case DW_OP_const2u:
23277     case DW_OP_const2s:
23278     case DW_OP_const4s:
23279     case DW_OP_const8s:
23280     case DW_OP_constu:
23281     case DW_OP_consts:
23282     case DW_OP_pick:
23283     case DW_OP_plus_uconst:
23284     case DW_OP_breg0:
23285     case DW_OP_breg1:
23286     case DW_OP_breg2:
23287     case DW_OP_breg3:
23288     case DW_OP_breg4:
23289     case DW_OP_breg5:
23290     case DW_OP_breg6:
23291     case DW_OP_breg7:
23292     case DW_OP_breg8:
23293     case DW_OP_breg9:
23294     case DW_OP_breg10:
23295     case DW_OP_breg11:
23296     case DW_OP_breg12:
23297     case DW_OP_breg13:
23298     case DW_OP_breg14:
23299     case DW_OP_breg15:
23300     case DW_OP_breg16:
23301     case DW_OP_breg17:
23302     case DW_OP_breg18:
23303     case DW_OP_breg19:
23304     case DW_OP_breg20:
23305     case DW_OP_breg21:
23306     case DW_OP_breg22:
23307     case DW_OP_breg23:
23308     case DW_OP_breg24:
23309     case DW_OP_breg25:
23310     case DW_OP_breg26:
23311     case DW_OP_breg27:
23312     case DW_OP_breg28:
23313     case DW_OP_breg29:
23314     case DW_OP_breg30:
23315     case DW_OP_breg31:
23316     case DW_OP_regx:
23317     case DW_OP_fbreg:
23318     case DW_OP_piece:
23319     case DW_OP_deref_size:
23320     case DW_OP_xderef_size:
23321       hash = iterative_hash_object (val1->v.val_int, hash);
23322       break;
23323     case DW_OP_skip:
23324     case DW_OP_bra:
23325       {
23326         int offset;
23327
23328         gcc_assert (val1->val_class == dw_val_class_loc);
23329         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23330         hash = iterative_hash_object (offset, hash);
23331       }
23332       break;
23333     case DW_OP_implicit_value:
23334       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23335       switch (val2->val_class)
23336         {
23337         case dw_val_class_const:
23338           hash = iterative_hash_object (val2->v.val_int, hash);
23339           break;
23340         case dw_val_class_vec:
23341           {
23342             unsigned int elt_size = val2->v.val_vec.elt_size;
23343             unsigned int len = val2->v.val_vec.length;
23344
23345             hash = iterative_hash_object (elt_size, hash);
23346             hash = iterative_hash_object (len, hash);
23347             hash = iterative_hash (val2->v.val_vec.array,
23348                                    len * elt_size, hash);
23349           }
23350           break;
23351         case dw_val_class_const_double:
23352           hash = iterative_hash_object (val2->v.val_double.low, hash);
23353           hash = iterative_hash_object (val2->v.val_double.high, hash);
23354           break;
23355         case dw_val_class_addr:
23356           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23357           break;
23358         default:
23359           gcc_unreachable ();
23360         }
23361       break;
23362     case DW_OP_bregx:
23363     case DW_OP_bit_piece:
23364       hash = iterative_hash_object (val1->v.val_int, hash);
23365       hash = iterative_hash_object (val2->v.val_int, hash);
23366       break;
23367     case DW_OP_addr:
23368     hash_addr:
23369       if (loc->dtprel)
23370         {
23371           unsigned char dtprel = 0xd1;
23372           hash = iterative_hash_object (dtprel, hash);
23373         }
23374       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23375       break;
23376     case DW_OP_GNU_implicit_pointer:
23377       hash = iterative_hash_object (val2->v.val_int, hash);
23378       break;
23379
23380     default:
23381       /* Other codes have no operands.  */
23382       break;
23383     }
23384   return hash;
23385 }
23386
23387 /* Iteratively hash the whole DWARF location expression LOC.  */
23388
23389 static inline hashval_t
23390 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
23391 {
23392   dw_loc_descr_ref l;
23393   bool sizes_computed = false;
23394   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
23395   size_of_locs (loc);
23396
23397   for (l = loc; l != NULL; l = l->dw_loc_next)
23398     {
23399       enum dwarf_location_atom opc = l->dw_loc_opc;
23400       hash = iterative_hash_object (opc, hash);
23401       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
23402         {
23403           size_of_locs (loc);
23404           sizes_computed = true;
23405         }
23406       hash = hash_loc_operands (l, hash);
23407     }
23408   return hash;
23409 }
23410
23411 /* Compute hash of the whole location list LIST_HEAD.  */
23412
23413 static inline void
23414 hash_loc_list (dw_loc_list_ref list_head)
23415 {
23416   dw_loc_list_ref curr = list_head;
23417   hashval_t hash = 0;
23418
23419   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23420     {
23421       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23422       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23423       if (curr->section)
23424         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23425                                hash);
23426       hash = hash_locs (curr->expr, hash);
23427     }
23428   list_head->hash = hash;
23429 }
23430
23431 /* Return true if X and Y opcodes have the same operands.  */
23432
23433 static inline bool
23434 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23435 {
23436   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23437   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23438   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23439   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23440
23441   switch (x->dw_loc_opc)
23442     {
23443     case DW_OP_const4u:
23444     case DW_OP_const8u:
23445       if (x->dtprel)
23446         goto hash_addr;
23447       /* FALLTHRU */
23448     case DW_OP_const1u:
23449     case DW_OP_const1s:
23450     case DW_OP_const2u:
23451     case DW_OP_const2s:
23452     case DW_OP_const4s:
23453     case DW_OP_const8s:
23454     case DW_OP_constu:
23455     case DW_OP_consts:
23456     case DW_OP_pick:
23457     case DW_OP_plus_uconst:
23458     case DW_OP_breg0:
23459     case DW_OP_breg1:
23460     case DW_OP_breg2:
23461     case DW_OP_breg3:
23462     case DW_OP_breg4:
23463     case DW_OP_breg5:
23464     case DW_OP_breg6:
23465     case DW_OP_breg7:
23466     case DW_OP_breg8:
23467     case DW_OP_breg9:
23468     case DW_OP_breg10:
23469     case DW_OP_breg11:
23470     case DW_OP_breg12:
23471     case DW_OP_breg13:
23472     case DW_OP_breg14:
23473     case DW_OP_breg15:
23474     case DW_OP_breg16:
23475     case DW_OP_breg17:
23476     case DW_OP_breg18:
23477     case DW_OP_breg19:
23478     case DW_OP_breg20:
23479     case DW_OP_breg21:
23480     case DW_OP_breg22:
23481     case DW_OP_breg23:
23482     case DW_OP_breg24:
23483     case DW_OP_breg25:
23484     case DW_OP_breg26:
23485     case DW_OP_breg27:
23486     case DW_OP_breg28:
23487     case DW_OP_breg29:
23488     case DW_OP_breg30:
23489     case DW_OP_breg31:
23490     case DW_OP_regx:
23491     case DW_OP_fbreg:
23492     case DW_OP_piece:
23493     case DW_OP_deref_size:
23494     case DW_OP_xderef_size:
23495       return valx1->v.val_int == valy1->v.val_int;
23496     case DW_OP_skip:
23497     case DW_OP_bra:
23498       gcc_assert (valx1->val_class == dw_val_class_loc
23499                   && valy1->val_class == dw_val_class_loc
23500                   && x->dw_loc_addr == y->dw_loc_addr);
23501       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23502     case DW_OP_implicit_value:
23503       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23504           || valx2->val_class != valy2->val_class)
23505         return false;
23506       switch (valx2->val_class)
23507         {
23508         case dw_val_class_const:
23509           return valx2->v.val_int == valy2->v.val_int;
23510         case dw_val_class_vec:
23511           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23512                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23513                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23514                             valx2->v.val_vec.elt_size
23515                             * valx2->v.val_vec.length) == 0;
23516         case dw_val_class_const_double:
23517           return valx2->v.val_double.low == valy2->v.val_double.low
23518                  && valx2->v.val_double.high == valy2->v.val_double.high;
23519         case dw_val_class_addr:
23520           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23521         default:
23522           gcc_unreachable ();
23523         }
23524     case DW_OP_bregx:
23525     case DW_OP_bit_piece:
23526       return valx1->v.val_int == valy1->v.val_int
23527              && valx2->v.val_int == valy2->v.val_int;
23528     case DW_OP_addr:
23529     hash_addr:
23530       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23531     case DW_OP_GNU_implicit_pointer:
23532       return valx1->val_class == dw_val_class_die_ref
23533              && valx1->val_class == valy1->val_class
23534              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23535              && valx2->v.val_int == valy2->v.val_int;
23536     default:
23537       /* Other codes have no operands.  */
23538       return true;
23539     }
23540 }
23541
23542 /* Return true if DWARF location expressions X and Y are the same.  */
23543
23544 static inline bool
23545 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23546 {
23547   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23548     if (x->dw_loc_opc != y->dw_loc_opc
23549         || x->dtprel != y->dtprel
23550         || !compare_loc_operands (x, y))
23551       break;
23552   return x == NULL && y == NULL;
23553 }
23554
23555 /* Return precomputed hash of location list X.  */
23556
23557 static hashval_t
23558 loc_list_hash (const void *x)
23559 {
23560   return ((const struct dw_loc_list_struct *) x)->hash;
23561 }
23562
23563 /* Return 1 if location lists X and Y are the same.  */
23564
23565 static int
23566 loc_list_eq (const void *x, const void *y)
23567 {
23568   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23569   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23570   if (a == b)
23571     return 1;
23572   if (a->hash != b->hash)
23573     return 0;
23574   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23575     if (strcmp (a->begin, b->begin) != 0
23576         || strcmp (a->end, b->end) != 0
23577         || (a->section == NULL) != (b->section == NULL)
23578         || (a->section && strcmp (a->section, b->section) != 0)
23579         || !compare_locs (a->expr, b->expr))
23580       break;
23581   return a == NULL && b == NULL;
23582 }
23583
23584 /* Recursively optimize location lists referenced from DIE
23585    children and share them whenever possible.  */
23586
23587 static void
23588 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23589 {
23590   dw_die_ref c;
23591   dw_attr_ref a;
23592   unsigned ix;
23593   void **slot;
23594
23595   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23596     if (AT_class (a) == dw_val_class_loc_list)
23597       {
23598         dw_loc_list_ref list = AT_loc_list (a);
23599         /* TODO: perform some optimizations here, before hashing
23600            it and storing into the hash table.  */
23601         hash_loc_list (list);
23602         slot = htab_find_slot_with_hash (htab, list, list->hash,
23603                                          INSERT);
23604         if (*slot == NULL)
23605           *slot = (void *) list;
23606         else
23607           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23608       }
23609
23610   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23611 }
23612
23613 /* Optimize location lists referenced from DIE
23614    children and share them whenever possible.  */
23615
23616 static void
23617 optimize_location_lists (dw_die_ref die)
23618 {
23619   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23620   optimize_location_lists_1 (die, htab);
23621   htab_delete (htab);
23622 }
23623 \f
23624 /* Output stuff that dwarf requires at the end of every file,
23625    and generate the DWARF-2 debugging info.  */
23626
23627 static void
23628 dwarf2out_finish (const char *filename)
23629 {
23630   limbo_die_node *node, *next_node;
23631   comdat_type_node *ctnode;
23632   htab_t comdat_type_table;
23633   unsigned int i;
23634
23635   gen_scheduled_generic_parms_dies ();
23636   gen_remaining_tmpl_value_param_die_attribute ();
23637
23638   /* Add the name for the main input file now.  We delayed this from
23639      dwarf2out_init to avoid complications with PCH.  */
23640   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23641   if (!IS_ABSOLUTE_PATH (filename))
23642     add_comp_dir_attribute (comp_unit_die ());
23643   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23644     {
23645       bool p = false;
23646       htab_traverse (file_table, file_table_relative_p, &p);
23647       if (p)
23648         add_comp_dir_attribute (comp_unit_die ());
23649     }
23650
23651   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
23652     {
23653       add_location_or_const_value_attribute (
23654         VEC_index (deferred_locations, deferred_locations_list, i)->die,
23655         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
23656         DW_AT_location);
23657     }
23658
23659   /* Traverse the limbo die list, and add parent/child links.  The only
23660      dies without parents that should be here are concrete instances of
23661      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23662      For concrete instances, we can get the parent die from the abstract
23663      instance.  */
23664   for (node = limbo_die_list; node; node = next_node)
23665     {
23666       dw_die_ref die = node->die;
23667       next_node = node->next;
23668
23669       if (die->die_parent == NULL)
23670         {
23671           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23672
23673           if (origin)
23674             add_child_die (origin->die_parent, die);
23675           else if (is_cu_die (die))
23676             ;
23677           else if (seen_error ())
23678             /* It's OK to be confused by errors in the input.  */
23679             add_child_die (comp_unit_die (), die);
23680           else
23681             {
23682               /* In certain situations, the lexical block containing a
23683                  nested function can be optimized away, which results
23684                  in the nested function die being orphaned.  Likewise
23685                  with the return type of that nested function.  Force
23686                  this to be a child of the containing function.
23687
23688                  It may happen that even the containing function got fully
23689                  inlined and optimized out.  In that case we are lost and
23690                  assign the empty child.  This should not be big issue as
23691                  the function is likely unreachable too.  */
23692               tree context = NULL_TREE;
23693
23694               gcc_assert (node->created_for);
23695
23696               if (DECL_P (node->created_for))
23697                 context = DECL_CONTEXT (node->created_for);
23698               else if (TYPE_P (node->created_for))
23699                 context = TYPE_CONTEXT (node->created_for);
23700
23701               gcc_assert (context
23702                           && (TREE_CODE (context) == FUNCTION_DECL
23703                               || TREE_CODE (context) == NAMESPACE_DECL));
23704
23705               origin = lookup_decl_die (context);
23706               if (origin)
23707                 add_child_die (origin, die);
23708               else
23709                 add_child_die (comp_unit_die (), die);
23710             }
23711         }
23712     }
23713
23714   limbo_die_list = NULL;
23715
23716   resolve_addr (comp_unit_die ());
23717
23718   for (node = deferred_asm_name; node; node = node->next)
23719     {
23720       tree decl = node->created_for;
23721       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23722         {
23723           add_linkage_attr (node->die, decl);
23724           move_linkage_attr (node->die);
23725         }
23726     }
23727
23728   deferred_asm_name = NULL;
23729
23730   /* Walk through the list of incomplete types again, trying once more to
23731      emit full debugging info for them.  */
23732   retry_incomplete_types ();
23733
23734   if (flag_eliminate_unused_debug_types)
23735     prune_unused_types ();
23736
23737   /* Generate separate CUs for each of the include files we've seen.
23738      They will go into limbo_die_list.  */
23739   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
23740     break_out_includes (comp_unit_die ());
23741
23742   /* Generate separate COMDAT sections for type DIEs. */
23743   if (dwarf_version >= 4)
23744     {
23745       break_out_comdat_types (comp_unit_die ());
23746
23747       /* Each new type_unit DIE was added to the limbo die list when created.
23748          Since these have all been added to comdat_type_list, clear the
23749          limbo die list.  */
23750       limbo_die_list = NULL;
23751
23752       /* For each new comdat type unit, copy declarations for incomplete
23753          types to make the new unit self-contained (i.e., no direct
23754          references to the main compile unit).  */
23755       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23756         copy_decls_for_unworthy_types (ctnode->root_die);
23757       copy_decls_for_unworthy_types (comp_unit_die ());
23758
23759       /* In the process of copying declarations from one unit to another,
23760          we may have left some declarations behind that are no longer
23761          referenced.  Prune them.  */
23762       prune_unused_types ();
23763     }
23764
23765   /* Traverse the DIE's and add add sibling attributes to those DIE's
23766      that have children.  */
23767   add_sibling_attributes (comp_unit_die ());
23768   for (node = limbo_die_list; node; node = node->next)
23769     add_sibling_attributes (node->die);
23770   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23771     add_sibling_attributes (ctnode->root_die);
23772
23773   /* Output a terminator label for the .text section.  */
23774   switch_to_section (text_section);
23775   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23776   if (cold_text_section)
23777     {
23778       switch_to_section (cold_text_section);
23779       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23780     }
23781
23782   /* We can only use the low/high_pc attributes if all of the code was
23783      in .text.  */
23784   if (!have_multiple_function_sections 
23785       || (dwarf_version < 3 && dwarf_strict))
23786     {
23787       add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
23788       add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
23789     }
23790
23791   else
23792     {
23793       unsigned fde_idx = 0;
23794       bool range_list_added = false;
23795
23796       /* We need to give .debug_loc and .debug_ranges an appropriate
23797          "base address".  Use zero so that these addresses become
23798          absolute.  Historically, we've emitted the unexpected
23799          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23800          Emit both to give time for other tools to adapt.  */
23801       add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
23802       add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
23803
23804       if (text_section_used)
23805         add_ranges_by_labels (comp_unit_die (), text_section_label,
23806                               text_end_label, &range_list_added);
23807       if (flag_reorder_blocks_and_partition && cold_text_section_used)
23808         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
23809                               cold_end_label, &range_list_added);
23810
23811       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
23812         {
23813           dw_fde_ref fde = &fde_table[fde_idx];
23814
23815           if (fde->dw_fde_switched_sections)
23816             {
23817               if (!fde->in_std_section)
23818                 add_ranges_by_labels (comp_unit_die (),
23819                                       fde->dw_fde_hot_section_label,
23820                                       fde->dw_fde_hot_section_end_label,
23821                                       &range_list_added);
23822               if (!fde->cold_in_std_section)
23823                 add_ranges_by_labels (comp_unit_die (),
23824                                       fde->dw_fde_unlikely_section_label,
23825                                       fde->dw_fde_unlikely_section_end_label,
23826                                       &range_list_added);
23827             }
23828           else if (!fde->in_std_section)
23829             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
23830                                   fde->dw_fde_end, &range_list_added);
23831         }
23832
23833       if (range_list_added)
23834         add_ranges (NULL);
23835     }
23836
23837   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23838     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
23839                     debug_line_section_label);
23840
23841   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23842     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
23843
23844   if (have_location_lists)
23845     optimize_location_lists (comp_unit_die ());
23846
23847   /* Output all of the compilation units.  We put the main one last so that
23848      the offsets are available to output_pubnames.  */
23849   for (node = limbo_die_list; node; node = node->next)
23850     output_comp_unit (node->die, 0);
23851
23852   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23853   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23854     {
23855       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23856
23857       /* Don't output duplicate types.  */
23858       if (*slot != HTAB_EMPTY_ENTRY)
23859         continue;
23860
23861       /* Add a pointer to the line table for the main compilation unit
23862          so that the debugger can make sense of DW_AT_decl_file
23863          attributes.  */
23864       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23865         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23866                         debug_line_section_label);
23867
23868       output_comdat_type_unit (ctnode);
23869       *slot = ctnode;
23870     }
23871   htab_delete (comdat_type_table);
23872
23873   /* Output the main compilation unit if non-empty or if .debug_macinfo
23874      will be emitted.  */
23875   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
23876
23877   /* Output the abbreviation table.  */
23878   switch_to_section (debug_abbrev_section);
23879   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23880   output_abbrev_section ();
23881
23882   /* Output location list section if necessary.  */
23883   if (have_location_lists)
23884     {
23885       /* Output the location lists info.  */
23886       switch_to_section (debug_loc_section);
23887       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
23888                                    DEBUG_LOC_SECTION_LABEL, 0);
23889       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23890       output_location_lists (comp_unit_die ());
23891     }
23892
23893   /* Output public names table if necessary.  */
23894   if (!VEC_empty (pubname_entry, pubname_table))
23895     {
23896       gcc_assert (info_section_emitted);
23897       switch_to_section (debug_pubnames_section);
23898       output_pubnames (pubname_table);
23899     }
23900
23901   /* Output public types table if necessary.  */
23902   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23903      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23904      simply won't look for the section.  */
23905   if (!VEC_empty (pubname_entry, pubtype_table))
23906     {
23907       bool empty = false;
23908       
23909       if (flag_eliminate_unused_debug_types)
23910         {
23911           /* The pubtypes table might be emptied by pruning unused items.  */
23912           unsigned i;
23913           pubname_ref p;
23914           empty = true;
23915           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
23916             if (p->die->die_offset != 0)
23917               {
23918                 empty = false;
23919                 break;
23920               }
23921         }
23922       if (!empty)
23923         {
23924           gcc_assert (info_section_emitted);
23925           switch_to_section (debug_pubtypes_section);
23926           output_pubnames (pubtype_table);
23927         }
23928     }
23929
23930   /* Output direct and virtual call tables if necessary.  */
23931   if (!VEC_empty (dcall_entry, dcall_table))
23932     {
23933       switch_to_section (debug_dcall_section);
23934       output_dcall_table ();
23935     }
23936   if (!VEC_empty (vcall_entry, vcall_table))
23937     {
23938       switch_to_section (debug_vcall_section);
23939       output_vcall_table ();
23940     }
23941
23942   /* Output the address range information.  We only put functions in the arange
23943      table, so don't write it out if we don't have any.  */
23944   if (arange_table_in_use)
23945     {
23946       switch_to_section (debug_aranges_section);
23947       output_aranges ();
23948     }
23949
23950   /* Output ranges section if necessary.  */
23951   if (ranges_table_in_use)
23952     {
23953       switch_to_section (debug_ranges_section);
23954       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23955       output_ranges ();
23956     }
23957
23958   /* Output the source line correspondence table.  We must do this
23959      even if there is no line information.  Otherwise, on an empty
23960      translation unit, we will generate a present, but empty,
23961      .debug_info section.  IRIX 6.5 `nm' will then complain when
23962      examining the file.  This is done late so that any filenames
23963      used by the debug_info section are marked as 'used'.  */
23964   switch_to_section (debug_line_section);
23965   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23966   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23967     output_line_info ();
23968
23969   /* Have to end the macro section.  */
23970   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23971     {
23972       switch_to_section (debug_macinfo_section);
23973       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23974       if (!VEC_empty (macinfo_entry, macinfo_table))
23975         output_macinfo ();
23976       dw2_asm_output_data (1, 0, "End compilation unit");
23977     }
23978
23979   /* If we emitted any DW_FORM_strp form attribute, output the string
23980      table too.  */
23981   if (debug_str_hash)
23982     htab_traverse (debug_str_hash, output_indirect_string, NULL);
23983 }
23984
23985 #include "gt-dwarf2out.h"