OSDN Git Service

2011-07-01 Richard Guenther <rguenther@suse.de>
[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 "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97
98 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 /* ??? Poison these here until it can be done generically.  They've been
118    totally replaced in this file; make sure it stays that way.  */
119 #undef DWARF2_UNWIND_INFO
120 #undef DWARF2_FRAME_INFO
121 #if (GCC_VERSION >= 3000)
122  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
123 #endif
124
125 #ifndef INCOMING_RETURN_ADDR_RTX
126 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
127 #endif
128
129 /* Map register numbers held in the call frame info that gcc has
130    collected using DWARF_FRAME_REGNUM to those that should be output in
131    .debug_frame and .eh_frame.  */
132 #ifndef DWARF2_FRAME_REG_OUT
133 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
134 #endif
135
136 /* Save the result of dwarf2out_do_frame across PCH.  */
137 static GTY(()) bool saved_do_cfi_asm = 0;
138
139 /* Decide whether we want to emit frame unwind information for the current
140    translation unit.  */
141
142 int
143 dwarf2out_do_frame (void)
144 {
145   /* We want to emit correct CFA location expressions or lists, so we
146      have to return true if we're going to output debug info, even if
147      we're not going to output frame or unwind info.  */
148   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
149     return true;
150
151   if (saved_do_cfi_asm)
152     return true;
153
154   if (targetm.debug_unwind_info () == UI_DWARF2)
155     return true;
156
157   if ((flag_unwind_tables || flag_exceptions)
158       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
159     return true;
160
161   return false;
162 }
163
164 /* Decide whether to emit frame unwind via assembler directives.  */
165
166 int
167 dwarf2out_do_cfi_asm (void)
168 {
169   int enc;
170
171 #ifdef MIPS_DEBUGGING_INFO
172   return false;
173 #endif
174   if (saved_do_cfi_asm)
175     return true;
176   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
177     return false;
178   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
179     return false;
180
181   /* Make sure the personality encoding is one the assembler can support.
182      In particular, aligned addresses can't be handled.  */
183   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
184   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
185     return false;
186   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
187   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
188     return false;
189
190   /* If we can't get the assembler to emit only .debug_frame, and we don't need
191      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
192   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
193       && !flag_unwind_tables && !flag_exceptions
194       && targetm_common.except_unwind_info (&global_options) != UI_DWARF2)
195     return false;
196
197   saved_do_cfi_asm = true;
198   return true;
199 }
200
201 /* The size of the target's pointer type.  */
202 #ifndef PTR_SIZE
203 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
204 #endif
205
206 /* Array of RTXes referenced by the debugging information, which therefore
207    must be kept around forever.  */
208 static GTY(()) VEC(rtx,gc) *used_rtx_array;
209
210 /* A pointer to the base of a list of incomplete types which might be
211    completed at some later time.  incomplete_types_list needs to be a
212    VEC(tree,gc) because we want to tell the garbage collector about
213    it.  */
214 static GTY(()) VEC(tree,gc) *incomplete_types;
215
216 /* A pointer to the base of a table of references to declaration
217    scopes.  This table is a display which tracks the nesting
218    of declaration scopes at the current scope and containing
219    scopes.  This table is used to find the proper place to
220    define type declaration DIE's.  */
221 static GTY(()) VEC(tree,gc) *decl_scope_table;
222
223 /* Pointers to various DWARF2 sections.  */
224 static GTY(()) section *debug_info_section;
225 static GTY(()) section *debug_abbrev_section;
226 static GTY(()) section *debug_aranges_section;
227 static GTY(()) section *debug_macinfo_section;
228 static GTY(()) section *debug_line_section;
229 static GTY(()) section *debug_loc_section;
230 static GTY(()) section *debug_pubnames_section;
231 static GTY(()) section *debug_pubtypes_section;
232 static GTY(()) section *debug_str_section;
233 static GTY(()) section *debug_ranges_section;
234 static GTY(()) section *debug_frame_section;
235
236 /* Personality decl of current unit.  Used only when assembler does not support
237    personality CFI.  */
238 static GTY(()) rtx current_unit_personality;
239
240 /* How to start an assembler comment.  */
241 #ifndef ASM_COMMENT_START
242 #define ASM_COMMENT_START ";#"
243 #endif
244
245 typedef struct dw_cfi_struct *dw_cfi_ref;
246 typedef struct dw_fde_struct *dw_fde_ref;
247 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
248
249 /* Call frames are described using a sequence of Call Frame
250    Information instructions.  The register number, offset
251    and address fields are provided as possible operands;
252    their use is selected by the opcode field.  */
253
254 enum dw_cfi_oprnd_type {
255   dw_cfi_oprnd_unused,
256   dw_cfi_oprnd_reg_num,
257   dw_cfi_oprnd_offset,
258   dw_cfi_oprnd_addr,
259   dw_cfi_oprnd_loc
260 };
261
262 typedef union GTY(()) dw_cfi_oprnd_struct {
263   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
264   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
265   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
266   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
267 }
268 dw_cfi_oprnd;
269
270 typedef struct GTY(()) dw_cfi_struct {
271   enum dwarf_call_frame_info dw_cfi_opc;
272   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
273     dw_cfi_oprnd1;
274   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
275     dw_cfi_oprnd2;
276 }
277 dw_cfi_node;
278
279 DEF_VEC_P (dw_cfi_ref);
280 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
281 DEF_VEC_ALLOC_P (dw_cfi_ref, gc);
282
283 typedef VEC(dw_cfi_ref, gc) *cfi_vec;
284
285 /* This is how we define the location of the CFA. We use to handle it
286    as REG + OFFSET all the time,  but now it can be more complex.
287    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
288    Instead of passing around REG and OFFSET, we pass a copy
289    of this structure.  */
290 typedef struct cfa_loc {
291   HOST_WIDE_INT offset;
292   HOST_WIDE_INT base_offset;
293   unsigned int reg;
294   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
295   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
296 } dw_cfa_location;
297
298 /* All call frame descriptions (FDE's) in the GCC generated DWARF
299    refer to a single Common Information Entry (CIE), defined at
300    the beginning of the .debug_frame section.  This use of a single
301    CIE obviates the need to keep track of multiple CIE's
302    in the DWARF generation routines below.  */
303
304 typedef struct GTY(()) dw_fde_struct {
305   tree decl;
306   const char *dw_fde_begin;
307   const char *dw_fde_current_label;
308   const char *dw_fde_end;
309   const char *dw_fde_vms_end_prologue;
310   const char *dw_fde_vms_begin_epilogue;
311   const char *dw_fde_second_begin;
312   const char *dw_fde_second_end;
313   cfi_vec dw_fde_cfi;
314   int dw_fde_switch_cfi_index; /* Last CFI before switching sections.  */
315   HOST_WIDE_INT stack_realignment;
316   unsigned funcdef_number;
317   /* Dynamic realign argument pointer register.  */
318   unsigned int drap_reg;
319   /* Virtual dynamic realign argument pointer register.  */
320   unsigned int vdrap_reg;
321   /* These 3 flags are copied from rtl_data in function.h.  */
322   unsigned all_throwers_are_sibcalls : 1;
323   unsigned uses_eh_lsda : 1;
324   unsigned nothrow : 1;
325   /* Whether we did stack realign in this call frame.  */
326   unsigned stack_realign : 1;
327   /* Whether dynamic realign argument pointer register has been saved.  */
328   unsigned drap_reg_saved: 1;
329   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
330   unsigned in_std_section : 1;
331   /* True iff dw_fde_second_begin label is in text_section or
332      cold_text_section.  */
333   unsigned second_in_std_section : 1;
334 }
335 dw_fde_node;
336
337 /* Maximum size (in bytes) of an artificially generated label.  */
338 #define MAX_ARTIFICIAL_LABEL_BYTES      30
339
340 /* The size of addresses as they appear in the Dwarf 2 data.
341    Some architectures use word addresses to refer to code locations,
342    but Dwarf 2 info always uses byte addresses.  On such machines,
343    Dwarf 2 addresses need to be larger than the architecture's
344    pointers.  */
345 #ifndef DWARF2_ADDR_SIZE
346 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
347 #endif
348
349 /* The size in bytes of a DWARF field indicating an offset or length
350    relative to a debug info section, specified to be 4 bytes in the
351    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
352    as PTR_SIZE.  */
353
354 #ifndef DWARF_OFFSET_SIZE
355 #define DWARF_OFFSET_SIZE 4
356 #endif
357
358 /* The size in bytes of a DWARF 4 type signature.  */
359
360 #ifndef DWARF_TYPE_SIGNATURE_SIZE
361 #define DWARF_TYPE_SIGNATURE_SIZE 8
362 #endif
363
364 /* According to the (draft) DWARF 3 specification, the initial length
365    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
366    bytes are 0xffffffff, followed by the length stored in the next 8
367    bytes.
368
369    However, the SGI/MIPS ABI uses an initial length which is equal to
370    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
371
372 #ifndef DWARF_INITIAL_LENGTH_SIZE
373 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
374 #endif
375
376 /* Round SIZE up to the nearest BOUNDARY.  */
377 #define DWARF_ROUND(SIZE,BOUNDARY) \
378   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
379
380 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
381 #ifndef DWARF_CIE_DATA_ALIGNMENT
382 #ifdef STACK_GROWS_DOWNWARD
383 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
384 #else
385 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
386 #endif
387 #endif
388
389 /* CIE identifier.  */
390 #if HOST_BITS_PER_WIDE_INT >= 64
391 #define DWARF_CIE_ID \
392   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
393 #else
394 #define DWARF_CIE_ID DW_CIE_ID
395 #endif
396
397 /* A pointer to the base of a table that contains frame description
398    information for each routine.  */
399 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
400
401 /* Number of elements currently allocated for fde_table.  */
402 static GTY(()) unsigned fde_table_allocated;
403
404 /* Number of elements in fde_table currently in use.  */
405 static GTY(()) unsigned fde_table_in_use;
406
407 /* Size (in elements) of increments by which we may expand the
408    fde_table.  */
409 #define FDE_TABLE_INCREMENT 256
410
411 /* Get the current fde_table entry we should use.  */
412
413 static inline dw_fde_ref
414 current_fde (void)
415 {
416   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
417 }
418
419 /* A vector of call frame insns for the CIE.  */
420 static GTY(()) cfi_vec cie_cfi_vec;
421
422 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
423    attribute that accelerates the lookup of the FDE associated
424    with the subprogram.  This variable holds the table index of the FDE
425    associated with the current function (body) definition.  */
426 static unsigned current_funcdef_fde;
427
428 struct GTY(()) indirect_string_node {
429   const char *str;
430   unsigned int refcount;
431   enum dwarf_form form;
432   char *label;
433 };
434
435 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
436
437 static GTY(()) int dw2_string_counter;
438 static GTY(()) unsigned long dwarf2out_cfi_label_num;
439
440 /* True if the compilation unit places functions in more than one section.  */
441 static GTY(()) bool have_multiple_function_sections = false;
442
443 /* Whether the default text and cold text sections have been used at all.  */
444
445 static GTY(()) bool text_section_used = false;
446 static GTY(()) bool cold_text_section_used = false;
447
448 /* The default cold text section.  */
449 static GTY(()) section *cold_text_section;
450
451 /* Forward declarations for functions defined in this file.  */
452
453 static char *stripattributes (const char *);
454 static const char *dwarf_cfi_name (unsigned);
455 static dw_cfi_ref new_cfi (void);
456 static void add_cfi (cfi_vec *, dw_cfi_ref);
457 static void add_fde_cfi (const char *, dw_cfi_ref);
458 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
459 static void lookup_cfa (dw_cfa_location *);
460 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
461 static void initial_return_save (rtx);
462 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
463                                           HOST_WIDE_INT);
464 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
465 static void output_cfi_directive (dw_cfi_ref);
466 static void output_call_frame_info (int);
467 static void dwarf2out_note_section_used (void);
468 static bool clobbers_queued_reg_save (const_rtx);
469 static void dwarf2out_frame_debug_expr (rtx, const char *);
470
471 /* Support for complex CFA locations.  */
472 static void output_cfa_loc (dw_cfi_ref, int);
473 static void output_cfa_loc_raw (dw_cfi_ref);
474 static void get_cfa_from_loc_descr (dw_cfa_location *,
475                                     struct dw_loc_descr_struct *);
476 static struct dw_loc_descr_struct *build_cfa_loc
477   (dw_cfa_location *, HOST_WIDE_INT);
478 static struct dw_loc_descr_struct *build_cfa_aligned_loc
479   (HOST_WIDE_INT, HOST_WIDE_INT);
480 static void def_cfa_1 (const char *, dw_cfa_location *);
481 static struct dw_loc_descr_struct *mem_loc_descriptor
482   (rtx, enum machine_mode mode, enum machine_mode mem_mode,
483    enum var_init_status);
484
485 /* How to start an assembler comment.  */
486 #ifndef ASM_COMMENT_START
487 #define ASM_COMMENT_START ";#"
488 #endif
489
490 /* Data and reference forms for relocatable data.  */
491 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
492 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
493
494 #ifndef DEBUG_FRAME_SECTION
495 #define DEBUG_FRAME_SECTION     ".debug_frame"
496 #endif
497
498 #ifndef FUNC_BEGIN_LABEL
499 #define FUNC_BEGIN_LABEL        "LFB"
500 #endif
501
502 #ifndef FUNC_END_LABEL
503 #define FUNC_END_LABEL          "LFE"
504 #endif
505
506 #ifndef PROLOGUE_END_LABEL
507 #define PROLOGUE_END_LABEL      "LPE"
508 #endif
509
510 #ifndef EPILOGUE_BEGIN_LABEL
511 #define EPILOGUE_BEGIN_LABEL    "LEB"
512 #endif
513
514 #ifndef FRAME_BEGIN_LABEL
515 #define FRAME_BEGIN_LABEL       "Lframe"
516 #endif
517 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
518 #define CIE_END_LABEL           "LECIE"
519 #define FDE_LABEL               "LSFDE"
520 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
521 #define FDE_END_LABEL           "LEFDE"
522 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
523 #define LINE_NUMBER_END_LABEL   "LELT"
524 #define LN_PROLOG_AS_LABEL      "LASLTP"
525 #define LN_PROLOG_END_LABEL     "LELTP"
526 #define DIE_LABEL_PREFIX        "DW"
527
528 /* The DWARF 2 CFA column which tracks the return address.  Normally this
529    is the column for PC, or the first column after all of the hard
530    registers.  */
531 #ifndef DWARF_FRAME_RETURN_COLUMN
532 #ifdef PC_REGNUM
533 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
534 #else
535 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
536 #endif
537 #endif
538
539 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
540    default, we just provide columns for all registers.  */
541 #ifndef DWARF_FRAME_REGNUM
542 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
543 #endif
544 \f
545 /* Match the base name of a file to the base name of a compilation unit. */
546
547 static int
548 matches_main_base (const char *path)
549 {
550   /* Cache the last query. */
551   static const char *last_path = NULL;
552   static int last_match = 0;
553   if (path != last_path)
554     {
555       const char *base;
556       int length = base_of_path (path, &base);
557       last_path = path;
558       last_match = (length == main_input_baselength
559                     && memcmp (base, main_input_basename, length) == 0);
560     }
561   return last_match;
562 }
563
564 #ifdef DEBUG_DEBUG_STRUCT
565
566 static int
567 dump_struct_debug (tree type, enum debug_info_usage usage,
568                    enum debug_struct_file criterion, int generic,
569                    int matches, int result)
570 {
571   /* Find the type name. */
572   tree type_decl = TYPE_STUB_DECL (type);
573   tree t = type_decl;
574   const char *name = 0;
575   if (TREE_CODE (t) == TYPE_DECL)
576     t = DECL_NAME (t);
577   if (t)
578     name = IDENTIFIER_POINTER (t);
579
580   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
581            criterion,
582            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
583            matches ? "bas" : "hdr",
584            generic ? "gen" : "ord",
585            usage == DINFO_USAGE_DFN ? ";" :
586              usage == DINFO_USAGE_DIR_USE ? "." : "*",
587            result,
588            (void*) type_decl, name);
589   return result;
590 }
591 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
592   dump_struct_debug (type, usage, criterion, generic, matches, result)
593
594 #else
595
596 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
597   (result)
598
599 #endif
600
601 static bool
602 should_emit_struct_debug (tree type, enum debug_info_usage usage)
603 {
604   enum debug_struct_file criterion;
605   tree type_decl;
606   bool generic = lang_hooks.types.generic_p (type);
607
608   if (generic)
609     criterion = debug_struct_generic[usage];
610   else
611     criterion = debug_struct_ordinary[usage];
612
613   if (criterion == DINFO_STRUCT_FILE_NONE)
614     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
615   if (criterion == DINFO_STRUCT_FILE_ANY)
616     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
617
618   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
619
620   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
621     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
622
623   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
624     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
625   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
626 }
627 \f
628 /* Hook used by __throw.  */
629
630 rtx
631 expand_builtin_dwarf_sp_column (void)
632 {
633   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
634   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
635 }
636
637 /* Return a pointer to a copy of the section string name S with all
638    attributes stripped off, and an asterisk prepended (for assemble_name).  */
639
640 static inline char *
641 stripattributes (const char *s)
642 {
643   char *stripped = XNEWVEC (char, strlen (s) + 2);
644   char *p = stripped;
645
646   *p++ = '*';
647
648   while (*s && *s != ',')
649     *p++ = *s++;
650
651   *p = '\0';
652   return stripped;
653 }
654
655 /* MEM is a memory reference for the register size table, each element of
656    which has mode MODE.  Initialize column C as a return address column.  */
657
658 static void
659 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
660 {
661   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
662   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
663   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
664 }
665
666 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
667
668 static inline HOST_WIDE_INT
669 div_data_align (HOST_WIDE_INT off)
670 {
671   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
672   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
673   return r;
674 }
675
676 /* Return true if we need a signed version of a given opcode
677    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
678
679 static inline bool
680 need_data_align_sf_opcode (HOST_WIDE_INT off)
681 {
682   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
683 }
684
685 /* Generate code to initialize the register size table.  */
686
687 void
688 expand_builtin_init_dwarf_reg_sizes (tree address)
689 {
690   unsigned int i;
691   enum machine_mode mode = TYPE_MODE (char_type_node);
692   rtx addr = expand_normal (address);
693   rtx mem = gen_rtx_MEM (BLKmode, addr);
694   bool wrote_return_column = false;
695
696   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
697     {
698       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
699
700       if (rnum < DWARF_FRAME_REGISTERS)
701         {
702           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
703           enum machine_mode save_mode = reg_raw_mode[i];
704           HOST_WIDE_INT size;
705
706           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
707             save_mode = choose_hard_reg_mode (i, 1, true);
708           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
709             {
710               if (save_mode == VOIDmode)
711                 continue;
712               wrote_return_column = true;
713             }
714           size = GET_MODE_SIZE (save_mode);
715           if (offset < 0)
716             continue;
717
718           emit_move_insn (adjust_address (mem, mode, offset),
719                           gen_int_mode (size, mode));
720         }
721     }
722
723   if (!wrote_return_column)
724     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
725
726 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
727   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
728 #endif
729
730   targetm.init_dwarf_reg_sizes_extra (address);
731 }
732
733 /* Convert a DWARF call frame info. operation to its string name */
734
735 static const char *
736 dwarf_cfi_name (unsigned int cfi_opc)
737 {
738   switch (cfi_opc)
739     {
740     case DW_CFA_advance_loc:
741       return "DW_CFA_advance_loc";
742     case DW_CFA_offset:
743       return "DW_CFA_offset";
744     case DW_CFA_restore:
745       return "DW_CFA_restore";
746     case DW_CFA_nop:
747       return "DW_CFA_nop";
748     case DW_CFA_set_loc:
749       return "DW_CFA_set_loc";
750     case DW_CFA_advance_loc1:
751       return "DW_CFA_advance_loc1";
752     case DW_CFA_advance_loc2:
753       return "DW_CFA_advance_loc2";
754     case DW_CFA_advance_loc4:
755       return "DW_CFA_advance_loc4";
756     case DW_CFA_offset_extended:
757       return "DW_CFA_offset_extended";
758     case DW_CFA_restore_extended:
759       return "DW_CFA_restore_extended";
760     case DW_CFA_undefined:
761       return "DW_CFA_undefined";
762     case DW_CFA_same_value:
763       return "DW_CFA_same_value";
764     case DW_CFA_register:
765       return "DW_CFA_register";
766     case DW_CFA_remember_state:
767       return "DW_CFA_remember_state";
768     case DW_CFA_restore_state:
769       return "DW_CFA_restore_state";
770     case DW_CFA_def_cfa:
771       return "DW_CFA_def_cfa";
772     case DW_CFA_def_cfa_register:
773       return "DW_CFA_def_cfa_register";
774     case DW_CFA_def_cfa_offset:
775       return "DW_CFA_def_cfa_offset";
776
777     /* DWARF 3 */
778     case DW_CFA_def_cfa_expression:
779       return "DW_CFA_def_cfa_expression";
780     case DW_CFA_expression:
781       return "DW_CFA_expression";
782     case DW_CFA_offset_extended_sf:
783       return "DW_CFA_offset_extended_sf";
784     case DW_CFA_def_cfa_sf:
785       return "DW_CFA_def_cfa_sf";
786     case DW_CFA_def_cfa_offset_sf:
787       return "DW_CFA_def_cfa_offset_sf";
788
789     /* SGI/MIPS specific */
790     case DW_CFA_MIPS_advance_loc8:
791       return "DW_CFA_MIPS_advance_loc8";
792
793     /* GNU extensions */
794     case DW_CFA_GNU_window_save:
795       return "DW_CFA_GNU_window_save";
796     case DW_CFA_GNU_args_size:
797       return "DW_CFA_GNU_args_size";
798     case DW_CFA_GNU_negative_offset_extended:
799       return "DW_CFA_GNU_negative_offset_extended";
800
801     default:
802       return "DW_CFA_<unknown>";
803     }
804 }
805
806 /* Return a pointer to a newly allocated Call Frame Instruction.  */
807
808 static inline dw_cfi_ref
809 new_cfi (void)
810 {
811   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
812
813   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
814   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
815
816   return cfi;
817 }
818
819 /* Add a Call Frame Instruction to list of instructions.  */
820
821 static inline void
822 add_cfi (cfi_vec *vec, dw_cfi_ref cfi)
823 {
824   dw_fde_ref fde = current_fde ();
825
826   /* When DRAP is used, CFA is defined with an expression.  Redefine
827      CFA may lead to a different CFA value.   */
828   /* ??? Of course, this heuristic fails when we're annotating epilogues,
829      because of course we'll always want to redefine the CFA back to the
830      stack pointer on the way out.  Where should we move this check?  */
831   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
832     switch (cfi->dw_cfi_opc)
833       {
834         case DW_CFA_def_cfa_register:
835         case DW_CFA_def_cfa_offset:
836         case DW_CFA_def_cfa_offset_sf:
837         case DW_CFA_def_cfa:
838         case DW_CFA_def_cfa_sf:
839           gcc_unreachable ();
840
841         default:
842           break;
843       }
844
845   VEC_safe_push (dw_cfi_ref, gc, *vec, cfi);
846 }
847
848 /* Generate a new label for the CFI info to refer to.  FORCE is true
849    if a label needs to be output even when using .cfi_* directives.  */
850
851 char *
852 dwarf2out_cfi_label (bool force)
853 {
854   static char label[20];
855
856   if (!force && dwarf2out_do_cfi_asm ())
857     {
858       /* In this case, we will be emitting the asm directive instead of
859          the label, so just return a placeholder to keep the rest of the
860          interfaces happy.  */
861       strcpy (label, "<do not output>");
862     }
863   else
864     {
865       int num = dwarf2out_cfi_label_num++;
866       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
867       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
868     }
869
870   return label;
871 }
872
873 /* True if remember_state should be emitted before following CFI directive.  */
874 static bool emit_cfa_remember;
875
876 /* True if any CFI directives were emitted at the current insn.  */
877 static bool any_cfis_emitted;
878
879 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
880    or to the CIE if LABEL is NULL.  */
881
882 static void
883 add_fde_cfi (const char *label, dw_cfi_ref cfi)
884 {
885   cfi_vec *vec;
886
887   if (cie_cfi_vec == NULL)
888     cie_cfi_vec = VEC_alloc (dw_cfi_ref, gc, 20);
889
890   vec = &cie_cfi_vec;
891
892   if (emit_cfa_remember)
893     {
894       dw_cfi_ref cfi_remember;
895
896       /* Emit the state save.  */
897       emit_cfa_remember = false;
898       cfi_remember = new_cfi ();
899       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
900       add_fde_cfi (label, cfi_remember);
901     }
902
903   if (dwarf2out_do_cfi_asm ())
904     {
905       if (label)
906         {
907           dw_fde_ref fde = current_fde ();
908
909           gcc_assert (fde != NULL);
910
911           /* We still have to add the cfi to the list so that lookup_cfa
912              works later on.  When -g2 and above we even need to force
913              emitting of CFI labels and add to list a DW_CFA_set_loc for
914              convert_cfa_to_fb_loc_list purposes.  If we're generating
915              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
916              convert_cfa_to_fb_loc_list.  */
917           if (dwarf_version == 2
918               && debug_info_level > DINFO_LEVEL_TERSE
919               && (write_symbols == DWARF2_DEBUG
920                   || write_symbols == VMS_AND_DWARF2_DEBUG))
921             {
922               switch (cfi->dw_cfi_opc)
923                 {
924                 case DW_CFA_def_cfa_offset:
925                 case DW_CFA_def_cfa_offset_sf:
926                 case DW_CFA_def_cfa_register:
927                 case DW_CFA_def_cfa:
928                 case DW_CFA_def_cfa_sf:
929                 case DW_CFA_def_cfa_expression:
930                 case DW_CFA_restore_state:
931                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
932                     label = dwarf2out_cfi_label (true);
933
934                   if (fde->dw_fde_current_label == NULL
935                       || strcmp (label, fde->dw_fde_current_label) != 0)
936                     {
937                       dw_cfi_ref xcfi;
938
939                       label = xstrdup (label);
940
941                       /* Set the location counter to the new label.  */
942                       xcfi = new_cfi ();
943                       /* It doesn't metter whether DW_CFA_set_loc
944                          or DW_CFA_advance_loc4 is added here, those aren't
945                          emitted into assembly, only looked up by
946                          convert_cfa_to_fb_loc_list.  */
947                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
948                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
949                       add_cfi (&fde->dw_fde_cfi, xcfi);
950                       fde->dw_fde_current_label = label;
951                     }
952                   break;
953                 default:
954                   break;
955                 }
956             }
957
958           output_cfi_directive (cfi);
959
960           vec = &fde->dw_fde_cfi;
961           any_cfis_emitted = true;
962         }
963       /* ??? If this is a CFI for the CIE, we don't emit.  This
964          assumes that the standard CIE contents that the assembler
965          uses matches the standard CIE contents that the compiler
966          uses.  This is probably a bad assumption.  I'm not quite
967          sure how to address this for now.  */
968     }
969   else if (label)
970     {
971       dw_fde_ref fde = current_fde ();
972
973       gcc_assert (fde != NULL);
974
975       if (*label == 0)
976         label = dwarf2out_cfi_label (false);
977
978       if (fde->dw_fde_current_label == NULL
979           || strcmp (label, fde->dw_fde_current_label) != 0)
980         {
981           dw_cfi_ref xcfi;
982
983           label = xstrdup (label);
984
985           /* Set the location counter to the new label.  */
986           xcfi = new_cfi ();
987           /* If we have a current label, advance from there, otherwise
988              set the location directly using set_loc.  */
989           xcfi->dw_cfi_opc = fde->dw_fde_current_label
990                              ? DW_CFA_advance_loc4
991                              : DW_CFA_set_loc;
992           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
993           add_cfi (&fde->dw_fde_cfi, xcfi);
994
995           fde->dw_fde_current_label = label;
996         }
997
998       vec = &fde->dw_fde_cfi;
999       any_cfis_emitted = true;
1000     }
1001
1002   add_cfi (vec, cfi);
1003 }
1004
1005 /* Subroutine of lookup_cfa.  */
1006
1007 static void
1008 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1009 {
1010   switch (cfi->dw_cfi_opc)
1011     {
1012     case DW_CFA_def_cfa_offset:
1013     case DW_CFA_def_cfa_offset_sf:
1014       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1015       break;
1016     case DW_CFA_def_cfa_register:
1017       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1018       break;
1019     case DW_CFA_def_cfa:
1020     case DW_CFA_def_cfa_sf:
1021       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1022       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1023       break;
1024     case DW_CFA_def_cfa_expression:
1025       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1026       break;
1027
1028     case DW_CFA_remember_state:
1029       gcc_assert (!remember->in_use);
1030       *remember = *loc;
1031       remember->in_use = 1;
1032       break;
1033     case DW_CFA_restore_state:
1034       gcc_assert (remember->in_use);
1035       *loc = *remember;
1036       remember->in_use = 0;
1037       break;
1038
1039     default:
1040       break;
1041     }
1042 }
1043
1044 /* Find the previous value for the CFA.  */
1045
1046 static void
1047 lookup_cfa (dw_cfa_location *loc)
1048 {
1049   int ix;
1050   dw_cfi_ref cfi;
1051   dw_fde_ref fde;
1052   dw_cfa_location remember;
1053
1054   memset (loc, 0, sizeof (*loc));
1055   loc->reg = INVALID_REGNUM;
1056   remember = *loc;
1057
1058   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
1059     lookup_cfa_1 (cfi, loc, &remember);
1060
1061   fde = current_fde ();
1062   if (fde)
1063     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1064       lookup_cfa_1 (cfi, loc, &remember);
1065 }
1066
1067 /* The current rule for calculating the DWARF2 canonical frame address.  */
1068 static dw_cfa_location cfa;
1069
1070 /* The register used for saving registers to the stack, and its offset
1071    from the CFA.  */
1072 static dw_cfa_location cfa_store;
1073
1074 /* The current save location around an epilogue.  */
1075 static dw_cfa_location cfa_remember;
1076
1077 /* The running total of the size of arguments pushed onto the stack.  */
1078 static HOST_WIDE_INT args_size;
1079
1080 /* The last args_size we actually output.  */
1081 static HOST_WIDE_INT old_args_size;
1082
1083 /* Entry point to update the canonical frame address (CFA).
1084    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1085    calculated from REG+OFFSET.  */
1086
1087 void
1088 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1089 {
1090   dw_cfa_location loc;
1091   loc.indirect = 0;
1092   loc.base_offset = 0;
1093   loc.reg = reg;
1094   loc.offset = offset;
1095   def_cfa_1 (label, &loc);
1096 }
1097
1098 /* Determine if two dw_cfa_location structures define the same data.  */
1099
1100 static bool
1101 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1102 {
1103   return (loc1->reg == loc2->reg
1104           && loc1->offset == loc2->offset
1105           && loc1->indirect == loc2->indirect
1106           && (loc1->indirect == 0
1107               || loc1->base_offset == loc2->base_offset));
1108 }
1109
1110 /* This routine does the actual work.  The CFA is now calculated from
1111    the dw_cfa_location structure.  */
1112
1113 static void
1114 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1115 {
1116   dw_cfi_ref cfi;
1117   dw_cfa_location old_cfa, loc;
1118
1119   cfa = *loc_p;
1120   loc = *loc_p;
1121
1122   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1123     cfa_store.offset = loc.offset;
1124
1125   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1126   lookup_cfa (&old_cfa);
1127
1128   /* If nothing changed, no need to issue any call frame instructions.  */
1129   if (cfa_equal_p (&loc, &old_cfa))
1130     return;
1131
1132   cfi = new_cfi ();
1133
1134   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1135     {
1136       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1137          the CFA register did not change but the offset did.  The data
1138          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1139          in the assembler via the .cfi_def_cfa_offset directive.  */
1140       if (loc.offset < 0)
1141         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1142       else
1143         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1144       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1145     }
1146
1147 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1148   else if (loc.offset == old_cfa.offset
1149            && old_cfa.reg != INVALID_REGNUM
1150            && !loc.indirect
1151            && !old_cfa.indirect)
1152     {
1153       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1154          indicating the CFA register has changed to <register> but the
1155          offset has not changed.  */
1156       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1157       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1158     }
1159 #endif
1160
1161   else if (loc.indirect == 0)
1162     {
1163       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1164          indicating the CFA register has changed to <register> with
1165          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1166          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1167          directive.  */
1168       if (loc.offset < 0)
1169         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1170       else
1171         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1172       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1173       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1174     }
1175   else
1176     {
1177       /* Construct a DW_CFA_def_cfa_expression instruction to
1178          calculate the CFA using a full location expression since no
1179          register-offset pair is available.  */
1180       struct dw_loc_descr_struct *loc_list;
1181
1182       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1183       loc_list = build_cfa_loc (&loc, 0);
1184       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1185     }
1186
1187   add_fde_cfi (label, cfi);
1188 }
1189
1190 /* Add the CFI for saving a register.  REG is the CFA column number.
1191    LABEL is passed to add_fde_cfi.
1192    If SREG is -1, the register is saved at OFFSET from the CFA;
1193    otherwise it is saved in SREG.  */
1194
1195 static void
1196 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1197 {
1198   dw_cfi_ref cfi = new_cfi ();
1199   dw_fde_ref fde = current_fde ();
1200
1201   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1202
1203   /* When stack is aligned, store REG using DW_CFA_expression with
1204      FP.  */
1205   if (fde
1206       && fde->stack_realign
1207       && sreg == INVALID_REGNUM)
1208     {
1209       cfi->dw_cfi_opc = DW_CFA_expression;
1210       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1211       cfi->dw_cfi_oprnd2.dw_cfi_loc
1212         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1213     }
1214   else if (sreg == INVALID_REGNUM)
1215     {
1216       if (need_data_align_sf_opcode (offset))
1217         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1218       else if (reg & ~0x3f)
1219         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1220       else
1221         cfi->dw_cfi_opc = DW_CFA_offset;
1222       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1223     }
1224   else if (sreg == reg)
1225     cfi->dw_cfi_opc = DW_CFA_same_value;
1226   else
1227     {
1228       cfi->dw_cfi_opc = DW_CFA_register;
1229       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1230     }
1231
1232   add_fde_cfi (label, cfi);
1233 }
1234
1235 /* Entry point for saving a register to the stack.  REG is the GCC register
1236    number.  LABEL and OFFSET are passed to reg_save.  */
1237
1238 void
1239 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1240 {
1241   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1242 }
1243
1244 /* Entry point for saving the return address in the stack.
1245    LABEL and OFFSET are passed to reg_save.  */
1246
1247 void
1248 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1249 {
1250   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1251 }
1252
1253 /* Entry point for saving the return address in a register.
1254    LABEL and SREG are passed to reg_save.  */
1255
1256 void
1257 dwarf2out_return_reg (const char *label, unsigned int sreg)
1258 {
1259   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1260 }
1261
1262 /* Record the initial position of the return address.  RTL is
1263    INCOMING_RETURN_ADDR_RTX.  */
1264
1265 static void
1266 initial_return_save (rtx rtl)
1267 {
1268   unsigned int reg = INVALID_REGNUM;
1269   HOST_WIDE_INT offset = 0;
1270
1271   switch (GET_CODE (rtl))
1272     {
1273     case REG:
1274       /* RA is in a register.  */
1275       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1276       break;
1277
1278     case MEM:
1279       /* RA is on the stack.  */
1280       rtl = XEXP (rtl, 0);
1281       switch (GET_CODE (rtl))
1282         {
1283         case REG:
1284           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1285           offset = 0;
1286           break;
1287
1288         case PLUS:
1289           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1290           offset = INTVAL (XEXP (rtl, 1));
1291           break;
1292
1293         case MINUS:
1294           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1295           offset = -INTVAL (XEXP (rtl, 1));
1296           break;
1297
1298         default:
1299           gcc_unreachable ();
1300         }
1301
1302       break;
1303
1304     case PLUS:
1305       /* The return address is at some offset from any value we can
1306          actually load.  For instance, on the SPARC it is in %i7+8. Just
1307          ignore the offset for now; it doesn't matter for unwinding frames.  */
1308       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1309       initial_return_save (XEXP (rtl, 0));
1310       return;
1311
1312     default:
1313       gcc_unreachable ();
1314     }
1315
1316   if (reg != DWARF_FRAME_RETURN_COLUMN)
1317     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1318 }
1319
1320 /* Given a SET, calculate the amount of stack adjustment it
1321    contains.  */
1322
1323 static HOST_WIDE_INT
1324 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1325                      HOST_WIDE_INT cur_offset)
1326 {
1327   const_rtx src = SET_SRC (pattern);
1328   const_rtx dest = SET_DEST (pattern);
1329   HOST_WIDE_INT offset = 0;
1330   enum rtx_code code;
1331
1332   if (dest == stack_pointer_rtx)
1333     {
1334       code = GET_CODE (src);
1335
1336       /* Assume (set (reg sp) (reg whatever)) sets args_size
1337          level to 0.  */
1338       if (code == REG && src != stack_pointer_rtx)
1339         {
1340           offset = -cur_args_size;
1341 #ifndef STACK_GROWS_DOWNWARD
1342           offset = -offset;
1343 #endif
1344           return offset - cur_offset;
1345         }
1346
1347       if (! (code == PLUS || code == MINUS)
1348           || XEXP (src, 0) != stack_pointer_rtx
1349           || !CONST_INT_P (XEXP (src, 1)))
1350         return 0;
1351
1352       /* (set (reg sp) (plus (reg sp) (const_int))) */
1353       offset = INTVAL (XEXP (src, 1));
1354       if (code == PLUS)
1355         offset = -offset;
1356       return offset;
1357     }
1358
1359   if (MEM_P (src) && !MEM_P (dest))
1360     dest = src;
1361   if (MEM_P (dest))
1362     {
1363       /* (set (mem (pre_dec (reg sp))) (foo)) */
1364       src = XEXP (dest, 0);
1365       code = GET_CODE (src);
1366
1367       switch (code)
1368         {
1369         case PRE_MODIFY:
1370         case POST_MODIFY:
1371           if (XEXP (src, 0) == stack_pointer_rtx)
1372             {
1373               rtx val = XEXP (XEXP (src, 1), 1);
1374               /* We handle only adjustments by constant amount.  */
1375               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1376                           && CONST_INT_P (val));
1377               offset = -INTVAL (val);
1378               break;
1379             }
1380           return 0;
1381
1382         case PRE_DEC:
1383         case POST_DEC:
1384           if (XEXP (src, 0) == stack_pointer_rtx)
1385             {
1386               offset = GET_MODE_SIZE (GET_MODE (dest));
1387               break;
1388             }
1389           return 0;
1390
1391         case PRE_INC:
1392         case POST_INC:
1393           if (XEXP (src, 0) == stack_pointer_rtx)
1394             {
1395               offset = -GET_MODE_SIZE (GET_MODE (dest));
1396               break;
1397             }
1398           return 0;
1399
1400         default:
1401           return 0;
1402         }
1403     }
1404   else
1405     return 0;
1406
1407   return offset;
1408 }
1409
1410 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1411    indexed by INSN_UID.  */
1412
1413 static HOST_WIDE_INT *barrier_args_size;
1414
1415 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1416
1417 static HOST_WIDE_INT
1418 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1419                              VEC (rtx, heap) **next)
1420 {
1421   HOST_WIDE_INT offset = 0;
1422   int i;
1423
1424   if (! RTX_FRAME_RELATED_P (insn))
1425     {
1426       if (prologue_epilogue_contains (insn))
1427         /* Nothing */;
1428       else if (GET_CODE (PATTERN (insn)) == SET)
1429         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1430       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1431                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1432         {
1433           /* There may be stack adjustments inside compound insns.  Search
1434              for them.  */
1435           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1436             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1437               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1438                                              cur_args_size, offset);
1439         }
1440     }
1441   else
1442     {
1443       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1444
1445       if (expr)
1446         {
1447           expr = XEXP (expr, 0);
1448           if (GET_CODE (expr) == PARALLEL
1449               || GET_CODE (expr) == SEQUENCE)
1450             for (i = 1; i < XVECLEN (expr, 0); i++)
1451               {
1452                 rtx elem = XVECEXP (expr, 0, i);
1453
1454                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1455                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1456               }
1457         }
1458     }
1459
1460 #ifndef STACK_GROWS_DOWNWARD
1461   offset = -offset;
1462 #endif
1463
1464   cur_args_size += offset;
1465   if (cur_args_size < 0)
1466     cur_args_size = 0;
1467
1468   if (JUMP_P (insn))
1469     {
1470       rtx dest = JUMP_LABEL (insn);
1471
1472       if (dest)
1473         {
1474           if (barrier_args_size [INSN_UID (dest)] < 0)
1475             {
1476               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1477               VEC_safe_push (rtx, heap, *next, dest);
1478             }
1479         }
1480     }
1481
1482   return cur_args_size;
1483 }
1484
1485 /* Walk the whole function and compute args_size on BARRIERs.  */
1486
1487 static void
1488 compute_barrier_args_size (void)
1489 {
1490   int max_uid = get_max_uid (), i;
1491   rtx insn;
1492   VEC (rtx, heap) *worklist, *next, *tmp;
1493
1494   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1495   for (i = 0; i < max_uid; i++)
1496     barrier_args_size[i] = -1;
1497
1498   worklist = VEC_alloc (rtx, heap, 20);
1499   next = VEC_alloc (rtx, heap, 20);
1500   insn = get_insns ();
1501   barrier_args_size[INSN_UID (insn)] = 0;
1502   VEC_quick_push (rtx, worklist, insn);
1503   for (;;)
1504     {
1505       while (!VEC_empty (rtx, worklist))
1506         {
1507           rtx prev, body, first_insn;
1508           HOST_WIDE_INT cur_args_size;
1509
1510           first_insn = insn = VEC_pop (rtx, worklist);
1511           cur_args_size = barrier_args_size[INSN_UID (insn)];
1512           prev = prev_nonnote_insn (insn);
1513           if (prev && BARRIER_P (prev))
1514             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1515
1516           for (; insn; insn = NEXT_INSN (insn))
1517             {
1518               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1519                 continue;
1520               if (BARRIER_P (insn))
1521                 break;
1522
1523               if (LABEL_P (insn))
1524                 {
1525                   if (insn == first_insn)
1526                     continue;
1527                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1528                     {
1529                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1530                       continue;
1531                     }
1532                   else
1533                     {
1534                       /* The insns starting with this label have been
1535                          already scanned or are in the worklist.  */
1536                       break;
1537                     }
1538                 }
1539
1540               body = PATTERN (insn);
1541               if (GET_CODE (body) == SEQUENCE)
1542                 {
1543                   HOST_WIDE_INT dest_args_size = cur_args_size;
1544                   for (i = 1; i < XVECLEN (body, 0); i++)
1545                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1546                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1547                       dest_args_size
1548                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1549                                                        dest_args_size, &next);
1550                     else
1551                       cur_args_size
1552                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1553                                                        cur_args_size, &next);
1554
1555                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1556                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1557                                                  dest_args_size, &next);
1558                   else
1559                     cur_args_size
1560                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1561                                                      cur_args_size, &next);
1562                 }
1563               else
1564                 cur_args_size
1565                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1566             }
1567         }
1568
1569       if (VEC_empty (rtx, next))
1570         break;
1571
1572       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1573       tmp = next;
1574       next = worklist;
1575       worklist = tmp;
1576       VEC_truncate (rtx, next, 0);
1577     }
1578
1579   VEC_free (rtx, heap, worklist);
1580   VEC_free (rtx, heap, next);
1581 }
1582
1583 /* Add a CFI to update the running total of the size of arguments
1584    pushed onto the stack.  */
1585
1586 static void
1587 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1588 {
1589   dw_cfi_ref cfi;
1590
1591   if (size == old_args_size)
1592     return;
1593
1594   old_args_size = size;
1595
1596   cfi = new_cfi ();
1597   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1598   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1599   add_fde_cfi (label, cfi);
1600 }
1601
1602 /* Record a stack adjustment of OFFSET bytes.  */
1603
1604 static void
1605 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1606 {
1607   if (cfa.reg == STACK_POINTER_REGNUM)
1608     cfa.offset += offset;
1609
1610   if (cfa_store.reg == STACK_POINTER_REGNUM)
1611     cfa_store.offset += offset;
1612
1613   if (ACCUMULATE_OUTGOING_ARGS)
1614     return;
1615
1616 #ifndef STACK_GROWS_DOWNWARD
1617   offset = -offset;
1618 #endif
1619
1620   args_size += offset;
1621   if (args_size < 0)
1622     args_size = 0;
1623
1624   def_cfa_1 (label, &cfa);
1625   if (flag_asynchronous_unwind_tables)
1626     dwarf2out_args_size (label, args_size);
1627 }
1628
1629 /* Check INSN to see if it looks like a push or a stack adjustment, and
1630    make a note of it if it does.  EH uses this information to find out
1631    how much extra space it needs to pop off the stack.  */
1632
1633 static void
1634 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1635 {
1636   HOST_WIDE_INT offset;
1637   const char *label;
1638   int i;
1639
1640   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1641      with this function.  Proper support would require all frame-related
1642      insns to be marked, and to be able to handle saving state around
1643      epilogues textually in the middle of the function.  */
1644   if (prologue_epilogue_contains (insn))
1645     return;
1646
1647   /* If INSN is an instruction from target of an annulled branch, the
1648      effects are for the target only and so current argument size
1649      shouldn't change at all.  */
1650   if (final_sequence
1651       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1652       && INSN_FROM_TARGET_P (insn))
1653     return;
1654
1655   /* If only calls can throw, and we have a frame pointer,
1656      save up adjustments until we see the CALL_INSN.  */
1657   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1658     {
1659       if (CALL_P (insn) && !after_p)
1660         {
1661           /* Extract the size of the args from the CALL rtx itself.  */
1662           insn = PATTERN (insn);
1663           if (GET_CODE (insn) == PARALLEL)
1664             insn = XVECEXP (insn, 0, 0);
1665           if (GET_CODE (insn) == SET)
1666             insn = SET_SRC (insn);
1667           gcc_assert (GET_CODE (insn) == CALL);
1668           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1669         }
1670       return;
1671     }
1672
1673   if (CALL_P (insn) && !after_p)
1674     {
1675       if (!flag_asynchronous_unwind_tables)
1676         dwarf2out_args_size ("", args_size);
1677       return;
1678     }
1679   else if (BARRIER_P (insn))
1680     {
1681       /* Don't call compute_barrier_args_size () if the only
1682          BARRIER is at the end of function.  */
1683       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1684         compute_barrier_args_size ();
1685       if (barrier_args_size == NULL)
1686         offset = 0;
1687       else
1688         {
1689           offset = barrier_args_size[INSN_UID (insn)];
1690           if (offset < 0)
1691             offset = 0;
1692         }
1693
1694       offset -= args_size;
1695 #ifndef STACK_GROWS_DOWNWARD
1696       offset = -offset;
1697 #endif
1698     }
1699   else if (GET_CODE (PATTERN (insn)) == SET)
1700     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1701   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1702            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1703     {
1704       /* There may be stack adjustments inside compound insns.  Search
1705          for them.  */
1706       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1707         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1708           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1709                                          args_size, offset);
1710     }
1711   else
1712     return;
1713
1714   if (offset == 0)
1715     return;
1716
1717   label = dwarf2out_cfi_label (false);
1718   dwarf2out_stack_adjust (offset, label);
1719 }
1720
1721 /* We delay emitting a register save until either (a) we reach the end
1722    of the prologue or (b) the register is clobbered.  This clusters
1723    register saves so that there are fewer pc advances.  */
1724
1725 struct GTY(()) queued_reg_save {
1726   struct queued_reg_save *next;
1727   rtx reg;
1728   HOST_WIDE_INT cfa_offset;
1729   rtx saved_reg;
1730 };
1731
1732 static GTY(()) struct queued_reg_save *queued_reg_saves;
1733
1734 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1735 typedef struct GTY(()) reg_saved_in_data {
1736   rtx orig_reg;
1737   rtx saved_in_reg;
1738 } reg_saved_in_data;
1739
1740 DEF_VEC_O (reg_saved_in_data);
1741 DEF_VEC_ALLOC_O (reg_saved_in_data, gc);
1742
1743 /* A set of registers saved in other registers.  This is implemented as
1744    a flat array because it normally contains zero or 1 entry, depending
1745    on the target.  IA-64 is the big spender here, using a maximum of
1746    5 entries.  */
1747 static GTY(()) VEC(reg_saved_in_data, gc) *regs_saved_in_regs;
1748
1749 /* Compare X and Y for equivalence.  The inputs may be REGs or PC_RTX.  */
1750
1751 static bool
1752 compare_reg_or_pc (rtx x, rtx y)
1753 {
1754   if (REG_P (x) && REG_P (y))
1755     return REGNO (x) == REGNO (y);
1756   return x == y;
1757 }
1758
1759 /* Record SRC as being saved in DEST.  DEST may be null to delete an
1760    existing entry.  SRC may be a register or PC_RTX.  */
1761
1762 static void
1763 record_reg_saved_in_reg (rtx dest, rtx src)
1764 {
1765   reg_saved_in_data *elt;
1766   size_t i;
1767
1768   FOR_EACH_VEC_ELT (reg_saved_in_data, regs_saved_in_regs, i, elt)
1769     if (compare_reg_or_pc (elt->orig_reg, src))
1770       {
1771         if (dest == NULL)
1772           VEC_unordered_remove(reg_saved_in_data, regs_saved_in_regs, i);
1773         else
1774           elt->saved_in_reg = dest;
1775         return;
1776       }
1777
1778   if (dest == NULL)
1779     return;
1780
1781   elt = VEC_safe_push(reg_saved_in_data, gc, regs_saved_in_regs, NULL);
1782   elt->orig_reg = src;
1783   elt->saved_in_reg = dest;
1784 }
1785
1786 static const char *last_reg_save_label;
1787
1788 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1789    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1790
1791 static void
1792 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1793 {
1794   struct queued_reg_save *q;
1795
1796   /* Duplicates waste space, but it's also necessary to remove them
1797      for correctness, since the queue gets output in reverse
1798      order.  */
1799   for (q = queued_reg_saves; q != NULL; q = q->next)
1800     if (REGNO (q->reg) == REGNO (reg))
1801       break;
1802
1803   if (q == NULL)
1804     {
1805       q = ggc_alloc_queued_reg_save ();
1806       q->next = queued_reg_saves;
1807       queued_reg_saves = q;
1808     }
1809
1810   q->reg = reg;
1811   q->cfa_offset = offset;
1812   q->saved_reg = sreg;
1813
1814   last_reg_save_label = label;
1815 }
1816
1817 /* Output all the entries in QUEUED_REG_SAVES.  */
1818
1819 void
1820 dwarf2out_flush_queued_reg_saves (void)
1821 {
1822   struct queued_reg_save *q;
1823
1824   for (q = queued_reg_saves; q; q = q->next)
1825     {
1826       unsigned int reg, sreg;
1827
1828       record_reg_saved_in_reg (q->saved_reg, q->reg);
1829
1830       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1831       if (q->saved_reg)
1832         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1833       else
1834         sreg = INVALID_REGNUM;
1835       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1836     }
1837
1838   queued_reg_saves = NULL;
1839   last_reg_save_label = NULL;
1840 }
1841
1842 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1843    location for?  Or, does it clobber a register which we've previously
1844    said that some other register is saved in, and for which we now
1845    have a new location for?  */
1846
1847 static bool
1848 clobbers_queued_reg_save (const_rtx insn)
1849 {
1850   struct queued_reg_save *q;
1851
1852   for (q = queued_reg_saves; q; q = q->next)
1853     {
1854       size_t i;
1855       reg_saved_in_data *rir;
1856
1857       if (modified_in_p (q->reg, insn))
1858         return true;
1859
1860       FOR_EACH_VEC_ELT (reg_saved_in_data, regs_saved_in_regs, i, rir)
1861         if (compare_reg_or_pc (q->reg, rir->orig_reg)
1862             && modified_in_p (rir->saved_in_reg, insn))
1863           return true;
1864     }
1865
1866   return false;
1867 }
1868
1869 /* Entry point for saving the first register into the second.  */
1870
1871 void
1872 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1873 {
1874   unsigned int regno, sregno;
1875
1876   record_reg_saved_in_reg (sreg, reg);
1877
1878   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1879   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1880   reg_save (label, regno, sregno, 0);
1881 }
1882
1883 /* What register, if any, is currently saved in REG?  */
1884
1885 static rtx
1886 reg_saved_in (rtx reg)
1887 {
1888   unsigned int regn = REGNO (reg);
1889   struct queued_reg_save *q;
1890   reg_saved_in_data *rir;
1891   size_t i;
1892
1893   for (q = queued_reg_saves; q; q = q->next)
1894     if (q->saved_reg && regn == REGNO (q->saved_reg))
1895       return q->reg;
1896
1897   FOR_EACH_VEC_ELT (reg_saved_in_data, regs_saved_in_regs, i, rir)
1898     if (regn == REGNO (rir->saved_in_reg))
1899       return rir->orig_reg;
1900
1901   return NULL_RTX;
1902 }
1903
1904
1905 /* A temporary register holding an integral value used in adjusting SP
1906    or setting up the store_reg.  The "offset" field holds the integer
1907    value, not an offset.  */
1908 static dw_cfa_location cfa_temp;
1909
1910 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1911
1912 static void
1913 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1914 {
1915   memset (&cfa, 0, sizeof (cfa));
1916
1917   switch (GET_CODE (pat))
1918     {
1919     case PLUS:
1920       cfa.reg = REGNO (XEXP (pat, 0));
1921       cfa.offset = INTVAL (XEXP (pat, 1));
1922       break;
1923
1924     case REG:
1925       cfa.reg = REGNO (pat);
1926       break;
1927
1928     case MEM:
1929       cfa.indirect = 1;
1930       pat = XEXP (pat, 0);
1931       if (GET_CODE (pat) == PLUS)
1932         {
1933           cfa.base_offset = INTVAL (XEXP (pat, 1));
1934           pat = XEXP (pat, 0);
1935         }
1936       cfa.reg = REGNO (pat);
1937       break;
1938
1939     default:
1940       /* Recurse and define an expression.  */
1941       gcc_unreachable ();
1942     }
1943
1944   def_cfa_1 (label, &cfa);
1945 }
1946
1947 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1948
1949 static void
1950 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1951 {
1952   rtx src, dest;
1953
1954   gcc_assert (GET_CODE (pat) == SET);
1955   dest = XEXP (pat, 0);
1956   src = XEXP (pat, 1);
1957
1958   switch (GET_CODE (src))
1959     {
1960     case PLUS:
1961       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1962       cfa.offset -= INTVAL (XEXP (src, 1));
1963       break;
1964
1965     case REG:
1966         break;
1967
1968     default:
1969         gcc_unreachable ();
1970     }
1971
1972   cfa.reg = REGNO (dest);
1973   gcc_assert (cfa.indirect == 0);
1974
1975   def_cfa_1 (label, &cfa);
1976 }
1977
1978 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1979
1980 static void
1981 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1982 {
1983   HOST_WIDE_INT offset;
1984   rtx src, addr, span;
1985   unsigned int sregno;
1986
1987   src = XEXP (set, 1);
1988   addr = XEXP (set, 0);
1989   gcc_assert (MEM_P (addr));
1990   addr = XEXP (addr, 0);
1991
1992   /* As documented, only consider extremely simple addresses.  */
1993   switch (GET_CODE (addr))
1994     {
1995     case REG:
1996       gcc_assert (REGNO (addr) == cfa.reg);
1997       offset = -cfa.offset;
1998       break;
1999     case PLUS:
2000       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
2001       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
2002       break;
2003     default:
2004       gcc_unreachable ();
2005     }
2006
2007   if (src == pc_rtx)
2008     {
2009       span = NULL;
2010       sregno = DWARF_FRAME_RETURN_COLUMN;
2011     }
2012   else 
2013     {
2014       span = targetm.dwarf_register_span (src);
2015       sregno = DWARF_FRAME_REGNUM (REGNO (src));
2016     }
2017
2018   /* ??? We'd like to use queue_reg_save, but we need to come up with
2019      a different flushing heuristic for epilogues.  */
2020   if (!span)
2021     reg_save (label, sregno, INVALID_REGNUM, offset);
2022   else
2023     {
2024       /* We have a PARALLEL describing where the contents of SRC live.
2025          Queue register saves for each piece of the PARALLEL.  */
2026       int par_index;
2027       int limit;
2028       HOST_WIDE_INT span_offset = offset;
2029
2030       gcc_assert (GET_CODE (span) == PARALLEL);
2031
2032       limit = XVECLEN (span, 0);
2033       for (par_index = 0; par_index < limit; par_index++)
2034         {
2035           rtx elem = XVECEXP (span, 0, par_index);
2036
2037           sregno = DWARF_FRAME_REGNUM (REGNO (src));
2038           reg_save (label, sregno, INVALID_REGNUM, span_offset);
2039           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2040         }
2041     }
2042 }
2043
2044 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2045
2046 static void
2047 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2048 {
2049   rtx src, dest;
2050   unsigned sregno, dregno;
2051
2052   src = XEXP (set, 1);
2053   dest = XEXP (set, 0);
2054
2055   if (src == pc_rtx)
2056     sregno = DWARF_FRAME_RETURN_COLUMN;
2057   else
2058     {
2059       record_reg_saved_in_reg (dest, src);
2060       sregno = DWARF_FRAME_REGNUM (REGNO (src));
2061     }
2062
2063   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2064
2065   /* ??? We'd like to use queue_reg_save, but we need to come up with
2066      a different flushing heuristic for epilogues.  */
2067   reg_save (label, sregno, dregno, 0);
2068 }
2069
2070 /* Helper function to get mode of MEM's address.  */
2071
2072 static inline enum machine_mode
2073 get_address_mode (rtx mem)
2074 {
2075   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
2076   if (mode != VOIDmode)
2077     return mode;
2078   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
2079 }
2080
2081 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2082
2083 static void
2084 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2085 {
2086   rtx src, dest, span;
2087   dw_cfi_ref cfi = new_cfi ();
2088
2089   dest = SET_DEST (set);
2090   src = SET_SRC (set);
2091
2092   gcc_assert (REG_P (src));
2093   gcc_assert (MEM_P (dest));
2094
2095   span = targetm.dwarf_register_span (src);
2096   gcc_assert (!span);
2097
2098   cfi->dw_cfi_opc = DW_CFA_expression;
2099   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2100   cfi->dw_cfi_oprnd2.dw_cfi_loc
2101     = mem_loc_descriptor (XEXP (dest, 0), get_address_mode (dest),
2102                           GET_MODE (dest), VAR_INIT_STATUS_INITIALIZED);
2103
2104   /* ??? We'd like to use queue_reg_save, were the interface different,
2105      and, as above, we could manage flushing for epilogues.  */
2106   add_fde_cfi (label, cfi);
2107 }
2108
2109 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2110
2111 static void
2112 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2113 {
2114   dw_cfi_ref cfi = new_cfi ();
2115   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2116
2117   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2118   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2119
2120   add_fde_cfi (label, cfi);
2121 }
2122
2123 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_WINDOW_SAVE.
2124    ??? Perhaps we should note in the CIE where windows are saved (instead of
2125    assuming 0(cfa)) and what registers are in the window.  */
2126
2127 static void
2128 dwarf2out_frame_debug_cfa_window_save (const char *label)
2129 {
2130   dw_cfi_ref cfi = new_cfi ();
2131
2132   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
2133   add_fde_cfi (label, cfi);
2134 }
2135
2136 /* Record call frame debugging information for an expression EXPR,
2137    which either sets SP or FP (adjusting how we calculate the frame
2138    address) or saves a register to the stack or another register.
2139    LABEL indicates the address of EXPR.
2140
2141    This function encodes a state machine mapping rtxes to actions on
2142    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2143    users need not read the source code.
2144
2145   The High-Level Picture
2146
2147   Changes in the register we use to calculate the CFA: Currently we
2148   assume that if you copy the CFA register into another register, we
2149   should take the other one as the new CFA register; this seems to
2150   work pretty well.  If it's wrong for some target, it's simple
2151   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2152
2153   Changes in the register we use for saving registers to the stack:
2154   This is usually SP, but not always.  Again, we deduce that if you
2155   copy SP into another register (and SP is not the CFA register),
2156   then the new register is the one we will be using for register
2157   saves.  This also seems to work.
2158
2159   Register saves: There's not much guesswork about this one; if
2160   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2161   register save, and the register used to calculate the destination
2162   had better be the one we think we're using for this purpose.
2163   It's also assumed that a copy from a call-saved register to another
2164   register is saving that register if RTX_FRAME_RELATED_P is set on
2165   that instruction.  If the copy is from a call-saved register to
2166   the *same* register, that means that the register is now the same
2167   value as in the caller.
2168
2169   Except: If the register being saved is the CFA register, and the
2170   offset is nonzero, we are saving the CFA, so we assume we have to
2171   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2172   the intent is to save the value of SP from the previous frame.
2173
2174   In addition, if a register has previously been saved to a different
2175   register,
2176
2177   Invariants / Summaries of Rules
2178
2179   cfa          current rule for calculating the CFA.  It usually
2180                consists of a register and an offset.
2181   cfa_store    register used by prologue code to save things to the stack
2182                cfa_store.offset is the offset from the value of
2183                cfa_store.reg to the actual CFA
2184   cfa_temp     register holding an integral value.  cfa_temp.offset
2185                stores the value, which will be used to adjust the
2186                stack pointer.  cfa_temp is also used like cfa_store,
2187                to track stores to the stack via fp or a temp reg.
2188
2189   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2190                with cfa.reg as the first operand changes the cfa.reg and its
2191                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2192                cfa_temp.offset.
2193
2194   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2195                expression yielding a constant.  This sets cfa_temp.reg
2196                and cfa_temp.offset.
2197
2198   Rule 5:      Create a new register cfa_store used to save items to the
2199                stack.
2200
2201   Rules 10-14: Save a register to the stack.  Define offset as the
2202                difference of the original location and cfa_store's
2203                location (or cfa_temp's location if cfa_temp is used).
2204
2205   Rules 16-20: If AND operation happens on sp in prologue, we assume
2206                stack is realigned.  We will use a group of DW_OP_XXX
2207                expressions to represent the location of the stored
2208                register instead of CFA+offset.
2209
2210   The Rules
2211
2212   "{a,b}" indicates a choice of a xor b.
2213   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2214
2215   Rule 1:
2216   (set <reg1> <reg2>:cfa.reg)
2217   effects: cfa.reg = <reg1>
2218            cfa.offset unchanged
2219            cfa_temp.reg = <reg1>
2220            cfa_temp.offset = cfa.offset
2221
2222   Rule 2:
2223   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2224                               {<const_int>,<reg>:cfa_temp.reg}))
2225   effects: cfa.reg = sp if fp used
2226            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2227            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2228              if cfa_store.reg==sp
2229
2230   Rule 3:
2231   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2232   effects: cfa.reg = fp
2233            cfa_offset += +/- <const_int>
2234
2235   Rule 4:
2236   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2237   constraints: <reg1> != fp
2238                <reg1> != sp
2239   effects: cfa.reg = <reg1>
2240            cfa_temp.reg = <reg1>
2241            cfa_temp.offset = cfa.offset
2242
2243   Rule 5:
2244   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2245   constraints: <reg1> != fp
2246                <reg1> != sp
2247   effects: cfa_store.reg = <reg1>
2248            cfa_store.offset = cfa.offset - cfa_temp.offset
2249
2250   Rule 6:
2251   (set <reg> <const_int>)
2252   effects: cfa_temp.reg = <reg>
2253            cfa_temp.offset = <const_int>
2254
2255   Rule 7:
2256   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2257   effects: cfa_temp.reg = <reg1>
2258            cfa_temp.offset |= <const_int>
2259
2260   Rule 8:
2261   (set <reg> (high <exp>))
2262   effects: none
2263
2264   Rule 9:
2265   (set <reg> (lo_sum <exp> <const_int>))
2266   effects: cfa_temp.reg = <reg>
2267            cfa_temp.offset = <const_int>
2268
2269   Rule 10:
2270   (set (mem ({pre,post}_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2271   effects: cfa_store.offset -= <const_int>
2272            cfa.offset = cfa_store.offset if cfa.reg == sp
2273            cfa.reg = sp
2274            cfa.base_offset = -cfa_store.offset
2275
2276   Rule 11:
2277   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2278   effects: cfa_store.offset += -/+ mode_size(mem)
2279            cfa.offset = cfa_store.offset if cfa.reg == sp
2280            cfa.reg = sp
2281            cfa.base_offset = -cfa_store.offset
2282
2283   Rule 12:
2284   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2285
2286        <reg2>)
2287   effects: cfa.reg = <reg1>
2288            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2289
2290   Rule 13:
2291   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2292   effects: cfa.reg = <reg1>
2293            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2294
2295   Rule 14:
2296   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2297   effects: cfa.reg = <reg1>
2298            cfa.base_offset = -cfa_temp.offset
2299            cfa_temp.offset -= mode_size(mem)
2300
2301   Rule 15:
2302   (set <reg> {unspec, unspec_volatile})
2303   effects: target-dependent
2304
2305   Rule 16:
2306   (set sp (and: sp <const_int>))
2307   constraints: cfa_store.reg == sp
2308   effects: current_fde.stack_realign = 1
2309            cfa_store.offset = 0
2310            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2311
2312   Rule 17:
2313   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2314   effects: cfa_store.offset += -/+ mode_size(mem)
2315
2316   Rule 18:
2317   (set (mem ({pre_inc, pre_dec} sp)) fp)
2318   constraints: fde->stack_realign == 1
2319   effects: cfa_store.offset = 0
2320            cfa.reg != HARD_FRAME_POINTER_REGNUM
2321
2322   Rule 19:
2323   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2324   constraints: fde->stack_realign == 1
2325                && cfa.offset == 0
2326                && cfa.indirect == 0
2327                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2328   effects: Use DW_CFA_def_cfa_expression to define cfa
2329            cfa.reg == fde->drap_reg  */
2330
2331 static void
2332 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2333 {
2334   rtx src, dest, span;
2335   HOST_WIDE_INT offset;
2336   dw_fde_ref fde;
2337
2338   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2339      the PARALLEL independently. The first element is always processed if
2340      it is a SET. This is for backward compatibility.   Other elements
2341      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2342      flag is set in them.  */
2343   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2344     {
2345       int par_index;
2346       int limit = XVECLEN (expr, 0);
2347       rtx elem;
2348
2349       /* PARALLELs have strict read-modify-write semantics, so we
2350          ought to evaluate every rvalue before changing any lvalue.
2351          It's cumbersome to do that in general, but there's an
2352          easy approximation that is enough for all current users:
2353          handle register saves before register assignments.  */
2354       if (GET_CODE (expr) == PARALLEL)
2355         for (par_index = 0; par_index < limit; par_index++)
2356           {
2357             elem = XVECEXP (expr, 0, par_index);
2358             if (GET_CODE (elem) == SET
2359                 && MEM_P (SET_DEST (elem))
2360                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2361               dwarf2out_frame_debug_expr (elem, label);
2362           }
2363
2364       for (par_index = 0; par_index < limit; par_index++)
2365         {
2366           elem = XVECEXP (expr, 0, par_index);
2367           if (GET_CODE (elem) == SET
2368               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2369               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2370             dwarf2out_frame_debug_expr (elem, label);
2371           else if (GET_CODE (elem) == SET
2372                    && par_index != 0
2373                    && !RTX_FRAME_RELATED_P (elem))
2374             {
2375               /* Stack adjustment combining might combine some post-prologue
2376                  stack adjustment into a prologue stack adjustment.  */
2377               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2378
2379               if (offset != 0)
2380                 dwarf2out_stack_adjust (offset, label);
2381             }
2382         }
2383       return;
2384     }
2385
2386   gcc_assert (GET_CODE (expr) == SET);
2387
2388   src = SET_SRC (expr);
2389   dest = SET_DEST (expr);
2390
2391   if (REG_P (src))
2392     {
2393       rtx rsi = reg_saved_in (src);
2394       if (rsi)
2395         src = rsi;
2396     }
2397
2398   fde = current_fde ();
2399
2400   switch (GET_CODE (dest))
2401     {
2402     case REG:
2403       switch (GET_CODE (src))
2404         {
2405           /* Setting FP from SP.  */
2406         case REG:
2407           if (cfa.reg == (unsigned) REGNO (src))
2408             {
2409               /* Rule 1 */
2410               /* Update the CFA rule wrt SP or FP.  Make sure src is
2411                  relative to the current CFA register.
2412
2413                  We used to require that dest be either SP or FP, but the
2414                  ARM copies SP to a temporary register, and from there to
2415                  FP.  So we just rely on the backends to only set
2416                  RTX_FRAME_RELATED_P on appropriate insns.  */
2417               cfa.reg = REGNO (dest);
2418               cfa_temp.reg = cfa.reg;
2419               cfa_temp.offset = cfa.offset;
2420             }
2421           else
2422             {
2423               /* Saving a register in a register.  */
2424               gcc_assert (!fixed_regs [REGNO (dest)]
2425                           /* For the SPARC and its register window.  */
2426                           || (DWARF_FRAME_REGNUM (REGNO (src))
2427                               == DWARF_FRAME_RETURN_COLUMN));
2428
2429               /* After stack is aligned, we can only save SP in FP
2430                  if drap register is used.  In this case, we have
2431                  to restore stack pointer with the CFA value and we
2432                  don't generate this DWARF information.  */
2433               if (fde
2434                   && fde->stack_realign
2435                   && REGNO (src) == STACK_POINTER_REGNUM)
2436                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2437                             && fde->drap_reg != INVALID_REGNUM
2438                             && cfa.reg != REGNO (src));
2439               else
2440                 queue_reg_save (label, src, dest, 0);
2441             }
2442           break;
2443
2444         case PLUS:
2445         case MINUS:
2446         case LO_SUM:
2447           if (dest == stack_pointer_rtx)
2448             {
2449               /* Rule 2 */
2450               /* Adjusting SP.  */
2451               switch (GET_CODE (XEXP (src, 1)))
2452                 {
2453                 case CONST_INT:
2454                   offset = INTVAL (XEXP (src, 1));
2455                   break;
2456                 case REG:
2457                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2458                               == cfa_temp.reg);
2459                   offset = cfa_temp.offset;
2460                   break;
2461                 default:
2462                   gcc_unreachable ();
2463                 }
2464
2465               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2466                 {
2467                   /* Restoring SP from FP in the epilogue.  */
2468                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2469                   cfa.reg = STACK_POINTER_REGNUM;
2470                 }
2471               else if (GET_CODE (src) == LO_SUM)
2472                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2473                 ;
2474               else
2475                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2476
2477               if (GET_CODE (src) != MINUS)
2478                 offset = -offset;
2479               if (cfa.reg == STACK_POINTER_REGNUM)
2480                 cfa.offset += offset;
2481               if (cfa_store.reg == STACK_POINTER_REGNUM)
2482                 cfa_store.offset += offset;
2483             }
2484           else if (dest == hard_frame_pointer_rtx)
2485             {
2486               /* Rule 3 */
2487               /* Either setting the FP from an offset of the SP,
2488                  or adjusting the FP */
2489               gcc_assert (frame_pointer_needed);
2490
2491               gcc_assert (REG_P (XEXP (src, 0))
2492                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2493                           && CONST_INT_P (XEXP (src, 1)));
2494               offset = INTVAL (XEXP (src, 1));
2495               if (GET_CODE (src) != MINUS)
2496                 offset = -offset;
2497               cfa.offset += offset;
2498               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2499             }
2500           else
2501             {
2502               gcc_assert (GET_CODE (src) != MINUS);
2503
2504               /* Rule 4 */
2505               if (REG_P (XEXP (src, 0))
2506                   && REGNO (XEXP (src, 0)) == cfa.reg
2507                   && CONST_INT_P (XEXP (src, 1)))
2508                 {
2509                   /* Setting a temporary CFA register that will be copied
2510                      into the FP later on.  */
2511                   offset = - INTVAL (XEXP (src, 1));
2512                   cfa.offset += offset;
2513                   cfa.reg = REGNO (dest);
2514                   /* Or used to save regs to the stack.  */
2515                   cfa_temp.reg = cfa.reg;
2516                   cfa_temp.offset = cfa.offset;
2517                 }
2518
2519               /* Rule 5 */
2520               else if (REG_P (XEXP (src, 0))
2521                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2522                        && XEXP (src, 1) == stack_pointer_rtx)
2523                 {
2524                   /* Setting a scratch register that we will use instead
2525                      of SP for saving registers to the stack.  */
2526                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2527                   cfa_store.reg = REGNO (dest);
2528                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2529                 }
2530
2531               /* Rule 9 */
2532               else if (GET_CODE (src) == LO_SUM
2533                        && CONST_INT_P (XEXP (src, 1)))
2534                 {
2535                   cfa_temp.reg = REGNO (dest);
2536                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2537                 }
2538               else
2539                 gcc_unreachable ();
2540             }
2541           break;
2542
2543           /* Rule 6 */
2544         case CONST_INT:
2545           cfa_temp.reg = REGNO (dest);
2546           cfa_temp.offset = INTVAL (src);
2547           break;
2548
2549           /* Rule 7 */
2550         case IOR:
2551           gcc_assert (REG_P (XEXP (src, 0))
2552                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2553                       && CONST_INT_P (XEXP (src, 1)));
2554
2555           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2556             cfa_temp.reg = REGNO (dest);
2557           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2558           break;
2559
2560           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2561              which will fill in all of the bits.  */
2562           /* Rule 8 */
2563         case HIGH:
2564           break;
2565
2566           /* Rule 15 */
2567         case UNSPEC:
2568         case UNSPEC_VOLATILE:
2569           gcc_assert (targetm.dwarf_handle_frame_unspec);
2570           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2571           return;
2572
2573           /* Rule 16 */
2574         case AND:
2575           /* If this AND operation happens on stack pointer in prologue,
2576              we assume the stack is realigned and we extract the
2577              alignment.  */
2578           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2579             {
2580               /* We interpret reg_save differently with stack_realign set.
2581                  Thus we must flush whatever we have queued first.  */
2582               dwarf2out_flush_queued_reg_saves ();
2583
2584               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2585               fde->stack_realign = 1;
2586               fde->stack_realignment = INTVAL (XEXP (src, 1));
2587               cfa_store.offset = 0;
2588
2589               if (cfa.reg != STACK_POINTER_REGNUM
2590                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2591                 fde->drap_reg = cfa.reg;
2592             }
2593           return;
2594
2595         default:
2596           gcc_unreachable ();
2597         }
2598
2599       def_cfa_1 (label, &cfa);
2600       break;
2601
2602     case MEM:
2603
2604       /* Saving a register to the stack.  Make sure dest is relative to the
2605          CFA register.  */
2606       switch (GET_CODE (XEXP (dest, 0)))
2607         {
2608           /* Rule 10 */
2609           /* With a push.  */
2610         case PRE_MODIFY:
2611         case POST_MODIFY:
2612           /* We can't handle variable size modifications.  */
2613           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2614                       == CONST_INT);
2615           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2616
2617           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2618                       && cfa_store.reg == STACK_POINTER_REGNUM);
2619
2620           cfa_store.offset += offset;
2621           if (cfa.reg == STACK_POINTER_REGNUM)
2622             cfa.offset = cfa_store.offset;
2623
2624           if (GET_CODE (XEXP (dest, 0)) == POST_MODIFY)
2625             offset -= cfa_store.offset;
2626           else
2627             offset = -cfa_store.offset;
2628           break;
2629
2630           /* Rule 11 */
2631         case PRE_INC:
2632         case PRE_DEC:
2633         case POST_DEC:
2634           offset = GET_MODE_SIZE (GET_MODE (dest));
2635           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2636             offset = -offset;
2637
2638           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2639                        == STACK_POINTER_REGNUM)
2640                       && cfa_store.reg == STACK_POINTER_REGNUM);
2641
2642           cfa_store.offset += offset;
2643
2644           /* Rule 18: If stack is aligned, we will use FP as a
2645              reference to represent the address of the stored
2646              regiser.  */
2647           if (fde
2648               && fde->stack_realign
2649               && src == hard_frame_pointer_rtx)
2650             {
2651               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2652               cfa_store.offset = 0;
2653             }
2654
2655           if (cfa.reg == STACK_POINTER_REGNUM)
2656             cfa.offset = cfa_store.offset;
2657
2658           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2659             offset += -cfa_store.offset;
2660           else
2661             offset = -cfa_store.offset;
2662           break;
2663
2664           /* Rule 12 */
2665           /* With an offset.  */
2666         case PLUS:
2667         case MINUS:
2668         case LO_SUM:
2669           {
2670             int regno;
2671
2672             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2673                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2674             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2675             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2676               offset = -offset;
2677
2678             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2679
2680             if (cfa.reg == (unsigned) regno)
2681               offset -= cfa.offset;
2682             else if (cfa_store.reg == (unsigned) regno)
2683               offset -= cfa_store.offset;
2684             else
2685               {
2686                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2687                 offset -= cfa_temp.offset;
2688               }
2689           }
2690           break;
2691
2692           /* Rule 13 */
2693           /* Without an offset.  */
2694         case REG:
2695           {
2696             int regno = REGNO (XEXP (dest, 0));
2697
2698             if (cfa.reg == (unsigned) regno)
2699               offset = -cfa.offset;
2700             else if (cfa_store.reg == (unsigned) regno)
2701               offset = -cfa_store.offset;
2702             else
2703               {
2704                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2705                 offset = -cfa_temp.offset;
2706               }
2707           }
2708           break;
2709
2710           /* Rule 14 */
2711         case POST_INC:
2712           gcc_assert (cfa_temp.reg
2713                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2714           offset = -cfa_temp.offset;
2715           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2716           break;
2717
2718         default:
2719           gcc_unreachable ();
2720         }
2721
2722         /* Rule 17 */
2723         /* If the source operand of this MEM operation is not a
2724            register, basically the source is return address.  Here
2725            we only care how much stack grew and we don't save it.  */
2726       if (!REG_P (src))
2727         break;
2728
2729       if (REGNO (src) != STACK_POINTER_REGNUM
2730           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2731           && (unsigned) REGNO (src) == cfa.reg)
2732         {
2733           /* We're storing the current CFA reg into the stack.  */
2734
2735           if (cfa.offset == 0)
2736             {
2737               /* Rule 19 */
2738               /* If stack is aligned, putting CFA reg into stack means
2739                  we can no longer use reg + offset to represent CFA.
2740                  Here we use DW_CFA_def_cfa_expression instead.  The
2741                  result of this expression equals to the original CFA
2742                  value.  */
2743               if (fde
2744                   && fde->stack_realign
2745                   && cfa.indirect == 0
2746                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2747                 {
2748                   dw_cfa_location cfa_exp;
2749
2750                   gcc_assert (fde->drap_reg == cfa.reg);
2751
2752                   cfa_exp.indirect = 1;
2753                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2754                   cfa_exp.base_offset = offset;
2755                   cfa_exp.offset = 0;
2756
2757                   fde->drap_reg_saved = 1;
2758
2759                   def_cfa_1 (label, &cfa_exp);
2760                   break;
2761                 }
2762
2763               /* If the source register is exactly the CFA, assume
2764                  we're saving SP like any other register; this happens
2765                  on the ARM.  */
2766               def_cfa_1 (label, &cfa);
2767               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2768               break;
2769             }
2770           else
2771             {
2772               /* Otherwise, we'll need to look in the stack to
2773                  calculate the CFA.  */
2774               rtx x = XEXP (dest, 0);
2775
2776               if (!REG_P (x))
2777                 x = XEXP (x, 0);
2778               gcc_assert (REG_P (x));
2779
2780               cfa.reg = REGNO (x);
2781               cfa.base_offset = offset;
2782               cfa.indirect = 1;
2783               def_cfa_1 (label, &cfa);
2784               break;
2785             }
2786         }
2787
2788       def_cfa_1 (label, &cfa);
2789       {
2790         span = targetm.dwarf_register_span (src);
2791
2792         if (!span)
2793           queue_reg_save (label, src, NULL_RTX, offset);
2794         else
2795           {
2796             /* We have a PARALLEL describing where the contents of SRC
2797                live.  Queue register saves for each piece of the
2798                PARALLEL.  */
2799             int par_index;
2800             int limit;
2801             HOST_WIDE_INT span_offset = offset;
2802
2803             gcc_assert (GET_CODE (span) == PARALLEL);
2804
2805             limit = XVECLEN (span, 0);
2806             for (par_index = 0; par_index < limit; par_index++)
2807               {
2808                 rtx elem = XVECEXP (span, 0, par_index);
2809
2810                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2811                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2812               }
2813           }
2814       }
2815       break;
2816
2817     default:
2818       gcc_unreachable ();
2819     }
2820 }
2821
2822 /* Record call frame debugging information for INSN, which either
2823    sets SP or FP (adjusting how we calculate the frame address) or saves a
2824    register to the stack.  If INSN is NULL_RTX, initialize our state.
2825
2826    If AFTER_P is false, we're being called before the insn is emitted,
2827    otherwise after.  Call instructions get invoked twice.  */
2828
2829 void
2830 dwarf2out_frame_debug (rtx insn, bool after_p)
2831 {
2832   const char *label;
2833   rtx note, n;
2834   bool handled_one = false;
2835   bool need_flush = false;
2836
2837   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2838     dwarf2out_flush_queued_reg_saves ();
2839
2840   if (!RTX_FRAME_RELATED_P (insn))
2841     {
2842       /* ??? This should be done unconditionally since stack adjustments
2843          matter if the stack pointer is not the CFA register anymore but
2844          is still used to save registers.  */
2845       if (!ACCUMULATE_OUTGOING_ARGS)
2846         dwarf2out_notice_stack_adjust (insn, after_p);
2847       return;
2848     }
2849
2850   label = dwarf2out_cfi_label (false);
2851   any_cfis_emitted = false;
2852
2853   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2854     switch (REG_NOTE_KIND (note))
2855       {
2856       case REG_FRAME_RELATED_EXPR:
2857         insn = XEXP (note, 0);
2858         goto do_frame_expr;
2859
2860       case REG_CFA_DEF_CFA:
2861         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2862         handled_one = true;
2863         break;
2864
2865       case REG_CFA_ADJUST_CFA:
2866         n = XEXP (note, 0);
2867         if (n == NULL)
2868           {
2869             n = PATTERN (insn);
2870             if (GET_CODE (n) == PARALLEL)
2871               n = XVECEXP (n, 0, 0);
2872           }
2873         dwarf2out_frame_debug_adjust_cfa (n, label);
2874         handled_one = true;
2875         break;
2876
2877       case REG_CFA_OFFSET:
2878         n = XEXP (note, 0);
2879         if (n == NULL)
2880           n = single_set (insn);
2881         dwarf2out_frame_debug_cfa_offset (n, label);
2882         handled_one = true;
2883         break;
2884
2885       case REG_CFA_REGISTER:
2886         n = XEXP (note, 0);
2887         if (n == NULL)
2888           {
2889             n = PATTERN (insn);
2890             if (GET_CODE (n) == PARALLEL)
2891               n = XVECEXP (n, 0, 0);
2892           }
2893         dwarf2out_frame_debug_cfa_register (n, label);
2894         handled_one = true;
2895         break;
2896
2897       case REG_CFA_EXPRESSION:
2898         n = XEXP (note, 0);
2899         if (n == NULL)
2900           n = single_set (insn);
2901         dwarf2out_frame_debug_cfa_expression (n, label);
2902         handled_one = true;
2903         break;
2904
2905       case REG_CFA_RESTORE:
2906         n = XEXP (note, 0);
2907         if (n == NULL)
2908           {
2909             n = PATTERN (insn);
2910             if (GET_CODE (n) == PARALLEL)
2911               n = XVECEXP (n, 0, 0);
2912             n = XEXP (n, 0);
2913           }
2914         dwarf2out_frame_debug_cfa_restore (n, label);
2915         handled_one = true;
2916         break;
2917
2918       case REG_CFA_SET_VDRAP:
2919         n = XEXP (note, 0);
2920         if (REG_P (n))
2921           {
2922             dw_fde_ref fde = current_fde ();
2923             if (fde)
2924               {
2925                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2926                 if (REG_P (n))
2927                   fde->vdrap_reg = REGNO (n);
2928               }
2929           }
2930         handled_one = true;
2931         break;
2932
2933       case REG_CFA_WINDOW_SAVE:
2934         dwarf2out_frame_debug_cfa_window_save (label);
2935         handled_one = true;
2936         break;
2937
2938       case REG_CFA_FLUSH_QUEUE:
2939         /* The actual flush happens below.  */
2940         need_flush = true;
2941         handled_one = true;
2942         break;
2943
2944       default:
2945         break;
2946       }
2947
2948   if (handled_one)
2949     {
2950       /* Minimize the number of advances by emitting the entire queue
2951          once anything is emitted.  */
2952       need_flush |= any_cfis_emitted;
2953     }
2954   else
2955     {
2956       insn = PATTERN (insn);
2957     do_frame_expr:
2958       dwarf2out_frame_debug_expr (insn, label);
2959
2960       /* Check again.  A parallel can save and update the same register.
2961          We could probably check just once, here, but this is safer than
2962          removing the check at the start of the function.  */
2963       if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2964         need_flush = true;
2965     }
2966
2967   if (need_flush)
2968     dwarf2out_flush_queued_reg_saves ();
2969 }
2970
2971 /* Called once at the start of final to initialize some data for the
2972    current function.  */
2973 void
2974 dwarf2out_frame_debug_init (void)
2975 {
2976   /* Flush any queued register saves.  */
2977   dwarf2out_flush_queued_reg_saves ();
2978
2979   /* Set up state for generating call frame debug info.  */
2980   lookup_cfa (&cfa);
2981   gcc_assert (cfa.reg
2982               == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2983
2984   cfa.reg = STACK_POINTER_REGNUM;
2985   cfa_store = cfa;
2986   cfa_temp.reg = -1;
2987   cfa_temp.offset = 0;
2988
2989   regs_saved_in_regs = NULL;
2990
2991   if (barrier_args_size)
2992     {
2993       XDELETEVEC (barrier_args_size);
2994       barrier_args_size = NULL;
2995     }
2996 }
2997
2998 /* Determine if we need to save and restore CFI information around this
2999    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
3000    we do need to save/restore, then emit the save now, and insert a
3001    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
3002
3003 void
3004 dwarf2out_cfi_begin_epilogue (rtx insn)
3005 {
3006   bool saw_frp = false;
3007   rtx i;
3008
3009   /* Scan forward to the return insn, noticing if there are possible
3010      frame related insns.  */
3011   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
3012     {
3013       if (!INSN_P (i))
3014         continue;
3015
3016       /* Look for both regular and sibcalls to end the block.  */
3017       if (returnjump_p (i))
3018         break;
3019       if (CALL_P (i) && SIBLING_CALL_P (i))
3020         break;
3021
3022       if (GET_CODE (PATTERN (i)) == SEQUENCE)
3023         {
3024           int idx;
3025           rtx seq = PATTERN (i);
3026
3027           if (returnjump_p (XVECEXP (seq, 0, 0)))
3028             break;
3029           if (CALL_P (XVECEXP (seq, 0, 0))
3030               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
3031             break;
3032
3033           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
3034             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
3035               saw_frp = true;
3036         }
3037
3038       if (RTX_FRAME_RELATED_P (i))
3039         saw_frp = true;
3040     }
3041
3042   /* If the port doesn't emit epilogue unwind info, we don't need a
3043      save/restore pair.  */
3044   if (!saw_frp)
3045     return;
3046
3047   /* Otherwise, search forward to see if the return insn was the last
3048      basic block of the function.  If so, we don't need save/restore.  */
3049   gcc_assert (i != NULL);
3050   i = next_real_insn (i);
3051   if (i == NULL)
3052     return;
3053
3054   /* Insert the restore before that next real insn in the stream, and before
3055      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3056      properly nested.  This should be after any label or alignment.  This
3057      will be pushed into the CFI stream by the function below.  */
3058   while (1)
3059     {
3060       rtx p = PREV_INSN (i);
3061       if (!NOTE_P (p))
3062         break;
3063       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3064         break;
3065       i = p;
3066     }
3067   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3068
3069   emit_cfa_remember = true;
3070
3071   /* And emulate the state save.  */
3072   gcc_assert (!cfa_remember.in_use);
3073   cfa_remember = cfa;
3074   cfa_remember.in_use = 1;
3075 }
3076
3077 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3078    required.  */
3079
3080 void
3081 dwarf2out_frame_debug_restore_state (void)
3082 {
3083   dw_cfi_ref cfi = new_cfi ();
3084   const char *label = dwarf2out_cfi_label (false);
3085
3086   cfi->dw_cfi_opc = DW_CFA_restore_state;
3087   add_fde_cfi (label, cfi);
3088
3089   gcc_assert (cfa_remember.in_use);
3090   cfa = cfa_remember;
3091   cfa_remember.in_use = 0;
3092 }
3093
3094 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3095 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3096  (enum dwarf_call_frame_info cfi);
3097
3098 static enum dw_cfi_oprnd_type
3099 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3100 {
3101   switch (cfi)
3102     {
3103     case DW_CFA_nop:
3104     case DW_CFA_GNU_window_save:
3105     case DW_CFA_remember_state:
3106     case DW_CFA_restore_state:
3107       return dw_cfi_oprnd_unused;
3108
3109     case DW_CFA_set_loc:
3110     case DW_CFA_advance_loc1:
3111     case DW_CFA_advance_loc2:
3112     case DW_CFA_advance_loc4:
3113     case DW_CFA_MIPS_advance_loc8:
3114       return dw_cfi_oprnd_addr;
3115
3116     case DW_CFA_offset:
3117     case DW_CFA_offset_extended:
3118     case DW_CFA_def_cfa:
3119     case DW_CFA_offset_extended_sf:
3120     case DW_CFA_def_cfa_sf:
3121     case DW_CFA_restore:
3122     case DW_CFA_restore_extended:
3123     case DW_CFA_undefined:
3124     case DW_CFA_same_value:
3125     case DW_CFA_def_cfa_register:
3126     case DW_CFA_register:
3127     case DW_CFA_expression:
3128       return dw_cfi_oprnd_reg_num;
3129
3130     case DW_CFA_def_cfa_offset:
3131     case DW_CFA_GNU_args_size:
3132     case DW_CFA_def_cfa_offset_sf:
3133       return dw_cfi_oprnd_offset;
3134
3135     case DW_CFA_def_cfa_expression:
3136       return dw_cfi_oprnd_loc;
3137
3138     default:
3139       gcc_unreachable ();
3140     }
3141 }
3142
3143 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3144 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3145  (enum dwarf_call_frame_info cfi);
3146
3147 static enum dw_cfi_oprnd_type
3148 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3149 {
3150   switch (cfi)
3151     {
3152     case DW_CFA_def_cfa:
3153     case DW_CFA_def_cfa_sf:
3154     case DW_CFA_offset:
3155     case DW_CFA_offset_extended_sf:
3156     case DW_CFA_offset_extended:
3157       return dw_cfi_oprnd_offset;
3158
3159     case DW_CFA_register:
3160       return dw_cfi_oprnd_reg_num;
3161
3162     case DW_CFA_expression:
3163       return dw_cfi_oprnd_loc;
3164
3165     default:
3166       return dw_cfi_oprnd_unused;
3167     }
3168 }
3169
3170 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3171    switch to the data section instead, and write out a synthetic start label
3172    for collect2 the first time around.  */
3173
3174 static void
3175 switch_to_eh_frame_section (bool back)
3176 {
3177   tree label;
3178
3179 #ifdef EH_FRAME_SECTION_NAME
3180   if (eh_frame_section == 0)
3181     {
3182       int flags;
3183
3184       if (EH_TABLES_CAN_BE_READ_ONLY)
3185         {
3186           int fde_encoding;
3187           int per_encoding;
3188           int lsda_encoding;
3189
3190           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3191                                                        /*global=*/0);
3192           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3193                                                        /*global=*/1);
3194           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3195                                                         /*global=*/0);
3196           flags = ((! flag_pic
3197                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3198                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3199                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3200                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3201                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3202                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3203                    ? 0 : SECTION_WRITE);
3204         }
3205       else
3206         flags = SECTION_WRITE;
3207       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3208     }
3209 #endif /* EH_FRAME_SECTION_NAME */
3210
3211   if (eh_frame_section)
3212     switch_to_section (eh_frame_section);
3213   else
3214     {
3215       /* We have no special eh_frame section.  Put the information in
3216          the data section and emit special labels to guide collect2.  */
3217       switch_to_section (data_section);
3218
3219       if (!back)
3220         {
3221           label = get_file_function_name ("F");
3222           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3223           targetm.asm_out.globalize_label (asm_out_file,
3224                                            IDENTIFIER_POINTER (label));
3225           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3226         }
3227     }
3228 }
3229
3230 /* Switch [BACK] to the eh or debug frame table section, depending on
3231    FOR_EH.  */
3232
3233 static void
3234 switch_to_frame_table_section (int for_eh, bool back)
3235 {
3236   if (for_eh)
3237     switch_to_eh_frame_section (back);
3238   else
3239     {
3240       if (!debug_frame_section)
3241         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3242                                            SECTION_DEBUG, NULL);
3243       switch_to_section (debug_frame_section);
3244     }
3245 }
3246
3247 /* Output a Call Frame Information opcode and its operand(s).  */
3248
3249 static void
3250 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3251 {
3252   unsigned long r;
3253   HOST_WIDE_INT off;
3254
3255   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3256     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3257                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3258                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3259                          ((unsigned HOST_WIDE_INT)
3260                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3261   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3262     {
3263       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3264       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3265                            "DW_CFA_offset, column %#lx", r);
3266       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3267       dw2_asm_output_data_uleb128 (off, NULL);
3268     }
3269   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3270     {
3271       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3272       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3273                            "DW_CFA_restore, column %#lx", r);
3274     }
3275   else
3276     {
3277       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3278                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3279
3280       switch (cfi->dw_cfi_opc)
3281         {
3282         case DW_CFA_set_loc:
3283           if (for_eh)
3284             dw2_asm_output_encoded_addr_rtx (
3285                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3286                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3287                 false, NULL);
3288           else
3289             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3290                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3291           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3292           break;
3293
3294         case DW_CFA_advance_loc1:
3295           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3296                                 fde->dw_fde_current_label, NULL);
3297           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3298           break;
3299
3300         case DW_CFA_advance_loc2:
3301           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3302                                 fde->dw_fde_current_label, NULL);
3303           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3304           break;
3305
3306         case DW_CFA_advance_loc4:
3307           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3308                                 fde->dw_fde_current_label, NULL);
3309           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3310           break;
3311
3312         case DW_CFA_MIPS_advance_loc8:
3313           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3314                                 fde->dw_fde_current_label, NULL);
3315           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3316           break;
3317
3318         case DW_CFA_offset_extended:
3319           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3320           dw2_asm_output_data_uleb128 (r, NULL);
3321           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3322           dw2_asm_output_data_uleb128 (off, NULL);
3323           break;
3324
3325         case DW_CFA_def_cfa:
3326           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3327           dw2_asm_output_data_uleb128 (r, NULL);
3328           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3329           break;
3330
3331         case DW_CFA_offset_extended_sf:
3332           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3333           dw2_asm_output_data_uleb128 (r, NULL);
3334           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3335           dw2_asm_output_data_sleb128 (off, NULL);
3336           break;
3337
3338         case DW_CFA_def_cfa_sf:
3339           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3340           dw2_asm_output_data_uleb128 (r, NULL);
3341           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3342           dw2_asm_output_data_sleb128 (off, NULL);
3343           break;
3344
3345         case DW_CFA_restore_extended:
3346         case DW_CFA_undefined:
3347         case DW_CFA_same_value:
3348         case DW_CFA_def_cfa_register:
3349           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3350           dw2_asm_output_data_uleb128 (r, NULL);
3351           break;
3352
3353         case DW_CFA_register:
3354           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3355           dw2_asm_output_data_uleb128 (r, NULL);
3356           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3357           dw2_asm_output_data_uleb128 (r, NULL);
3358           break;
3359
3360         case DW_CFA_def_cfa_offset:
3361         case DW_CFA_GNU_args_size:
3362           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3363           break;
3364
3365         case DW_CFA_def_cfa_offset_sf:
3366           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3367           dw2_asm_output_data_sleb128 (off, NULL);
3368           break;
3369
3370         case DW_CFA_GNU_window_save:
3371           break;
3372
3373         case DW_CFA_def_cfa_expression:
3374         case DW_CFA_expression:
3375           output_cfa_loc (cfi, for_eh);
3376           break;
3377
3378         case DW_CFA_GNU_negative_offset_extended:
3379           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3380           gcc_unreachable ();
3381
3382         default:
3383           break;
3384         }
3385     }
3386 }
3387
3388 /* Similar, but do it via assembler directives instead.  */
3389
3390 static void
3391 output_cfi_directive (dw_cfi_ref cfi)
3392 {
3393   unsigned long r, r2;
3394
3395   switch (cfi->dw_cfi_opc)
3396     {
3397     case DW_CFA_advance_loc:
3398     case DW_CFA_advance_loc1:
3399     case DW_CFA_advance_loc2:
3400     case DW_CFA_advance_loc4:
3401     case DW_CFA_MIPS_advance_loc8:
3402     case DW_CFA_set_loc:
3403       /* Should only be created by add_fde_cfi in a code path not
3404          followed when emitting via directives.  The assembler is
3405          going to take care of this for us.  */
3406       gcc_unreachable ();
3407
3408     case DW_CFA_offset:
3409     case DW_CFA_offset_extended:
3410     case DW_CFA_offset_extended_sf:
3411       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3412       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3413                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3414       break;
3415
3416     case DW_CFA_restore:
3417     case DW_CFA_restore_extended:
3418       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3419       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3420       break;
3421
3422     case DW_CFA_undefined:
3423       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3424       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3425       break;
3426
3427     case DW_CFA_same_value:
3428       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3429       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3430       break;
3431
3432     case DW_CFA_def_cfa:
3433     case DW_CFA_def_cfa_sf:
3434       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3435       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3436                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3437       break;
3438
3439     case DW_CFA_def_cfa_register:
3440       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3441       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3442       break;
3443
3444     case DW_CFA_register:
3445       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3446       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3447       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3448       break;
3449
3450     case DW_CFA_def_cfa_offset:
3451     case DW_CFA_def_cfa_offset_sf:
3452       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3453                HOST_WIDE_INT_PRINT_DEC"\n",
3454                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3455       break;
3456
3457     case DW_CFA_remember_state:
3458       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3459       break;
3460     case DW_CFA_restore_state:
3461       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3462       break;
3463
3464     case DW_CFA_GNU_args_size:
3465       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3466       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3467       if (flag_debug_asm)
3468         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3469                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3470       fputc ('\n', asm_out_file);
3471       break;
3472
3473     case DW_CFA_GNU_window_save:
3474       fprintf (asm_out_file, "\t.cfi_window_save\n");
3475       break;
3476
3477     case DW_CFA_def_cfa_expression:
3478     case DW_CFA_expression:
3479       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3480       output_cfa_loc_raw (cfi);
3481       fputc ('\n', asm_out_file);
3482       break;
3483
3484     default:
3485       gcc_unreachable ();
3486     }
3487 }
3488
3489 /* Output CFIs from VEC, up to index UPTO, to bring current FDE to the
3490    same state as after executing CFIs in CFI chain.  DO_CFI_ASM is
3491    true if .cfi_* directives shall be emitted, false otherwise.  If it
3492    is false, FDE and FOR_EH are the other arguments to pass to
3493    output_cfi.  */
3494
3495 static void
3496 output_cfis (cfi_vec vec, int upto, bool do_cfi_asm,
3497              dw_fde_ref fde, bool for_eh)
3498 {
3499   int ix;
3500   struct dw_cfi_struct cfi_buf;
3501   dw_cfi_ref cfi2;
3502   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3503   VEC(dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3504   unsigned int len, idx;
3505
3506   for (ix = 0; ix < upto + 1; ix++)
3507     {
3508       dw_cfi_ref cfi = ix < upto ? VEC_index (dw_cfi_ref, vec, ix) : NULL;
3509       switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3510         {
3511         case DW_CFA_advance_loc:
3512         case DW_CFA_advance_loc1:
3513         case DW_CFA_advance_loc2:
3514         case DW_CFA_advance_loc4:
3515         case DW_CFA_MIPS_advance_loc8:
3516         case DW_CFA_set_loc:
3517           /* All advances should be ignored.  */
3518           break;
3519         case DW_CFA_remember_state:
3520           {
3521             dw_cfi_ref args_size = cfi_args_size;
3522
3523             /* Skip everything between .cfi_remember_state and
3524                .cfi_restore_state.  */
3525             ix++;
3526             if (ix == upto)
3527               goto flush_all;
3528
3529             for (; ix < upto; ix++)
3530               {
3531                 cfi2 = VEC_index (dw_cfi_ref, vec, ix);
3532                 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3533                   break;
3534                 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3535                   args_size = cfi2;
3536                 else
3537                   gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3538               }
3539
3540             cfi_args_size = args_size;
3541             break;
3542           }
3543         case DW_CFA_GNU_args_size:
3544           cfi_args_size = cfi;
3545           break;
3546         case DW_CFA_GNU_window_save:
3547           goto flush_all;
3548         case DW_CFA_offset:
3549         case DW_CFA_offset_extended:
3550         case DW_CFA_offset_extended_sf:
3551         case DW_CFA_restore:
3552         case DW_CFA_restore_extended:
3553         case DW_CFA_undefined:
3554         case DW_CFA_same_value:
3555         case DW_CFA_register:
3556         case DW_CFA_val_offset:
3557         case DW_CFA_val_offset_sf:
3558         case DW_CFA_expression:
3559         case DW_CFA_val_expression:
3560         case DW_CFA_GNU_negative_offset_extended:
3561           if (VEC_length (dw_cfi_ref, regs)
3562               <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3563             VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3564                                    cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3565           VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
3566                        cfi);
3567           break;
3568         case DW_CFA_def_cfa:
3569         case DW_CFA_def_cfa_sf:
3570         case DW_CFA_def_cfa_expression:
3571           cfi_cfa = cfi;
3572           cfi_cfa_offset = cfi;
3573           break;
3574         case DW_CFA_def_cfa_register:
3575           cfi_cfa = cfi;
3576           break;
3577         case DW_CFA_def_cfa_offset:
3578         case DW_CFA_def_cfa_offset_sf:
3579           cfi_cfa_offset = cfi;
3580           break;
3581         case DW_CFA_nop:
3582           gcc_assert (cfi == NULL);
3583         flush_all:
3584           len = VEC_length (dw_cfi_ref, regs);
3585           for (idx = 0; idx < len; idx++)
3586             {
3587               cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3588               if (cfi2 != NULL
3589                   && cfi2->dw_cfi_opc != DW_CFA_restore
3590                   && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3591                 {
3592                   if (do_cfi_asm)
3593                     output_cfi_directive (cfi2);
3594                   else
3595                     output_cfi (cfi2, fde, for_eh);
3596                 }
3597             }
3598           if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3599             {
3600               gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3601               cfi_buf = *cfi_cfa;
3602               switch (cfi_cfa_offset->dw_cfi_opc)
3603                 {
3604                 case DW_CFA_def_cfa_offset:
3605                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3606                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3607                   break;
3608                 case DW_CFA_def_cfa_offset_sf:
3609                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3610                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3611                   break;
3612                 case DW_CFA_def_cfa:
3613                 case DW_CFA_def_cfa_sf:
3614                   cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3615                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3616                   break;
3617                 default:
3618                   gcc_unreachable ();
3619                 }
3620               cfi_cfa = &cfi_buf;
3621             }
3622           else if (cfi_cfa_offset)
3623             cfi_cfa = cfi_cfa_offset;
3624           if (cfi_cfa)
3625             {
3626               if (do_cfi_asm)
3627                 output_cfi_directive (cfi_cfa);
3628               else
3629                 output_cfi (cfi_cfa, fde, for_eh);
3630             }
3631           cfi_cfa = NULL;
3632           cfi_cfa_offset = NULL;
3633           if (cfi_args_size
3634               && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3635             {
3636               if (do_cfi_asm)
3637                 output_cfi_directive (cfi_args_size);
3638               else
3639                 output_cfi (cfi_args_size, fde, for_eh);
3640             }
3641           cfi_args_size = NULL;
3642           if (cfi == NULL)
3643             {
3644               VEC_free (dw_cfi_ref, heap, regs);
3645               return;
3646             }
3647           else if (do_cfi_asm)
3648             output_cfi_directive (cfi);
3649           else
3650             output_cfi (cfi, fde, for_eh);
3651           break;
3652         default:
3653           gcc_unreachable ();
3654         }
3655     }
3656 }
3657
3658 /* Like output_cfis, but emit all CFIs in the vector.  */
3659 static void
3660 output_all_cfis (cfi_vec vec, bool do_cfi_asm,
3661                  dw_fde_ref fde, bool for_eh)
3662 {
3663   output_cfis (vec, VEC_length (dw_cfi_ref, vec), do_cfi_asm, fde, for_eh);
3664 }
3665
3666 /* Output one FDE.  */
3667
3668 static void
3669 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3670             char *section_start_label, int fde_encoding, char *augmentation,
3671             bool any_lsda_needed, int lsda_encoding)
3672 {
3673   int ix;
3674   const char *begin, *end;
3675   static unsigned int j;
3676   char l1[20], l2[20];
3677   dw_cfi_ref cfi;
3678
3679   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3680                                      /* empty */ 0);
3681   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3682                                   for_eh + j);
3683   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3684   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3685   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3686     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3687                          " indicating 64-bit DWARF extension");
3688   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3689                         "FDE Length");
3690   ASM_OUTPUT_LABEL (asm_out_file, l1);
3691
3692   if (for_eh)
3693     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3694   else
3695     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3696                            debug_frame_section, "FDE CIE offset");
3697
3698   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3699   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3700
3701   if (for_eh)
3702     {
3703       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3704       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3705       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3706                                        "FDE initial location");
3707       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3708                             end, begin, "FDE address range");
3709     }
3710   else
3711     {
3712       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3713       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3714     }
3715
3716   if (augmentation[0])
3717     {
3718       if (any_lsda_needed)
3719         {
3720           int size = size_of_encoded_value (lsda_encoding);
3721
3722           if (lsda_encoding == DW_EH_PE_aligned)
3723             {
3724               int offset = (  4         /* Length */
3725                             + 4         /* CIE offset */
3726                             + 2 * size_of_encoded_value (fde_encoding)
3727                             + 1         /* Augmentation size */ );
3728               int pad = -offset & (PTR_SIZE - 1);
3729
3730               size += pad;
3731               gcc_assert (size_of_uleb128 (size) == 1);
3732             }
3733
3734           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3735
3736           if (fde->uses_eh_lsda)
3737             {
3738               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3739                                            fde->funcdef_number);
3740               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3741                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3742                                                false,
3743                                                "Language Specific Data Area");
3744             }
3745           else
3746             {
3747               if (lsda_encoding == DW_EH_PE_aligned)
3748                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3749               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3750                                    "Language Specific Data Area (none)");
3751             }
3752         }
3753       else
3754         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3755     }
3756
3757   /* Loop through the Call Frame Instructions associated with
3758      this FDE.  */
3759   fde->dw_fde_current_label = begin;
3760   if (fde->dw_fde_second_begin == NULL)
3761     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3762       output_cfi (cfi, fde, for_eh);
3763   else if (!second)
3764     {
3765       if (fde->dw_fde_switch_cfi_index > 0)
3766         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3767           {
3768             if (ix == fde->dw_fde_switch_cfi_index)
3769               break;
3770             output_cfi (cfi, fde, for_eh);
3771           }
3772     }
3773   else
3774     {
3775       int i, from = 0;
3776       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
3777
3778       if (fde->dw_fde_switch_cfi_index > 0)
3779         {
3780           from = fde->dw_fde_switch_cfi_index;
3781           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
3782         }
3783       for (i = from; i < until; i++)
3784         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
3785                     fde, for_eh);
3786     }
3787
3788   /* If we are to emit a ref/link from function bodies to their frame tables,
3789      do it now.  This is typically performed to make sure that tables
3790      associated with functions are dragged with them and not discarded in
3791      garbage collecting links. We need to do this on a per function basis to
3792      cope with -ffunction-sections.  */
3793
3794 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3795   /* Switch to the function section, emit the ref to the tables, and
3796      switch *back* into the table section.  */
3797   switch_to_section (function_section (fde->decl));
3798   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3799   switch_to_frame_table_section (for_eh, true);
3800 #endif
3801
3802   /* Pad the FDE out to an address sized boundary.  */
3803   ASM_OUTPUT_ALIGN (asm_out_file,
3804                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3805   ASM_OUTPUT_LABEL (asm_out_file, l2);
3806
3807   j += 2;
3808 }
3809
3810 /* Return true if frame description entry FDE is needed for EH.  */
3811
3812 static bool
3813 fde_needed_for_eh_p (dw_fde_ref fde)
3814 {
3815   if (flag_asynchronous_unwind_tables)
3816     return true;
3817
3818   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3819     return true;
3820
3821   if (fde->uses_eh_lsda)
3822     return true;
3823
3824   /* If exceptions are enabled, we have collected nothrow info.  */
3825   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3826     return false;
3827
3828   return true;
3829 }
3830
3831 /* Output the call frame information used to record information
3832    that relates to calculating the frame pointer, and records the
3833    location of saved registers.  */
3834
3835 static void
3836 output_call_frame_info (int for_eh)
3837 {
3838   unsigned int i;
3839   dw_fde_ref fde;
3840   dw_cfi_ref cfi;
3841   char l1[20], l2[20], section_start_label[20];
3842   bool any_lsda_needed = false;
3843   char augmentation[6];
3844   int augmentation_size;
3845   int fde_encoding = DW_EH_PE_absptr;
3846   int per_encoding = DW_EH_PE_absptr;
3847   int lsda_encoding = DW_EH_PE_absptr;
3848   int return_reg;
3849   rtx personality = NULL;
3850   int dw_cie_version;
3851
3852   /* Don't emit a CIE if there won't be any FDEs.  */
3853   if (fde_table_in_use == 0)
3854     return;
3855
3856   /* Nothing to do if the assembler's doing it all.  */
3857   if (dwarf2out_do_cfi_asm ())
3858     return;
3859
3860   /* If we don't have any functions we'll want to unwind out of, don't emit
3861      any EH unwind information.  If we make FDEs linkonce, we may have to
3862      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3863      want to avoid having an FDE kept around when the function it refers to
3864      is discarded.  Example where this matters: a primary function template
3865      in C++ requires EH information, an explicit specialization doesn't.  */
3866   if (for_eh)
3867     {
3868       bool any_eh_needed = false;
3869
3870       for (i = 0; i < fde_table_in_use; i++)
3871         if (fde_table[i].uses_eh_lsda)
3872           any_eh_needed = any_lsda_needed = true;
3873         else if (fde_needed_for_eh_p (&fde_table[i]))
3874           any_eh_needed = true;
3875         else if (TARGET_USES_WEAK_UNWIND_INFO)
3876           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3877                                              1, 1);
3878
3879       if (!any_eh_needed)
3880         return;
3881     }
3882
3883   /* We're going to be generating comments, so turn on app.  */
3884   if (flag_debug_asm)
3885     app_enable ();
3886
3887   /* Switch to the proper frame section, first time.  */
3888   switch_to_frame_table_section (for_eh, false);
3889
3890   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3891   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3892
3893   /* Output the CIE.  */
3894   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3895   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3896   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3897     dw2_asm_output_data (4, 0xffffffff,
3898       "Initial length escape value indicating 64-bit DWARF extension");
3899   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3900                         "Length of Common Information Entry");
3901   ASM_OUTPUT_LABEL (asm_out_file, l1);
3902
3903   /* Now that the CIE pointer is PC-relative for EH,
3904      use 0 to identify the CIE.  */
3905   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3906                        (for_eh ? 0 : DWARF_CIE_ID),
3907                        "CIE Identifier Tag");
3908
3909   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3910      use CIE version 1, unless that would produce incorrect results
3911      due to overflowing the return register column.  */
3912   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3913   dw_cie_version = 1;
3914   if (return_reg >= 256 || dwarf_version > 2)
3915     dw_cie_version = 3;
3916   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3917
3918   augmentation[0] = 0;
3919   augmentation_size = 0;
3920
3921   personality = current_unit_personality;
3922   if (for_eh)
3923     {
3924       char *p;
3925
3926       /* Augmentation:
3927          z      Indicates that a uleb128 is present to size the
3928                 augmentation section.
3929          L      Indicates the encoding (and thus presence) of
3930                 an LSDA pointer in the FDE augmentation.
3931          R      Indicates a non-default pointer encoding for
3932                 FDE code pointers.
3933          P      Indicates the presence of an encoding + language
3934                 personality routine in the CIE augmentation.  */
3935
3936       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3937       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3938       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3939
3940       p = augmentation + 1;
3941       if (personality)
3942         {
3943           *p++ = 'P';
3944           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3945           assemble_external_libcall (personality);
3946         }
3947       if (any_lsda_needed)
3948         {
3949           *p++ = 'L';
3950           augmentation_size += 1;
3951         }
3952       if (fde_encoding != DW_EH_PE_absptr)
3953         {
3954           *p++ = 'R';
3955           augmentation_size += 1;
3956         }
3957       if (p > augmentation + 1)
3958         {
3959           augmentation[0] = 'z';
3960           *p = '\0';
3961         }
3962
3963       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3964       if (personality && per_encoding == DW_EH_PE_aligned)
3965         {
3966           int offset = (  4             /* Length */
3967                         + 4             /* CIE Id */
3968                         + 1             /* CIE version */
3969                         + strlen (augmentation) + 1     /* Augmentation */
3970                         + size_of_uleb128 (1)           /* Code alignment */
3971                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3972                         + 1             /* RA column */
3973                         + 1             /* Augmentation size */
3974                         + 1             /* Personality encoding */ );
3975           int pad = -offset & (PTR_SIZE - 1);
3976
3977           augmentation_size += pad;
3978
3979           /* Augmentations should be small, so there's scarce need to
3980              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3981           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3982         }
3983     }
3984
3985   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3986   if (dw_cie_version >= 4)
3987     {
3988       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3989       dw2_asm_output_data (1, 0, "CIE Segment Size");
3990     }
3991   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3992   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3993                                "CIE Data Alignment Factor");
3994
3995   if (dw_cie_version == 1)
3996     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3997   else
3998     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3999
4000   if (augmentation[0])
4001     {
4002       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
4003       if (personality)
4004         {
4005           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
4006                                eh_data_format_name (per_encoding));
4007           dw2_asm_output_encoded_addr_rtx (per_encoding,
4008                                            personality,
4009                                            true, NULL);
4010         }
4011
4012       if (any_lsda_needed)
4013         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
4014                              eh_data_format_name (lsda_encoding));
4015
4016       if (fde_encoding != DW_EH_PE_absptr)
4017         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
4018                              eh_data_format_name (fde_encoding));
4019     }
4020
4021   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
4022     output_cfi (cfi, NULL, for_eh);
4023
4024   /* Pad the CIE out to an address sized boundary.  */
4025   ASM_OUTPUT_ALIGN (asm_out_file,
4026                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
4027   ASM_OUTPUT_LABEL (asm_out_file, l2);
4028
4029   /* Loop through all of the FDE's.  */
4030   for (i = 0; i < fde_table_in_use; i++)
4031     {
4032       unsigned int k;
4033       fde = &fde_table[i];
4034
4035       /* Don't emit EH unwind info for leaf functions that don't need it.  */
4036       if (for_eh && !fde_needed_for_eh_p (fde))
4037         continue;
4038
4039       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
4040         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
4041                     augmentation, any_lsda_needed, lsda_encoding);
4042     }
4043
4044   if (for_eh && targetm.terminate_dw2_eh_frame_info)
4045     dw2_asm_output_data (4, 0, "End of Table");
4046 #ifdef MIPS_DEBUGGING_INFO
4047   /* Work around Irix 6 assembler bug whereby labels at the end of a section
4048      get a value of 0.  Putting .align 0 after the label fixes it.  */
4049   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4050 #endif
4051
4052   /* Turn off app to make assembly quicker.  */
4053   if (flag_debug_asm)
4054     app_disable ();
4055 }
4056
4057 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4058
4059 static void
4060 dwarf2out_do_cfi_startproc (bool second)
4061 {
4062   int enc;
4063   rtx ref;
4064   rtx personality = get_personality_function (current_function_decl);
4065
4066   fprintf (asm_out_file, "\t.cfi_startproc\n");
4067
4068   if (personality)
4069     {
4070       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4071       ref = personality;
4072
4073       /* ??? The GAS support isn't entirely consistent.  We have to
4074          handle indirect support ourselves, but PC-relative is done
4075          in the assembler.  Further, the assembler can't handle any
4076          of the weirder relocation types.  */
4077       if (enc & DW_EH_PE_indirect)
4078         ref = dw2_force_const_mem (ref, true);
4079
4080       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4081       output_addr_const (asm_out_file, ref);
4082       fputc ('\n', asm_out_file);
4083     }
4084
4085   if (crtl->uses_eh_lsda)
4086     {
4087       char lab[20];
4088
4089       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4090       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4091                                    current_function_funcdef_no);
4092       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4093       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4094
4095       if (enc & DW_EH_PE_indirect)
4096         ref = dw2_force_const_mem (ref, true);
4097
4098       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4099       output_addr_const (asm_out_file, ref);
4100       fputc ('\n', asm_out_file);
4101     }
4102 }
4103
4104 /* Output a marker (i.e. a label) for the beginning of a function, before
4105    the prologue.  */
4106
4107 void
4108 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4109                           const char *file ATTRIBUTE_UNUSED)
4110 {
4111   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4112   char * dup_label;
4113   dw_fde_ref fde;
4114   section *fnsec;
4115   bool do_frame;
4116
4117   current_function_func_begin_label = NULL;
4118
4119   do_frame = dwarf2out_do_frame ();
4120
4121   /* ??? current_function_func_begin_label is also used by except.c for
4122      call-site information.  We must emit this label if it might be used.  */
4123   if (!do_frame
4124       && (!flag_exceptions
4125           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
4126     return;
4127
4128   fnsec = function_section (current_function_decl);
4129   switch_to_section (fnsec);
4130   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4131                                current_function_funcdef_no);
4132   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4133                           current_function_funcdef_no);
4134   dup_label = xstrdup (label);
4135   current_function_func_begin_label = dup_label;
4136
4137   /* We can elide the fde allocation if we're not emitting debug info.  */
4138   if (!do_frame)
4139     return;
4140
4141   /* Expand the fde table if necessary.  */
4142   if (fde_table_in_use == fde_table_allocated)
4143     {
4144       fde_table_allocated += FDE_TABLE_INCREMENT;
4145       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4146       memset (fde_table + fde_table_in_use, 0,
4147               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4148     }
4149
4150   /* Record the FDE associated with this function.  */
4151   current_funcdef_fde = fde_table_in_use;
4152
4153   /* Add the new FDE at the end of the fde_table.  */
4154   fde = &fde_table[fde_table_in_use++];
4155   fde->decl = current_function_decl;
4156   fde->dw_fde_begin = dup_label;
4157   fde->dw_fde_end = NULL;
4158   fde->dw_fde_current_label = dup_label;
4159   fde->dw_fde_second_begin = NULL;
4160   fde->dw_fde_second_end = NULL;
4161   fde->dw_fde_vms_end_prologue = NULL;
4162   fde->dw_fde_vms_begin_epilogue = NULL;
4163   fde->dw_fde_cfi = VEC_alloc (dw_cfi_ref, gc, 20);
4164   fde->dw_fde_switch_cfi_index = 0;
4165   fde->funcdef_number = current_function_funcdef_no;
4166   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4167   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4168   fde->nothrow = crtl->nothrow;
4169   fde->drap_reg = INVALID_REGNUM;
4170   fde->vdrap_reg = INVALID_REGNUM;
4171   fde->in_std_section = (fnsec == text_section
4172                          || (cold_text_section && fnsec == cold_text_section));
4173   fde->second_in_std_section = 0;
4174
4175   args_size = old_args_size = 0;
4176
4177   /* We only want to output line number information for the genuine dwarf2
4178      prologue case, not the eh frame case.  */
4179 #ifdef DWARF2_DEBUGGING_INFO
4180   if (file)
4181     dwarf2out_source_line (line, file, 0, true);
4182 #endif
4183
4184   if (dwarf2out_do_cfi_asm ())
4185     dwarf2out_do_cfi_startproc (false);
4186   else
4187     {
4188       rtx personality = get_personality_function (current_function_decl);
4189       if (!current_unit_personality)
4190         current_unit_personality = personality;
4191
4192       /* We cannot keep a current personality per function as without CFI
4193          asm, at the point where we emit the CFI data, there is no current
4194          function anymore.  */
4195       if (personality && current_unit_personality != personality)
4196         sorry ("multiple EH personalities are supported only with assemblers "
4197                "supporting .cfi_personality directive");
4198     }
4199 }
4200
4201 /* Output a marker (i.e. a label) for the end of the generated code
4202    for a function prologue.  This gets called *after* the prologue code has
4203    been generated.  */
4204
4205 void
4206 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4207                         const char *file ATTRIBUTE_UNUSED)
4208 {
4209   dw_fde_ref fde;
4210   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4211
4212   /* Output a label to mark the endpoint of the code generated for this
4213      function.  */
4214   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4215                                current_function_funcdef_no);
4216   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4217                           current_function_funcdef_no);
4218   fde = &fde_table[fde_table_in_use - 1];
4219   fde->dw_fde_vms_end_prologue = xstrdup (label);
4220 }
4221
4222 /* Output a marker (i.e. a label) for the beginning of the generated code
4223    for a function epilogue.  This gets called *before* the prologue code has
4224    been generated.  */
4225
4226 void
4227 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4228                           const char *file ATTRIBUTE_UNUSED)
4229 {
4230   dw_fde_ref fde;
4231   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4232
4233   fde = &fde_table[fde_table_in_use - 1];
4234   if (fde->dw_fde_vms_begin_epilogue)
4235     return;
4236
4237   /* Output a label to mark the endpoint of the code generated for this
4238      function.  */
4239   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4240                                current_function_funcdef_no);
4241   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4242                           current_function_funcdef_no);
4243   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4244 }
4245
4246 /* Output a marker (i.e. a label) for the absolute end of the generated code
4247    for a function definition.  This gets called *after* the epilogue code has
4248    been generated.  */
4249
4250 void
4251 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4252                         const char *file ATTRIBUTE_UNUSED)
4253 {
4254   dw_fde_ref fde;
4255   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4256
4257   last_var_location_insn = NULL_RTX;
4258
4259   if (dwarf2out_do_cfi_asm ())
4260     fprintf (asm_out_file, "\t.cfi_endproc\n");
4261
4262   /* Output a label to mark the endpoint of the code generated for this
4263      function.  */
4264   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4265                                current_function_funcdef_no);
4266   ASM_OUTPUT_LABEL (asm_out_file, label);
4267   fde = current_fde ();
4268   gcc_assert (fde != NULL);
4269   if (fde->dw_fde_second_begin == NULL)
4270     fde->dw_fde_end = xstrdup (label);
4271 }
4272
4273 void
4274 dwarf2out_frame_init (void)
4275 {
4276   /* Allocate the initial hunk of the fde_table.  */
4277   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4278   fde_table_allocated = FDE_TABLE_INCREMENT;
4279   fde_table_in_use = 0;
4280
4281   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4282      sake of lookup_cfa.  */
4283
4284   /* On entry, the Canonical Frame Address is at SP.  */
4285   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4286
4287   if (targetm.debug_unwind_info () == UI_DWARF2
4288       || targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
4289     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4290 }
4291
4292 void
4293 dwarf2out_frame_finish (void)
4294 {
4295   /* Output call frame information.  */
4296   if (targetm.debug_unwind_info () == UI_DWARF2)
4297     output_call_frame_info (0);
4298
4299   /* Output another copy for the unwinder.  */
4300   if ((flag_unwind_tables || flag_exceptions)
4301       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
4302     output_call_frame_info (1);
4303 }
4304
4305 /* Note that the current function section is being used for code.  */
4306
4307 static void
4308 dwarf2out_note_section_used (void)
4309 {
4310   section *sec = current_function_section ();
4311   if (sec == text_section)
4312     text_section_used = true;
4313   else if (sec == cold_text_section)
4314     cold_text_section_used = true;
4315 }
4316
4317 static void var_location_switch_text_section (void);
4318 static void set_cur_line_info_table (section *);
4319
4320 void
4321 dwarf2out_switch_text_section (void)
4322 {
4323   section *sect;
4324   dw_fde_ref fde = current_fde ();
4325
4326   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4327
4328   if (!in_cold_section_p)
4329     {
4330       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4331       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4332       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4333     }
4334   else
4335     {
4336       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4337       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4338       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4339     }
4340   have_multiple_function_sections = true;
4341
4342   /* Reset the current label on switching text sections, so that we
4343      don't attempt to advance_loc4 between labels in different sections.  */
4344   fde->dw_fde_current_label = NULL;
4345
4346   /* There is no need to mark used sections when not debugging.  */
4347   if (cold_text_section != NULL)
4348     dwarf2out_note_section_used ();
4349
4350   if (dwarf2out_do_cfi_asm ())
4351     fprintf (asm_out_file, "\t.cfi_endproc\n");
4352
4353   /* Now do the real section switch.  */
4354   sect = current_function_section ();
4355   switch_to_section (sect);
4356
4357   fde->second_in_std_section
4358     = (sect == text_section
4359        || (cold_text_section && sect == cold_text_section));
4360
4361   if (dwarf2out_do_cfi_asm ())
4362     {
4363       dwarf2out_do_cfi_startproc (true);
4364       /* As this is a different FDE, insert all current CFI instructions
4365          again.  */
4366       output_all_cfis (fde->dw_fde_cfi, true, fde, true);
4367     }
4368   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
4369   var_location_switch_text_section ();
4370
4371   set_cur_line_info_table (sect);
4372 }
4373 \f
4374 /* And now, the subset of the debugging information support code necessary
4375    for emitting location expressions.  */
4376
4377 /* Data about a single source file.  */
4378 struct GTY(()) dwarf_file_data {
4379   const char * filename;
4380   int emitted_number;
4381 };
4382
4383 typedef struct dw_val_struct *dw_val_ref;
4384 typedef struct die_struct *dw_die_ref;
4385 typedef const struct die_struct *const_dw_die_ref;
4386 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4387 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4388
4389 typedef struct GTY(()) deferred_locations_struct
4390 {
4391   tree variable;
4392   dw_die_ref die;
4393 } deferred_locations;
4394
4395 DEF_VEC_O(deferred_locations);
4396 DEF_VEC_ALLOC_O(deferred_locations,gc);
4397
4398 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4399
4400 DEF_VEC_P(dw_die_ref);
4401 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4402
4403 /* Each DIE may have a series of attribute/value pairs.  Values
4404    can take on several forms.  The forms that are used in this
4405    implementation are listed below.  */
4406
4407 enum dw_val_class
4408 {
4409   dw_val_class_addr,
4410   dw_val_class_offset,
4411   dw_val_class_loc,
4412   dw_val_class_loc_list,
4413   dw_val_class_range_list,
4414   dw_val_class_const,
4415   dw_val_class_unsigned_const,
4416   dw_val_class_const_double,
4417   dw_val_class_vec,
4418   dw_val_class_flag,
4419   dw_val_class_die_ref,
4420   dw_val_class_fde_ref,
4421   dw_val_class_lbl_id,
4422   dw_val_class_lineptr,
4423   dw_val_class_str,
4424   dw_val_class_macptr,
4425   dw_val_class_file,
4426   dw_val_class_data8,
4427   dw_val_class_decl_ref,
4428   dw_val_class_vms_delta
4429 };
4430
4431 /* Describe a floating point constant value, or a vector constant value.  */
4432
4433 typedef struct GTY(()) dw_vec_struct {
4434   unsigned char * GTY((length ("%h.length"))) array;
4435   unsigned length;
4436   unsigned elt_size;
4437 }
4438 dw_vec_const;
4439
4440 /* The dw_val_node describes an attribute's value, as it is
4441    represented internally.  */
4442
4443 typedef struct GTY(()) dw_val_struct {
4444   enum dw_val_class val_class;
4445   union dw_val_struct_union
4446     {
4447       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4448       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4449       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4450       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4451       HOST_WIDE_INT GTY ((default)) val_int;
4452       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4453       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4454       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4455       struct dw_val_die_union
4456         {
4457           dw_die_ref die;
4458           int external;
4459         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4460       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4461       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4462       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4463       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4464       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4465       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4466       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4467       struct dw_val_vms_delta_union
4468         {
4469           char * lbl1;
4470           char * lbl2;
4471         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4472     }
4473   GTY ((desc ("%1.val_class"))) v;
4474 }
4475 dw_val_node;
4476
4477 /* Locations in memory are described using a sequence of stack machine
4478    operations.  */
4479
4480 typedef struct GTY(()) dw_loc_descr_struct {
4481   dw_loc_descr_ref dw_loc_next;
4482   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4483   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4484      from DW_OP_addr with a dtp-relative symbol relocation.  */
4485   unsigned int dtprel : 1;
4486   int dw_loc_addr;
4487   dw_val_node dw_loc_oprnd1;
4488   dw_val_node dw_loc_oprnd2;
4489 }
4490 dw_loc_descr_node;
4491
4492 /* Location lists are ranges + location descriptions for that range,
4493    so you can track variables that are in different places over
4494    their entire life.  */
4495 typedef struct GTY(()) dw_loc_list_struct {
4496   dw_loc_list_ref dw_loc_next;
4497   const char *begin; /* Label for begin address of range */
4498   const char *end;  /* Label for end address of range */
4499   char *ll_symbol; /* Label for beginning of location list.
4500                       Only on head of list */
4501   const char *section; /* Section this loclist is relative to */
4502   dw_loc_descr_ref expr;
4503   hashval_t hash;
4504   /* True if all addresses in this and subsequent lists are known to be
4505      resolved.  */
4506   bool resolved_addr;
4507   /* True if this list has been replaced by dw_loc_next.  */
4508   bool replaced;
4509   bool emitted;
4510   /* True if the range should be emitted even if begin and end
4511      are the same.  */
4512   bool force;
4513 } dw_loc_list_node;
4514
4515 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4516
4517 /* Convert a DWARF stack opcode into its string name.  */
4518
4519 static const char *
4520 dwarf_stack_op_name (unsigned int op)
4521 {
4522   switch (op)
4523     {
4524     case DW_OP_addr:
4525       return "DW_OP_addr";
4526     case DW_OP_deref:
4527       return "DW_OP_deref";
4528     case DW_OP_const1u:
4529       return "DW_OP_const1u";
4530     case DW_OP_const1s:
4531       return "DW_OP_const1s";
4532     case DW_OP_const2u:
4533       return "DW_OP_const2u";
4534     case DW_OP_const2s:
4535       return "DW_OP_const2s";
4536     case DW_OP_const4u:
4537       return "DW_OP_const4u";
4538     case DW_OP_const4s:
4539       return "DW_OP_const4s";
4540     case DW_OP_const8u:
4541       return "DW_OP_const8u";
4542     case DW_OP_const8s:
4543       return "DW_OP_const8s";
4544     case DW_OP_constu:
4545       return "DW_OP_constu";
4546     case DW_OP_consts:
4547       return "DW_OP_consts";
4548     case DW_OP_dup:
4549       return "DW_OP_dup";
4550     case DW_OP_drop:
4551       return "DW_OP_drop";
4552     case DW_OP_over:
4553       return "DW_OP_over";
4554     case DW_OP_pick:
4555       return "DW_OP_pick";
4556     case DW_OP_swap:
4557       return "DW_OP_swap";
4558     case DW_OP_rot:
4559       return "DW_OP_rot";
4560     case DW_OP_xderef:
4561       return "DW_OP_xderef";
4562     case DW_OP_abs:
4563       return "DW_OP_abs";
4564     case DW_OP_and:
4565       return "DW_OP_and";
4566     case DW_OP_div:
4567       return "DW_OP_div";
4568     case DW_OP_minus:
4569       return "DW_OP_minus";
4570     case DW_OP_mod:
4571       return "DW_OP_mod";
4572     case DW_OP_mul:
4573       return "DW_OP_mul";
4574     case DW_OP_neg:
4575       return "DW_OP_neg";
4576     case DW_OP_not:
4577       return "DW_OP_not";
4578     case DW_OP_or:
4579       return "DW_OP_or";
4580     case DW_OP_plus:
4581       return "DW_OP_plus";
4582     case DW_OP_plus_uconst:
4583       return "DW_OP_plus_uconst";
4584     case DW_OP_shl:
4585       return "DW_OP_shl";
4586     case DW_OP_shr:
4587       return "DW_OP_shr";
4588     case DW_OP_shra:
4589       return "DW_OP_shra";
4590     case DW_OP_xor:
4591       return "DW_OP_xor";
4592     case DW_OP_bra:
4593       return "DW_OP_bra";
4594     case DW_OP_eq:
4595       return "DW_OP_eq";
4596     case DW_OP_ge:
4597       return "DW_OP_ge";
4598     case DW_OP_gt:
4599       return "DW_OP_gt";
4600     case DW_OP_le:
4601       return "DW_OP_le";
4602     case DW_OP_lt:
4603       return "DW_OP_lt";
4604     case DW_OP_ne:
4605       return "DW_OP_ne";
4606     case DW_OP_skip:
4607       return "DW_OP_skip";
4608     case DW_OP_lit0:
4609       return "DW_OP_lit0";
4610     case DW_OP_lit1:
4611       return "DW_OP_lit1";
4612     case DW_OP_lit2:
4613       return "DW_OP_lit2";
4614     case DW_OP_lit3:
4615       return "DW_OP_lit3";
4616     case DW_OP_lit4:
4617       return "DW_OP_lit4";
4618     case DW_OP_lit5:
4619       return "DW_OP_lit5";
4620     case DW_OP_lit6:
4621       return "DW_OP_lit6";
4622     case DW_OP_lit7:
4623       return "DW_OP_lit7";
4624     case DW_OP_lit8:
4625       return "DW_OP_lit8";
4626     case DW_OP_lit9:
4627       return "DW_OP_lit9";
4628     case DW_OP_lit10:
4629       return "DW_OP_lit10";
4630     case DW_OP_lit11:
4631       return "DW_OP_lit11";
4632     case DW_OP_lit12:
4633       return "DW_OP_lit12";
4634     case DW_OP_lit13:
4635       return "DW_OP_lit13";
4636     case DW_OP_lit14:
4637       return "DW_OP_lit14";
4638     case DW_OP_lit15:
4639       return "DW_OP_lit15";
4640     case DW_OP_lit16:
4641       return "DW_OP_lit16";
4642     case DW_OP_lit17:
4643       return "DW_OP_lit17";
4644     case DW_OP_lit18:
4645       return "DW_OP_lit18";
4646     case DW_OP_lit19:
4647       return "DW_OP_lit19";
4648     case DW_OP_lit20:
4649       return "DW_OP_lit20";
4650     case DW_OP_lit21:
4651       return "DW_OP_lit21";
4652     case DW_OP_lit22:
4653       return "DW_OP_lit22";
4654     case DW_OP_lit23:
4655       return "DW_OP_lit23";
4656     case DW_OP_lit24:
4657       return "DW_OP_lit24";
4658     case DW_OP_lit25:
4659       return "DW_OP_lit25";
4660     case DW_OP_lit26:
4661       return "DW_OP_lit26";
4662     case DW_OP_lit27:
4663       return "DW_OP_lit27";
4664     case DW_OP_lit28:
4665       return "DW_OP_lit28";
4666     case DW_OP_lit29:
4667       return "DW_OP_lit29";
4668     case DW_OP_lit30:
4669       return "DW_OP_lit30";
4670     case DW_OP_lit31:
4671       return "DW_OP_lit31";
4672     case DW_OP_reg0:
4673       return "DW_OP_reg0";
4674     case DW_OP_reg1:
4675       return "DW_OP_reg1";
4676     case DW_OP_reg2:
4677       return "DW_OP_reg2";
4678     case DW_OP_reg3:
4679       return "DW_OP_reg3";
4680     case DW_OP_reg4:
4681       return "DW_OP_reg4";
4682     case DW_OP_reg5:
4683       return "DW_OP_reg5";
4684     case DW_OP_reg6:
4685       return "DW_OP_reg6";
4686     case DW_OP_reg7:
4687       return "DW_OP_reg7";
4688     case DW_OP_reg8:
4689       return "DW_OP_reg8";
4690     case DW_OP_reg9:
4691       return "DW_OP_reg9";
4692     case DW_OP_reg10:
4693       return "DW_OP_reg10";
4694     case DW_OP_reg11:
4695       return "DW_OP_reg11";
4696     case DW_OP_reg12:
4697       return "DW_OP_reg12";
4698     case DW_OP_reg13:
4699       return "DW_OP_reg13";
4700     case DW_OP_reg14:
4701       return "DW_OP_reg14";
4702     case DW_OP_reg15:
4703       return "DW_OP_reg15";
4704     case DW_OP_reg16:
4705       return "DW_OP_reg16";
4706     case DW_OP_reg17:
4707       return "DW_OP_reg17";
4708     case DW_OP_reg18:
4709       return "DW_OP_reg18";
4710     case DW_OP_reg19:
4711       return "DW_OP_reg19";
4712     case DW_OP_reg20:
4713       return "DW_OP_reg20";
4714     case DW_OP_reg21:
4715       return "DW_OP_reg21";
4716     case DW_OP_reg22:
4717       return "DW_OP_reg22";
4718     case DW_OP_reg23:
4719       return "DW_OP_reg23";
4720     case DW_OP_reg24:
4721       return "DW_OP_reg24";
4722     case DW_OP_reg25:
4723       return "DW_OP_reg25";
4724     case DW_OP_reg26:
4725       return "DW_OP_reg26";
4726     case DW_OP_reg27:
4727       return "DW_OP_reg27";
4728     case DW_OP_reg28:
4729       return "DW_OP_reg28";
4730     case DW_OP_reg29:
4731       return "DW_OP_reg29";
4732     case DW_OP_reg30:
4733       return "DW_OP_reg30";
4734     case DW_OP_reg31:
4735       return "DW_OP_reg31";
4736     case DW_OP_breg0:
4737       return "DW_OP_breg0";
4738     case DW_OP_breg1:
4739       return "DW_OP_breg1";
4740     case DW_OP_breg2:
4741       return "DW_OP_breg2";
4742     case DW_OP_breg3:
4743       return "DW_OP_breg3";
4744     case DW_OP_breg4:
4745       return "DW_OP_breg4";
4746     case DW_OP_breg5:
4747       return "DW_OP_breg5";
4748     case DW_OP_breg6:
4749       return "DW_OP_breg6";
4750     case DW_OP_breg7:
4751       return "DW_OP_breg7";
4752     case DW_OP_breg8:
4753       return "DW_OP_breg8";
4754     case DW_OP_breg9:
4755       return "DW_OP_breg9";
4756     case DW_OP_breg10:
4757       return "DW_OP_breg10";
4758     case DW_OP_breg11:
4759       return "DW_OP_breg11";
4760     case DW_OP_breg12:
4761       return "DW_OP_breg12";
4762     case DW_OP_breg13:
4763       return "DW_OP_breg13";
4764     case DW_OP_breg14:
4765       return "DW_OP_breg14";
4766     case DW_OP_breg15:
4767       return "DW_OP_breg15";
4768     case DW_OP_breg16:
4769       return "DW_OP_breg16";
4770     case DW_OP_breg17:
4771       return "DW_OP_breg17";
4772     case DW_OP_breg18:
4773       return "DW_OP_breg18";
4774     case DW_OP_breg19:
4775       return "DW_OP_breg19";
4776     case DW_OP_breg20:
4777       return "DW_OP_breg20";
4778     case DW_OP_breg21:
4779       return "DW_OP_breg21";
4780     case DW_OP_breg22:
4781       return "DW_OP_breg22";
4782     case DW_OP_breg23:
4783       return "DW_OP_breg23";
4784     case DW_OP_breg24:
4785       return "DW_OP_breg24";
4786     case DW_OP_breg25:
4787       return "DW_OP_breg25";
4788     case DW_OP_breg26:
4789       return "DW_OP_breg26";
4790     case DW_OP_breg27:
4791       return "DW_OP_breg27";
4792     case DW_OP_breg28:
4793       return "DW_OP_breg28";
4794     case DW_OP_breg29:
4795       return "DW_OP_breg29";
4796     case DW_OP_breg30:
4797       return "DW_OP_breg30";
4798     case DW_OP_breg31:
4799       return "DW_OP_breg31";
4800     case DW_OP_regx:
4801       return "DW_OP_regx";
4802     case DW_OP_fbreg:
4803       return "DW_OP_fbreg";
4804     case DW_OP_bregx:
4805       return "DW_OP_bregx";
4806     case DW_OP_piece:
4807       return "DW_OP_piece";
4808     case DW_OP_deref_size:
4809       return "DW_OP_deref_size";
4810     case DW_OP_xderef_size:
4811       return "DW_OP_xderef_size";
4812     case DW_OP_nop:
4813       return "DW_OP_nop";
4814
4815     case DW_OP_push_object_address:
4816       return "DW_OP_push_object_address";
4817     case DW_OP_call2:
4818       return "DW_OP_call2";
4819     case DW_OP_call4:
4820       return "DW_OP_call4";
4821     case DW_OP_call_ref:
4822       return "DW_OP_call_ref";
4823     case DW_OP_implicit_value:
4824       return "DW_OP_implicit_value";
4825     case DW_OP_stack_value:
4826       return "DW_OP_stack_value";
4827     case DW_OP_form_tls_address:
4828       return "DW_OP_form_tls_address";
4829     case DW_OP_call_frame_cfa:
4830       return "DW_OP_call_frame_cfa";
4831     case DW_OP_bit_piece:
4832       return "DW_OP_bit_piece";
4833
4834     case DW_OP_GNU_push_tls_address:
4835       return "DW_OP_GNU_push_tls_address";
4836     case DW_OP_GNU_uninit:
4837       return "DW_OP_GNU_uninit";
4838     case DW_OP_GNU_encoded_addr:
4839       return "DW_OP_GNU_encoded_addr";
4840     case DW_OP_GNU_implicit_pointer:
4841       return "DW_OP_GNU_implicit_pointer";
4842     case DW_OP_GNU_entry_value:
4843       return "DW_OP_GNU_entry_value";
4844     case DW_OP_GNU_const_type:
4845       return "DW_OP_GNU_const_type";
4846     case DW_OP_GNU_regval_type:
4847       return "DW_OP_GNU_regval_type";
4848     case DW_OP_GNU_deref_type:
4849       return "DW_OP_GNU_deref_type";
4850     case DW_OP_GNU_convert:
4851       return "DW_OP_GNU_convert";
4852     case DW_OP_GNU_reinterpret:
4853       return "DW_OP_GNU_reinterpret";
4854     case DW_OP_GNU_parameter_ref:
4855       return "DW_OP_GNU_parameter_ref";
4856
4857     default:
4858       return "OP_<unknown>";
4859     }
4860 }
4861
4862 /* Return a pointer to a newly allocated location description.  Location
4863    descriptions are simple expression terms that can be strung
4864    together to form more complicated location (address) descriptions.  */
4865
4866 static inline dw_loc_descr_ref
4867 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4868                unsigned HOST_WIDE_INT oprnd2)
4869 {
4870   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4871
4872   descr->dw_loc_opc = op;
4873   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4874   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4875   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4876   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4877
4878   return descr;
4879 }
4880
4881 /* Return a pointer to a newly allocated location description for
4882    REG and OFFSET.  */
4883
4884 static inline dw_loc_descr_ref
4885 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4886 {
4887   if (reg <= 31)
4888     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4889                           offset, 0);
4890   else
4891     return new_loc_descr (DW_OP_bregx, reg, offset);
4892 }
4893
4894 /* Add a location description term to a location description expression.  */
4895
4896 static inline void
4897 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4898 {
4899   dw_loc_descr_ref *d;
4900
4901   /* Find the end of the chain.  */
4902   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4903     ;
4904
4905   *d = descr;
4906 }
4907
4908 /* Add a constant OFFSET to a location expression.  */
4909
4910 static void
4911 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4912 {
4913   dw_loc_descr_ref loc;
4914   HOST_WIDE_INT *p;
4915
4916   gcc_assert (*list_head != NULL);
4917
4918   if (!offset)
4919     return;
4920
4921   /* Find the end of the chain.  */
4922   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4923     ;
4924
4925   p = NULL;
4926   if (loc->dw_loc_opc == DW_OP_fbreg
4927       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4928     p = &loc->dw_loc_oprnd1.v.val_int;
4929   else if (loc->dw_loc_opc == DW_OP_bregx)
4930     p = &loc->dw_loc_oprnd2.v.val_int;
4931
4932   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4933      offset.  Don't optimize if an signed integer overflow would happen.  */
4934   if (p != NULL
4935       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4936           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4937     *p += offset;
4938
4939   else if (offset > 0)
4940     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4941
4942   else
4943     {
4944       loc->dw_loc_next = int_loc_descriptor (-offset);
4945       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4946     }
4947 }
4948
4949 /* Add a constant OFFSET to a location list.  */
4950
4951 static void
4952 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4953 {
4954   dw_loc_list_ref d;
4955   for (d = list_head; d != NULL; d = d->dw_loc_next)
4956     loc_descr_plus_const (&d->expr, offset);
4957 }
4958
4959 #define DWARF_REF_SIZE  \
4960   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4961
4962 static unsigned long size_of_locs (dw_loc_descr_ref);
4963 static unsigned long int get_base_type_offset (dw_die_ref);
4964
4965 /* Return the size of a location descriptor.  */
4966
4967 static unsigned long
4968 size_of_loc_descr (dw_loc_descr_ref loc)
4969 {
4970   unsigned long size = 1;
4971
4972   switch (loc->dw_loc_opc)
4973     {
4974     case DW_OP_addr:
4975       size += DWARF2_ADDR_SIZE;
4976       break;
4977     case DW_OP_const1u:
4978     case DW_OP_const1s:
4979       size += 1;
4980       break;
4981     case DW_OP_const2u:
4982     case DW_OP_const2s:
4983       size += 2;
4984       break;
4985     case DW_OP_const4u:
4986     case DW_OP_const4s:
4987       size += 4;
4988       break;
4989     case DW_OP_const8u:
4990     case DW_OP_const8s:
4991       size += 8;
4992       break;
4993     case DW_OP_constu:
4994       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4995       break;
4996     case DW_OP_consts:
4997       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4998       break;
4999     case DW_OP_pick:
5000       size += 1;
5001       break;
5002     case DW_OP_plus_uconst:
5003       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5004       break;
5005     case DW_OP_skip:
5006     case DW_OP_bra:
5007       size += 2;
5008       break;
5009     case DW_OP_breg0:
5010     case DW_OP_breg1:
5011     case DW_OP_breg2:
5012     case DW_OP_breg3:
5013     case DW_OP_breg4:
5014     case DW_OP_breg5:
5015     case DW_OP_breg6:
5016     case DW_OP_breg7:
5017     case DW_OP_breg8:
5018     case DW_OP_breg9:
5019     case DW_OP_breg10:
5020     case DW_OP_breg11:
5021     case DW_OP_breg12:
5022     case DW_OP_breg13:
5023     case DW_OP_breg14:
5024     case DW_OP_breg15:
5025     case DW_OP_breg16:
5026     case DW_OP_breg17:
5027     case DW_OP_breg18:
5028     case DW_OP_breg19:
5029     case DW_OP_breg20:
5030     case DW_OP_breg21:
5031     case DW_OP_breg22:
5032     case DW_OP_breg23:
5033     case DW_OP_breg24:
5034     case DW_OP_breg25:
5035     case DW_OP_breg26:
5036     case DW_OP_breg27:
5037     case DW_OP_breg28:
5038     case DW_OP_breg29:
5039     case DW_OP_breg30:
5040     case DW_OP_breg31:
5041       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
5042       break;
5043     case DW_OP_regx:
5044       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5045       break;
5046     case DW_OP_fbreg:
5047       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
5048       break;
5049     case DW_OP_bregx:
5050       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5051       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5052       break;
5053     case DW_OP_piece:
5054       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5055       break;
5056     case DW_OP_bit_piece:
5057       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5058       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5059       break;
5060     case DW_OP_deref_size:
5061     case DW_OP_xderef_size:
5062       size += 1;
5063       break;
5064     case DW_OP_call2:
5065       size += 2;
5066       break;
5067     case DW_OP_call4:
5068       size += 4;
5069       break;
5070     case DW_OP_call_ref:
5071       size += DWARF_REF_SIZE;
5072       break;
5073     case DW_OP_implicit_value:
5074       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5075               + loc->dw_loc_oprnd1.v.val_unsigned;
5076       break;
5077     case DW_OP_GNU_implicit_pointer:
5078       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5079       break;
5080     case DW_OP_GNU_entry_value:
5081       {
5082         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5083         size += size_of_uleb128 (op_size) + op_size;
5084         break;
5085       }
5086     case DW_OP_GNU_const_type:
5087       {
5088         unsigned long o
5089           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5090         size += size_of_uleb128 (o) + 1;
5091         switch (loc->dw_loc_oprnd2.val_class)
5092           {
5093           case dw_val_class_vec:
5094             size += loc->dw_loc_oprnd2.v.val_vec.length
5095                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
5096             break;
5097           case dw_val_class_const:
5098             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5099             break;
5100           case dw_val_class_const_double:
5101             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5102             break;
5103           default:
5104             gcc_unreachable ();
5105           }
5106         break;
5107       }
5108     case DW_OP_GNU_regval_type:
5109       {
5110         unsigned long o
5111           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5112         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5113                 + size_of_uleb128 (o);
5114       }
5115       break;
5116     case DW_OP_GNU_deref_type:
5117       {
5118         unsigned long o
5119           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5120         size += 1 + size_of_uleb128 (o);
5121       }
5122       break;
5123     case DW_OP_GNU_convert:
5124     case DW_OP_GNU_reinterpret:
5125       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
5126         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5127       else
5128         {
5129           unsigned long o
5130             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5131           size += size_of_uleb128 (o);
5132         }
5133       break;
5134     case DW_OP_GNU_parameter_ref:
5135       size += 4;
5136       break;
5137     default:
5138       break;
5139     }
5140
5141   return size;
5142 }
5143
5144 /* Return the size of a series of location descriptors.  */
5145
5146 static unsigned long
5147 size_of_locs (dw_loc_descr_ref loc)
5148 {
5149   dw_loc_descr_ref l;
5150   unsigned long size;
5151
5152   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5153      field, to avoid writing to a PCH file.  */
5154   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5155     {
5156       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5157         break;
5158       size += size_of_loc_descr (l);
5159     }
5160   if (! l)
5161     return size;
5162
5163   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5164     {
5165       l->dw_loc_addr = size;
5166       size += size_of_loc_descr (l);
5167     }
5168
5169   return size;
5170 }
5171
5172 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5173 static void get_ref_die_offset_label (char *, dw_die_ref);
5174 static unsigned long int get_ref_die_offset (dw_die_ref);
5175 static void output_loc_sequence (dw_loc_descr_ref, int);
5176
5177 /* Output location description stack opcode's operands (if any).
5178    The for_eh_or_skip parameter controls whether register numbers are
5179    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5180    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5181    info).  This should be suppressed for the cases that have not been converted
5182    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5183
5184 static void
5185 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5186 {
5187   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5188   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5189
5190   switch (loc->dw_loc_opc)
5191     {
5192 #ifdef DWARF2_DEBUGGING_INFO
5193     case DW_OP_const2u:
5194     case DW_OP_const2s:
5195       dw2_asm_output_data (2, val1->v.val_int, NULL);
5196       break;
5197     case DW_OP_const4u:
5198       if (loc->dtprel)
5199         {
5200           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5201           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5202                                                val1->v.val_addr);
5203           fputc ('\n', asm_out_file);
5204           break;
5205         }
5206       /* FALLTHRU */
5207     case DW_OP_const4s:
5208       dw2_asm_output_data (4, val1->v.val_int, NULL);
5209       break;
5210     case DW_OP_const8u:
5211       if (loc->dtprel)
5212         {
5213           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5214           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5215                                                val1->v.val_addr);
5216           fputc ('\n', asm_out_file);
5217           break;
5218         }
5219       /* FALLTHRU */
5220     case DW_OP_const8s:
5221       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5222       dw2_asm_output_data (8, val1->v.val_int, NULL);
5223       break;
5224     case DW_OP_skip:
5225     case DW_OP_bra:
5226       {
5227         int offset;
5228
5229         gcc_assert (val1->val_class == dw_val_class_loc);
5230         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5231
5232         dw2_asm_output_data (2, offset, NULL);
5233       }
5234       break;
5235     case DW_OP_implicit_value:
5236       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5237       switch (val2->val_class)
5238         {
5239         case dw_val_class_const:
5240           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5241           break;
5242         case dw_val_class_vec:
5243           {
5244             unsigned int elt_size = val2->v.val_vec.elt_size;
5245             unsigned int len = val2->v.val_vec.length;
5246             unsigned int i;
5247             unsigned char *p;
5248
5249             if (elt_size > sizeof (HOST_WIDE_INT))
5250               {
5251                 elt_size /= 2;
5252                 len *= 2;
5253               }
5254             for (i = 0, p = val2->v.val_vec.array;
5255                  i < len;
5256                  i++, p += elt_size)
5257               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5258                                    "fp or vector constant word %u", i);
5259           }
5260           break;
5261         case dw_val_class_const_double:
5262           {
5263             unsigned HOST_WIDE_INT first, second;
5264
5265             if (WORDS_BIG_ENDIAN)
5266               {
5267                 first = val2->v.val_double.high;
5268                 second = val2->v.val_double.low;
5269               }
5270             else
5271               {
5272                 first = val2->v.val_double.low;
5273                 second = val2->v.val_double.high;
5274               }
5275             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5276                                  first, NULL);
5277             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5278                                  second, NULL);
5279           }
5280           break;
5281         case dw_val_class_addr:
5282           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5283           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5284           break;
5285         default:
5286           gcc_unreachable ();
5287         }
5288       break;
5289 #else
5290     case DW_OP_const2u:
5291     case DW_OP_const2s:
5292     case DW_OP_const4u:
5293     case DW_OP_const4s:
5294     case DW_OP_const8u:
5295     case DW_OP_const8s:
5296     case DW_OP_skip:
5297     case DW_OP_bra:
5298     case DW_OP_implicit_value:
5299       /* We currently don't make any attempt to make sure these are
5300          aligned properly like we do for the main unwind info, so
5301          don't support emitting things larger than a byte if we're
5302          only doing unwinding.  */
5303       gcc_unreachable ();
5304 #endif
5305     case DW_OP_const1u:
5306     case DW_OP_const1s:
5307       dw2_asm_output_data (1, val1->v.val_int, NULL);
5308       break;
5309     case DW_OP_constu:
5310       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5311       break;
5312     case DW_OP_consts:
5313       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5314       break;
5315     case DW_OP_pick:
5316       dw2_asm_output_data (1, val1->v.val_int, NULL);
5317       break;
5318     case DW_OP_plus_uconst:
5319       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5320       break;
5321     case DW_OP_breg0:
5322     case DW_OP_breg1:
5323     case DW_OP_breg2:
5324     case DW_OP_breg3:
5325     case DW_OP_breg4:
5326     case DW_OP_breg5:
5327     case DW_OP_breg6:
5328     case DW_OP_breg7:
5329     case DW_OP_breg8:
5330     case DW_OP_breg9:
5331     case DW_OP_breg10:
5332     case DW_OP_breg11:
5333     case DW_OP_breg12:
5334     case DW_OP_breg13:
5335     case DW_OP_breg14:
5336     case DW_OP_breg15:
5337     case DW_OP_breg16:
5338     case DW_OP_breg17:
5339     case DW_OP_breg18:
5340     case DW_OP_breg19:
5341     case DW_OP_breg20:
5342     case DW_OP_breg21:
5343     case DW_OP_breg22:
5344     case DW_OP_breg23:
5345     case DW_OP_breg24:
5346     case DW_OP_breg25:
5347     case DW_OP_breg26:
5348     case DW_OP_breg27:
5349     case DW_OP_breg28:
5350     case DW_OP_breg29:
5351     case DW_OP_breg30:
5352     case DW_OP_breg31:
5353       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5354       break;
5355     case DW_OP_regx:
5356       {
5357         unsigned r = val1->v.val_unsigned;
5358         if (for_eh_or_skip >= 0)
5359           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5360         gcc_assert (size_of_uleb128 (r) 
5361                     == size_of_uleb128 (val1->v.val_unsigned));
5362         dw2_asm_output_data_uleb128 (r, NULL);  
5363       }
5364       break;
5365     case DW_OP_fbreg:
5366       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5367       break;
5368     case DW_OP_bregx:
5369       {
5370         unsigned r = val1->v.val_unsigned;
5371         if (for_eh_or_skip >= 0)
5372           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5373         gcc_assert (size_of_uleb128 (r) 
5374                     == size_of_uleb128 (val1->v.val_unsigned));
5375         dw2_asm_output_data_uleb128 (r, NULL);  
5376         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5377       }
5378       break;
5379     case DW_OP_piece:
5380       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5381       break;
5382     case DW_OP_bit_piece:
5383       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5384       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5385       break;
5386     case DW_OP_deref_size:
5387     case DW_OP_xderef_size:
5388       dw2_asm_output_data (1, val1->v.val_int, NULL);
5389       break;
5390
5391     case DW_OP_addr:
5392       if (loc->dtprel)
5393         {
5394           if (targetm.asm_out.output_dwarf_dtprel)
5395             {
5396               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5397                                                    DWARF2_ADDR_SIZE,
5398                                                    val1->v.val_addr);
5399               fputc ('\n', asm_out_file);
5400             }
5401           else
5402             gcc_unreachable ();
5403         }
5404       else
5405         {
5406 #ifdef DWARF2_DEBUGGING_INFO
5407           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5408 #else
5409           gcc_unreachable ();
5410 #endif
5411         }
5412       break;
5413
5414     case DW_OP_GNU_implicit_pointer:
5415       {
5416         char label[MAX_ARTIFICIAL_LABEL_BYTES
5417                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5418         gcc_assert (val1->val_class == dw_val_class_die_ref);
5419         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5420         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5421         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5422       }
5423       break;
5424
5425     case DW_OP_GNU_entry_value:
5426       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5427       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5428       break;
5429
5430     case DW_OP_GNU_const_type:
5431       {
5432         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
5433         gcc_assert (o);
5434         dw2_asm_output_data_uleb128 (o, NULL);
5435         switch (val2->val_class)
5436           {
5437           case dw_val_class_const:
5438             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5439             dw2_asm_output_data (1, l, NULL);
5440             dw2_asm_output_data (l, val2->v.val_int, NULL);
5441             break;
5442           case dw_val_class_vec:
5443             {
5444               unsigned int elt_size = val2->v.val_vec.elt_size;
5445               unsigned int len = val2->v.val_vec.length;
5446               unsigned int i;
5447               unsigned char *p;
5448
5449               l = len * elt_size;
5450               dw2_asm_output_data (1, l, NULL);
5451               if (elt_size > sizeof (HOST_WIDE_INT))
5452                 {
5453                   elt_size /= 2;
5454                   len *= 2;
5455                 }
5456               for (i = 0, p = val2->v.val_vec.array;
5457                    i < len;
5458                    i++, p += elt_size)
5459                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5460                                      "fp or vector constant word %u", i);
5461             }
5462             break;
5463           case dw_val_class_const_double:
5464             {
5465               unsigned HOST_WIDE_INT first, second;
5466               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5467
5468               dw2_asm_output_data (1, 2 * l, NULL);
5469               if (WORDS_BIG_ENDIAN)
5470                 {
5471                   first = val2->v.val_double.high;
5472                   second = val2->v.val_double.low;
5473                 }
5474               else
5475                 {
5476                   first = val2->v.val_double.low;
5477                   second = val2->v.val_double.high;
5478                 }
5479               dw2_asm_output_data (l, first, NULL);
5480               dw2_asm_output_data (l, second, NULL);
5481             }
5482             break;
5483           default:
5484             gcc_unreachable ();
5485           }
5486       }
5487       break;
5488     case DW_OP_GNU_regval_type:
5489       {
5490         unsigned r = val1->v.val_unsigned;
5491         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5492         gcc_assert (o);
5493         if (for_eh_or_skip >= 0)
5494           {
5495             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5496             gcc_assert (size_of_uleb128 (r)
5497                         == size_of_uleb128 (val1->v.val_unsigned));
5498           }
5499         dw2_asm_output_data_uleb128 (r, NULL);
5500         dw2_asm_output_data_uleb128 (o, NULL);
5501       }
5502       break;
5503     case DW_OP_GNU_deref_type:
5504       {
5505         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5506         gcc_assert (o);
5507         dw2_asm_output_data (1, val1->v.val_int, NULL);
5508         dw2_asm_output_data_uleb128 (o, NULL);
5509       }
5510       break;
5511     case DW_OP_GNU_convert:
5512     case DW_OP_GNU_reinterpret:
5513       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
5514         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5515       else
5516         {
5517           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
5518           gcc_assert (o);
5519           dw2_asm_output_data_uleb128 (o, NULL);
5520         }
5521       break;
5522
5523     case DW_OP_GNU_parameter_ref:
5524       {
5525         unsigned long o;
5526         gcc_assert (val1->val_class == dw_val_class_die_ref);
5527         o = get_ref_die_offset (val1->v.val_die_ref.die);
5528         dw2_asm_output_data (4, o, NULL);
5529       }
5530       break;
5531
5532     default:
5533       /* Other codes have no operands.  */
5534       break;
5535     }
5536 }
5537
5538 /* Output a sequence of location operations.  
5539    The for_eh_or_skip parameter controls whether register numbers are
5540    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5541    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5542    info).  This should be suppressed for the cases that have not been converted
5543    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5544
5545 static void
5546 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5547 {
5548   for (; loc != NULL; loc = loc->dw_loc_next)
5549     {
5550       enum dwarf_location_atom opc = loc->dw_loc_opc;
5551       /* Output the opcode.  */
5552       if (for_eh_or_skip >= 0 
5553           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5554         {
5555           unsigned r = (opc - DW_OP_breg0);
5556           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5557           gcc_assert (r <= 31);
5558           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5559         }
5560       else if (for_eh_or_skip >= 0 
5561                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5562         {
5563           unsigned r = (opc - DW_OP_reg0);
5564           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5565           gcc_assert (r <= 31);
5566           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5567         }
5568
5569       dw2_asm_output_data (1, opc,
5570                              "%s", dwarf_stack_op_name (opc));
5571
5572       /* Output the operand(s) (if any).  */
5573       output_loc_operands (loc, for_eh_or_skip);
5574     }
5575 }
5576
5577 /* Output location description stack opcode's operands (if any).
5578    The output is single bytes on a line, suitable for .cfi_escape.  */
5579
5580 static void
5581 output_loc_operands_raw (dw_loc_descr_ref loc)
5582 {
5583   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5584   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5585
5586   switch (loc->dw_loc_opc)
5587     {
5588     case DW_OP_addr:
5589     case DW_OP_implicit_value:
5590       /* We cannot output addresses in .cfi_escape, only bytes.  */
5591       gcc_unreachable ();
5592
5593     case DW_OP_const1u:
5594     case DW_OP_const1s:
5595     case DW_OP_pick:
5596     case DW_OP_deref_size:
5597     case DW_OP_xderef_size:
5598       fputc (',', asm_out_file);
5599       dw2_asm_output_data_raw (1, val1->v.val_int);
5600       break;
5601
5602     case DW_OP_const2u:
5603     case DW_OP_const2s:
5604       fputc (',', asm_out_file);
5605       dw2_asm_output_data_raw (2, val1->v.val_int);
5606       break;
5607
5608     case DW_OP_const4u:
5609     case DW_OP_const4s:
5610       fputc (',', asm_out_file);
5611       dw2_asm_output_data_raw (4, val1->v.val_int);
5612       break;
5613
5614     case DW_OP_const8u:
5615     case DW_OP_const8s:
5616       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5617       fputc (',', asm_out_file);
5618       dw2_asm_output_data_raw (8, val1->v.val_int);
5619       break;
5620
5621     case DW_OP_skip:
5622     case DW_OP_bra:
5623       {
5624         int offset;
5625
5626         gcc_assert (val1->val_class == dw_val_class_loc);
5627         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5628
5629         fputc (',', asm_out_file);
5630         dw2_asm_output_data_raw (2, offset);
5631       }
5632       break;
5633
5634     case DW_OP_regx:
5635       {
5636         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5637         gcc_assert (size_of_uleb128 (r) 
5638                     == size_of_uleb128 (val1->v.val_unsigned));
5639         fputc (',', asm_out_file);
5640         dw2_asm_output_data_uleb128_raw (r);
5641       }
5642       break;
5643       
5644     case DW_OP_constu:
5645     case DW_OP_plus_uconst:
5646     case DW_OP_piece:
5647       fputc (',', asm_out_file);
5648       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5649       break;
5650
5651     case DW_OP_bit_piece:
5652       fputc (',', asm_out_file);
5653       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5654       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5655       break;
5656
5657     case DW_OP_consts:
5658     case DW_OP_breg0:
5659     case DW_OP_breg1:
5660     case DW_OP_breg2:
5661     case DW_OP_breg3:
5662     case DW_OP_breg4:
5663     case DW_OP_breg5:
5664     case DW_OP_breg6:
5665     case DW_OP_breg7:
5666     case DW_OP_breg8:
5667     case DW_OP_breg9:
5668     case DW_OP_breg10:
5669     case DW_OP_breg11:
5670     case DW_OP_breg12:
5671     case DW_OP_breg13:
5672     case DW_OP_breg14:
5673     case DW_OP_breg15:
5674     case DW_OP_breg16:
5675     case DW_OP_breg17:
5676     case DW_OP_breg18:
5677     case DW_OP_breg19:
5678     case DW_OP_breg20:
5679     case DW_OP_breg21:
5680     case DW_OP_breg22:
5681     case DW_OP_breg23:
5682     case DW_OP_breg24:
5683     case DW_OP_breg25:
5684     case DW_OP_breg26:
5685     case DW_OP_breg27:
5686     case DW_OP_breg28:
5687     case DW_OP_breg29:
5688     case DW_OP_breg30:
5689     case DW_OP_breg31:
5690     case DW_OP_fbreg:
5691       fputc (',', asm_out_file);
5692       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5693       break;
5694
5695     case DW_OP_bregx:
5696       {
5697         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5698         gcc_assert (size_of_uleb128 (r) 
5699                     == size_of_uleb128 (val1->v.val_unsigned));
5700         fputc (',', asm_out_file);
5701         dw2_asm_output_data_uleb128_raw (r);
5702         fputc (',', asm_out_file);
5703         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5704       }
5705       break;
5706
5707     case DW_OP_GNU_implicit_pointer:
5708     case DW_OP_GNU_entry_value:
5709     case DW_OP_GNU_const_type:
5710     case DW_OP_GNU_regval_type:
5711     case DW_OP_GNU_deref_type:
5712     case DW_OP_GNU_convert:
5713     case DW_OP_GNU_reinterpret:
5714     case DW_OP_GNU_parameter_ref:
5715       gcc_unreachable ();
5716       break;
5717
5718     default:
5719       /* Other codes have no operands.  */
5720       break;
5721     }
5722 }
5723
5724 static void
5725 output_loc_sequence_raw (dw_loc_descr_ref loc)
5726 {
5727   while (1)
5728     {
5729       enum dwarf_location_atom opc = loc->dw_loc_opc;
5730       /* Output the opcode.  */
5731       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5732         {
5733           unsigned r = (opc - DW_OP_breg0);
5734           r = DWARF2_FRAME_REG_OUT (r, 1);
5735           gcc_assert (r <= 31);
5736           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5737         }
5738       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5739         {
5740           unsigned r = (opc - DW_OP_reg0);
5741           r = DWARF2_FRAME_REG_OUT (r, 1);
5742           gcc_assert (r <= 31);
5743           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5744         }
5745       /* Output the opcode.  */
5746       fprintf (asm_out_file, "%#x", opc);
5747       output_loc_operands_raw (loc);
5748
5749       if (!loc->dw_loc_next)
5750         break;
5751       loc = loc->dw_loc_next;
5752
5753       fputc (',', asm_out_file);
5754     }
5755 }
5756
5757 /* This routine will generate the correct assembly data for a location
5758    description based on a cfi entry with a complex address.  */
5759
5760 static void
5761 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5762 {
5763   dw_loc_descr_ref loc;
5764   unsigned long size;
5765
5766   if (cfi->dw_cfi_opc == DW_CFA_expression)
5767     {
5768       unsigned r = 
5769         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5770       dw2_asm_output_data (1, r, NULL);
5771       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5772     }
5773   else
5774     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5775
5776   /* Output the size of the block.  */
5777   size = size_of_locs (loc);
5778   dw2_asm_output_data_uleb128 (size, NULL);
5779
5780   /* Now output the operations themselves.  */
5781   output_loc_sequence (loc, for_eh);
5782 }
5783
5784 /* Similar, but used for .cfi_escape.  */
5785
5786 static void
5787 output_cfa_loc_raw (dw_cfi_ref cfi)
5788 {
5789   dw_loc_descr_ref loc;
5790   unsigned long size;
5791
5792   if (cfi->dw_cfi_opc == DW_CFA_expression)
5793     {
5794       unsigned r = 
5795         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5796       fprintf (asm_out_file, "%#x,", r);
5797       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5798     }
5799   else
5800     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5801
5802   /* Output the size of the block.  */
5803   size = size_of_locs (loc);
5804   dw2_asm_output_data_uleb128_raw (size);
5805   fputc (',', asm_out_file);
5806
5807   /* Now output the operations themselves.  */
5808   output_loc_sequence_raw (loc);
5809 }
5810
5811 /* This function builds a dwarf location descriptor sequence from a
5812    dw_cfa_location, adding the given OFFSET to the result of the
5813    expression.  */
5814
5815 static struct dw_loc_descr_struct *
5816 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5817 {
5818   struct dw_loc_descr_struct *head, *tmp;
5819
5820   offset += cfa->offset;
5821
5822   if (cfa->indirect)
5823     {
5824       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5825       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5826       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5827       add_loc_descr (&head, tmp);
5828       if (offset != 0)
5829         {
5830           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5831           add_loc_descr (&head, tmp);
5832         }
5833     }
5834   else
5835     head = new_reg_loc_descr (cfa->reg, offset);
5836
5837   return head;
5838 }
5839
5840 /* This function builds a dwarf location descriptor sequence for
5841    the address at OFFSET from the CFA when stack is aligned to
5842    ALIGNMENT byte.  */
5843
5844 static struct dw_loc_descr_struct *
5845 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5846 {
5847   struct dw_loc_descr_struct *head;
5848   unsigned int dwarf_fp
5849     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5850
5851  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5852   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5853     {
5854       head = new_reg_loc_descr (dwarf_fp, 0);
5855       add_loc_descr (&head, int_loc_descriptor (alignment));
5856       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5857       loc_descr_plus_const (&head, offset);
5858     }
5859   else
5860     head = new_reg_loc_descr (dwarf_fp, offset);
5861   return head;
5862 }
5863
5864 /* This function fills in aa dw_cfa_location structure from a dwarf location
5865    descriptor sequence.  */
5866
5867 static void
5868 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5869 {
5870   struct dw_loc_descr_struct *ptr;
5871   cfa->offset = 0;
5872   cfa->base_offset = 0;
5873   cfa->indirect = 0;
5874   cfa->reg = -1;
5875
5876   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5877     {
5878       enum dwarf_location_atom op = ptr->dw_loc_opc;
5879
5880       switch (op)
5881         {
5882         case DW_OP_reg0:
5883         case DW_OP_reg1:
5884         case DW_OP_reg2:
5885         case DW_OP_reg3:
5886         case DW_OP_reg4:
5887         case DW_OP_reg5:
5888         case DW_OP_reg6:
5889         case DW_OP_reg7:
5890         case DW_OP_reg8:
5891         case DW_OP_reg9:
5892         case DW_OP_reg10:
5893         case DW_OP_reg11:
5894         case DW_OP_reg12:
5895         case DW_OP_reg13:
5896         case DW_OP_reg14:
5897         case DW_OP_reg15:
5898         case DW_OP_reg16:
5899         case DW_OP_reg17:
5900         case DW_OP_reg18:
5901         case DW_OP_reg19:
5902         case DW_OP_reg20:
5903         case DW_OP_reg21:
5904         case DW_OP_reg22:
5905         case DW_OP_reg23:
5906         case DW_OP_reg24:
5907         case DW_OP_reg25:
5908         case DW_OP_reg26:
5909         case DW_OP_reg27:
5910         case DW_OP_reg28:
5911         case DW_OP_reg29:
5912         case DW_OP_reg30:
5913         case DW_OP_reg31:
5914           cfa->reg = op - DW_OP_reg0;
5915           break;
5916         case DW_OP_regx:
5917           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5918           break;
5919         case DW_OP_breg0:
5920         case DW_OP_breg1:
5921         case DW_OP_breg2:
5922         case DW_OP_breg3:
5923         case DW_OP_breg4:
5924         case DW_OP_breg5:
5925         case DW_OP_breg6:
5926         case DW_OP_breg7:
5927         case DW_OP_breg8:
5928         case DW_OP_breg9:
5929         case DW_OP_breg10:
5930         case DW_OP_breg11:
5931         case DW_OP_breg12:
5932         case DW_OP_breg13:
5933         case DW_OP_breg14:
5934         case DW_OP_breg15:
5935         case DW_OP_breg16:
5936         case DW_OP_breg17:
5937         case DW_OP_breg18:
5938         case DW_OP_breg19:
5939         case DW_OP_breg20:
5940         case DW_OP_breg21:
5941         case DW_OP_breg22:
5942         case DW_OP_breg23:
5943         case DW_OP_breg24:
5944         case DW_OP_breg25:
5945         case DW_OP_breg26:
5946         case DW_OP_breg27:
5947         case DW_OP_breg28:
5948         case DW_OP_breg29:
5949         case DW_OP_breg30:
5950         case DW_OP_breg31:
5951           cfa->reg = op - DW_OP_breg0;
5952           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5953           break;
5954         case DW_OP_bregx:
5955           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5956           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5957           break;
5958         case DW_OP_deref:
5959           cfa->indirect = 1;
5960           break;
5961         case DW_OP_plus_uconst:
5962           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5963           break;
5964         default:
5965           internal_error ("DW_LOC_OP %s not implemented",
5966                           dwarf_stack_op_name (ptr->dw_loc_opc));
5967         }
5968     }
5969 }
5970 \f
5971 /* And now, the support for symbolic debugging information.  */
5972
5973 /* .debug_str support.  */
5974 static int output_indirect_string (void **, void *);
5975
5976 static void dwarf2out_init (const char *);
5977 static void dwarf2out_finish (const char *);
5978 static void dwarf2out_assembly_start (void);
5979 static void dwarf2out_define (unsigned int, const char *);
5980 static void dwarf2out_undef (unsigned int, const char *);
5981 static void dwarf2out_start_source_file (unsigned, const char *);
5982 static void dwarf2out_end_source_file (unsigned);
5983 static void dwarf2out_function_decl (tree);
5984 static void dwarf2out_begin_block (unsigned, unsigned);
5985 static void dwarf2out_end_block (unsigned, unsigned);
5986 static bool dwarf2out_ignore_block (const_tree);
5987 static void dwarf2out_global_decl (tree);
5988 static void dwarf2out_type_decl (tree, int);
5989 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5990 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5991                                                  dw_die_ref);
5992 static void dwarf2out_abstract_function (tree);
5993 static void dwarf2out_var_location (rtx);
5994 static void dwarf2out_begin_function (tree);
5995 static void dwarf2out_set_name (tree, tree);
5996
5997 /* The debug hooks structure.  */
5998
5999 const struct gcc_debug_hooks dwarf2_debug_hooks =
6000 {
6001   dwarf2out_init,
6002   dwarf2out_finish,
6003   dwarf2out_assembly_start,
6004   dwarf2out_define,
6005   dwarf2out_undef,
6006   dwarf2out_start_source_file,
6007   dwarf2out_end_source_file,
6008   dwarf2out_begin_block,
6009   dwarf2out_end_block,
6010   dwarf2out_ignore_block,
6011   dwarf2out_source_line,
6012   dwarf2out_begin_prologue,
6013 #if VMS_DEBUGGING_INFO
6014   dwarf2out_vms_end_prologue,
6015   dwarf2out_vms_begin_epilogue,
6016 #else
6017   debug_nothing_int_charstar,
6018   debug_nothing_int_charstar,
6019 #endif
6020   dwarf2out_end_epilogue,
6021   dwarf2out_begin_function,
6022   debug_nothing_int,            /* end_function */
6023   dwarf2out_function_decl,      /* function_decl */
6024   dwarf2out_global_decl,
6025   dwarf2out_type_decl,          /* type_decl */
6026   dwarf2out_imported_module_or_decl,
6027   debug_nothing_tree,           /* deferred_inline_function */
6028   /* The DWARF 2 backend tries to reduce debugging bloat by not
6029      emitting the abstract description of inline functions until
6030      something tries to reference them.  */
6031   dwarf2out_abstract_function,  /* outlining_inline_function */
6032   debug_nothing_rtx,            /* label */
6033   debug_nothing_int,            /* handle_pch */
6034   dwarf2out_var_location,
6035   dwarf2out_switch_text_section,
6036   dwarf2out_set_name,
6037   1,                            /* start_end_main_source_file */
6038   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
6039 };
6040 \f
6041 /* NOTE: In the comments in this file, many references are made to
6042    "Debugging Information Entries".  This term is abbreviated as `DIE'
6043    throughout the remainder of this file.  */
6044
6045 /* An internal representation of the DWARF output is built, and then
6046    walked to generate the DWARF debugging info.  The walk of the internal
6047    representation is done after the entire program has been compiled.
6048    The types below are used to describe the internal representation.  */
6049
6050 /* Whether to put type DIEs into their own section .debug_types instead
6051    of making them part of the .debug_info section.  Only supported for
6052    Dwarf V4 or higher and the user didn't disable them through
6053    -fno-debug-types-section.  It is more efficient to put them in a
6054    separate comdat sections since the linker will then be able to
6055    remove duplicates.  But not all tools support .debug_types sections
6056    yet.  */
6057
6058 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
6059
6060 /* Various DIE's use offsets relative to the beginning of the
6061    .debug_info section to refer to each other.  */
6062
6063 typedef long int dw_offset;
6064
6065 /* Define typedefs here to avoid circular dependencies.  */
6066
6067 typedef struct dw_attr_struct *dw_attr_ref;
6068 typedef struct dw_line_info_struct *dw_line_info_ref;
6069 typedef struct pubname_struct *pubname_ref;
6070 typedef struct dw_ranges_struct *dw_ranges_ref;
6071 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
6072 typedef struct comdat_type_struct *comdat_type_node_ref;
6073
6074 /* The entries in the line_info table more-or-less mirror the opcodes
6075    that are used in the real dwarf line table.  Arrays of these entries
6076    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
6077    supported.  */
6078
6079 enum dw_line_info_opcode {
6080   /* Emit DW_LNE_set_address; the operand is the label index.  */
6081   LI_set_address,
6082
6083   /* Emit a row to the matrix with the given line.  This may be done
6084      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
6085      special opcodes.  */
6086   LI_set_line,
6087
6088   /* Emit a DW_LNS_set_file.  */
6089   LI_set_file,
6090
6091   /* Emit a DW_LNS_set_column.  */
6092   LI_set_column,
6093
6094   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
6095   LI_negate_stmt,
6096
6097   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
6098   LI_set_prologue_end,
6099   LI_set_epilogue_begin,
6100
6101   /* Emit a DW_LNE_set_discriminator.  */
6102   LI_set_discriminator
6103 };
6104
6105 typedef struct GTY(()) dw_line_info_struct {
6106   enum dw_line_info_opcode opcode;
6107   unsigned int val;
6108 } dw_line_info_entry;
6109
6110 DEF_VEC_O(dw_line_info_entry);
6111 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
6112
6113 typedef struct GTY(()) dw_line_info_table_struct {
6114   /* The label that marks the end of this section.  */
6115   const char *end_label;
6116
6117   /* The values for the last row of the matrix, as collected in the table.
6118      These are used to minimize the changes to the next row.  */
6119   unsigned int file_num;
6120   unsigned int line_num;
6121   unsigned int column_num;
6122   int discrim_num;
6123   bool is_stmt;
6124   bool in_use;
6125
6126   VEC(dw_line_info_entry, gc) *entries;
6127 } dw_line_info_table;
6128
6129 typedef dw_line_info_table *dw_line_info_table_p;
6130
6131 DEF_VEC_P(dw_line_info_table_p);
6132 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
6133
6134 /* Each DIE attribute has a field specifying the attribute kind,
6135    a link to the next attribute in the chain, and an attribute value.
6136    Attributes are typically linked below the DIE they modify.  */
6137
6138 typedef struct GTY(()) dw_attr_struct {
6139   enum dwarf_attribute dw_attr;
6140   dw_val_node dw_attr_val;
6141 }
6142 dw_attr_node;
6143
6144 DEF_VEC_O(dw_attr_node);
6145 DEF_VEC_ALLOC_O(dw_attr_node,gc);
6146
6147 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
6148    The children of each node form a circular list linked by
6149    die_sib.  die_child points to the node *before* the "first" child node.  */
6150
6151 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
6152   union die_symbol_or_type_node
6153     {
6154       char * GTY ((tag ("0"))) die_symbol;
6155       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
6156     }
6157   GTY ((desc ("use_debug_types"))) die_id;
6158   VEC(dw_attr_node,gc) * die_attr;
6159   dw_die_ref die_parent;
6160   dw_die_ref die_child;
6161   dw_die_ref die_sib;
6162   dw_die_ref die_definition; /* ref from a specification to its definition */
6163   dw_offset die_offset;
6164   unsigned long die_abbrev;
6165   int die_mark;
6166   /* Die is used and must not be pruned as unused.  */
6167   int die_perennial_p;
6168   unsigned int decl_id;
6169   enum dwarf_tag die_tag;
6170 }
6171 die_node;
6172
6173 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
6174 #define FOR_EACH_CHILD(die, c, expr) do {       \
6175   c = die->die_child;                           \
6176   if (c) do {                                   \
6177     c = c->die_sib;                             \
6178     expr;                                       \
6179   } while (c != die->die_child);                \
6180 } while (0)
6181
6182 /* The pubname structure */
6183
6184 typedef struct GTY(()) pubname_struct {
6185   dw_die_ref die;
6186   const char *name;
6187 }
6188 pubname_entry;
6189
6190 DEF_VEC_O(pubname_entry);
6191 DEF_VEC_ALLOC_O(pubname_entry, gc);
6192
6193 struct GTY(()) dw_ranges_struct {
6194   /* If this is positive, it's a block number, otherwise it's a
6195      bitwise-negated index into dw_ranges_by_label.  */
6196   int num;
6197 };
6198
6199 /* A structure to hold a macinfo entry.  */
6200
6201 typedef struct GTY(()) macinfo_struct {
6202   unsigned HOST_WIDE_INT code;
6203   unsigned HOST_WIDE_INT lineno;
6204   const char *info;
6205 }
6206 macinfo_entry;
6207
6208 DEF_VEC_O(macinfo_entry);
6209 DEF_VEC_ALLOC_O(macinfo_entry, gc);
6210
6211 struct GTY(()) dw_ranges_by_label_struct {
6212   const char *begin;
6213   const char *end;
6214 };
6215
6216 /* The comdat type node structure.  */
6217 typedef struct GTY(()) comdat_type_struct
6218 {
6219   dw_die_ref root_die;
6220   dw_die_ref type_die;
6221   char signature[DWARF_TYPE_SIGNATURE_SIZE];
6222   struct comdat_type_struct *next;
6223 }
6224 comdat_type_node;
6225
6226 /* The limbo die list structure.  */
6227 typedef struct GTY(()) limbo_die_struct {
6228   dw_die_ref die;
6229   tree created_for;
6230   struct limbo_die_struct *next;
6231 }
6232 limbo_die_node;
6233
6234 typedef struct skeleton_chain_struct
6235 {
6236   dw_die_ref old_die;
6237   dw_die_ref new_die;
6238   struct skeleton_chain_struct *parent;
6239 }
6240 skeleton_chain_node;
6241
6242 /* How to start an assembler comment.  */
6243 #ifndef ASM_COMMENT_START
6244 #define ASM_COMMENT_START ";#"
6245 #endif
6246
6247 /* Define a macro which returns nonzero for a TYPE_DECL which was
6248    implicitly generated for a type.
6249
6250    Note that, unlike the C front-end (which generates a NULL named
6251    TYPE_DECL node for each complete tagged type, each array type,
6252    and each function type node created) the C++ front-end generates
6253    a _named_ TYPE_DECL node for each tagged type node created.
6254    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
6255    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
6256    front-end, but for each type, tagged or not.  */
6257
6258 #define TYPE_DECL_IS_STUB(decl)                         \
6259   (DECL_NAME (decl) == NULL_TREE                        \
6260    || (DECL_ARTIFICIAL (decl)                           \
6261        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
6262            /* This is necessary for stub decls that     \
6263               appear in nested inline functions.  */    \
6264            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6265                && (decl_ultimate_origin (decl)          \
6266                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6267
6268 /* Information concerning the compilation unit's programming
6269    language, and compiler version.  */
6270
6271 /* Fixed size portion of the DWARF compilation unit header.  */
6272 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6273   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6274
6275 /* Fixed size portion of the DWARF comdat type unit header.  */
6276 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6277   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6278    + DWARF_OFFSET_SIZE)
6279
6280 /* Fixed size portion of public names info.  */
6281 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6282
6283 /* Fixed size portion of the address range info.  */
6284 #define DWARF_ARANGES_HEADER_SIZE                                       \
6285   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6286                 DWARF2_ADDR_SIZE * 2)                                   \
6287    - DWARF_INITIAL_LENGTH_SIZE)
6288
6289 /* Size of padding portion in the address range info.  It must be
6290    aligned to twice the pointer size.  */
6291 #define DWARF_ARANGES_PAD_SIZE \
6292   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6293                 DWARF2_ADDR_SIZE * 2)                              \
6294    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6295
6296 /* Use assembler line directives if available.  */
6297 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6298 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6299 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6300 #else
6301 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6302 #endif
6303 #endif
6304
6305 /* Minimum line offset in a special line info. opcode.
6306    This value was chosen to give a reasonable range of values.  */
6307 #define DWARF_LINE_BASE  -10
6308
6309 /* First special line opcode - leave room for the standard opcodes.  */
6310 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
6311
6312 /* Range of line offsets in a special line info. opcode.  */
6313 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6314
6315 /* Flag that indicates the initial value of the is_stmt_start flag.
6316    In the present implementation, we do not mark any lines as
6317    the beginning of a source statement, because that information
6318    is not made available by the GCC front-end.  */
6319 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6320
6321 /* Maximum number of operations per instruction bundle.  */
6322 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6323 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6324 #endif
6325
6326 /* This location is used by calc_die_sizes() to keep track
6327    the offset of each DIE within the .debug_info section.  */
6328 static unsigned long next_die_offset;
6329
6330 /* Record the root of the DIE's built for the current compilation unit.  */
6331 static GTY(()) dw_die_ref single_comp_unit_die;
6332
6333 /* A list of type DIEs that have been separated into comdat sections.  */
6334 static GTY(()) comdat_type_node *comdat_type_list;
6335
6336 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6337 static GTY(()) limbo_die_node *limbo_die_list;
6338
6339 /* A list of DIEs for which we may have to generate
6340    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6341 static GTY(()) limbo_die_node *deferred_asm_name;
6342
6343 /* Filenames referenced by this compilation unit.  */
6344 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6345
6346 /* A hash table of references to DIE's that describe declarations.
6347    The key is a DECL_UID() which is a unique number identifying each decl.  */
6348 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6349
6350 /* A hash table of references to DIE's that describe COMMON blocks.
6351    The key is DECL_UID() ^ die_parent.  */
6352 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6353
6354 typedef struct GTY(()) die_arg_entry_struct {
6355     dw_die_ref die;
6356     tree arg;
6357 } die_arg_entry;
6358
6359 DEF_VEC_O(die_arg_entry);
6360 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6361
6362 /* Node of the variable location list.  */
6363 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6364   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6365      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6366      in mode of the EXPR_LIST node and first EXPR_LIST operand
6367      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6368      location or NULL for padding.  For larger bitsizes,
6369      mode is 0 and first operand is a CONCAT with bitsize
6370      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6371      NULL as second operand.  */
6372   rtx GTY (()) loc;
6373   const char * GTY (()) label;
6374   struct var_loc_node * GTY (()) next;
6375 };
6376
6377 /* Variable location list.  */
6378 struct GTY (()) var_loc_list_def {
6379   struct var_loc_node * GTY (()) first;
6380
6381   /* Pointer to the last but one or last element of the
6382      chained list.  If the list is empty, both first and
6383      last are NULL, if the list contains just one node
6384      or the last node certainly is not redundant, it points
6385      to the last node, otherwise points to the last but one.
6386      Do not mark it for GC because it is marked through the chain.  */
6387   struct var_loc_node * GTY ((skip ("%h"))) last;
6388
6389   /* Pointer to the last element before section switch,
6390      if NULL, either sections weren't switched or first
6391      is after section switch.  */
6392   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6393
6394   /* DECL_UID of the variable decl.  */
6395   unsigned int decl_id;
6396 };
6397 typedef struct var_loc_list_def var_loc_list;
6398
6399 /* Call argument location list.  */
6400 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6401   rtx GTY (()) call_arg_loc_note;
6402   const char * GTY (()) label;
6403   tree GTY (()) block;
6404   bool tail_call_p;
6405   rtx GTY (()) symbol_ref;
6406   struct call_arg_loc_node * GTY (()) next;
6407 };
6408
6409
6410 /* Table of decl location linked lists.  */
6411 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6412
6413 /* Head and tail of call_arg_loc chain.  */
6414 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6415 static struct call_arg_loc_node *call_arg_loc_last;
6416
6417 /* Number of call sites in the current function.  */
6418 static int call_site_count = -1;
6419 /* Number of tail call sites in the current function.  */
6420 static int tail_call_site_count = -1;
6421
6422 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6423    DIEs.  */
6424 static VEC (dw_die_ref, heap) *block_map;
6425
6426 /* A cached location list.  */
6427 struct GTY (()) cached_dw_loc_list_def {
6428   /* The DECL_UID of the decl that this entry describes.  */
6429   unsigned int decl_id;
6430
6431   /* The cached location list.  */
6432   dw_loc_list_ref loc_list;
6433 };
6434 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6435
6436 /* Table of cached location lists.  */
6437 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6438
6439 /* A pointer to the base of a list of references to DIE's that
6440    are uniquely identified by their tag, presence/absence of
6441    children DIE's, and list of attribute/value pairs.  */
6442 static GTY((length ("abbrev_die_table_allocated")))
6443   dw_die_ref *abbrev_die_table;
6444
6445 /* Number of elements currently allocated for abbrev_die_table.  */
6446 static GTY(()) unsigned abbrev_die_table_allocated;
6447
6448 /* Number of elements in type_die_table currently in use.  */
6449 static GTY(()) unsigned abbrev_die_table_in_use;
6450
6451 /* Size (in elements) of increments by which we may expand the
6452    abbrev_die_table.  */
6453 #define ABBREV_DIE_TABLE_INCREMENT 256
6454
6455 /* A global counter for generating labels for line number data.  */
6456 static unsigned int line_info_label_num;
6457
6458 /* The current table to which we should emit line number information
6459    for the current function.  This will be set up at the beginning of
6460    assembly for the function.  */
6461 static dw_line_info_table *cur_line_info_table;
6462
6463 /* The two default tables of line number info.  */
6464 static GTY(()) dw_line_info_table *text_section_line_info;
6465 static GTY(()) dw_line_info_table *cold_text_section_line_info;
6466
6467 /* The set of all non-default tables of line number info.  */
6468 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
6469
6470 /* A flag to tell pubnames/types export if there is an info section to
6471    refer to.  */
6472 static bool info_section_emitted;
6473
6474 /* A pointer to the base of a table that contains a list of publicly
6475    accessible names.  */
6476 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6477
6478 /* A pointer to the base of a table that contains a list of publicly
6479    accessible types.  */
6480 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6481
6482 /* A pointer to the base of a table that contains a list of macro
6483    defines/undefines (and file start/end markers).  */
6484 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6485
6486 /* Array of dies for which we should generate .debug_ranges info.  */
6487 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6488
6489 /* Number of elements currently allocated for ranges_table.  */
6490 static GTY(()) unsigned ranges_table_allocated;
6491
6492 /* Number of elements in ranges_table currently in use.  */
6493 static GTY(()) unsigned ranges_table_in_use;
6494
6495 /* Array of pairs of labels referenced in ranges_table.  */
6496 static GTY ((length ("ranges_by_label_allocated")))
6497      dw_ranges_by_label_ref ranges_by_label;
6498
6499 /* Number of elements currently allocated for ranges_by_label.  */
6500 static GTY(()) unsigned ranges_by_label_allocated;
6501
6502 /* Number of elements in ranges_by_label currently in use.  */
6503 static GTY(()) unsigned ranges_by_label_in_use;
6504
6505 /* Size (in elements) of increments by which we may expand the
6506    ranges_table.  */
6507 #define RANGES_TABLE_INCREMENT 64
6508
6509 /* Whether we have location lists that need outputting */
6510 static GTY(()) bool have_location_lists;
6511
6512 /* Unique label counter.  */
6513 static GTY(()) unsigned int loclabel_num;
6514
6515 /* Unique label counter for point-of-call tables.  */
6516 static GTY(()) unsigned int poc_label_num;
6517
6518 /* Record whether the function being analyzed contains inlined functions.  */
6519 static int current_function_has_inlines;
6520
6521 /* The last file entry emitted by maybe_emit_file().  */
6522 static GTY(()) struct dwarf_file_data * last_emitted_file;
6523
6524 /* Number of internal labels generated by gen_internal_sym().  */
6525 static GTY(()) int label_num;
6526
6527 /* Cached result of previous call to lookup_filename.  */
6528 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6529
6530 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6531
6532 /* Instances of generic types for which we need to generate debug
6533    info that describe their generic parameters and arguments. That
6534    generation needs to happen once all types are properly laid out so
6535    we do it at the end of compilation.  */
6536 static GTY(()) VEC(tree,gc) *generic_type_instances;
6537
6538 /* Offset from the "steady-state frame pointer" to the frame base,
6539    within the current function.  */
6540 static HOST_WIDE_INT frame_pointer_fb_offset;
6541 static bool frame_pointer_fb_offset_valid;
6542
6543 static VEC (dw_die_ref, heap) *base_types;
6544
6545 /* Forward declarations for functions defined in this file.  */
6546
6547 static int is_pseudo_reg (const_rtx);
6548 static tree type_main_variant (tree);
6549 static int is_tagged_type (const_tree);
6550 static const char *dwarf_tag_name (unsigned);
6551 static const char *dwarf_attr_name (unsigned);
6552 static const char *dwarf_form_name (unsigned);
6553 static tree decl_ultimate_origin (const_tree);
6554 static tree decl_class_context (tree);
6555 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6556 static inline enum dw_val_class AT_class (dw_attr_ref);
6557 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6558 static inline unsigned AT_flag (dw_attr_ref);
6559 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6560 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6561 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6562 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6563 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6564                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6565 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6566                                unsigned int, unsigned char *);
6567 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6568 static hashval_t debug_str_do_hash (const void *);
6569 static int debug_str_eq (const void *, const void *);
6570 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6571 static inline const char *AT_string (dw_attr_ref);
6572 static enum dwarf_form AT_string_form (dw_attr_ref);
6573 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6574 static void add_AT_specification (dw_die_ref, dw_die_ref);
6575 static inline dw_die_ref AT_ref (dw_attr_ref);
6576 static inline int AT_ref_external (dw_attr_ref);
6577 static inline void set_AT_ref_external (dw_attr_ref, int);
6578 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6579 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6580 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6581 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6582                              dw_loc_list_ref);
6583 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6584 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6585 static inline rtx AT_addr (dw_attr_ref);
6586 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6587 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6588 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6589 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6590                            unsigned HOST_WIDE_INT);
6591 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6592                                unsigned long);
6593 static inline const char *AT_lbl (dw_attr_ref);
6594 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6595 static const char *get_AT_low_pc (dw_die_ref);
6596 static const char *get_AT_hi_pc (dw_die_ref);
6597 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6598 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6599 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6600 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6601 static bool is_cxx (void);
6602 static bool is_fortran (void);
6603 static bool is_ada (void);
6604 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6605 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6606 static void add_child_die (dw_die_ref, dw_die_ref);
6607 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6608 static dw_die_ref lookup_type_die (tree);
6609 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6610 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6611 static void equate_type_number_to_die (tree, dw_die_ref);
6612 static hashval_t decl_die_table_hash (const void *);
6613 static int decl_die_table_eq (const void *, const void *);
6614 static dw_die_ref lookup_decl_die (tree);
6615 static hashval_t common_block_die_table_hash (const void *);
6616 static int common_block_die_table_eq (const void *, const void *);
6617 static hashval_t decl_loc_table_hash (const void *);
6618 static int decl_loc_table_eq (const void *, const void *);
6619 static var_loc_list *lookup_decl_loc (const_tree);
6620 static void equate_decl_number_to_die (tree, dw_die_ref);
6621 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6622 static void print_spaces (FILE *);
6623 static void print_die (dw_die_ref, FILE *);
6624 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6625 static dw_die_ref pop_compile_unit (dw_die_ref);
6626 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6627 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6628 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6629 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6630 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6631 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6632 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6633                                    struct md5_ctx *, int *);
6634 struct checksum_attributes;
6635 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6636 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6637 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6638 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6639 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6640 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6641 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6642 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6643 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6644 static void compute_section_prefix (dw_die_ref);
6645 static int is_type_die (dw_die_ref);
6646 static int is_comdat_die (dw_die_ref);
6647 static int is_symbol_die (dw_die_ref);
6648 static void assign_symbol_names (dw_die_ref);
6649 static void break_out_includes (dw_die_ref);
6650 static int is_declaration_die (dw_die_ref);
6651 static int should_move_die_to_comdat (dw_die_ref);
6652 static dw_die_ref clone_as_declaration (dw_die_ref);
6653 static dw_die_ref clone_die (dw_die_ref);
6654 static dw_die_ref clone_tree (dw_die_ref);
6655 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6656 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6657 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6658 static dw_die_ref generate_skeleton (dw_die_ref);
6659 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6660                                                          dw_die_ref);
6661 static void break_out_comdat_types (dw_die_ref);
6662 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6663 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6664 static void copy_decls_for_unworthy_types (dw_die_ref);
6665
6666 static hashval_t htab_cu_hash (const void *);
6667 static int htab_cu_eq (const void *, const void *);
6668 static void htab_cu_del (void *);
6669 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6670 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6671 static void add_sibling_attributes (dw_die_ref);
6672 static void build_abbrev_table (dw_die_ref);
6673 static void output_location_lists (dw_die_ref);
6674 static int constant_size (unsigned HOST_WIDE_INT);
6675 static unsigned long size_of_die (dw_die_ref);
6676 static void calc_die_sizes (dw_die_ref);
6677 static void calc_base_type_die_sizes (void);
6678 static void mark_dies (dw_die_ref);
6679 static void unmark_dies (dw_die_ref);
6680 static void unmark_all_dies (dw_die_ref);
6681 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6682 static unsigned long size_of_aranges (void);
6683 static enum dwarf_form value_format (dw_attr_ref);
6684 static void output_value_format (dw_attr_ref);
6685 static void output_abbrev_section (void);
6686 static void output_die_symbol (dw_die_ref);
6687 static void output_die (dw_die_ref);
6688 static void output_compilation_unit_header (void);
6689 static void output_comp_unit (dw_die_ref, int);
6690 static void output_comdat_type_unit (comdat_type_node *);
6691 static const char *dwarf2_name (tree, int);
6692 static void add_pubname (tree, dw_die_ref);
6693 static void add_pubname_string (const char *, dw_die_ref);
6694 static void add_pubtype (tree, dw_die_ref);
6695 static void output_pubnames (VEC (pubname_entry,gc) *);
6696 static void output_aranges (unsigned long);
6697 static unsigned int add_ranges_num (int);
6698 static unsigned int add_ranges (const_tree);
6699 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6700                                   bool *);
6701 static void output_ranges (void);
6702 static dw_line_info_table *new_line_info_table (void);
6703 static void output_line_info (void);
6704 static void output_file_names (void);
6705 static dw_die_ref base_type_die (tree);
6706 static int is_base_type (tree);
6707 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6708 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6709 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6710 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6711 static int type_is_enum (const_tree);
6712 static unsigned int dbx_reg_number (const_rtx);
6713 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6714 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6715 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6716                                                 enum var_init_status);
6717 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6718                                                      enum var_init_status);
6719 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6720                                          enum var_init_status);
6721 static int is_based_loc (const_rtx);
6722 static int resolve_one_addr (rtx *, void *);
6723 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6724                                                enum var_init_status);
6725 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6726                                         enum var_init_status);
6727 static dw_loc_list_ref loc_list_from_tree (tree, int);
6728 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6729 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6730 static tree field_type (const_tree);
6731 static unsigned int simple_type_align_in_bits (const_tree);
6732 static unsigned int simple_decl_align_in_bits (const_tree);
6733 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6734 static HOST_WIDE_INT field_byte_offset (const_tree);
6735 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6736                                          dw_loc_list_ref);
6737 static void add_data_member_location_attribute (dw_die_ref, tree);
6738 static bool add_const_value_attribute (dw_die_ref, rtx);
6739 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6740 static void insert_double (double_int, unsigned char *);
6741 static void insert_float (const_rtx, unsigned char *);
6742 static rtx rtl_for_decl_location (tree);
6743 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6744                                                    enum dwarf_attribute);
6745 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6746 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6747 static void add_name_attribute (dw_die_ref, const char *);
6748 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
6749 static void add_comp_dir_attribute (dw_die_ref);
6750 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6751 static void add_subscript_info (dw_die_ref, tree, bool);
6752 static void add_byte_size_attribute (dw_die_ref, tree);
6753 static void add_bit_offset_attribute (dw_die_ref, tree);
6754 static void add_bit_size_attribute (dw_die_ref, tree);
6755 static void add_prototyped_attribute (dw_die_ref, tree);
6756 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6757 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6758 static void add_src_coords_attributes (dw_die_ref, tree);
6759 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6760 static void push_decl_scope (tree);
6761 static void pop_decl_scope (void);
6762 static dw_die_ref scope_die_for (tree, dw_die_ref);
6763 static inline int local_scope_p (dw_die_ref);
6764 static inline int class_scope_p (dw_die_ref);
6765 static inline int class_or_namespace_scope_p (dw_die_ref);
6766 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6767 static void add_calling_convention_attribute (dw_die_ref, tree);
6768 static const char *type_tag (const_tree);
6769 static tree member_declared_type (const_tree);
6770 #if 0
6771 static const char *decl_start_label (tree);
6772 #endif
6773 static void gen_array_type_die (tree, dw_die_ref);
6774 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6775 #if 0
6776 static void gen_entry_point_die (tree, dw_die_ref);
6777 #endif
6778 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6779 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6780 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6781 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6782 static void gen_formal_types_die (tree, dw_die_ref);
6783 static void gen_subprogram_die (tree, dw_die_ref);
6784 static void gen_variable_die (tree, tree, dw_die_ref);
6785 static void gen_const_die (tree, dw_die_ref);
6786 static void gen_label_die (tree, dw_die_ref);
6787 static void gen_lexical_block_die (tree, dw_die_ref, int);
6788 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6789 static void gen_field_die (tree, dw_die_ref);
6790 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6791 static dw_die_ref gen_compile_unit_die (const char *);
6792 static void gen_inheritance_die (tree, tree, dw_die_ref);
6793 static void gen_member_die (tree, dw_die_ref);
6794 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6795                                                 enum debug_info_usage);
6796 static void gen_subroutine_type_die (tree, dw_die_ref);
6797 static void gen_typedef_die (tree, dw_die_ref);
6798 static void gen_type_die (tree, dw_die_ref);
6799 static void gen_block_die (tree, dw_die_ref, int);
6800 static void decls_for_scope (tree, dw_die_ref, int);
6801 static inline int is_redundant_typedef (const_tree);
6802 static bool is_naming_typedef_decl (const_tree);
6803 static inline dw_die_ref get_context_die (tree);
6804 static void gen_namespace_die (tree, dw_die_ref);
6805 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6806 static dw_die_ref force_decl_die (tree);
6807 static dw_die_ref force_type_die (tree);
6808 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6809 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6810 static struct dwarf_file_data * lookup_filename (const char *);
6811 static void retry_incomplete_types (void);
6812 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6813 static void gen_generic_params_dies (tree);
6814 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6815 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6816 static void splice_child_die (dw_die_ref, dw_die_ref);
6817 static int file_info_cmp (const void *, const void *);
6818 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6819                                      const char *, const char *);
6820 static void output_loc_list (dw_loc_list_ref);
6821 static char *gen_internal_sym (const char *);
6822
6823 static void prune_unmark_dies (dw_die_ref);
6824 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6825 static void prune_unused_types_mark (dw_die_ref, int);
6826 static void prune_unused_types_walk (dw_die_ref);
6827 static void prune_unused_types_walk_attribs (dw_die_ref);
6828 static void prune_unused_types_prune (dw_die_ref);
6829 static void prune_unused_types (void);
6830 static int maybe_emit_file (struct dwarf_file_data *fd);
6831 static inline const char *AT_vms_delta1 (dw_attr_ref);
6832 static inline const char *AT_vms_delta2 (dw_attr_ref);
6833 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6834                                      const char *, const char *);
6835 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6836 static void gen_remaining_tmpl_value_param_die_attribute (void);
6837 static bool generic_type_p (tree);
6838 static void schedule_generic_params_dies_gen (tree t);
6839 static void gen_scheduled_generic_parms_dies (void);
6840
6841 /* Section names used to hold DWARF debugging information.  */
6842 #ifndef DEBUG_INFO_SECTION
6843 #define DEBUG_INFO_SECTION      ".debug_info"
6844 #endif
6845 #ifndef DEBUG_ABBREV_SECTION
6846 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6847 #endif
6848 #ifndef DEBUG_ARANGES_SECTION
6849 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6850 #endif
6851 #ifndef DEBUG_MACINFO_SECTION
6852 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6853 #endif
6854 #ifndef DEBUG_LINE_SECTION
6855 #define DEBUG_LINE_SECTION      ".debug_line"
6856 #endif
6857 #ifndef DEBUG_LOC_SECTION
6858 #define DEBUG_LOC_SECTION       ".debug_loc"
6859 #endif
6860 #ifndef DEBUG_PUBNAMES_SECTION
6861 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6862 #endif
6863 #ifndef DEBUG_PUBTYPES_SECTION
6864 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6865 #endif
6866 #ifndef DEBUG_STR_SECTION
6867 #define DEBUG_STR_SECTION       ".debug_str"
6868 #endif
6869 #ifndef DEBUG_RANGES_SECTION
6870 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6871 #endif
6872
6873 /* Standard ELF section names for compiled code and data.  */
6874 #ifndef TEXT_SECTION_NAME
6875 #define TEXT_SECTION_NAME       ".text"
6876 #endif
6877
6878 /* Section flags for .debug_str section.  */
6879 #define DEBUG_STR_SECTION_FLAGS \
6880   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6881    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6882    : SECTION_DEBUG)
6883
6884 /* Labels we insert at beginning sections we can reference instead of
6885    the section names themselves.  */
6886
6887 #ifndef TEXT_SECTION_LABEL
6888 #define TEXT_SECTION_LABEL              "Ltext"
6889 #endif
6890 #ifndef COLD_TEXT_SECTION_LABEL
6891 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6892 #endif
6893 #ifndef DEBUG_LINE_SECTION_LABEL
6894 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6895 #endif
6896 #ifndef DEBUG_INFO_SECTION_LABEL
6897 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6898 #endif
6899 #ifndef DEBUG_ABBREV_SECTION_LABEL
6900 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6901 #endif
6902 #ifndef DEBUG_LOC_SECTION_LABEL
6903 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6904 #endif
6905 #ifndef DEBUG_RANGES_SECTION_LABEL
6906 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6907 #endif
6908 #ifndef DEBUG_MACINFO_SECTION_LABEL
6909 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6910 #endif
6911
6912
6913 /* Definitions of defaults for formats and names of various special
6914    (artificial) labels which may be generated within this file (when the -g
6915    options is used and DWARF2_DEBUGGING_INFO is in effect.
6916    If necessary, these may be overridden from within the tm.h file, but
6917    typically, overriding these defaults is unnecessary.  */
6918
6919 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6920 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6921 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6922 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6923 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6924 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6925 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6926 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6927 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6928 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6929
6930 #ifndef TEXT_END_LABEL
6931 #define TEXT_END_LABEL          "Letext"
6932 #endif
6933 #ifndef COLD_END_LABEL
6934 #define COLD_END_LABEL          "Letext_cold"
6935 #endif
6936 #ifndef BLOCK_BEGIN_LABEL
6937 #define BLOCK_BEGIN_LABEL       "LBB"
6938 #endif
6939 #ifndef BLOCK_END_LABEL
6940 #define BLOCK_END_LABEL         "LBE"
6941 #endif
6942 #ifndef LINE_CODE_LABEL
6943 #define LINE_CODE_LABEL         "LM"
6944 #endif
6945
6946 \f
6947 /* Return the root of the DIE's built for the current compilation unit.  */
6948 static dw_die_ref
6949 comp_unit_die (void)
6950 {
6951   if (!single_comp_unit_die)
6952     single_comp_unit_die = gen_compile_unit_die (NULL);
6953   return single_comp_unit_die;
6954 }
6955
6956 /* We allow a language front-end to designate a function that is to be
6957    called to "demangle" any name before it is put into a DIE.  */
6958
6959 static const char *(*demangle_name_func) (const char *);
6960
6961 void
6962 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6963 {
6964   demangle_name_func = func;
6965 }
6966
6967 /* Test if rtl node points to a pseudo register.  */
6968
6969 static inline int
6970 is_pseudo_reg (const_rtx rtl)
6971 {
6972   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6973           || (GET_CODE (rtl) == SUBREG
6974               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6975 }
6976
6977 /* Return a reference to a type, with its const and volatile qualifiers
6978    removed.  */
6979
6980 static inline tree
6981 type_main_variant (tree type)
6982 {
6983   type = TYPE_MAIN_VARIANT (type);
6984
6985   /* ??? There really should be only one main variant among any group of
6986      variants of a given type (and all of the MAIN_VARIANT values for all
6987      members of the group should point to that one type) but sometimes the C
6988      front-end messes this up for array types, so we work around that bug
6989      here.  */
6990   if (TREE_CODE (type) == ARRAY_TYPE)
6991     while (type != TYPE_MAIN_VARIANT (type))
6992       type = TYPE_MAIN_VARIANT (type);
6993
6994   return type;
6995 }
6996
6997 /* Return nonzero if the given type node represents a tagged type.  */
6998
6999 static inline int
7000 is_tagged_type (const_tree type)
7001 {
7002   enum tree_code code = TREE_CODE (type);
7003
7004   return (code == RECORD_TYPE || code == UNION_TYPE
7005           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
7006 }
7007
7008 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
7009
7010 static void
7011 get_ref_die_offset_label (char *label, dw_die_ref ref)
7012 {
7013   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
7014 }
7015
7016 /* Return die_offset of a DIE reference to a base type.  */
7017
7018 static unsigned long int
7019 get_base_type_offset (dw_die_ref ref)
7020 {
7021   if (ref->die_offset)
7022     return ref->die_offset;
7023   if (comp_unit_die ()->die_abbrev)
7024     {
7025       calc_base_type_die_sizes ();
7026       gcc_assert (ref->die_offset);
7027     }
7028   return ref->die_offset;
7029 }
7030
7031 /* Return die_offset of a DIE reference other than base type.  */
7032
7033 static unsigned long int
7034 get_ref_die_offset (dw_die_ref ref)
7035 {
7036   gcc_assert (ref->die_offset);
7037   return ref->die_offset;
7038 }
7039
7040 /* Convert a DIE tag into its string name.  */
7041
7042 static const char *
7043 dwarf_tag_name (unsigned int tag)
7044 {
7045   switch (tag)
7046     {
7047     case DW_TAG_padding:
7048       return "DW_TAG_padding";
7049     case DW_TAG_array_type:
7050       return "DW_TAG_array_type";
7051     case DW_TAG_class_type:
7052       return "DW_TAG_class_type";
7053     case DW_TAG_entry_point:
7054       return "DW_TAG_entry_point";
7055     case DW_TAG_enumeration_type:
7056       return "DW_TAG_enumeration_type";
7057     case DW_TAG_formal_parameter:
7058       return "DW_TAG_formal_parameter";
7059     case DW_TAG_imported_declaration:
7060       return "DW_TAG_imported_declaration";
7061     case DW_TAG_label:
7062       return "DW_TAG_label";
7063     case DW_TAG_lexical_block:
7064       return "DW_TAG_lexical_block";
7065     case DW_TAG_member:
7066       return "DW_TAG_member";
7067     case DW_TAG_pointer_type:
7068       return "DW_TAG_pointer_type";
7069     case DW_TAG_reference_type:
7070       return "DW_TAG_reference_type";
7071     case DW_TAG_compile_unit:
7072       return "DW_TAG_compile_unit";
7073     case DW_TAG_string_type:
7074       return "DW_TAG_string_type";
7075     case DW_TAG_structure_type:
7076       return "DW_TAG_structure_type";
7077     case DW_TAG_subroutine_type:
7078       return "DW_TAG_subroutine_type";
7079     case DW_TAG_typedef:
7080       return "DW_TAG_typedef";
7081     case DW_TAG_union_type:
7082       return "DW_TAG_union_type";
7083     case DW_TAG_unspecified_parameters:
7084       return "DW_TAG_unspecified_parameters";
7085     case DW_TAG_variant:
7086       return "DW_TAG_variant";
7087     case DW_TAG_common_block:
7088       return "DW_TAG_common_block";
7089     case DW_TAG_common_inclusion:
7090       return "DW_TAG_common_inclusion";
7091     case DW_TAG_inheritance:
7092       return "DW_TAG_inheritance";
7093     case DW_TAG_inlined_subroutine:
7094       return "DW_TAG_inlined_subroutine";
7095     case DW_TAG_module:
7096       return "DW_TAG_module";
7097     case DW_TAG_ptr_to_member_type:
7098       return "DW_TAG_ptr_to_member_type";
7099     case DW_TAG_set_type:
7100       return "DW_TAG_set_type";
7101     case DW_TAG_subrange_type:
7102       return "DW_TAG_subrange_type";
7103     case DW_TAG_with_stmt:
7104       return "DW_TAG_with_stmt";
7105     case DW_TAG_access_declaration:
7106       return "DW_TAG_access_declaration";
7107     case DW_TAG_base_type:
7108       return "DW_TAG_base_type";
7109     case DW_TAG_catch_block:
7110       return "DW_TAG_catch_block";
7111     case DW_TAG_const_type:
7112       return "DW_TAG_const_type";
7113     case DW_TAG_constant:
7114       return "DW_TAG_constant";
7115     case DW_TAG_enumerator:
7116       return "DW_TAG_enumerator";
7117     case DW_TAG_file_type:
7118       return "DW_TAG_file_type";
7119     case DW_TAG_friend:
7120       return "DW_TAG_friend";
7121     case DW_TAG_namelist:
7122       return "DW_TAG_namelist";
7123     case DW_TAG_namelist_item:
7124       return "DW_TAG_namelist_item";
7125     case DW_TAG_packed_type:
7126       return "DW_TAG_packed_type";
7127     case DW_TAG_subprogram:
7128       return "DW_TAG_subprogram";
7129     case DW_TAG_template_type_param:
7130       return "DW_TAG_template_type_param";
7131     case DW_TAG_template_value_param:
7132       return "DW_TAG_template_value_param";
7133     case DW_TAG_thrown_type:
7134       return "DW_TAG_thrown_type";
7135     case DW_TAG_try_block:
7136       return "DW_TAG_try_block";
7137     case DW_TAG_variant_part:
7138       return "DW_TAG_variant_part";
7139     case DW_TAG_variable:
7140       return "DW_TAG_variable";
7141     case DW_TAG_volatile_type:
7142       return "DW_TAG_volatile_type";
7143     case DW_TAG_dwarf_procedure:
7144       return "DW_TAG_dwarf_procedure";
7145     case DW_TAG_restrict_type:
7146       return "DW_TAG_restrict_type";
7147     case DW_TAG_interface_type:
7148       return "DW_TAG_interface_type";
7149     case DW_TAG_namespace:
7150       return "DW_TAG_namespace";
7151     case DW_TAG_imported_module:
7152       return "DW_TAG_imported_module";
7153     case DW_TAG_unspecified_type:
7154       return "DW_TAG_unspecified_type";
7155     case DW_TAG_partial_unit:
7156       return "DW_TAG_partial_unit";
7157     case DW_TAG_imported_unit:
7158       return "DW_TAG_imported_unit";
7159     case DW_TAG_condition:
7160       return "DW_TAG_condition";
7161     case DW_TAG_shared_type:
7162       return "DW_TAG_shared_type";
7163     case DW_TAG_type_unit:
7164       return "DW_TAG_type_unit";
7165     case DW_TAG_rvalue_reference_type:
7166       return "DW_TAG_rvalue_reference_type";
7167     case DW_TAG_template_alias:
7168       return "DW_TAG_template_alias";
7169     case DW_TAG_GNU_template_parameter_pack:
7170       return "DW_TAG_GNU_template_parameter_pack";
7171     case DW_TAG_GNU_formal_parameter_pack:
7172       return "DW_TAG_GNU_formal_parameter_pack";
7173     case DW_TAG_MIPS_loop:
7174       return "DW_TAG_MIPS_loop";
7175     case DW_TAG_format_label:
7176       return "DW_TAG_format_label";
7177     case DW_TAG_function_template:
7178       return "DW_TAG_function_template";
7179     case DW_TAG_class_template:
7180       return "DW_TAG_class_template";
7181     case DW_TAG_GNU_BINCL:
7182       return "DW_TAG_GNU_BINCL";
7183     case DW_TAG_GNU_EINCL:
7184       return "DW_TAG_GNU_EINCL";
7185     case DW_TAG_GNU_template_template_param:
7186       return "DW_TAG_GNU_template_template_param";
7187     case DW_TAG_GNU_call_site:
7188       return "DW_TAG_GNU_call_site";
7189     case DW_TAG_GNU_call_site_parameter:
7190       return "DW_TAG_GNU_call_site_parameter";
7191     default:
7192       return "DW_TAG_<unknown>";
7193     }
7194 }
7195
7196 /* Convert a DWARF attribute code into its string name.  */
7197
7198 static const char *
7199 dwarf_attr_name (unsigned int attr)
7200 {
7201   switch (attr)
7202     {
7203     case DW_AT_sibling:
7204       return "DW_AT_sibling";
7205     case DW_AT_location:
7206       return "DW_AT_location";
7207     case DW_AT_name:
7208       return "DW_AT_name";
7209     case DW_AT_ordering:
7210       return "DW_AT_ordering";
7211     case DW_AT_subscr_data:
7212       return "DW_AT_subscr_data";
7213     case DW_AT_byte_size:
7214       return "DW_AT_byte_size";
7215     case DW_AT_bit_offset:
7216       return "DW_AT_bit_offset";
7217     case DW_AT_bit_size:
7218       return "DW_AT_bit_size";
7219     case DW_AT_element_list:
7220       return "DW_AT_element_list";
7221     case DW_AT_stmt_list:
7222       return "DW_AT_stmt_list";
7223     case DW_AT_low_pc:
7224       return "DW_AT_low_pc";
7225     case DW_AT_high_pc:
7226       return "DW_AT_high_pc";
7227     case DW_AT_language:
7228       return "DW_AT_language";
7229     case DW_AT_member:
7230       return "DW_AT_member";
7231     case DW_AT_discr:
7232       return "DW_AT_discr";
7233     case DW_AT_discr_value:
7234       return "DW_AT_discr_value";
7235     case DW_AT_visibility:
7236       return "DW_AT_visibility";
7237     case DW_AT_import:
7238       return "DW_AT_import";
7239     case DW_AT_string_length:
7240       return "DW_AT_string_length";
7241     case DW_AT_common_reference:
7242       return "DW_AT_common_reference";
7243     case DW_AT_comp_dir:
7244       return "DW_AT_comp_dir";
7245     case DW_AT_const_value:
7246       return "DW_AT_const_value";
7247     case DW_AT_containing_type:
7248       return "DW_AT_containing_type";
7249     case DW_AT_default_value:
7250       return "DW_AT_default_value";
7251     case DW_AT_inline:
7252       return "DW_AT_inline";
7253     case DW_AT_is_optional:
7254       return "DW_AT_is_optional";
7255     case DW_AT_lower_bound:
7256       return "DW_AT_lower_bound";
7257     case DW_AT_producer:
7258       return "DW_AT_producer";
7259     case DW_AT_prototyped:
7260       return "DW_AT_prototyped";
7261     case DW_AT_return_addr:
7262       return "DW_AT_return_addr";
7263     case DW_AT_start_scope:
7264       return "DW_AT_start_scope";
7265     case DW_AT_bit_stride:
7266       return "DW_AT_bit_stride";
7267     case DW_AT_upper_bound:
7268       return "DW_AT_upper_bound";
7269     case DW_AT_abstract_origin:
7270       return "DW_AT_abstract_origin";
7271     case DW_AT_accessibility:
7272       return "DW_AT_accessibility";
7273     case DW_AT_address_class:
7274       return "DW_AT_address_class";
7275     case DW_AT_artificial:
7276       return "DW_AT_artificial";
7277     case DW_AT_base_types:
7278       return "DW_AT_base_types";
7279     case DW_AT_calling_convention:
7280       return "DW_AT_calling_convention";
7281     case DW_AT_count:
7282       return "DW_AT_count";
7283     case DW_AT_data_member_location:
7284       return "DW_AT_data_member_location";
7285     case DW_AT_decl_column:
7286       return "DW_AT_decl_column";
7287     case DW_AT_decl_file:
7288       return "DW_AT_decl_file";
7289     case DW_AT_decl_line:
7290       return "DW_AT_decl_line";
7291     case DW_AT_declaration:
7292       return "DW_AT_declaration";
7293     case DW_AT_discr_list:
7294       return "DW_AT_discr_list";
7295     case DW_AT_encoding:
7296       return "DW_AT_encoding";
7297     case DW_AT_external:
7298       return "DW_AT_external";
7299     case DW_AT_explicit:
7300       return "DW_AT_explicit";
7301     case DW_AT_frame_base:
7302       return "DW_AT_frame_base";
7303     case DW_AT_friend:
7304       return "DW_AT_friend";
7305     case DW_AT_identifier_case:
7306       return "DW_AT_identifier_case";
7307     case DW_AT_macro_info:
7308       return "DW_AT_macro_info";
7309     case DW_AT_namelist_items:
7310       return "DW_AT_namelist_items";
7311     case DW_AT_priority:
7312       return "DW_AT_priority";
7313     case DW_AT_segment:
7314       return "DW_AT_segment";
7315     case DW_AT_specification:
7316       return "DW_AT_specification";
7317     case DW_AT_static_link:
7318       return "DW_AT_static_link";
7319     case DW_AT_type:
7320       return "DW_AT_type";
7321     case DW_AT_use_location:
7322       return "DW_AT_use_location";
7323     case DW_AT_variable_parameter:
7324       return "DW_AT_variable_parameter";
7325     case DW_AT_virtuality:
7326       return "DW_AT_virtuality";
7327     case DW_AT_vtable_elem_location:
7328       return "DW_AT_vtable_elem_location";
7329
7330     case DW_AT_allocated:
7331       return "DW_AT_allocated";
7332     case DW_AT_associated:
7333       return "DW_AT_associated";
7334     case DW_AT_data_location:
7335       return "DW_AT_data_location";
7336     case DW_AT_byte_stride:
7337       return "DW_AT_byte_stride";
7338     case DW_AT_entry_pc:
7339       return "DW_AT_entry_pc";
7340     case DW_AT_use_UTF8:
7341       return "DW_AT_use_UTF8";
7342     case DW_AT_extension:
7343       return "DW_AT_extension";
7344     case DW_AT_ranges:
7345       return "DW_AT_ranges";
7346     case DW_AT_trampoline:
7347       return "DW_AT_trampoline";
7348     case DW_AT_call_column:
7349       return "DW_AT_call_column";
7350     case DW_AT_call_file:
7351       return "DW_AT_call_file";
7352     case DW_AT_call_line:
7353       return "DW_AT_call_line";
7354     case DW_AT_object_pointer:
7355       return "DW_AT_object_pointer";
7356
7357     case DW_AT_signature:
7358       return "DW_AT_signature";
7359     case DW_AT_main_subprogram:
7360       return "DW_AT_main_subprogram";
7361     case DW_AT_data_bit_offset:
7362       return "DW_AT_data_bit_offset";
7363     case DW_AT_const_expr:
7364       return "DW_AT_const_expr";
7365     case DW_AT_enum_class:
7366       return "DW_AT_enum_class";
7367     case DW_AT_linkage_name:
7368       return "DW_AT_linkage_name";
7369
7370     case DW_AT_MIPS_fde:
7371       return "DW_AT_MIPS_fde";
7372     case DW_AT_MIPS_loop_begin:
7373       return "DW_AT_MIPS_loop_begin";
7374     case DW_AT_MIPS_tail_loop_begin:
7375       return "DW_AT_MIPS_tail_loop_begin";
7376     case DW_AT_MIPS_epilog_begin:
7377       return "DW_AT_MIPS_epilog_begin";
7378 #if VMS_DEBUGGING_INFO
7379     case DW_AT_HP_prologue:
7380       return "DW_AT_HP_prologue";
7381 #else
7382     case DW_AT_MIPS_loop_unroll_factor:
7383       return "DW_AT_MIPS_loop_unroll_factor";
7384 #endif
7385     case DW_AT_MIPS_software_pipeline_depth:
7386       return "DW_AT_MIPS_software_pipeline_depth";
7387     case DW_AT_MIPS_linkage_name:
7388       return "DW_AT_MIPS_linkage_name";
7389 #if VMS_DEBUGGING_INFO
7390     case DW_AT_HP_epilogue:
7391       return "DW_AT_HP_epilogue";
7392 #else
7393     case DW_AT_MIPS_stride:
7394       return "DW_AT_MIPS_stride";
7395 #endif
7396     case DW_AT_MIPS_abstract_name:
7397       return "DW_AT_MIPS_abstract_name";
7398     case DW_AT_MIPS_clone_origin:
7399       return "DW_AT_MIPS_clone_origin";
7400     case DW_AT_MIPS_has_inlines:
7401       return "DW_AT_MIPS_has_inlines";
7402
7403     case DW_AT_sf_names:
7404       return "DW_AT_sf_names";
7405     case DW_AT_src_info:
7406       return "DW_AT_src_info";
7407     case DW_AT_mac_info:
7408       return "DW_AT_mac_info";
7409     case DW_AT_src_coords:
7410       return "DW_AT_src_coords";
7411     case DW_AT_body_begin:
7412       return "DW_AT_body_begin";
7413     case DW_AT_body_end:
7414       return "DW_AT_body_end";
7415
7416     case DW_AT_GNU_vector:
7417       return "DW_AT_GNU_vector";
7418     case DW_AT_GNU_guarded_by:
7419       return "DW_AT_GNU_guarded_by";
7420     case DW_AT_GNU_pt_guarded_by:
7421       return "DW_AT_GNU_pt_guarded_by";
7422     case DW_AT_GNU_guarded:
7423       return "DW_AT_GNU_guarded";
7424     case DW_AT_GNU_pt_guarded:
7425       return "DW_AT_GNU_pt_guarded";
7426     case DW_AT_GNU_locks_excluded:
7427       return "DW_AT_GNU_locks_excluded";
7428     case DW_AT_GNU_exclusive_locks_required:
7429       return "DW_AT_GNU_exclusive_locks_required";
7430     case DW_AT_GNU_shared_locks_required:
7431       return "DW_AT_GNU_shared_locks_required";
7432     case DW_AT_GNU_odr_signature:
7433       return "DW_AT_GNU_odr_signature";
7434     case DW_AT_GNU_template_name:
7435       return "DW_AT_GNU_template_name";
7436     case DW_AT_GNU_call_site_value:
7437       return "DW_AT_GNU_call_site_value";
7438     case DW_AT_GNU_call_site_data_value:
7439       return "DW_AT_GNU_call_site_data_value";
7440     case DW_AT_GNU_call_site_target:
7441       return "DW_AT_GNU_call_site_target";
7442     case DW_AT_GNU_call_site_target_clobbered:
7443       return "DW_AT_GNU_call_site_target_clobbered";
7444     case DW_AT_GNU_tail_call:
7445       return "DW_AT_GNU_tail_call";
7446     case DW_AT_GNU_all_tail_call_sites:
7447       return "DW_AT_GNU_all_tail_call_sites";
7448     case DW_AT_GNU_all_call_sites:
7449       return "DW_AT_GNU_all_call_sites";
7450     case DW_AT_GNU_all_source_call_sites:
7451       return "DW_AT_GNU_all_source_call_sites";
7452
7453     case DW_AT_GNAT_descriptive_type:
7454       return "DW_AT_GNAT_descriptive_type";
7455
7456     case DW_AT_VMS_rtnbeg_pd_address:
7457       return "DW_AT_VMS_rtnbeg_pd_address";
7458
7459     default:
7460       return "DW_AT_<unknown>";
7461     }
7462 }
7463
7464 /* Convert a DWARF value form code into its string name.  */
7465
7466 static const char *
7467 dwarf_form_name (unsigned int form)
7468 {
7469   switch (form)
7470     {
7471     case DW_FORM_addr:
7472       return "DW_FORM_addr";
7473     case DW_FORM_block2:
7474       return "DW_FORM_block2";
7475     case DW_FORM_block4:
7476       return "DW_FORM_block4";
7477     case DW_FORM_data2:
7478       return "DW_FORM_data2";
7479     case DW_FORM_data4:
7480       return "DW_FORM_data4";
7481     case DW_FORM_data8:
7482       return "DW_FORM_data8";
7483     case DW_FORM_string:
7484       return "DW_FORM_string";
7485     case DW_FORM_block:
7486       return "DW_FORM_block";
7487     case DW_FORM_block1:
7488       return "DW_FORM_block1";
7489     case DW_FORM_data1:
7490       return "DW_FORM_data1";
7491     case DW_FORM_flag:
7492       return "DW_FORM_flag";
7493     case DW_FORM_sdata:
7494       return "DW_FORM_sdata";
7495     case DW_FORM_strp:
7496       return "DW_FORM_strp";
7497     case DW_FORM_udata:
7498       return "DW_FORM_udata";
7499     case DW_FORM_ref_addr:
7500       return "DW_FORM_ref_addr";
7501     case DW_FORM_ref1:
7502       return "DW_FORM_ref1";
7503     case DW_FORM_ref2:
7504       return "DW_FORM_ref2";
7505     case DW_FORM_ref4:
7506       return "DW_FORM_ref4";
7507     case DW_FORM_ref8:
7508       return "DW_FORM_ref8";
7509     case DW_FORM_ref_udata:
7510       return "DW_FORM_ref_udata";
7511     case DW_FORM_indirect:
7512       return "DW_FORM_indirect";
7513     case DW_FORM_sec_offset:
7514       return "DW_FORM_sec_offset";
7515     case DW_FORM_exprloc:
7516       return "DW_FORM_exprloc";
7517     case DW_FORM_flag_present:
7518       return "DW_FORM_flag_present";
7519     case DW_FORM_ref_sig8:
7520       return "DW_FORM_ref_sig8";
7521     default:
7522       return "DW_FORM_<unknown>";
7523     }
7524 }
7525 \f
7526 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7527    instance of an inlined instance of a decl which is local to an inline
7528    function, so we have to trace all of the way back through the origin chain
7529    to find out what sort of node actually served as the original seed for the
7530    given block.  */
7531
7532 static tree
7533 decl_ultimate_origin (const_tree decl)
7534 {
7535   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7536     return NULL_TREE;
7537
7538   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7539      nodes in the function to point to themselves; ignore that if
7540      we're trying to output the abstract instance of this function.  */
7541   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7542     return NULL_TREE;
7543
7544   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7545      most distant ancestor, this should never happen.  */
7546   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7547
7548   return DECL_ABSTRACT_ORIGIN (decl);
7549 }
7550
7551 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7552    of a virtual function may refer to a base class, so we check the 'this'
7553    parameter.  */
7554
7555 static tree
7556 decl_class_context (tree decl)
7557 {
7558   tree context = NULL_TREE;
7559
7560   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7561     context = DECL_CONTEXT (decl);
7562   else
7563     context = TYPE_MAIN_VARIANT
7564       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7565
7566   if (context && !TYPE_P (context))
7567     context = NULL_TREE;
7568
7569   return context;
7570 }
7571 \f
7572 /* Add an attribute/value pair to a DIE.  */
7573
7574 static inline void
7575 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7576 {
7577   /* Maybe this should be an assert?  */
7578   if (die == NULL)
7579     return;
7580
7581   if (die->die_attr == NULL)
7582     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7583   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7584 }
7585
7586 static inline enum dw_val_class
7587 AT_class (dw_attr_ref a)
7588 {
7589   return a->dw_attr_val.val_class;
7590 }
7591
7592 /* Add a flag value attribute to a DIE.  */
7593
7594 static inline void
7595 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7596 {
7597   dw_attr_node attr;
7598
7599   attr.dw_attr = attr_kind;
7600   attr.dw_attr_val.val_class = dw_val_class_flag;
7601   attr.dw_attr_val.v.val_flag = flag;
7602   add_dwarf_attr (die, &attr);
7603 }
7604
7605 static inline unsigned
7606 AT_flag (dw_attr_ref a)
7607 {
7608   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7609   return a->dw_attr_val.v.val_flag;
7610 }
7611
7612 /* Add a signed integer attribute value to a DIE.  */
7613
7614 static inline void
7615 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7616 {
7617   dw_attr_node attr;
7618
7619   attr.dw_attr = attr_kind;
7620   attr.dw_attr_val.val_class = dw_val_class_const;
7621   attr.dw_attr_val.v.val_int = int_val;
7622   add_dwarf_attr (die, &attr);
7623 }
7624
7625 static inline HOST_WIDE_INT
7626 AT_int (dw_attr_ref a)
7627 {
7628   gcc_assert (a && AT_class (a) == dw_val_class_const);
7629   return a->dw_attr_val.v.val_int;
7630 }
7631
7632 /* Add an unsigned integer attribute value to a DIE.  */
7633
7634 static inline void
7635 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7636                  unsigned HOST_WIDE_INT unsigned_val)
7637 {
7638   dw_attr_node attr;
7639
7640   attr.dw_attr = attr_kind;
7641   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7642   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7643   add_dwarf_attr (die, &attr);
7644 }
7645
7646 static inline unsigned HOST_WIDE_INT
7647 AT_unsigned (dw_attr_ref a)
7648 {
7649   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7650   return a->dw_attr_val.v.val_unsigned;
7651 }
7652
7653 /* Add an unsigned double integer attribute value to a DIE.  */
7654
7655 static inline void
7656 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7657                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7658 {
7659   dw_attr_node attr;
7660
7661   attr.dw_attr = attr_kind;
7662   attr.dw_attr_val.val_class = dw_val_class_const_double;
7663   attr.dw_attr_val.v.val_double.high = high;
7664   attr.dw_attr_val.v.val_double.low = low;
7665   add_dwarf_attr (die, &attr);
7666 }
7667
7668 /* Add a floating point attribute value to a DIE and return it.  */
7669
7670 static inline void
7671 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7672             unsigned int length, unsigned int elt_size, unsigned char *array)
7673 {
7674   dw_attr_node attr;
7675
7676   attr.dw_attr = attr_kind;
7677   attr.dw_attr_val.val_class = dw_val_class_vec;
7678   attr.dw_attr_val.v.val_vec.length = length;
7679   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7680   attr.dw_attr_val.v.val_vec.array = array;
7681   add_dwarf_attr (die, &attr);
7682 }
7683
7684 /* Add an 8-byte data attribute value to a DIE.  */
7685
7686 static inline void
7687 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7688               unsigned char data8[8])
7689 {
7690   dw_attr_node attr;
7691
7692   attr.dw_attr = attr_kind;
7693   attr.dw_attr_val.val_class = dw_val_class_data8;
7694   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7695   add_dwarf_attr (die, &attr);
7696 }
7697
7698 /* Hash and equality functions for debug_str_hash.  */
7699
7700 static hashval_t
7701 debug_str_do_hash (const void *x)
7702 {
7703   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7704 }
7705
7706 static int
7707 debug_str_eq (const void *x1, const void *x2)
7708 {
7709   return strcmp ((((const struct indirect_string_node *)x1)->str),
7710                  (const char *)x2) == 0;
7711 }
7712
7713 /* Add STR to the indirect string hash table.  */
7714
7715 static struct indirect_string_node *
7716 find_AT_string (const char *str)
7717 {
7718   struct indirect_string_node *node;
7719   void **slot;
7720
7721   if (! debug_str_hash)
7722     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7723                                       debug_str_eq, NULL);
7724
7725   slot = htab_find_slot_with_hash (debug_str_hash, str,
7726                                    htab_hash_string (str), INSERT);
7727   if (*slot == NULL)
7728     {
7729       node = ggc_alloc_cleared_indirect_string_node ();
7730       node->str = ggc_strdup (str);
7731       *slot = node;
7732     }
7733   else
7734     node = (struct indirect_string_node *) *slot;
7735
7736   node->refcount++;
7737   return node;
7738 }
7739
7740 /* Add a string attribute value to a DIE.  */
7741
7742 static inline void
7743 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7744 {
7745   dw_attr_node attr;
7746   struct indirect_string_node *node;
7747
7748   node = find_AT_string (str);
7749
7750   attr.dw_attr = attr_kind;
7751   attr.dw_attr_val.val_class = dw_val_class_str;
7752   attr.dw_attr_val.v.val_str = node;
7753   add_dwarf_attr (die, &attr);
7754 }
7755
7756 static inline const char *
7757 AT_string (dw_attr_ref a)
7758 {
7759   gcc_assert (a && AT_class (a) == dw_val_class_str);
7760   return a->dw_attr_val.v.val_str->str;
7761 }
7762
7763 /* Find out whether a string should be output inline in DIE
7764    or out-of-line in .debug_str section.  */
7765
7766 static enum dwarf_form
7767 AT_string_form (dw_attr_ref a)
7768 {
7769   struct indirect_string_node *node;
7770   unsigned int len;
7771   char label[32];
7772
7773   gcc_assert (a && AT_class (a) == dw_val_class_str);
7774
7775   node = a->dw_attr_val.v.val_str;
7776   if (node->form)
7777     return node->form;
7778
7779   len = strlen (node->str) + 1;
7780
7781   /* If the string is shorter or equal to the size of the reference, it is
7782      always better to put it inline.  */
7783   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7784     return node->form = DW_FORM_string;
7785
7786   /* If we cannot expect the linker to merge strings in .debug_str
7787      section, only put it into .debug_str if it is worth even in this
7788      single module.  */
7789   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7790       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7791       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7792     return node->form = DW_FORM_string;
7793
7794   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7795   ++dw2_string_counter;
7796   node->label = xstrdup (label);
7797
7798   return node->form = DW_FORM_strp;
7799 }
7800
7801 /* Add a DIE reference attribute value to a DIE.  */
7802
7803 static inline void
7804 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7805 {
7806   dw_attr_node attr;
7807
7808 #ifdef ENABLE_CHECKING
7809   gcc_assert (targ_die != NULL);
7810 #else
7811   /* With LTO we can end up trying to reference something we didn't create
7812      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7813   if (targ_die == NULL)
7814     return;
7815 #endif
7816
7817   attr.dw_attr = attr_kind;
7818   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7819   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7820   attr.dw_attr_val.v.val_die_ref.external = 0;
7821   add_dwarf_attr (die, &attr);
7822 }
7823
7824 /* Add an AT_specification attribute to a DIE, and also make the back
7825    pointer from the specification to the definition.  */
7826
7827 static inline void
7828 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7829 {
7830   add_AT_die_ref (die, DW_AT_specification, targ_die);
7831   gcc_assert (!targ_die->die_definition);
7832   targ_die->die_definition = die;
7833 }
7834
7835 static inline dw_die_ref
7836 AT_ref (dw_attr_ref a)
7837 {
7838   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7839   return a->dw_attr_val.v.val_die_ref.die;
7840 }
7841
7842 static inline int
7843 AT_ref_external (dw_attr_ref a)
7844 {
7845   if (a && AT_class (a) == dw_val_class_die_ref)
7846     return a->dw_attr_val.v.val_die_ref.external;
7847
7848   return 0;
7849 }
7850
7851 static inline void
7852 set_AT_ref_external (dw_attr_ref a, int i)
7853 {
7854   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7855   a->dw_attr_val.v.val_die_ref.external = i;
7856 }
7857
7858 /* Add an FDE reference attribute value to a DIE.  */
7859
7860 static inline void
7861 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7862 {
7863   dw_attr_node attr;
7864
7865   attr.dw_attr = attr_kind;
7866   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7867   attr.dw_attr_val.v.val_fde_index = targ_fde;
7868   add_dwarf_attr (die, &attr);
7869 }
7870
7871 /* Add a location description attribute value to a DIE.  */
7872
7873 static inline void
7874 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7875 {
7876   dw_attr_node attr;
7877
7878   attr.dw_attr = attr_kind;
7879   attr.dw_attr_val.val_class = dw_val_class_loc;
7880   attr.dw_attr_val.v.val_loc = loc;
7881   add_dwarf_attr (die, &attr);
7882 }
7883
7884 static inline dw_loc_descr_ref
7885 AT_loc (dw_attr_ref a)
7886 {
7887   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7888   return a->dw_attr_val.v.val_loc;
7889 }
7890
7891 static inline void
7892 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7893 {
7894   dw_attr_node attr;
7895
7896   attr.dw_attr = attr_kind;
7897   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7898   attr.dw_attr_val.v.val_loc_list = loc_list;
7899   add_dwarf_attr (die, &attr);
7900   have_location_lists = true;
7901 }
7902
7903 static inline dw_loc_list_ref
7904 AT_loc_list (dw_attr_ref a)
7905 {
7906   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7907   return a->dw_attr_val.v.val_loc_list;
7908 }
7909
7910 static inline dw_loc_list_ref *
7911 AT_loc_list_ptr (dw_attr_ref a)
7912 {
7913   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7914   return &a->dw_attr_val.v.val_loc_list;
7915 }
7916
7917 /* Add an address constant attribute value to a DIE.  */
7918
7919 static inline void
7920 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7921 {
7922   dw_attr_node attr;
7923
7924   attr.dw_attr = attr_kind;
7925   attr.dw_attr_val.val_class = dw_val_class_addr;
7926   attr.dw_attr_val.v.val_addr = addr;
7927   add_dwarf_attr (die, &attr);
7928 }
7929
7930 /* Get the RTX from to an address DIE attribute.  */
7931
7932 static inline rtx
7933 AT_addr (dw_attr_ref a)
7934 {
7935   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7936   return a->dw_attr_val.v.val_addr;
7937 }
7938
7939 /* Add a file attribute value to a DIE.  */
7940
7941 static inline void
7942 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7943              struct dwarf_file_data *fd)
7944 {
7945   dw_attr_node attr;
7946
7947   attr.dw_attr = attr_kind;
7948   attr.dw_attr_val.val_class = dw_val_class_file;
7949   attr.dw_attr_val.v.val_file = fd;
7950   add_dwarf_attr (die, &attr);
7951 }
7952
7953 /* Get the dwarf_file_data from a file DIE attribute.  */
7954
7955 static inline struct dwarf_file_data *
7956 AT_file (dw_attr_ref a)
7957 {
7958   gcc_assert (a && AT_class (a) == dw_val_class_file);
7959   return a->dw_attr_val.v.val_file;
7960 }
7961
7962 /* Add a vms delta attribute value to a DIE.  */
7963
7964 static inline void
7965 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7966                   const char *lbl1, const char *lbl2)
7967 {
7968   dw_attr_node attr;
7969
7970   attr.dw_attr = attr_kind;
7971   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7972   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7973   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7974   add_dwarf_attr (die, &attr);
7975 }
7976
7977 /* Add a label identifier attribute value to a DIE.  */
7978
7979 static inline void
7980 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7981 {
7982   dw_attr_node attr;
7983
7984   attr.dw_attr = attr_kind;
7985   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7986   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7987   add_dwarf_attr (die, &attr);
7988 }
7989
7990 /* Add a section offset attribute value to a DIE, an offset into the
7991    debug_line section.  */
7992
7993 static inline void
7994 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7995                 const char *label)
7996 {
7997   dw_attr_node attr;
7998
7999   attr.dw_attr = attr_kind;
8000   attr.dw_attr_val.val_class = dw_val_class_lineptr;
8001   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
8002   add_dwarf_attr (die, &attr);
8003 }
8004
8005 /* Add a section offset attribute value to a DIE, an offset into the
8006    debug_macinfo section.  */
8007
8008 static inline void
8009 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
8010                const char *label)
8011 {
8012   dw_attr_node attr;
8013
8014   attr.dw_attr = attr_kind;
8015   attr.dw_attr_val.val_class = dw_val_class_macptr;
8016   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
8017   add_dwarf_attr (die, &attr);
8018 }
8019
8020 /* Add an offset attribute value to a DIE.  */
8021
8022 static inline void
8023 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
8024                unsigned HOST_WIDE_INT offset)
8025 {
8026   dw_attr_node attr;
8027
8028   attr.dw_attr = attr_kind;
8029   attr.dw_attr_val.val_class = dw_val_class_offset;
8030   attr.dw_attr_val.v.val_offset = offset;
8031   add_dwarf_attr (die, &attr);
8032 }
8033
8034 /* Add an range_list attribute value to a DIE.  */
8035
8036 static void
8037 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
8038                    long unsigned int offset)
8039 {
8040   dw_attr_node attr;
8041
8042   attr.dw_attr = attr_kind;
8043   attr.dw_attr_val.val_class = dw_val_class_range_list;
8044   attr.dw_attr_val.v.val_offset = offset;
8045   add_dwarf_attr (die, &attr);
8046 }
8047
8048 /* Return the start label of a delta attribute.  */
8049
8050 static inline const char *
8051 AT_vms_delta1 (dw_attr_ref a)
8052 {
8053   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
8054   return a->dw_attr_val.v.val_vms_delta.lbl1;
8055 }
8056
8057 /* Return the end label of a delta attribute.  */
8058
8059 static inline const char *
8060 AT_vms_delta2 (dw_attr_ref a)
8061 {
8062   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
8063   return a->dw_attr_val.v.val_vms_delta.lbl2;
8064 }
8065
8066 static inline const char *
8067 AT_lbl (dw_attr_ref a)
8068 {
8069   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
8070                     || AT_class (a) == dw_val_class_lineptr
8071                     || AT_class (a) == dw_val_class_macptr));
8072   return a->dw_attr_val.v.val_lbl_id;
8073 }
8074
8075 /* Get the attribute of type attr_kind.  */
8076
8077 static dw_attr_ref
8078 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8079 {
8080   dw_attr_ref a;
8081   unsigned ix;
8082   dw_die_ref spec = NULL;
8083
8084   if (! die)
8085     return NULL;
8086
8087   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8088     if (a->dw_attr == attr_kind)
8089       return a;
8090     else if (a->dw_attr == DW_AT_specification
8091              || a->dw_attr == DW_AT_abstract_origin)
8092       spec = AT_ref (a);
8093
8094   if (spec)
8095     return get_AT (spec, attr_kind);
8096
8097   return NULL;
8098 }
8099
8100 /* Return the "low pc" attribute value, typically associated with a subprogram
8101    DIE.  Return null if the "low pc" attribute is either not present, or if it
8102    cannot be represented as an assembler label identifier.  */
8103
8104 static inline const char *
8105 get_AT_low_pc (dw_die_ref die)
8106 {
8107   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
8108
8109   return a ? AT_lbl (a) : NULL;
8110 }
8111
8112 /* Return the "high pc" attribute value, typically associated with a subprogram
8113    DIE.  Return null if the "high pc" attribute is either not present, or if it
8114    cannot be represented as an assembler label identifier.  */
8115
8116 static inline const char *
8117 get_AT_hi_pc (dw_die_ref die)
8118 {
8119   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
8120
8121   return a ? AT_lbl (a) : NULL;
8122 }
8123
8124 /* Return the value of the string attribute designated by ATTR_KIND, or
8125    NULL if it is not present.  */
8126
8127 static inline const char *
8128 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
8129 {
8130   dw_attr_ref a = get_AT (die, attr_kind);
8131
8132   return a ? AT_string (a) : NULL;
8133 }
8134
8135 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
8136    if it is not present.  */
8137
8138 static inline int
8139 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
8140 {
8141   dw_attr_ref a = get_AT (die, attr_kind);
8142
8143   return a ? AT_flag (a) : 0;
8144 }
8145
8146 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
8147    if it is not present.  */
8148
8149 static inline unsigned
8150 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
8151 {
8152   dw_attr_ref a = get_AT (die, attr_kind);
8153
8154   return a ? AT_unsigned (a) : 0;
8155 }
8156
8157 static inline dw_die_ref
8158 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
8159 {
8160   dw_attr_ref a = get_AT (die, attr_kind);
8161
8162   return a ? AT_ref (a) : NULL;
8163 }
8164
8165 static inline struct dwarf_file_data *
8166 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
8167 {
8168   dw_attr_ref a = get_AT (die, attr_kind);
8169
8170   return a ? AT_file (a) : NULL;
8171 }
8172
8173 /* Return TRUE if the language is C++.  */
8174
8175 static inline bool
8176 is_cxx (void)
8177 {
8178   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8179
8180   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
8181 }
8182
8183 /* Return TRUE if the language is Fortran.  */
8184
8185 static inline bool
8186 is_fortran (void)
8187 {
8188   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8189
8190   return (lang == DW_LANG_Fortran77
8191           || lang == DW_LANG_Fortran90
8192           || lang == DW_LANG_Fortran95);
8193 }
8194
8195 /* Return TRUE if the language is Ada.  */
8196
8197 static inline bool
8198 is_ada (void)
8199 {
8200   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8201
8202   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
8203 }
8204
8205 /* Remove the specified attribute if present.  */
8206
8207 static void
8208 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8209 {
8210   dw_attr_ref a;
8211   unsigned ix;
8212
8213   if (! die)
8214     return;
8215
8216   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8217     if (a->dw_attr == attr_kind)
8218       {
8219         if (AT_class (a) == dw_val_class_str)
8220           if (a->dw_attr_val.v.val_str->refcount)
8221             a->dw_attr_val.v.val_str->refcount--;
8222
8223         /* VEC_ordered_remove should help reduce the number of abbrevs
8224            that are needed.  */
8225         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
8226         return;
8227       }
8228 }
8229
8230 /* Remove CHILD from its parent.  PREV must have the property that
8231    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
8232
8233 static void
8234 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
8235 {
8236   gcc_assert (child->die_parent == prev->die_parent);
8237   gcc_assert (prev->die_sib == child);
8238   if (prev == child)
8239     {
8240       gcc_assert (child->die_parent->die_child == child);
8241       prev = NULL;
8242     }
8243   else
8244     prev->die_sib = child->die_sib;
8245   if (child->die_parent->die_child == child)
8246     child->die_parent->die_child = prev;
8247 }
8248
8249 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
8250    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
8251
8252 static void
8253 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8254 {
8255   dw_die_ref parent = old_child->die_parent;
8256
8257   gcc_assert (parent == prev->die_parent);
8258   gcc_assert (prev->die_sib == old_child);
8259
8260   new_child->die_parent = parent;
8261   if (prev == old_child)
8262     {
8263       gcc_assert (parent->die_child == old_child);
8264       new_child->die_sib = new_child;
8265     }
8266   else
8267     {
8268       prev->die_sib = new_child;
8269       new_child->die_sib = old_child->die_sib;
8270     }
8271   if (old_child->die_parent->die_child == old_child)
8272     old_child->die_parent->die_child = new_child;
8273 }
8274
8275 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8276
8277 static void
8278 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8279 {
8280   dw_die_ref c;
8281   new_parent->die_child = old_parent->die_child;
8282   old_parent->die_child = NULL;
8283   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8284 }
8285
8286 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8287    matches TAG.  */
8288
8289 static void
8290 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8291 {
8292   dw_die_ref c;
8293
8294   c = die->die_child;
8295   if (c) do {
8296     dw_die_ref prev = c;
8297     c = c->die_sib;
8298     while (c->die_tag == tag)
8299       {
8300         remove_child_with_prev (c, prev);
8301         /* Might have removed every child.  */
8302         if (c == c->die_sib)
8303           return;
8304         c = c->die_sib;
8305       }
8306   } while (c != die->die_child);
8307 }
8308
8309 /* Add a CHILD_DIE as the last child of DIE.  */
8310
8311 static void
8312 add_child_die (dw_die_ref die, dw_die_ref child_die)
8313 {
8314   /* FIXME this should probably be an assert.  */
8315   if (! die || ! child_die)
8316     return;
8317   gcc_assert (die != child_die);
8318
8319   child_die->die_parent = die;
8320   if (die->die_child)
8321     {
8322       child_die->die_sib = die->die_child->die_sib;
8323       die->die_child->die_sib = child_die;
8324     }
8325   else
8326     child_die->die_sib = child_die;
8327   die->die_child = child_die;
8328 }
8329
8330 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8331    is the specification, to the end of PARENT's list of children.
8332    This is done by removing and re-adding it.  */
8333
8334 static void
8335 splice_child_die (dw_die_ref parent, dw_die_ref child)
8336 {
8337   dw_die_ref p;
8338
8339   /* We want the declaration DIE from inside the class, not the
8340      specification DIE at toplevel.  */
8341   if (child->die_parent != parent)
8342     {
8343       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8344
8345       if (tmp)
8346         child = tmp;
8347     }
8348
8349   gcc_assert (child->die_parent == parent
8350               || (child->die_parent
8351                   == get_AT_ref (parent, DW_AT_specification)));
8352
8353   for (p = child->die_parent->die_child; ; p = p->die_sib)
8354     if (p->die_sib == child)
8355       {
8356         remove_child_with_prev (child, p);
8357         break;
8358       }
8359
8360   add_child_die (parent, child);
8361 }
8362
8363 /* Return a pointer to a newly created DIE node.  */
8364
8365 static inline dw_die_ref
8366 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8367 {
8368   dw_die_ref die = ggc_alloc_cleared_die_node ();
8369
8370   die->die_tag = tag_value;
8371
8372   if (parent_die != NULL)
8373     add_child_die (parent_die, die);
8374   else
8375     {
8376       limbo_die_node *limbo_node;
8377
8378       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8379       limbo_node->die = die;
8380       limbo_node->created_for = t;
8381       limbo_node->next = limbo_die_list;
8382       limbo_die_list = limbo_node;
8383     }
8384
8385   return die;
8386 }
8387
8388 /* Return the DIE associated with the given type specifier.  */
8389
8390 static inline dw_die_ref
8391 lookup_type_die (tree type)
8392 {
8393   return TYPE_SYMTAB_DIE (type);
8394 }
8395
8396 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8397    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8398    anonymous type instead the one of the naming typedef.  */
8399
8400 static inline dw_die_ref
8401 strip_naming_typedef (tree type, dw_die_ref type_die)
8402 {
8403   if (type
8404       && TREE_CODE (type) == RECORD_TYPE
8405       && type_die
8406       && type_die->die_tag == DW_TAG_typedef
8407       && is_naming_typedef_decl (TYPE_NAME (type)))
8408     type_die = get_AT_ref (type_die, DW_AT_type);
8409   return type_die;
8410 }
8411
8412 /* Like lookup_type_die, but if type is an anonymous type named by a
8413    typedef[1], return the DIE of the anonymous type instead the one of
8414    the naming typedef.  This is because in gen_typedef_die, we did
8415    equate the anonymous struct named by the typedef with the DIE of
8416    the naming typedef. So by default, lookup_type_die on an anonymous
8417    struct yields the DIE of the naming typedef.
8418
8419    [1]: Read the comment of is_naming_typedef_decl to learn about what
8420    a naming typedef is.  */
8421
8422 static inline dw_die_ref
8423 lookup_type_die_strip_naming_typedef (tree type)
8424 {
8425   dw_die_ref die = lookup_type_die (type);
8426   return strip_naming_typedef (type, die);
8427 }
8428
8429 /* Equate a DIE to a given type specifier.  */
8430
8431 static inline void
8432 equate_type_number_to_die (tree type, dw_die_ref type_die)
8433 {
8434   TYPE_SYMTAB_DIE (type) = type_die;
8435 }
8436
8437 /* Returns a hash value for X (which really is a die_struct).  */
8438
8439 static hashval_t
8440 decl_die_table_hash (const void *x)
8441 {
8442   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8443 }
8444
8445 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8446
8447 static int
8448 decl_die_table_eq (const void *x, const void *y)
8449 {
8450   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8451 }
8452
8453 /* Return the DIE associated with a given declaration.  */
8454
8455 static inline dw_die_ref
8456 lookup_decl_die (tree decl)
8457 {
8458   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8459 }
8460
8461 /* Returns a hash value for X (which really is a var_loc_list).  */
8462
8463 static hashval_t
8464 decl_loc_table_hash (const void *x)
8465 {
8466   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8467 }
8468
8469 /* Return nonzero if decl_id of var_loc_list X is the same as
8470    UID of decl *Y.  */
8471
8472 static int
8473 decl_loc_table_eq (const void *x, const void *y)
8474 {
8475   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8476 }
8477
8478 /* Return the var_loc list associated with a given declaration.  */
8479
8480 static inline var_loc_list *
8481 lookup_decl_loc (const_tree decl)
8482 {
8483   if (!decl_loc_table)
8484     return NULL;
8485   return (var_loc_list *)
8486     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8487 }
8488
8489 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8490
8491 static hashval_t
8492 cached_dw_loc_list_table_hash (const void *x)
8493 {
8494   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8495 }
8496
8497 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8498    UID of decl *Y.  */
8499
8500 static int
8501 cached_dw_loc_list_table_eq (const void *x, const void *y)
8502 {
8503   return (((const cached_dw_loc_list *) x)->decl_id
8504           == DECL_UID ((const_tree) y));
8505 }
8506
8507 /* Equate a DIE to a particular declaration.  */
8508
8509 static void
8510 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8511 {
8512   unsigned int decl_id = DECL_UID (decl);
8513   void **slot;
8514
8515   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8516   *slot = decl_die;
8517   decl_die->decl_id = decl_id;
8518 }
8519
8520 /* Return how many bits covers PIECE EXPR_LIST.  */
8521
8522 static int
8523 decl_piece_bitsize (rtx piece)
8524 {
8525   int ret = (int) GET_MODE (piece);
8526   if (ret)
8527     return ret;
8528   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8529               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8530   return INTVAL (XEXP (XEXP (piece, 0), 0));
8531 }
8532
8533 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8534
8535 static rtx *
8536 decl_piece_varloc_ptr (rtx piece)
8537 {
8538   if ((int) GET_MODE (piece))
8539     return &XEXP (piece, 0);
8540   else
8541     return &XEXP (XEXP (piece, 0), 1);
8542 }
8543
8544 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8545    Next is the chain of following piece nodes.  */
8546
8547 static rtx
8548 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8549 {
8550   if (bitsize <= (int) MAX_MACHINE_MODE)
8551     return alloc_EXPR_LIST (bitsize, loc_note, next);
8552   else
8553     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8554                                                GEN_INT (bitsize),
8555                                                loc_note), next);
8556 }
8557
8558 /* Return rtx that should be stored into loc field for
8559    LOC_NOTE and BITPOS/BITSIZE.  */
8560
8561 static rtx
8562 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8563                       HOST_WIDE_INT bitsize)
8564 {
8565   if (bitsize != -1)
8566     {
8567       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8568       if (bitpos != 0)
8569         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8570     }
8571   return loc_note;
8572 }
8573
8574 /* This function either modifies location piece list *DEST in
8575    place (if SRC and INNER is NULL), or copies location piece list
8576    *SRC to *DEST while modifying it.  Location BITPOS is modified
8577    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8578    not copied and if needed some padding around it is added.
8579    When modifying in place, DEST should point to EXPR_LIST where
8580    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8581    to the start of the whole list and INNER points to the EXPR_LIST
8582    where earlier pieces cover PIECE_BITPOS bits.  */
8583
8584 static void
8585 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8586                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8587                    HOST_WIDE_INT bitsize, rtx loc_note)
8588 {
8589   int diff;
8590   bool copy = inner != NULL;
8591
8592   if (copy)
8593     {
8594       /* First copy all nodes preceeding the current bitpos.  */
8595       while (src != inner)
8596         {
8597           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8598                                    decl_piece_bitsize (*src), NULL_RTX);
8599           dest = &XEXP (*dest, 1);
8600           src = &XEXP (*src, 1);
8601         }
8602     }
8603   /* Add padding if needed.  */
8604   if (bitpos != piece_bitpos)
8605     {
8606       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8607                                copy ? NULL_RTX : *dest);
8608       dest = &XEXP (*dest, 1);
8609     }
8610   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8611     {
8612       gcc_assert (!copy);
8613       /* A piece with correct bitpos and bitsize already exist,
8614          just update the location for it and return.  */
8615       *decl_piece_varloc_ptr (*dest) = loc_note;
8616       return;
8617     }
8618   /* Add the piece that changed.  */
8619   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8620   dest = &XEXP (*dest, 1);
8621   /* Skip over pieces that overlap it.  */
8622   diff = bitpos - piece_bitpos + bitsize;
8623   if (!copy)
8624     src = dest;
8625   while (diff > 0 && *src)
8626     {
8627       rtx piece = *src;
8628       diff -= decl_piece_bitsize (piece);
8629       if (copy)
8630         src = &XEXP (piece, 1);
8631       else
8632         {
8633           *src = XEXP (piece, 1);
8634           free_EXPR_LIST_node (piece);
8635         }
8636     }
8637   /* Add padding if needed.  */
8638   if (diff < 0 && *src)
8639     {
8640       if (!copy)
8641         dest = src;
8642       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8643       dest = &XEXP (*dest, 1);
8644     }
8645   if (!copy)
8646     return;
8647   /* Finally copy all nodes following it.  */
8648   while (*src)
8649     {
8650       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8651                                decl_piece_bitsize (*src), NULL_RTX);
8652       dest = &XEXP (*dest, 1);
8653       src = &XEXP (*src, 1);
8654     }
8655 }
8656
8657 /* Add a variable location node to the linked list for DECL.  */
8658
8659 static struct var_loc_node *
8660 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8661 {
8662   unsigned int decl_id;
8663   var_loc_list *temp;
8664   void **slot;
8665   struct var_loc_node *loc = NULL;
8666   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8667
8668   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8669     {
8670       tree realdecl = DECL_DEBUG_EXPR (decl);
8671       if (realdecl && handled_component_p (realdecl))
8672         {
8673           HOST_WIDE_INT maxsize;
8674           tree innerdecl;
8675           innerdecl
8676             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8677           if (!DECL_P (innerdecl)
8678               || DECL_IGNORED_P (innerdecl)
8679               || TREE_STATIC (innerdecl)
8680               || bitsize <= 0
8681               || bitpos + bitsize > 256
8682               || bitsize != maxsize)
8683             return NULL;
8684           decl = innerdecl;
8685         }
8686     }
8687
8688   decl_id = DECL_UID (decl);
8689   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8690   if (*slot == NULL)
8691     {
8692       temp = ggc_alloc_cleared_var_loc_list ();
8693       temp->decl_id = decl_id;
8694       *slot = temp;
8695     }
8696   else
8697     temp = (var_loc_list *) *slot;
8698
8699   /* For PARM_DECLs try to keep around the original incoming value,
8700      even if that means we'll emit a zero-range .debug_loc entry.  */
8701   if (temp->last
8702       && temp->first == temp->last
8703       && TREE_CODE (decl) == PARM_DECL
8704       && GET_CODE (temp->first->loc) == NOTE
8705       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
8706       && DECL_INCOMING_RTL (decl)
8707       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
8708       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
8709          == GET_CODE (DECL_INCOMING_RTL (decl))
8710       && prev_real_insn (temp->first->loc) == NULL_RTX
8711       && (bitsize != -1
8712           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
8713                            NOTE_VAR_LOCATION_LOC (loc_note))
8714           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
8715               != NOTE_VAR_LOCATION_STATUS (loc_note))))
8716     {
8717       loc = ggc_alloc_cleared_var_loc_node ();
8718       temp->first->next = loc;
8719       temp->last = loc;
8720       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8721     }
8722   else if (temp->last)
8723     {
8724       struct var_loc_node *last = temp->last, *unused = NULL;
8725       rtx *piece_loc = NULL, last_loc_note;
8726       int piece_bitpos = 0;
8727       if (last->next)
8728         {
8729           last = last->next;
8730           gcc_assert (last->next == NULL);
8731         }
8732       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8733         {
8734           piece_loc = &last->loc;
8735           do
8736             {
8737               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8738               if (piece_bitpos + cur_bitsize > bitpos)
8739                 break;
8740               piece_bitpos += cur_bitsize;
8741               piece_loc = &XEXP (*piece_loc, 1);
8742             }
8743           while (*piece_loc);
8744         }
8745       /* TEMP->LAST here is either pointer to the last but one or
8746          last element in the chained list, LAST is pointer to the
8747          last element.  */
8748       if (label && strcmp (last->label, label) == 0)
8749         {
8750           /* For SRA optimized variables if there weren't any real
8751              insns since last note, just modify the last node.  */
8752           if (piece_loc != NULL)
8753             {
8754               adjust_piece_list (piece_loc, NULL, NULL,
8755                                  bitpos, piece_bitpos, bitsize, loc_note);
8756               return NULL;
8757             }
8758           /* If the last note doesn't cover any instructions, remove it.  */
8759           if (temp->last != last)
8760             {
8761               temp->last->next = NULL;
8762               unused = last;
8763               last = temp->last;
8764               gcc_assert (strcmp (last->label, label) != 0);
8765             }
8766           else
8767             {
8768               gcc_assert (temp->first == temp->last
8769                           || (temp->first->next == temp->last
8770                               && TREE_CODE (decl) == PARM_DECL));
8771               memset (temp->last, '\0', sizeof (*temp->last));
8772               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8773               return temp->last;
8774             }
8775         }
8776       if (bitsize == -1 && NOTE_P (last->loc))
8777         last_loc_note = last->loc;
8778       else if (piece_loc != NULL
8779                && *piece_loc != NULL_RTX
8780                && piece_bitpos == bitpos
8781                && decl_piece_bitsize (*piece_loc) == bitsize)
8782         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8783       else
8784         last_loc_note = NULL_RTX;
8785       /* If the current location is the same as the end of the list,
8786          and either both or neither of the locations is uninitialized,
8787          we have nothing to do.  */
8788       if (last_loc_note == NULL_RTX
8789           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8790                             NOTE_VAR_LOCATION_LOC (loc_note)))
8791           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8792                != NOTE_VAR_LOCATION_STATUS (loc_note))
8793               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8794                    == VAR_INIT_STATUS_UNINITIALIZED)
8795                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8796                       == VAR_INIT_STATUS_UNINITIALIZED))))
8797         {
8798           /* Add LOC to the end of list and update LAST.  If the last
8799              element of the list has been removed above, reuse its
8800              memory for the new node, otherwise allocate a new one.  */
8801           if (unused)
8802             {
8803               loc = unused;
8804               memset (loc, '\0', sizeof (*loc));
8805             }
8806           else
8807             loc = ggc_alloc_cleared_var_loc_node ();
8808           if (bitsize == -1 || piece_loc == NULL)
8809             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8810           else
8811             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8812                                bitpos, piece_bitpos, bitsize, loc_note);
8813           last->next = loc;
8814           /* Ensure TEMP->LAST will point either to the new last but one
8815              element of the chain, or to the last element in it.  */
8816           if (last != temp->last)
8817             temp->last = last;
8818         }
8819       else if (unused)
8820         ggc_free (unused);
8821     }
8822   else
8823     {
8824       loc = ggc_alloc_cleared_var_loc_node ();
8825       temp->first = loc;
8826       temp->last = loc;
8827       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8828     }
8829   return loc;
8830 }
8831 \f
8832 /* Keep track of the number of spaces used to indent the
8833    output of the debugging routines that print the structure of
8834    the DIE internal representation.  */
8835 static int print_indent;
8836
8837 /* Indent the line the number of spaces given by print_indent.  */
8838
8839 static inline void
8840 print_spaces (FILE *outfile)
8841 {
8842   fprintf (outfile, "%*s", print_indent, "");
8843 }
8844
8845 /* Print a type signature in hex.  */
8846
8847 static inline void
8848 print_signature (FILE *outfile, char *sig)
8849 {
8850   int i;
8851
8852   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8853     fprintf (outfile, "%02x", sig[i] & 0xff);
8854 }
8855
8856 /* Print the information associated with a given DIE, and its children.
8857    This routine is a debugging aid only.  */
8858
8859 static void
8860 print_die (dw_die_ref die, FILE *outfile)
8861 {
8862   dw_attr_ref a;
8863   dw_die_ref c;
8864   unsigned ix;
8865
8866   print_spaces (outfile);
8867   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8868            die->die_offset, dwarf_tag_name (die->die_tag),
8869            (void*) die);
8870   print_spaces (outfile);
8871   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8872   fprintf (outfile, " offset: %ld", die->die_offset);
8873   fprintf (outfile, " mark: %d\n", die->die_mark);
8874
8875   if (use_debug_types && die->die_id.die_type_node)
8876     {
8877       print_spaces (outfile);
8878       fprintf (outfile, "  signature: ");
8879       print_signature (outfile, die->die_id.die_type_node->signature);
8880       fprintf (outfile, "\n");
8881     }
8882
8883   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8884     {
8885       print_spaces (outfile);
8886       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8887
8888       switch (AT_class (a))
8889         {
8890         case dw_val_class_addr:
8891           fprintf (outfile, "address");
8892           break;
8893         case dw_val_class_offset:
8894           fprintf (outfile, "offset");
8895           break;
8896         case dw_val_class_loc:
8897           fprintf (outfile, "location descriptor");
8898           break;
8899         case dw_val_class_loc_list:
8900           fprintf (outfile, "location list -> label:%s",
8901                    AT_loc_list (a)->ll_symbol);
8902           break;
8903         case dw_val_class_range_list:
8904           fprintf (outfile, "range list");
8905           break;
8906         case dw_val_class_const:
8907           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8908           break;
8909         case dw_val_class_unsigned_const:
8910           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8911           break;
8912         case dw_val_class_const_double:
8913           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8914                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8915                    a->dw_attr_val.v.val_double.high,
8916                    a->dw_attr_val.v.val_double.low);
8917           break;
8918         case dw_val_class_vec:
8919           fprintf (outfile, "floating-point or vector constant");
8920           break;
8921         case dw_val_class_flag:
8922           fprintf (outfile, "%u", AT_flag (a));
8923           break;
8924         case dw_val_class_die_ref:
8925           if (AT_ref (a) != NULL)
8926             {
8927               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8928                 {
8929                   fprintf (outfile, "die -> signature: ");
8930                   print_signature (outfile,
8931                                    AT_ref (a)->die_id.die_type_node->signature);
8932                 }
8933               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8934                 fprintf (outfile, "die -> label: %s",
8935                          AT_ref (a)->die_id.die_symbol);
8936               else
8937                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8938               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8939             }
8940           else
8941             fprintf (outfile, "die -> <null>");
8942           break;
8943         case dw_val_class_vms_delta:
8944           fprintf (outfile, "delta: @slotcount(%s-%s)",
8945                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8946           break;
8947         case dw_val_class_lbl_id:
8948         case dw_val_class_lineptr:
8949         case dw_val_class_macptr:
8950           fprintf (outfile, "label: %s", AT_lbl (a));
8951           break;
8952         case dw_val_class_str:
8953           if (AT_string (a) != NULL)
8954             fprintf (outfile, "\"%s\"", AT_string (a));
8955           else
8956             fprintf (outfile, "<null>");
8957           break;
8958         case dw_val_class_file:
8959           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8960                    AT_file (a)->emitted_number);
8961           break;
8962         case dw_val_class_data8:
8963           {
8964             int i;
8965
8966             for (i = 0; i < 8; i++)
8967               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8968             break;
8969           }
8970         default:
8971           break;
8972         }
8973
8974       fprintf (outfile, "\n");
8975     }
8976
8977   if (die->die_child != NULL)
8978     {
8979       print_indent += 4;
8980       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8981       print_indent -= 4;
8982     }
8983   if (print_indent == 0)
8984     fprintf (outfile, "\n");
8985 }
8986
8987 /* Print the information collected for a given DIE.  */
8988
8989 DEBUG_FUNCTION void
8990 debug_dwarf_die (dw_die_ref die)
8991 {
8992   print_die (die, stderr);
8993 }
8994
8995 /* Print all DWARF information collected for the compilation unit.
8996    This routine is a debugging aid only.  */
8997
8998 DEBUG_FUNCTION void
8999 debug_dwarf (void)
9000 {
9001   print_indent = 0;
9002   print_die (comp_unit_die (), stderr);
9003 }
9004 \f
9005 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
9006    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
9007    DIE that marks the start of the DIEs for this include file.  */
9008
9009 static dw_die_ref
9010 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
9011 {
9012   const char *filename = get_AT_string (bincl_die, DW_AT_name);
9013   dw_die_ref new_unit = gen_compile_unit_die (filename);
9014
9015   new_unit->die_sib = old_unit;
9016   return new_unit;
9017 }
9018
9019 /* Close an include-file CU and reopen the enclosing one.  */
9020
9021 static dw_die_ref
9022 pop_compile_unit (dw_die_ref old_unit)
9023 {
9024   dw_die_ref new_unit = old_unit->die_sib;
9025
9026   old_unit->die_sib = NULL;
9027   return new_unit;
9028 }
9029
9030 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
9031 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
9032
9033 /* Calculate the checksum of a location expression.  */
9034
9035 static inline void
9036 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
9037 {
9038   int tem;
9039
9040   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
9041   CHECKSUM (tem);
9042   CHECKSUM (loc->dw_loc_oprnd1);
9043   CHECKSUM (loc->dw_loc_oprnd2);
9044 }
9045
9046 /* Calculate the checksum of an attribute.  */
9047
9048 static void
9049 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
9050 {
9051   dw_loc_descr_ref loc;
9052   rtx r;
9053
9054   CHECKSUM (at->dw_attr);
9055
9056   /* We don't care that this was compiled with a different compiler
9057      snapshot; if the output is the same, that's what matters.  */
9058   if (at->dw_attr == DW_AT_producer)
9059     return;
9060
9061   switch (AT_class (at))
9062     {
9063     case dw_val_class_const:
9064       CHECKSUM (at->dw_attr_val.v.val_int);
9065       break;
9066     case dw_val_class_unsigned_const:
9067       CHECKSUM (at->dw_attr_val.v.val_unsigned);
9068       break;
9069     case dw_val_class_const_double:
9070       CHECKSUM (at->dw_attr_val.v.val_double);
9071       break;
9072     case dw_val_class_vec:
9073       CHECKSUM (at->dw_attr_val.v.val_vec);
9074       break;
9075     case dw_val_class_flag:
9076       CHECKSUM (at->dw_attr_val.v.val_flag);
9077       break;
9078     case dw_val_class_str:
9079       CHECKSUM_STRING (AT_string (at));
9080       break;
9081
9082     case dw_val_class_addr:
9083       r = AT_addr (at);
9084       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9085       CHECKSUM_STRING (XSTR (r, 0));
9086       break;
9087
9088     case dw_val_class_offset:
9089       CHECKSUM (at->dw_attr_val.v.val_offset);
9090       break;
9091
9092     case dw_val_class_loc:
9093       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9094         loc_checksum (loc, ctx);
9095       break;
9096
9097     case dw_val_class_die_ref:
9098       die_checksum (AT_ref (at), ctx, mark);
9099       break;
9100
9101     case dw_val_class_fde_ref:
9102     case dw_val_class_vms_delta:
9103     case dw_val_class_lbl_id:
9104     case dw_val_class_lineptr:
9105     case dw_val_class_macptr:
9106       break;
9107
9108     case dw_val_class_file:
9109       CHECKSUM_STRING (AT_file (at)->filename);
9110       break;
9111
9112     case dw_val_class_data8:
9113       CHECKSUM (at->dw_attr_val.v.val_data8);
9114       break;
9115
9116     default:
9117       break;
9118     }
9119 }
9120
9121 /* Calculate the checksum of a DIE.  */
9122
9123 static void
9124 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9125 {
9126   dw_die_ref c;
9127   dw_attr_ref a;
9128   unsigned ix;
9129
9130   /* To avoid infinite recursion.  */
9131   if (die->die_mark)
9132     {
9133       CHECKSUM (die->die_mark);
9134       return;
9135     }
9136   die->die_mark = ++(*mark);
9137
9138   CHECKSUM (die->die_tag);
9139
9140   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9141     attr_checksum (a, ctx, mark);
9142
9143   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
9144 }
9145
9146 #undef CHECKSUM
9147 #undef CHECKSUM_STRING
9148
9149 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
9150 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
9151 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
9152 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
9153 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
9154 #define CHECKSUM_ATTR(FOO) \
9155   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
9156
9157 /* Calculate the checksum of a number in signed LEB128 format.  */
9158
9159 static void
9160 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
9161 {
9162   unsigned char byte;
9163   bool more;
9164
9165   while (1)
9166     {
9167       byte = (value & 0x7f);
9168       value >>= 7;
9169       more = !((value == 0 && (byte & 0x40) == 0)
9170                 || (value == -1 && (byte & 0x40) != 0));
9171       if (more)
9172         byte |= 0x80;
9173       CHECKSUM (byte);
9174       if (!more)
9175         break;
9176     }
9177 }
9178
9179 /* Calculate the checksum of a number in unsigned LEB128 format.  */
9180
9181 static void
9182 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
9183 {
9184   while (1)
9185     {
9186       unsigned char byte = (value & 0x7f);
9187       value >>= 7;
9188       if (value != 0)
9189         /* More bytes to follow.  */
9190         byte |= 0x80;
9191       CHECKSUM (byte);
9192       if (value == 0)
9193         break;
9194     }
9195 }
9196
9197 /* Checksum the context of the DIE.  This adds the names of any
9198    surrounding namespaces or structures to the checksum.  */
9199
9200 static void
9201 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
9202 {
9203   const char *name;
9204   dw_die_ref spec;
9205   int tag = die->die_tag;
9206
9207   if (tag != DW_TAG_namespace
9208       && tag != DW_TAG_structure_type
9209       && tag != DW_TAG_class_type)
9210     return;
9211
9212   name = get_AT_string (die, DW_AT_name);
9213
9214   spec = get_AT_ref (die, DW_AT_specification);
9215   if (spec != NULL)
9216     die = spec;
9217
9218   if (die->die_parent != NULL)
9219     checksum_die_context (die->die_parent, ctx);
9220
9221   CHECKSUM_ULEB128 ('C');
9222   CHECKSUM_ULEB128 (tag);
9223   if (name != NULL)
9224     CHECKSUM_STRING (name);
9225 }
9226
9227 /* Calculate the checksum of a location expression.  */
9228
9229 static inline void
9230 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
9231 {
9232   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
9233      were emitted as a DW_FORM_sdata instead of a location expression.  */
9234   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
9235     {
9236       CHECKSUM_ULEB128 (DW_FORM_sdata);
9237       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
9238       return;
9239     }
9240
9241   /* Otherwise, just checksum the raw location expression.  */
9242   while (loc != NULL)
9243     {
9244       CHECKSUM_ULEB128 (loc->dw_loc_opc);
9245       CHECKSUM (loc->dw_loc_oprnd1);
9246       CHECKSUM (loc->dw_loc_oprnd2);
9247       loc = loc->dw_loc_next;
9248     }
9249 }
9250
9251 /* Calculate the checksum of an attribute.  */
9252
9253 static void
9254 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
9255                        struct md5_ctx *ctx, int *mark)
9256 {
9257   dw_loc_descr_ref loc;
9258   rtx r;
9259
9260   if (AT_class (at) == dw_val_class_die_ref)
9261     {
9262       dw_die_ref target_die = AT_ref (at);
9263
9264       /* For pointer and reference types, we checksum only the (qualified)
9265          name of the target type (if there is a name).  For friend entries,
9266          we checksum only the (qualified) name of the target type or function.
9267          This allows the checksum to remain the same whether the target type
9268          is complete or not.  */
9269       if ((at->dw_attr == DW_AT_type
9270            && (tag == DW_TAG_pointer_type
9271                || tag == DW_TAG_reference_type
9272                || tag == DW_TAG_rvalue_reference_type
9273                || tag == DW_TAG_ptr_to_member_type))
9274           || (at->dw_attr == DW_AT_friend
9275               && tag == DW_TAG_friend))
9276         {
9277           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9278
9279           if (name_attr != NULL)
9280             {
9281               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9282
9283               if (decl == NULL)
9284                 decl = target_die;
9285               CHECKSUM_ULEB128 ('N');
9286               CHECKSUM_ULEB128 (at->dw_attr);
9287               if (decl->die_parent != NULL)
9288                 checksum_die_context (decl->die_parent, ctx);
9289               CHECKSUM_ULEB128 ('E');
9290               CHECKSUM_STRING (AT_string (name_attr));
9291               return;
9292             }
9293         }
9294
9295       /* For all other references to another DIE, we check to see if the
9296          target DIE has already been visited.  If it has, we emit a
9297          backward reference; if not, we descend recursively.  */
9298       if (target_die->die_mark > 0)
9299         {
9300           CHECKSUM_ULEB128 ('R');
9301           CHECKSUM_ULEB128 (at->dw_attr);
9302           CHECKSUM_ULEB128 (target_die->die_mark);
9303         }
9304       else
9305         {
9306           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9307
9308           if (decl == NULL)
9309             decl = target_die;
9310           target_die->die_mark = ++(*mark);
9311           CHECKSUM_ULEB128 ('T');
9312           CHECKSUM_ULEB128 (at->dw_attr);
9313           if (decl->die_parent != NULL)
9314             checksum_die_context (decl->die_parent, ctx);
9315           die_checksum_ordered (target_die, ctx, mark);
9316         }
9317       return;
9318     }
9319
9320   CHECKSUM_ULEB128 ('A');
9321   CHECKSUM_ULEB128 (at->dw_attr);
9322
9323   switch (AT_class (at))
9324     {
9325     case dw_val_class_const:
9326       CHECKSUM_ULEB128 (DW_FORM_sdata);
9327       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9328       break;
9329
9330     case dw_val_class_unsigned_const:
9331       CHECKSUM_ULEB128 (DW_FORM_sdata);
9332       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9333       break;
9334
9335     case dw_val_class_const_double:
9336       CHECKSUM_ULEB128 (DW_FORM_block);
9337       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9338       CHECKSUM (at->dw_attr_val.v.val_double);
9339       break;
9340
9341     case dw_val_class_vec:
9342       CHECKSUM_ULEB128 (DW_FORM_block);
9343       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9344       CHECKSUM (at->dw_attr_val.v.val_vec);
9345       break;
9346
9347     case dw_val_class_flag:
9348       CHECKSUM_ULEB128 (DW_FORM_flag);
9349       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9350       break;
9351
9352     case dw_val_class_str:
9353       CHECKSUM_ULEB128 (DW_FORM_string);
9354       CHECKSUM_STRING (AT_string (at));
9355       break;
9356
9357     case dw_val_class_addr:
9358       r = AT_addr (at);
9359       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9360       CHECKSUM_ULEB128 (DW_FORM_string);
9361       CHECKSUM_STRING (XSTR (r, 0));
9362       break;
9363
9364     case dw_val_class_offset:
9365       CHECKSUM_ULEB128 (DW_FORM_sdata);
9366       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9367       break;
9368
9369     case dw_val_class_loc:
9370       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9371         loc_checksum_ordered (loc, ctx);
9372       break;
9373
9374     case dw_val_class_fde_ref:
9375     case dw_val_class_lbl_id:
9376     case dw_val_class_lineptr:
9377     case dw_val_class_macptr:
9378       break;
9379
9380     case dw_val_class_file:
9381       CHECKSUM_ULEB128 (DW_FORM_string);
9382       CHECKSUM_STRING (AT_file (at)->filename);
9383       break;
9384
9385     case dw_val_class_data8:
9386       CHECKSUM (at->dw_attr_val.v.val_data8);
9387       break;
9388
9389     default:
9390       break;
9391     }
9392 }
9393
9394 struct checksum_attributes
9395 {
9396   dw_attr_ref at_name;
9397   dw_attr_ref at_type;
9398   dw_attr_ref at_friend;
9399   dw_attr_ref at_accessibility;
9400   dw_attr_ref at_address_class;
9401   dw_attr_ref at_allocated;
9402   dw_attr_ref at_artificial;
9403   dw_attr_ref at_associated;
9404   dw_attr_ref at_binary_scale;
9405   dw_attr_ref at_bit_offset;
9406   dw_attr_ref at_bit_size;
9407   dw_attr_ref at_bit_stride;
9408   dw_attr_ref at_byte_size;
9409   dw_attr_ref at_byte_stride;
9410   dw_attr_ref at_const_value;
9411   dw_attr_ref at_containing_type;
9412   dw_attr_ref at_count;
9413   dw_attr_ref at_data_location;
9414   dw_attr_ref at_data_member_location;
9415   dw_attr_ref at_decimal_scale;
9416   dw_attr_ref at_decimal_sign;
9417   dw_attr_ref at_default_value;
9418   dw_attr_ref at_digit_count;
9419   dw_attr_ref at_discr;
9420   dw_attr_ref at_discr_list;
9421   dw_attr_ref at_discr_value;
9422   dw_attr_ref at_encoding;
9423   dw_attr_ref at_endianity;
9424   dw_attr_ref at_explicit;
9425   dw_attr_ref at_is_optional;
9426   dw_attr_ref at_location;
9427   dw_attr_ref at_lower_bound;
9428   dw_attr_ref at_mutable;
9429   dw_attr_ref at_ordering;
9430   dw_attr_ref at_picture_string;
9431   dw_attr_ref at_prototyped;
9432   dw_attr_ref at_small;
9433   dw_attr_ref at_segment;
9434   dw_attr_ref at_string_length;
9435   dw_attr_ref at_threads_scaled;
9436   dw_attr_ref at_upper_bound;
9437   dw_attr_ref at_use_location;
9438   dw_attr_ref at_use_UTF8;
9439   dw_attr_ref at_variable_parameter;
9440   dw_attr_ref at_virtuality;
9441   dw_attr_ref at_visibility;
9442   dw_attr_ref at_vtable_elem_location;
9443 };
9444
9445 /* Collect the attributes that we will want to use for the checksum.  */
9446
9447 static void
9448 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9449 {
9450   dw_attr_ref a;
9451   unsigned ix;
9452
9453   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9454     {
9455       switch (a->dw_attr)
9456         {
9457         case DW_AT_name:
9458           attrs->at_name = a;
9459           break;
9460         case DW_AT_type:
9461           attrs->at_type = a;
9462           break;
9463         case DW_AT_friend:
9464           attrs->at_friend = a;
9465           break;
9466         case DW_AT_accessibility:
9467           attrs->at_accessibility = a;
9468           break;
9469         case DW_AT_address_class:
9470           attrs->at_address_class = a;
9471           break;
9472         case DW_AT_allocated:
9473           attrs->at_allocated = a;
9474           break;
9475         case DW_AT_artificial:
9476           attrs->at_artificial = a;
9477           break;
9478         case DW_AT_associated:
9479           attrs->at_associated = a;
9480           break;
9481         case DW_AT_binary_scale:
9482           attrs->at_binary_scale = a;
9483           break;
9484         case DW_AT_bit_offset:
9485           attrs->at_bit_offset = a;
9486           break;
9487         case DW_AT_bit_size:
9488           attrs->at_bit_size = a;
9489           break;
9490         case DW_AT_bit_stride:
9491           attrs->at_bit_stride = a;
9492           break;
9493         case DW_AT_byte_size:
9494           attrs->at_byte_size = a;
9495           break;
9496         case DW_AT_byte_stride:
9497           attrs->at_byte_stride = a;
9498           break;
9499         case DW_AT_const_value:
9500           attrs->at_const_value = a;
9501           break;
9502         case DW_AT_containing_type:
9503           attrs->at_containing_type = a;
9504           break;
9505         case DW_AT_count:
9506           attrs->at_count = a;
9507           break;
9508         case DW_AT_data_location:
9509           attrs->at_data_location = a;
9510           break;
9511         case DW_AT_data_member_location:
9512           attrs->at_data_member_location = a;
9513           break;
9514         case DW_AT_decimal_scale:
9515           attrs->at_decimal_scale = a;
9516           break;
9517         case DW_AT_decimal_sign:
9518           attrs->at_decimal_sign = a;
9519           break;
9520         case DW_AT_default_value:
9521           attrs->at_default_value = a;
9522           break;
9523         case DW_AT_digit_count:
9524           attrs->at_digit_count = a;
9525           break;
9526         case DW_AT_discr:
9527           attrs->at_discr = a;
9528           break;
9529         case DW_AT_discr_list:
9530           attrs->at_discr_list = a;
9531           break;
9532         case DW_AT_discr_value:
9533           attrs->at_discr_value = a;
9534           break;
9535         case DW_AT_encoding:
9536           attrs->at_encoding = a;
9537           break;
9538         case DW_AT_endianity:
9539           attrs->at_endianity = a;
9540           break;
9541         case DW_AT_explicit:
9542           attrs->at_explicit = a;
9543           break;
9544         case DW_AT_is_optional:
9545           attrs->at_is_optional = a;
9546           break;
9547         case DW_AT_location:
9548           attrs->at_location = a;
9549           break;
9550         case DW_AT_lower_bound:
9551           attrs->at_lower_bound = a;
9552           break;
9553         case DW_AT_mutable:
9554           attrs->at_mutable = a;
9555           break;
9556         case DW_AT_ordering:
9557           attrs->at_ordering = a;
9558           break;
9559         case DW_AT_picture_string:
9560           attrs->at_picture_string = a;
9561           break;
9562         case DW_AT_prototyped:
9563           attrs->at_prototyped = a;
9564           break;
9565         case DW_AT_small:
9566           attrs->at_small = a;
9567           break;
9568         case DW_AT_segment:
9569           attrs->at_segment = a;
9570           break;
9571         case DW_AT_string_length:
9572           attrs->at_string_length = a;
9573           break;
9574         case DW_AT_threads_scaled:
9575           attrs->at_threads_scaled = a;
9576           break;
9577         case DW_AT_upper_bound:
9578           attrs->at_upper_bound = a;
9579           break;
9580         case DW_AT_use_location:
9581           attrs->at_use_location = a;
9582           break;
9583         case DW_AT_use_UTF8:
9584           attrs->at_use_UTF8 = a;
9585           break;
9586         case DW_AT_variable_parameter:
9587           attrs->at_variable_parameter = a;
9588           break;
9589         case DW_AT_virtuality:
9590           attrs->at_virtuality = a;
9591           break;
9592         case DW_AT_visibility:
9593           attrs->at_visibility = a;
9594           break;
9595         case DW_AT_vtable_elem_location:
9596           attrs->at_vtable_elem_location = a;
9597           break;
9598         default:
9599           break;
9600         }
9601     }
9602 }
9603
9604 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9605
9606 static void
9607 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9608 {
9609   dw_die_ref c;
9610   dw_die_ref decl;
9611   struct checksum_attributes attrs;
9612
9613   CHECKSUM_ULEB128 ('D');
9614   CHECKSUM_ULEB128 (die->die_tag);
9615
9616   memset (&attrs, 0, sizeof (attrs));
9617
9618   decl = get_AT_ref (die, DW_AT_specification);
9619   if (decl != NULL)
9620     collect_checksum_attributes (&attrs, decl);
9621   collect_checksum_attributes (&attrs, die);
9622
9623   CHECKSUM_ATTR (attrs.at_name);
9624   CHECKSUM_ATTR (attrs.at_accessibility);
9625   CHECKSUM_ATTR (attrs.at_address_class);
9626   CHECKSUM_ATTR (attrs.at_allocated);
9627   CHECKSUM_ATTR (attrs.at_artificial);
9628   CHECKSUM_ATTR (attrs.at_associated);
9629   CHECKSUM_ATTR (attrs.at_binary_scale);
9630   CHECKSUM_ATTR (attrs.at_bit_offset);
9631   CHECKSUM_ATTR (attrs.at_bit_size);
9632   CHECKSUM_ATTR (attrs.at_bit_stride);
9633   CHECKSUM_ATTR (attrs.at_byte_size);
9634   CHECKSUM_ATTR (attrs.at_byte_stride);
9635   CHECKSUM_ATTR (attrs.at_const_value);
9636   CHECKSUM_ATTR (attrs.at_containing_type);
9637   CHECKSUM_ATTR (attrs.at_count);
9638   CHECKSUM_ATTR (attrs.at_data_location);
9639   CHECKSUM_ATTR (attrs.at_data_member_location);
9640   CHECKSUM_ATTR (attrs.at_decimal_scale);
9641   CHECKSUM_ATTR (attrs.at_decimal_sign);
9642   CHECKSUM_ATTR (attrs.at_default_value);
9643   CHECKSUM_ATTR (attrs.at_digit_count);
9644   CHECKSUM_ATTR (attrs.at_discr);
9645   CHECKSUM_ATTR (attrs.at_discr_list);
9646   CHECKSUM_ATTR (attrs.at_discr_value);
9647   CHECKSUM_ATTR (attrs.at_encoding);
9648   CHECKSUM_ATTR (attrs.at_endianity);
9649   CHECKSUM_ATTR (attrs.at_explicit);
9650   CHECKSUM_ATTR (attrs.at_is_optional);
9651   CHECKSUM_ATTR (attrs.at_location);
9652   CHECKSUM_ATTR (attrs.at_lower_bound);
9653   CHECKSUM_ATTR (attrs.at_mutable);
9654   CHECKSUM_ATTR (attrs.at_ordering);
9655   CHECKSUM_ATTR (attrs.at_picture_string);
9656   CHECKSUM_ATTR (attrs.at_prototyped);
9657   CHECKSUM_ATTR (attrs.at_small);
9658   CHECKSUM_ATTR (attrs.at_segment);
9659   CHECKSUM_ATTR (attrs.at_string_length);
9660   CHECKSUM_ATTR (attrs.at_threads_scaled);
9661   CHECKSUM_ATTR (attrs.at_upper_bound);
9662   CHECKSUM_ATTR (attrs.at_use_location);
9663   CHECKSUM_ATTR (attrs.at_use_UTF8);
9664   CHECKSUM_ATTR (attrs.at_variable_parameter);
9665   CHECKSUM_ATTR (attrs.at_virtuality);
9666   CHECKSUM_ATTR (attrs.at_visibility);
9667   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9668   CHECKSUM_ATTR (attrs.at_type);
9669   CHECKSUM_ATTR (attrs.at_friend);
9670
9671   /* Checksum the child DIEs, except for nested types and member functions.  */
9672   c = die->die_child;
9673   if (c) do {
9674     dw_attr_ref name_attr;
9675
9676     c = c->die_sib;
9677     name_attr = get_AT (c, DW_AT_name);
9678     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9679         && name_attr != NULL)
9680       {
9681         CHECKSUM_ULEB128 ('S');
9682         CHECKSUM_ULEB128 (c->die_tag);
9683         CHECKSUM_STRING (AT_string (name_attr));
9684       }
9685     else
9686       {
9687         /* Mark this DIE so it gets processed when unmarking.  */
9688         if (c->die_mark == 0)
9689           c->die_mark = -1;
9690         die_checksum_ordered (c, ctx, mark);
9691       }
9692   } while (c != die->die_child);
9693
9694   CHECKSUM_ULEB128 (0);
9695 }
9696
9697 #undef CHECKSUM
9698 #undef CHECKSUM_STRING
9699 #undef CHECKSUM_ATTR
9700 #undef CHECKSUM_LEB128
9701 #undef CHECKSUM_ULEB128
9702
9703 /* Generate the type signature for DIE.  This is computed by generating an
9704    MD5 checksum over the DIE's tag, its relevant attributes, and its
9705    children.  Attributes that are references to other DIEs are processed
9706    by recursion, using the MARK field to prevent infinite recursion.
9707    If the DIE is nested inside a namespace or another type, we also
9708    need to include that context in the signature.  The lower 64 bits
9709    of the resulting MD5 checksum comprise the signature.  */
9710
9711 static void
9712 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9713 {
9714   int mark;
9715   const char *name;
9716   unsigned char checksum[16];
9717   struct md5_ctx ctx;
9718   dw_die_ref decl;
9719
9720   name = get_AT_string (die, DW_AT_name);
9721   decl = get_AT_ref (die, DW_AT_specification);
9722
9723   /* First, compute a signature for just the type name (and its surrounding
9724      context, if any.  This is stored in the type unit DIE for link-time
9725      ODR (one-definition rule) checking.  */
9726
9727   if (is_cxx() && name != NULL)
9728     {
9729       md5_init_ctx (&ctx);
9730
9731       /* Checksum the names of surrounding namespaces and structures.  */
9732       if (decl != NULL && decl->die_parent != NULL)
9733         checksum_die_context (decl->die_parent, &ctx);
9734
9735       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9736       md5_process_bytes (name, strlen (name) + 1, &ctx);
9737       md5_finish_ctx (&ctx, checksum);
9738
9739       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9740     }
9741
9742   /* Next, compute the complete type signature.  */
9743
9744   md5_init_ctx (&ctx);
9745   mark = 1;
9746   die->die_mark = mark;
9747
9748   /* Checksum the names of surrounding namespaces and structures.  */
9749   if (decl != NULL && decl->die_parent != NULL)
9750     checksum_die_context (decl->die_parent, &ctx);
9751
9752   /* Checksum the DIE and its children.  */
9753   die_checksum_ordered (die, &ctx, &mark);
9754   unmark_all_dies (die);
9755   md5_finish_ctx (&ctx, checksum);
9756
9757   /* Store the signature in the type node and link the type DIE and the
9758      type node together.  */
9759   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9760           DWARF_TYPE_SIGNATURE_SIZE);
9761   die->die_id.die_type_node = type_node;
9762   type_node->type_die = die;
9763
9764   /* If the DIE is a specification, link its declaration to the type node
9765      as well.  */
9766   if (decl != NULL)
9767     decl->die_id.die_type_node = type_node;
9768 }
9769
9770 /* Do the location expressions look same?  */
9771 static inline int
9772 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9773 {
9774   return loc1->dw_loc_opc == loc2->dw_loc_opc
9775          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9776          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9777 }
9778
9779 /* Do the values look the same?  */
9780 static int
9781 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9782 {
9783   dw_loc_descr_ref loc1, loc2;
9784   rtx r1, r2;
9785
9786   if (v1->val_class != v2->val_class)
9787     return 0;
9788
9789   switch (v1->val_class)
9790     {
9791     case dw_val_class_const:
9792       return v1->v.val_int == v2->v.val_int;
9793     case dw_val_class_unsigned_const:
9794       return v1->v.val_unsigned == v2->v.val_unsigned;
9795     case dw_val_class_const_double:
9796       return v1->v.val_double.high == v2->v.val_double.high
9797              && v1->v.val_double.low == v2->v.val_double.low;
9798     case dw_val_class_vec:
9799       if (v1->v.val_vec.length != v2->v.val_vec.length
9800           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9801         return 0;
9802       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9803                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9804         return 0;
9805       return 1;
9806     case dw_val_class_flag:
9807       return v1->v.val_flag == v2->v.val_flag;
9808     case dw_val_class_str:
9809       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9810
9811     case dw_val_class_addr:
9812       r1 = v1->v.val_addr;
9813       r2 = v2->v.val_addr;
9814       if (GET_CODE (r1) != GET_CODE (r2))
9815         return 0;
9816       return !rtx_equal_p (r1, r2);
9817
9818     case dw_val_class_offset:
9819       return v1->v.val_offset == v2->v.val_offset;
9820
9821     case dw_val_class_loc:
9822       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9823            loc1 && loc2;
9824            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9825         if (!same_loc_p (loc1, loc2, mark))
9826           return 0;
9827       return !loc1 && !loc2;
9828
9829     case dw_val_class_die_ref:
9830       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9831
9832     case dw_val_class_fde_ref:
9833     case dw_val_class_vms_delta:
9834     case dw_val_class_lbl_id:
9835     case dw_val_class_lineptr:
9836     case dw_val_class_macptr:
9837       return 1;
9838
9839     case dw_val_class_file:
9840       return v1->v.val_file == v2->v.val_file;
9841
9842     case dw_val_class_data8:
9843       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9844
9845     default:
9846       return 1;
9847     }
9848 }
9849
9850 /* Do the attributes look the same?  */
9851
9852 static int
9853 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9854 {
9855   if (at1->dw_attr != at2->dw_attr)
9856     return 0;
9857
9858   /* We don't care that this was compiled with a different compiler
9859      snapshot; if the output is the same, that's what matters. */
9860   if (at1->dw_attr == DW_AT_producer)
9861     return 1;
9862
9863   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9864 }
9865
9866 /* Do the dies look the same?  */
9867
9868 static int
9869 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9870 {
9871   dw_die_ref c1, c2;
9872   dw_attr_ref a1;
9873   unsigned ix;
9874
9875   /* To avoid infinite recursion.  */
9876   if (die1->die_mark)
9877     return die1->die_mark == die2->die_mark;
9878   die1->die_mark = die2->die_mark = ++(*mark);
9879
9880   if (die1->die_tag != die2->die_tag)
9881     return 0;
9882
9883   if (VEC_length (dw_attr_node, die1->die_attr)
9884       != VEC_length (dw_attr_node, die2->die_attr))
9885     return 0;
9886
9887   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9888     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9889       return 0;
9890
9891   c1 = die1->die_child;
9892   c2 = die2->die_child;
9893   if (! c1)
9894     {
9895       if (c2)
9896         return 0;
9897     }
9898   else
9899     for (;;)
9900       {
9901         if (!same_die_p (c1, c2, mark))
9902           return 0;
9903         c1 = c1->die_sib;
9904         c2 = c2->die_sib;
9905         if (c1 == die1->die_child)
9906           {
9907             if (c2 == die2->die_child)
9908               break;
9909             else
9910               return 0;
9911           }
9912     }
9913
9914   return 1;
9915 }
9916
9917 /* Do the dies look the same?  Wrapper around same_die_p.  */
9918
9919 static int
9920 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9921 {
9922   int mark = 0;
9923   int ret = same_die_p (die1, die2, &mark);
9924
9925   unmark_all_dies (die1);
9926   unmark_all_dies (die2);
9927
9928   return ret;
9929 }
9930
9931 /* The prefix to attach to symbols on DIEs in the current comdat debug
9932    info section.  */
9933 static char *comdat_symbol_id;
9934
9935 /* The index of the current symbol within the current comdat CU.  */
9936 static unsigned int comdat_symbol_number;
9937
9938 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9939    children, and set comdat_symbol_id accordingly.  */
9940
9941 static void
9942 compute_section_prefix (dw_die_ref unit_die)
9943 {
9944   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9945   const char *base = die_name ? lbasename (die_name) : "anonymous";
9946   char *name = XALLOCAVEC (char, strlen (base) + 64);
9947   char *p;
9948   int i, mark;
9949   unsigned char checksum[16];
9950   struct md5_ctx ctx;
9951
9952   /* Compute the checksum of the DIE, then append part of it as hex digits to
9953      the name filename of the unit.  */
9954
9955   md5_init_ctx (&ctx);
9956   mark = 0;
9957   die_checksum (unit_die, &ctx, &mark);
9958   unmark_all_dies (unit_die);
9959   md5_finish_ctx (&ctx, checksum);
9960
9961   sprintf (name, "%s.", base);
9962   clean_symbol_name (name);
9963
9964   p = name + strlen (name);
9965   for (i = 0; i < 4; i++)
9966     {
9967       sprintf (p, "%.2x", checksum[i]);
9968       p += 2;
9969     }
9970
9971   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9972   comdat_symbol_number = 0;
9973 }
9974
9975 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9976
9977 static int
9978 is_type_die (dw_die_ref die)
9979 {
9980   switch (die->die_tag)
9981     {
9982     case DW_TAG_array_type:
9983     case DW_TAG_class_type:
9984     case DW_TAG_interface_type:
9985     case DW_TAG_enumeration_type:
9986     case DW_TAG_pointer_type:
9987     case DW_TAG_reference_type:
9988     case DW_TAG_rvalue_reference_type:
9989     case DW_TAG_string_type:
9990     case DW_TAG_structure_type:
9991     case DW_TAG_subroutine_type:
9992     case DW_TAG_union_type:
9993     case DW_TAG_ptr_to_member_type:
9994     case DW_TAG_set_type:
9995     case DW_TAG_subrange_type:
9996     case DW_TAG_base_type:
9997     case DW_TAG_const_type:
9998     case DW_TAG_file_type:
9999     case DW_TAG_packed_type:
10000     case DW_TAG_volatile_type:
10001     case DW_TAG_typedef:
10002       return 1;
10003     default:
10004       return 0;
10005     }
10006 }
10007
10008 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
10009    Basically, we want to choose the bits that are likely to be shared between
10010    compilations (types) and leave out the bits that are specific to individual
10011    compilations (functions).  */
10012
10013 static int
10014 is_comdat_die (dw_die_ref c)
10015 {
10016   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
10017      we do for stabs.  The advantage is a greater likelihood of sharing between
10018      objects that don't include headers in the same order (and therefore would
10019      put the base types in a different comdat).  jason 8/28/00 */
10020
10021   if (c->die_tag == DW_TAG_base_type)
10022     return 0;
10023
10024   if (c->die_tag == DW_TAG_pointer_type
10025       || c->die_tag == DW_TAG_reference_type
10026       || c->die_tag == DW_TAG_rvalue_reference_type
10027       || c->die_tag == DW_TAG_const_type
10028       || c->die_tag == DW_TAG_volatile_type)
10029     {
10030       dw_die_ref t = get_AT_ref (c, DW_AT_type);
10031
10032       return t ? is_comdat_die (t) : 0;
10033     }
10034
10035   return is_type_die (c);
10036 }
10037
10038 /* Returns 1 iff C is the sort of DIE that might be referred to from another
10039    compilation unit.  */
10040
10041 static int
10042 is_symbol_die (dw_die_ref c)
10043 {
10044   return (is_type_die (c)
10045           || is_declaration_die (c)
10046           || c->die_tag == DW_TAG_namespace
10047           || c->die_tag == DW_TAG_module);
10048 }
10049
10050 /* Returns true iff C is a compile-unit DIE.  */
10051
10052 static inline bool
10053 is_cu_die (dw_die_ref c)
10054 {
10055   return c && c->die_tag == DW_TAG_compile_unit;
10056 }
10057
10058 static char *
10059 gen_internal_sym (const char *prefix)
10060 {
10061   char buf[256];
10062
10063   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
10064   return xstrdup (buf);
10065 }
10066
10067 /* Assign symbols to all worthy DIEs under DIE.  */
10068
10069 static void
10070 assign_symbol_names (dw_die_ref die)
10071 {
10072   dw_die_ref c;
10073
10074   if (is_symbol_die (die))
10075     {
10076       if (comdat_symbol_id)
10077         {
10078           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
10079
10080           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
10081                    comdat_symbol_id, comdat_symbol_number++);
10082           die->die_id.die_symbol = xstrdup (p);
10083         }
10084       else
10085         die->die_id.die_symbol = gen_internal_sym ("LDIE");
10086     }
10087
10088   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
10089 }
10090
10091 struct cu_hash_table_entry
10092 {
10093   dw_die_ref cu;
10094   unsigned min_comdat_num, max_comdat_num;
10095   struct cu_hash_table_entry *next;
10096 };
10097
10098 /* Routines to manipulate hash table of CUs.  */
10099 static hashval_t
10100 htab_cu_hash (const void *of)
10101 {
10102   const struct cu_hash_table_entry *const entry =
10103     (const struct cu_hash_table_entry *) of;
10104
10105   return htab_hash_string (entry->cu->die_id.die_symbol);
10106 }
10107
10108 static int
10109 htab_cu_eq (const void *of1, const void *of2)
10110 {
10111   const struct cu_hash_table_entry *const entry1 =
10112     (const struct cu_hash_table_entry *) of1;
10113   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10114
10115   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
10116 }
10117
10118 static void
10119 htab_cu_del (void *what)
10120 {
10121   struct cu_hash_table_entry *next,
10122     *entry = (struct cu_hash_table_entry *) what;
10123
10124   while (entry)
10125     {
10126       next = entry->next;
10127       free (entry);
10128       entry = next;
10129     }
10130 }
10131
10132 /* Check whether we have already seen this CU and set up SYM_NUM
10133    accordingly.  */
10134 static int
10135 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
10136 {
10137   struct cu_hash_table_entry dummy;
10138   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
10139
10140   dummy.max_comdat_num = 0;
10141
10142   slot = (struct cu_hash_table_entry **)
10143     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10144         INSERT);
10145   entry = *slot;
10146
10147   for (; entry; last = entry, entry = entry->next)
10148     {
10149       if (same_die_p_wrap (cu, entry->cu))
10150         break;
10151     }
10152
10153   if (entry)
10154     {
10155       *sym_num = entry->min_comdat_num;
10156       return 1;
10157     }
10158
10159   entry = XCNEW (struct cu_hash_table_entry);
10160   entry->cu = cu;
10161   entry->min_comdat_num = *sym_num = last->max_comdat_num;
10162   entry->next = *slot;
10163   *slot = entry;
10164
10165   return 0;
10166 }
10167
10168 /* Record SYM_NUM to record of CU in HTABLE.  */
10169 static void
10170 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
10171 {
10172   struct cu_hash_table_entry **slot, *entry;
10173
10174   slot = (struct cu_hash_table_entry **)
10175     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10176         NO_INSERT);
10177   entry = *slot;
10178
10179   entry->max_comdat_num = sym_num;
10180 }
10181
10182 /* Traverse the DIE (which is always comp_unit_die), and set up
10183    additional compilation units for each of the include files we see
10184    bracketed by BINCL/EINCL.  */
10185
10186 static void
10187 break_out_includes (dw_die_ref die)
10188 {
10189   dw_die_ref c;
10190   dw_die_ref unit = NULL;
10191   limbo_die_node *node, **pnode;
10192   htab_t cu_hash_table;
10193
10194   c = die->die_child;
10195   if (c) do {
10196     dw_die_ref prev = c;
10197     c = c->die_sib;
10198     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
10199            || (unit && is_comdat_die (c)))
10200       {
10201         dw_die_ref next = c->die_sib;
10202
10203         /* This DIE is for a secondary CU; remove it from the main one.  */
10204         remove_child_with_prev (c, prev);
10205
10206         if (c->die_tag == DW_TAG_GNU_BINCL)
10207           unit = push_new_compile_unit (unit, c);
10208         else if (c->die_tag == DW_TAG_GNU_EINCL)
10209           unit = pop_compile_unit (unit);
10210         else
10211           add_child_die (unit, c);
10212         c = next;
10213         if (c == die->die_child)
10214           break;
10215       }
10216   } while (c != die->die_child);
10217
10218 #if 0
10219   /* We can only use this in debugging, since the frontend doesn't check
10220      to make sure that we leave every include file we enter.  */
10221   gcc_assert (!unit);
10222 #endif
10223
10224   assign_symbol_names (die);
10225   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
10226   for (node = limbo_die_list, pnode = &limbo_die_list;
10227        node;
10228        node = node->next)
10229     {
10230       int is_dupl;
10231
10232       compute_section_prefix (node->die);
10233       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
10234                         &comdat_symbol_number);
10235       assign_symbol_names (node->die);
10236       if (is_dupl)
10237         *pnode = node->next;
10238       else
10239         {
10240           pnode = &node->next;
10241           record_comdat_symbol_number (node->die, cu_hash_table,
10242                 comdat_symbol_number);
10243         }
10244     }
10245   htab_delete (cu_hash_table);
10246 }
10247
10248 /* Return non-zero if this DIE is a declaration.  */
10249
10250 static int
10251 is_declaration_die (dw_die_ref die)
10252 {
10253   dw_attr_ref a;
10254   unsigned ix;
10255
10256   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10257     if (a->dw_attr == DW_AT_declaration)
10258       return 1;
10259
10260   return 0;
10261 }
10262
10263 /* Return non-zero if this DIE is nested inside a subprogram.  */
10264
10265 static int
10266 is_nested_in_subprogram (dw_die_ref die)
10267 {
10268   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
10269
10270   if (decl == NULL)
10271     decl = die;
10272   return local_scope_p (decl);
10273 }
10274
10275 /* Return non-zero if this DIE contains a defining declaration of a
10276    subprogram.  */
10277
10278 static int
10279 contains_subprogram_definition (dw_die_ref die)
10280 {
10281   dw_die_ref c;
10282
10283   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
10284     return 1;
10285   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
10286   return 0;
10287 }
10288
10289 /* Return non-zero if this is a type DIE that should be moved to a
10290    COMDAT .debug_types section.  */
10291
10292 static int
10293 should_move_die_to_comdat (dw_die_ref die)
10294 {
10295   switch (die->die_tag)
10296     {
10297     case DW_TAG_class_type:
10298     case DW_TAG_structure_type:
10299     case DW_TAG_enumeration_type:
10300     case DW_TAG_union_type:
10301       /* Don't move declarations, inlined instances, or types nested in a
10302          subprogram.  */
10303       if (is_declaration_die (die)
10304           || get_AT (die, DW_AT_abstract_origin)
10305           || is_nested_in_subprogram (die))
10306         return 0;
10307       /* A type definition should never contain a subprogram definition.  */
10308       gcc_assert (!contains_subprogram_definition (die));
10309       return 1;
10310     case DW_TAG_array_type:
10311     case DW_TAG_interface_type:
10312     case DW_TAG_pointer_type:
10313     case DW_TAG_reference_type:
10314     case DW_TAG_rvalue_reference_type:
10315     case DW_TAG_string_type:
10316     case DW_TAG_subroutine_type:
10317     case DW_TAG_ptr_to_member_type:
10318     case DW_TAG_set_type:
10319     case DW_TAG_subrange_type:
10320     case DW_TAG_base_type:
10321     case DW_TAG_const_type:
10322     case DW_TAG_file_type:
10323     case DW_TAG_packed_type:
10324     case DW_TAG_volatile_type:
10325     case DW_TAG_typedef:
10326     default:
10327       return 0;
10328     }
10329 }
10330
10331 /* Make a clone of DIE.  */
10332
10333 static dw_die_ref
10334 clone_die (dw_die_ref die)
10335 {
10336   dw_die_ref clone;
10337   dw_attr_ref a;
10338   unsigned ix;
10339
10340   clone = ggc_alloc_cleared_die_node ();
10341   clone->die_tag = die->die_tag;
10342
10343   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10344     add_dwarf_attr (clone, a);
10345
10346   return clone;
10347 }
10348
10349 /* Make a clone of the tree rooted at DIE.  */
10350
10351 static dw_die_ref
10352 clone_tree (dw_die_ref die)
10353 {
10354   dw_die_ref c;
10355   dw_die_ref clone = clone_die (die);
10356
10357   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10358
10359   return clone;
10360 }
10361
10362 /* Make a clone of DIE as a declaration.  */
10363
10364 static dw_die_ref
10365 clone_as_declaration (dw_die_ref die)
10366 {
10367   dw_die_ref clone;
10368   dw_die_ref decl;
10369   dw_attr_ref a;
10370   unsigned ix;
10371
10372   /* If the DIE is already a declaration, just clone it.  */
10373   if (is_declaration_die (die))
10374     return clone_die (die);
10375
10376   /* If the DIE is a specification, just clone its declaration DIE.  */
10377   decl = get_AT_ref (die, DW_AT_specification);
10378   if (decl != NULL)
10379     return clone_die (decl);
10380
10381   clone = ggc_alloc_cleared_die_node ();
10382   clone->die_tag = die->die_tag;
10383
10384   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10385     {
10386       /* We don't want to copy over all attributes.
10387          For example we don't want DW_AT_byte_size because otherwise we will no
10388          longer have a declaration and GDB will treat it as a definition.  */
10389
10390       switch (a->dw_attr)
10391         {
10392         case DW_AT_artificial:
10393         case DW_AT_containing_type:
10394         case DW_AT_external:
10395         case DW_AT_name:
10396         case DW_AT_type:
10397         case DW_AT_virtuality:
10398         case DW_AT_linkage_name:
10399         case DW_AT_MIPS_linkage_name:
10400           add_dwarf_attr (clone, a);
10401           break;
10402         case DW_AT_byte_size:
10403         default:
10404           break;
10405         }
10406     }
10407
10408   if (die->die_id.die_type_node)
10409     add_AT_die_ref (clone, DW_AT_signature, die);
10410
10411   add_AT_flag (clone, DW_AT_declaration, 1);
10412   return clone;
10413 }
10414
10415 /* Copy the declaration context to the new compile unit DIE.  This includes
10416    any surrounding namespace or type declarations.  If the DIE has an
10417    AT_specification attribute, it also includes attributes and children
10418    attached to the specification.  */
10419
10420 static void
10421 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10422 {
10423   dw_die_ref decl;
10424   dw_die_ref new_decl;
10425
10426   decl = get_AT_ref (die, DW_AT_specification);
10427   if (decl == NULL)
10428     decl = die;
10429   else
10430     {
10431       unsigned ix;
10432       dw_die_ref c;
10433       dw_attr_ref a;
10434
10435       /* Copy the type node pointer from the new DIE to the original
10436          declaration DIE so we can forward references later.  */
10437       decl->die_id.die_type_node = die->die_id.die_type_node;
10438
10439       remove_AT (die, DW_AT_specification);
10440
10441       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10442         {
10443           if (a->dw_attr != DW_AT_name
10444               && a->dw_attr != DW_AT_declaration
10445               && a->dw_attr != DW_AT_external)
10446             add_dwarf_attr (die, a);
10447         }
10448
10449       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10450     }
10451
10452   if (decl->die_parent != NULL
10453       && decl->die_parent->die_tag != DW_TAG_compile_unit
10454       && decl->die_parent->die_tag != DW_TAG_type_unit)
10455     {
10456       new_decl = copy_ancestor_tree (unit, decl, NULL);
10457       if (new_decl != NULL)
10458         {
10459           remove_AT (new_decl, DW_AT_signature);
10460           add_AT_specification (die, new_decl);
10461         }
10462     }
10463 }
10464
10465 /* Generate the skeleton ancestor tree for the given NODE, then clone
10466    the DIE and add the clone into the tree.  */
10467
10468 static void
10469 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10470 {
10471   if (node->new_die != NULL)
10472     return;
10473
10474   node->new_die = clone_as_declaration (node->old_die);
10475
10476   if (node->parent != NULL)
10477     {
10478       generate_skeleton_ancestor_tree (node->parent);
10479       add_child_die (node->parent->new_die, node->new_die);
10480     }
10481 }
10482
10483 /* Generate a skeleton tree of DIEs containing any declarations that are
10484    found in the original tree.  We traverse the tree looking for declaration
10485    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10486
10487 static void
10488 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10489 {
10490   skeleton_chain_node node;
10491   dw_die_ref c;
10492   dw_die_ref first;
10493   dw_die_ref prev = NULL;
10494   dw_die_ref next = NULL;
10495
10496   node.parent = parent;
10497
10498   first = c = parent->old_die->die_child;
10499   if (c)
10500     next = c->die_sib;
10501   if (c) do {
10502     if (prev == NULL || prev->die_sib == c)
10503       prev = c;
10504     c = next;
10505     next = (c == first ? NULL : c->die_sib);
10506     node.old_die = c;
10507     node.new_die = NULL;
10508     if (is_declaration_die (c))
10509       {
10510         /* Clone the existing DIE, move the original to the skeleton
10511            tree (which is in the main CU), and put the clone, with
10512            all the original's children, where the original came from.  */
10513         dw_die_ref clone = clone_die (c);
10514         move_all_children (c, clone);
10515
10516         replace_child (c, clone, prev);
10517         generate_skeleton_ancestor_tree (parent);
10518         add_child_die (parent->new_die, c);
10519         node.new_die = c;
10520         c = clone;
10521       }
10522     generate_skeleton_bottom_up (&node);
10523   } while (next != NULL);
10524 }
10525
10526 /* Wrapper function for generate_skeleton_bottom_up.  */
10527
10528 static dw_die_ref
10529 generate_skeleton (dw_die_ref die)
10530 {
10531   skeleton_chain_node node;
10532
10533   node.old_die = die;
10534   node.new_die = NULL;
10535   node.parent = NULL;
10536
10537   /* If this type definition is nested inside another type,
10538      always leave at least a declaration in its place.  */
10539   if (die->die_parent != NULL && is_type_die (die->die_parent))
10540     node.new_die = clone_as_declaration (die);
10541
10542   generate_skeleton_bottom_up (&node);
10543   return node.new_die;
10544 }
10545
10546 /* Remove the DIE from its parent, possibly replacing it with a cloned
10547    declaration.  The original DIE will be moved to a new compile unit
10548    so that existing references to it follow it to the new location.  If
10549    any of the original DIE's descendants is a declaration, we need to
10550    replace the original DIE with a skeleton tree and move the
10551    declarations back into the skeleton tree.  */
10552
10553 static dw_die_ref
10554 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10555 {
10556   dw_die_ref skeleton;
10557
10558   skeleton = generate_skeleton (child);
10559   if (skeleton == NULL)
10560     remove_child_with_prev (child, prev);
10561   else
10562     {
10563       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10564       replace_child (child, skeleton, prev);
10565     }
10566
10567   return skeleton;
10568 }
10569
10570 /* Traverse the DIE and set up additional .debug_types sections for each
10571    type worthy of being placed in a COMDAT section.  */
10572
10573 static void
10574 break_out_comdat_types (dw_die_ref die)
10575 {
10576   dw_die_ref c;
10577   dw_die_ref first;
10578   dw_die_ref prev = NULL;
10579   dw_die_ref next = NULL;
10580   dw_die_ref unit = NULL;
10581
10582   first = c = die->die_child;
10583   if (c)
10584     next = c->die_sib;
10585   if (c) do {
10586     if (prev == NULL || prev->die_sib == c)
10587       prev = c;
10588     c = next;
10589     next = (c == first ? NULL : c->die_sib);
10590     if (should_move_die_to_comdat (c))
10591       {
10592         dw_die_ref replacement;
10593         comdat_type_node_ref type_node;
10594
10595         /* Create a new type unit DIE as the root for the new tree, and
10596            add it to the list of comdat types.  */
10597         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10598         add_AT_unsigned (unit, DW_AT_language,
10599                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10600         type_node = ggc_alloc_cleared_comdat_type_node ();
10601         type_node->root_die = unit;
10602         type_node->next = comdat_type_list;
10603         comdat_type_list = type_node;
10604
10605         /* Generate the type signature.  */
10606         generate_type_signature (c, type_node);
10607
10608         /* Copy the declaration context, attributes, and children of the
10609            declaration into the new compile unit DIE.  */
10610         copy_declaration_context (unit, c);
10611
10612         /* Remove this DIE from the main CU.  */
10613         replacement = remove_child_or_replace_with_skeleton (c, prev);
10614
10615         /* Break out nested types into their own type units.  */
10616         break_out_comdat_types (c);
10617
10618         /* Add the DIE to the new compunit.  */
10619         add_child_die (unit, c);
10620
10621         if (replacement != NULL)
10622           c = replacement;
10623       }
10624     else if (c->die_tag == DW_TAG_namespace
10625              || c->die_tag == DW_TAG_class_type
10626              || c->die_tag == DW_TAG_structure_type
10627              || c->die_tag == DW_TAG_union_type)
10628       {
10629         /* Look for nested types that can be broken out.  */
10630         break_out_comdat_types (c);
10631       }
10632   } while (next != NULL);
10633 }
10634
10635 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10636
10637 struct decl_table_entry
10638 {
10639   dw_die_ref orig;
10640   dw_die_ref copy;
10641 };
10642
10643 /* Routines to manipulate hash table of copied declarations.  */
10644
10645 static hashval_t
10646 htab_decl_hash (const void *of)
10647 {
10648   const struct decl_table_entry *const entry =
10649     (const struct decl_table_entry *) of;
10650
10651   return htab_hash_pointer (entry->orig);
10652 }
10653
10654 static int
10655 htab_decl_eq (const void *of1, const void *of2)
10656 {
10657   const struct decl_table_entry *const entry1 =
10658     (const struct decl_table_entry *) of1;
10659   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10660
10661   return entry1->orig == entry2;
10662 }
10663
10664 static void
10665 htab_decl_del (void *what)
10666 {
10667   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10668
10669   free (entry);
10670 }
10671
10672 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10673    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10674    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10675    to check if the ancestor has already been copied into UNIT.  */
10676
10677 static dw_die_ref
10678 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10679 {
10680   dw_die_ref parent = die->die_parent;
10681   dw_die_ref new_parent = unit;
10682   dw_die_ref copy;
10683   void **slot = NULL;
10684   struct decl_table_entry *entry = NULL;
10685
10686   if (decl_table)
10687     {
10688       /* Check if the entry has already been copied to UNIT.  */
10689       slot = htab_find_slot_with_hash (decl_table, die,
10690                                        htab_hash_pointer (die), INSERT);
10691       if (*slot != HTAB_EMPTY_ENTRY)
10692         {
10693           entry = (struct decl_table_entry *) *slot;
10694           return entry->copy;
10695         }
10696
10697       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10698       entry = XCNEW (struct decl_table_entry);
10699       entry->orig = die;
10700       entry->copy = NULL;
10701       *slot = entry;
10702     }
10703
10704   if (parent != NULL)
10705     {
10706       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10707       if (spec != NULL)
10708         parent = spec;
10709       if (parent->die_tag != DW_TAG_compile_unit
10710           && parent->die_tag != DW_TAG_type_unit)
10711         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10712     }
10713
10714   copy = clone_as_declaration (die);
10715   add_child_die (new_parent, copy);
10716
10717   if (decl_table != NULL)
10718     {
10719       /* Record the pointer to the copy.  */
10720       entry->copy = copy;
10721     }
10722
10723   return copy;
10724 }
10725
10726 /* Walk the DIE and its children, looking for references to incomplete
10727    or trivial types that are unmarked (i.e., that are not in the current
10728    type_unit).  */
10729
10730 static void
10731 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10732 {
10733   dw_die_ref c;
10734   dw_attr_ref a;
10735   unsigned ix;
10736
10737   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10738     {
10739       if (AT_class (a) == dw_val_class_die_ref)
10740         {
10741           dw_die_ref targ = AT_ref (a);
10742           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10743           void **slot;
10744           struct decl_table_entry *entry;
10745
10746           if (targ->die_mark != 0 || type_node != NULL)
10747             continue;
10748
10749           slot = htab_find_slot_with_hash (decl_table, targ,
10750                                            htab_hash_pointer (targ), INSERT);
10751
10752           if (*slot != HTAB_EMPTY_ENTRY)
10753             {
10754               /* TARG has already been copied, so we just need to
10755                  modify the reference to point to the copy.  */
10756               entry = (struct decl_table_entry *) *slot;
10757               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10758             }
10759           else
10760             {
10761               dw_die_ref parent = unit;
10762               dw_die_ref copy = clone_tree (targ);
10763
10764               /* Make sure the cloned tree is marked as part of the
10765                  type unit.  */
10766               mark_dies (copy);
10767
10768               /* Record in DECL_TABLE that TARG has been copied.
10769                  Need to do this now, before the recursive call,
10770                  because DECL_TABLE may be expanded and SLOT
10771                  would no longer be a valid pointer.  */
10772               entry = XCNEW (struct decl_table_entry);
10773               entry->orig = targ;
10774               entry->copy = copy;
10775               *slot = entry;
10776
10777               /* If TARG has surrounding context, copy its ancestor tree
10778                  into the new type unit.  */
10779               if (targ->die_parent != NULL
10780                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10781                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10782                 parent = copy_ancestor_tree (unit, targ->die_parent,
10783                                              decl_table);
10784
10785               add_child_die (parent, copy);
10786               a->dw_attr_val.v.val_die_ref.die = copy;
10787
10788               /* Make sure the newly-copied DIE is walked.  If it was
10789                  installed in a previously-added context, it won't
10790                  get visited otherwise.  */
10791               if (parent != unit)
10792                 {
10793                   /* Find the highest point of the newly-added tree,
10794                      mark each node along the way, and walk from there.  */
10795                   parent->die_mark = 1;
10796                   while (parent->die_parent
10797                          && parent->die_parent->die_mark == 0)
10798                     {
10799                       parent = parent->die_parent;
10800                       parent->die_mark = 1;
10801                     }
10802                   copy_decls_walk (unit, parent, decl_table);
10803                 }
10804             }
10805         }
10806     }
10807
10808   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10809 }
10810
10811 /* Copy declarations for "unworthy" types into the new comdat section.
10812    Incomplete types, modified types, and certain other types aren't broken
10813    out into comdat sections of their own, so they don't have a signature,
10814    and we need to copy the declaration into the same section so that we
10815    don't have an external reference.  */
10816
10817 static void
10818 copy_decls_for_unworthy_types (dw_die_ref unit)
10819 {
10820   htab_t decl_table;
10821
10822   mark_dies (unit);
10823   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10824   copy_decls_walk (unit, unit, decl_table);
10825   htab_delete (decl_table);
10826   unmark_dies (unit);
10827 }
10828
10829 /* Traverse the DIE and add a sibling attribute if it may have the
10830    effect of speeding up access to siblings.  To save some space,
10831    avoid generating sibling attributes for DIE's without children.  */
10832
10833 static void
10834 add_sibling_attributes (dw_die_ref die)
10835 {
10836   dw_die_ref c;
10837
10838   if (! die->die_child)
10839     return;
10840
10841   if (die->die_parent && die != die->die_parent->die_child)
10842     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10843
10844   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10845 }
10846
10847 /* Output all location lists for the DIE and its children.  */
10848
10849 static void
10850 output_location_lists (dw_die_ref die)
10851 {
10852   dw_die_ref c;
10853   dw_attr_ref a;
10854   unsigned ix;
10855
10856   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10857     if (AT_class (a) == dw_val_class_loc_list)
10858       output_loc_list (AT_loc_list (a));
10859
10860   FOR_EACH_CHILD (die, c, output_location_lists (c));
10861 }
10862
10863 /* The format of each DIE (and its attribute value pairs) is encoded in an
10864    abbreviation table.  This routine builds the abbreviation table and assigns
10865    a unique abbreviation id for each abbreviation entry.  The children of each
10866    die are visited recursively.  */
10867
10868 static void
10869 build_abbrev_table (dw_die_ref die)
10870 {
10871   unsigned long abbrev_id;
10872   unsigned int n_alloc;
10873   dw_die_ref c;
10874   dw_attr_ref a;
10875   unsigned ix;
10876
10877   /* Scan the DIE references, and mark as external any that refer to
10878      DIEs from other CUs (i.e. those which are not marked).  */
10879   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10880     if (AT_class (a) == dw_val_class_die_ref
10881         && AT_ref (a)->die_mark == 0)
10882       {
10883         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10884         set_AT_ref_external (a, 1);
10885       }
10886
10887   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10888     {
10889       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10890       dw_attr_ref die_a, abbrev_a;
10891       unsigned ix;
10892       bool ok = true;
10893
10894       if (abbrev->die_tag != die->die_tag)
10895         continue;
10896       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10897         continue;
10898
10899       if (VEC_length (dw_attr_node, abbrev->die_attr)
10900           != VEC_length (dw_attr_node, die->die_attr))
10901         continue;
10902
10903       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10904         {
10905           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10906           if ((abbrev_a->dw_attr != die_a->dw_attr)
10907               || (value_format (abbrev_a) != value_format (die_a)))
10908             {
10909               ok = false;
10910               break;
10911             }
10912         }
10913       if (ok)
10914         break;
10915     }
10916
10917   if (abbrev_id >= abbrev_die_table_in_use)
10918     {
10919       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10920         {
10921           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10922           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10923                                             n_alloc);
10924
10925           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10926                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10927           abbrev_die_table_allocated = n_alloc;
10928         }
10929
10930       ++abbrev_die_table_in_use;
10931       abbrev_die_table[abbrev_id] = die;
10932     }
10933
10934   die->die_abbrev = abbrev_id;
10935   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10936 }
10937 \f
10938 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10939
10940 static int
10941 constant_size (unsigned HOST_WIDE_INT value)
10942 {
10943   int log;
10944
10945   if (value == 0)
10946     log = 0;
10947   else
10948     log = floor_log2 (value);
10949
10950   log = log / 8;
10951   log = 1 << (floor_log2 (log) + 1);
10952
10953   return log;
10954 }
10955
10956 /* Return the size of a DIE as it is represented in the
10957    .debug_info section.  */
10958
10959 static unsigned long
10960 size_of_die (dw_die_ref die)
10961 {
10962   unsigned long size = 0;
10963   dw_attr_ref a;
10964   unsigned ix;
10965
10966   size += size_of_uleb128 (die->die_abbrev);
10967   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10968     {
10969       switch (AT_class (a))
10970         {
10971         case dw_val_class_addr:
10972           size += DWARF2_ADDR_SIZE;
10973           break;
10974         case dw_val_class_offset:
10975           size += DWARF_OFFSET_SIZE;
10976           break;
10977         case dw_val_class_loc:
10978           {
10979             unsigned long lsize = size_of_locs (AT_loc (a));
10980
10981             /* Block length.  */
10982             if (dwarf_version >= 4)
10983               size += size_of_uleb128 (lsize);
10984             else
10985               size += constant_size (lsize);
10986             size += lsize;
10987           }
10988           break;
10989         case dw_val_class_loc_list:
10990           size += DWARF_OFFSET_SIZE;
10991           break;
10992         case dw_val_class_range_list:
10993           size += DWARF_OFFSET_SIZE;
10994           break;
10995         case dw_val_class_const:
10996           size += size_of_sleb128 (AT_int (a));
10997           break;
10998         case dw_val_class_unsigned_const:
10999           size += constant_size (AT_unsigned (a));
11000           break;
11001         case dw_val_class_const_double:
11002           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
11003           if (HOST_BITS_PER_WIDE_INT >= 64)
11004             size++; /* block */
11005           break;
11006         case dw_val_class_vec:
11007           size += constant_size (a->dw_attr_val.v.val_vec.length
11008                                  * a->dw_attr_val.v.val_vec.elt_size)
11009                   + a->dw_attr_val.v.val_vec.length
11010                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
11011           break;
11012         case dw_val_class_flag:
11013           if (dwarf_version >= 4)
11014             /* Currently all add_AT_flag calls pass in 1 as last argument,
11015                so DW_FORM_flag_present can be used.  If that ever changes,
11016                we'll need to use DW_FORM_flag and have some optimization
11017                in build_abbrev_table that will change those to
11018                DW_FORM_flag_present if it is set to 1 in all DIEs using
11019                the same abbrev entry.  */
11020             gcc_assert (a->dw_attr_val.v.val_flag == 1);
11021           else
11022             size += 1;
11023           break;
11024         case dw_val_class_die_ref:
11025           if (AT_ref_external (a))
11026             {
11027               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
11028                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
11029                  is sized by target address length, whereas in DWARF3
11030                  it's always sized as an offset.  */
11031               if (use_debug_types)
11032                 size += DWARF_TYPE_SIGNATURE_SIZE;
11033               else if (dwarf_version == 2)
11034                 size += DWARF2_ADDR_SIZE;
11035               else
11036                 size += DWARF_OFFSET_SIZE;
11037             }
11038           else
11039             size += DWARF_OFFSET_SIZE;
11040           break;
11041         case dw_val_class_fde_ref:
11042           size += DWARF_OFFSET_SIZE;
11043           break;
11044         case dw_val_class_lbl_id:
11045           size += DWARF2_ADDR_SIZE;
11046           break;
11047         case dw_val_class_lineptr:
11048         case dw_val_class_macptr:
11049           size += DWARF_OFFSET_SIZE;
11050           break;
11051         case dw_val_class_str:
11052           if (AT_string_form (a) == DW_FORM_strp)
11053             size += DWARF_OFFSET_SIZE;
11054           else
11055             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
11056           break;
11057         case dw_val_class_file:
11058           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
11059           break;
11060         case dw_val_class_data8:
11061           size += 8;
11062           break;
11063         case dw_val_class_vms_delta:
11064           size += DWARF_OFFSET_SIZE;
11065           break;
11066         default:
11067           gcc_unreachable ();
11068         }
11069     }
11070
11071   return size;
11072 }
11073
11074 /* Size the debugging information associated with a given DIE.  Visits the
11075    DIE's children recursively.  Updates the global variable next_die_offset, on
11076    each time through.  Uses the current value of next_die_offset to update the
11077    die_offset field in each DIE.  */
11078
11079 static void
11080 calc_die_sizes (dw_die_ref die)
11081 {
11082   dw_die_ref c;
11083
11084   gcc_assert (die->die_offset == 0
11085               || (unsigned long int) die->die_offset == next_die_offset);
11086   die->die_offset = next_die_offset;
11087   next_die_offset += size_of_die (die);
11088
11089   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
11090
11091   if (die->die_child != NULL)
11092     /* Count the null byte used to terminate sibling lists.  */
11093     next_die_offset += 1;
11094 }
11095
11096 /* Size just the base type children at the start of the CU.
11097    This is needed because build_abbrev needs to size locs
11098    and sizing of type based stack ops needs to know die_offset
11099    values for the base types.  */
11100
11101 static void
11102 calc_base_type_die_sizes (void)
11103 {
11104   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11105   unsigned int i;
11106   dw_die_ref base_type;
11107 #if ENABLE_ASSERT_CHECKING
11108   dw_die_ref prev = comp_unit_die ()->die_child;
11109 #endif
11110
11111   die_offset += size_of_die (comp_unit_die ());
11112   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
11113     {
11114 #if ENABLE_ASSERT_CHECKING
11115       gcc_assert (base_type->die_offset == 0
11116                   && prev->die_sib == base_type
11117                   && base_type->die_child == NULL
11118                   && base_type->die_abbrev);
11119       prev = base_type;
11120 #endif
11121       base_type->die_offset = die_offset;
11122       die_offset += size_of_die (base_type);
11123     }
11124 }
11125
11126 /* Set the marks for a die and its children.  We do this so
11127    that we know whether or not a reference needs to use FORM_ref_addr; only
11128    DIEs in the same CU will be marked.  We used to clear out the offset
11129    and use that as the flag, but ran into ordering problems.  */
11130
11131 static void
11132 mark_dies (dw_die_ref die)
11133 {
11134   dw_die_ref c;
11135
11136   gcc_assert (!die->die_mark);
11137
11138   die->die_mark = 1;
11139   FOR_EACH_CHILD (die, c, mark_dies (c));
11140 }
11141
11142 /* Clear the marks for a die and its children.  */
11143
11144 static void
11145 unmark_dies (dw_die_ref die)
11146 {
11147   dw_die_ref c;
11148
11149   if (! use_debug_types)
11150     gcc_assert (die->die_mark);
11151
11152   die->die_mark = 0;
11153   FOR_EACH_CHILD (die, c, unmark_dies (c));
11154 }
11155
11156 /* Clear the marks for a die, its children and referred dies.  */
11157
11158 static void
11159 unmark_all_dies (dw_die_ref die)
11160 {
11161   dw_die_ref c;
11162   dw_attr_ref a;
11163   unsigned ix;
11164
11165   if (!die->die_mark)
11166     return;
11167   die->die_mark = 0;
11168
11169   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
11170
11171   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11172     if (AT_class (a) == dw_val_class_die_ref)
11173       unmark_all_dies (AT_ref (a));
11174 }
11175
11176 /* Return the size of the .debug_pubnames or .debug_pubtypes table
11177    generated for the compilation unit.  */
11178
11179 static unsigned long
11180 size_of_pubnames (VEC (pubname_entry, gc) * names)
11181 {
11182   unsigned long size;
11183   unsigned i;
11184   pubname_ref p;
11185
11186   size = DWARF_PUBNAMES_HEADER_SIZE;
11187   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
11188     if (names != pubtype_table
11189         || p->die->die_offset != 0
11190         || !flag_eliminate_unused_debug_types)
11191       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
11192
11193   size += DWARF_OFFSET_SIZE;
11194   return size;
11195 }
11196
11197 /* Return the size of the information in the .debug_aranges section.  */
11198
11199 static unsigned long
11200 size_of_aranges (void)
11201 {
11202   unsigned long size;
11203
11204   size = DWARF_ARANGES_HEADER_SIZE;
11205
11206   /* Count the address/length pair for this compilation unit.  */
11207   if (text_section_used)
11208     size += 2 * DWARF2_ADDR_SIZE;
11209   if (cold_text_section_used)
11210     size += 2 * DWARF2_ADDR_SIZE;
11211   if (have_multiple_function_sections)
11212     {
11213       unsigned fde_idx = 0;
11214
11215       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11216         {
11217           dw_fde_ref fde = &fde_table[fde_idx];
11218
11219           if (!fde->in_std_section)
11220             size += 2 * DWARF2_ADDR_SIZE;
11221           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11222             size += 2 * DWARF2_ADDR_SIZE;
11223         }
11224     }
11225
11226   /* Count the two zero words used to terminated the address range table.  */
11227   size += 2 * DWARF2_ADDR_SIZE;
11228   return size;
11229 }
11230 \f
11231 /* Select the encoding of an attribute value.  */
11232
11233 static enum dwarf_form
11234 value_format (dw_attr_ref a)
11235 {
11236   switch (a->dw_attr_val.val_class)
11237     {
11238     case dw_val_class_addr:
11239       /* Only very few attributes allow DW_FORM_addr.  */
11240       switch (a->dw_attr)
11241         {
11242         case DW_AT_low_pc:
11243         case DW_AT_high_pc:
11244         case DW_AT_entry_pc:
11245         case DW_AT_trampoline:
11246           return DW_FORM_addr;
11247         default:
11248           break;
11249         }
11250       switch (DWARF2_ADDR_SIZE)
11251         {
11252         case 1:
11253           return DW_FORM_data1;
11254         case 2:
11255           return DW_FORM_data2;
11256         case 4:
11257           return DW_FORM_data4;
11258         case 8:
11259           return DW_FORM_data8;
11260         default:
11261           gcc_unreachable ();
11262         }
11263     case dw_val_class_range_list:
11264     case dw_val_class_loc_list:
11265       if (dwarf_version >= 4)
11266         return DW_FORM_sec_offset;
11267       /* FALLTHRU */
11268     case dw_val_class_vms_delta:
11269     case dw_val_class_offset:
11270       switch (DWARF_OFFSET_SIZE)
11271         {
11272         case 4:
11273           return DW_FORM_data4;
11274         case 8:
11275           return DW_FORM_data8;
11276         default:
11277           gcc_unreachable ();
11278         }
11279     case dw_val_class_loc:
11280       if (dwarf_version >= 4)
11281         return DW_FORM_exprloc;
11282       switch (constant_size (size_of_locs (AT_loc (a))))
11283         {
11284         case 1:
11285           return DW_FORM_block1;
11286         case 2:
11287           return DW_FORM_block2;
11288         default:
11289           gcc_unreachable ();
11290         }
11291     case dw_val_class_const:
11292       return DW_FORM_sdata;
11293     case dw_val_class_unsigned_const:
11294       switch (constant_size (AT_unsigned (a)))
11295         {
11296         case 1:
11297           return DW_FORM_data1;
11298         case 2:
11299           return DW_FORM_data2;
11300         case 4:
11301           return DW_FORM_data4;
11302         case 8:
11303           return DW_FORM_data8;
11304         default:
11305           gcc_unreachable ();
11306         }
11307     case dw_val_class_const_double:
11308       switch (HOST_BITS_PER_WIDE_INT)
11309         {
11310         case 8:
11311           return DW_FORM_data2;
11312         case 16:
11313           return DW_FORM_data4;
11314         case 32:
11315           return DW_FORM_data8;
11316         case 64:
11317         default:
11318           return DW_FORM_block1;
11319         }
11320     case dw_val_class_vec:
11321       switch (constant_size (a->dw_attr_val.v.val_vec.length
11322                              * a->dw_attr_val.v.val_vec.elt_size))
11323         {
11324         case 1:
11325           return DW_FORM_block1;
11326         case 2:
11327           return DW_FORM_block2;
11328         case 4:
11329           return DW_FORM_block4;
11330         default:
11331           gcc_unreachable ();
11332         }
11333     case dw_val_class_flag:
11334       if (dwarf_version >= 4)
11335         {
11336           /* Currently all add_AT_flag calls pass in 1 as last argument,
11337              so DW_FORM_flag_present can be used.  If that ever changes,
11338              we'll need to use DW_FORM_flag and have some optimization
11339              in build_abbrev_table that will change those to
11340              DW_FORM_flag_present if it is set to 1 in all DIEs using
11341              the same abbrev entry.  */
11342           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11343           return DW_FORM_flag_present;
11344         }
11345       return DW_FORM_flag;
11346     case dw_val_class_die_ref:
11347       if (AT_ref_external (a))
11348         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11349       else
11350         return DW_FORM_ref;
11351     case dw_val_class_fde_ref:
11352       return DW_FORM_data;
11353     case dw_val_class_lbl_id:
11354       return DW_FORM_addr;
11355     case dw_val_class_lineptr:
11356     case dw_val_class_macptr:
11357       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11358     case dw_val_class_str:
11359       return AT_string_form (a);
11360     case dw_val_class_file:
11361       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11362         {
11363         case 1:
11364           return DW_FORM_data1;
11365         case 2:
11366           return DW_FORM_data2;
11367         case 4:
11368           return DW_FORM_data4;
11369         default:
11370           gcc_unreachable ();
11371         }
11372
11373     case dw_val_class_data8:
11374       return DW_FORM_data8;
11375
11376     default:
11377       gcc_unreachable ();
11378     }
11379 }
11380
11381 /* Output the encoding of an attribute value.  */
11382
11383 static void
11384 output_value_format (dw_attr_ref a)
11385 {
11386   enum dwarf_form form = value_format (a);
11387
11388   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11389 }
11390
11391 /* Output the .debug_abbrev section which defines the DIE abbreviation
11392    table.  */
11393
11394 static void
11395 output_abbrev_section (void)
11396 {
11397   unsigned long abbrev_id;
11398
11399   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11400     {
11401       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11402       unsigned ix;
11403       dw_attr_ref a_attr;
11404
11405       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11406       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11407                                    dwarf_tag_name (abbrev->die_tag));
11408
11409       if (abbrev->die_child != NULL)
11410         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11411       else
11412         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11413
11414       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11415            ix++)
11416         {
11417           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11418                                        dwarf_attr_name (a_attr->dw_attr));
11419           output_value_format (a_attr);
11420         }
11421
11422       dw2_asm_output_data (1, 0, NULL);
11423       dw2_asm_output_data (1, 0, NULL);
11424     }
11425
11426   /* Terminate the table.  */
11427   dw2_asm_output_data (1, 0, NULL);
11428 }
11429
11430 /* Output a symbol we can use to refer to this DIE from another CU.  */
11431
11432 static inline void
11433 output_die_symbol (dw_die_ref die)
11434 {
11435   char *sym = die->die_id.die_symbol;
11436
11437   if (sym == 0)
11438     return;
11439
11440   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11441     /* We make these global, not weak; if the target doesn't support
11442        .linkonce, it doesn't support combining the sections, so debugging
11443        will break.  */
11444     targetm.asm_out.globalize_label (asm_out_file, sym);
11445
11446   ASM_OUTPUT_LABEL (asm_out_file, sym);
11447 }
11448
11449 /* Return a new location list, given the begin and end range, and the
11450    expression.  */
11451
11452 static inline dw_loc_list_ref
11453 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11454               const char *section)
11455 {
11456   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11457
11458   retlist->begin = begin;
11459   retlist->end = end;
11460   retlist->expr = expr;
11461   retlist->section = section;
11462
11463   return retlist;
11464 }
11465
11466 /* Generate a new internal symbol for this location list node, if it
11467    hasn't got one yet.  */
11468
11469 static inline void
11470 gen_llsym (dw_loc_list_ref list)
11471 {
11472   gcc_assert (!list->ll_symbol);
11473   list->ll_symbol = gen_internal_sym ("LLST");
11474 }
11475
11476 /* Output the location list given to us.  */
11477
11478 static void
11479 output_loc_list (dw_loc_list_ref list_head)
11480 {
11481   dw_loc_list_ref curr = list_head;
11482
11483   if (list_head->emitted)
11484     return;
11485   list_head->emitted = true;
11486
11487   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11488
11489   /* Walk the location list, and output each range + expression.  */
11490   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11491     {
11492       unsigned long size;
11493       /* Don't output an entry that starts and ends at the same address.  */
11494       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
11495         continue;
11496       if (!have_multiple_function_sections)
11497         {
11498           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11499                                 "Location list begin address (%s)",
11500                                 list_head->ll_symbol);
11501           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11502                                 "Location list end address (%s)",
11503                                 list_head->ll_symbol);
11504         }
11505       else
11506         {
11507           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11508                                "Location list begin address (%s)",
11509                                list_head->ll_symbol);
11510           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11511                                "Location list end address (%s)",
11512                                list_head->ll_symbol);
11513         }
11514       size = size_of_locs (curr->expr);
11515
11516       /* Output the block length for this list of location operations.  */
11517       gcc_assert (size <= 0xffff);
11518       dw2_asm_output_data (2, size, "%s", "Location expression size");
11519
11520       output_loc_sequence (curr->expr, -1);
11521     }
11522
11523   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11524                        "Location list terminator begin (%s)",
11525                        list_head->ll_symbol);
11526   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11527                        "Location list terminator end (%s)",
11528                        list_head->ll_symbol);
11529 }
11530
11531 /* Output a type signature.  */
11532
11533 static inline void
11534 output_signature (const char *sig, const char *name)
11535 {
11536   int i;
11537
11538   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11539     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11540 }
11541
11542 /* Output the DIE and its attributes.  Called recursively to generate
11543    the definitions of each child DIE.  */
11544
11545 static void
11546 output_die (dw_die_ref die)
11547 {
11548   dw_attr_ref a;
11549   dw_die_ref c;
11550   unsigned long size;
11551   unsigned ix;
11552
11553   /* If someone in another CU might refer to us, set up a symbol for
11554      them to point to.  */
11555   if (! use_debug_types && die->die_id.die_symbol)
11556     output_die_symbol (die);
11557
11558   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11559                                (unsigned long)die->die_offset,
11560                                dwarf_tag_name (die->die_tag));
11561
11562   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11563     {
11564       const char *name = dwarf_attr_name (a->dw_attr);
11565
11566       switch (AT_class (a))
11567         {
11568         case dw_val_class_addr:
11569           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11570           break;
11571
11572         case dw_val_class_offset:
11573           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11574                                "%s", name);
11575           break;
11576
11577         case dw_val_class_range_list:
11578           {
11579             char *p = strchr (ranges_section_label, '\0');
11580
11581             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11582                      a->dw_attr_val.v.val_offset);
11583             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11584                                    debug_ranges_section, "%s", name);
11585             *p = '\0';
11586           }
11587           break;
11588
11589         case dw_val_class_loc:
11590           size = size_of_locs (AT_loc (a));
11591
11592           /* Output the block length for this list of location operations.  */
11593           if (dwarf_version >= 4)
11594             dw2_asm_output_data_uleb128 (size, "%s", name);
11595           else
11596             dw2_asm_output_data (constant_size (size), size, "%s", name);
11597
11598           output_loc_sequence (AT_loc (a), -1);
11599           break;
11600
11601         case dw_val_class_const:
11602           /* ??? It would be slightly more efficient to use a scheme like is
11603              used for unsigned constants below, but gdb 4.x does not sign
11604              extend.  Gdb 5.x does sign extend.  */
11605           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11606           break;
11607
11608         case dw_val_class_unsigned_const:
11609           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11610                                AT_unsigned (a), "%s", name);
11611           break;
11612
11613         case dw_val_class_const_double:
11614           {
11615             unsigned HOST_WIDE_INT first, second;
11616
11617             if (HOST_BITS_PER_WIDE_INT >= 64)
11618               dw2_asm_output_data (1,
11619                                    2 * HOST_BITS_PER_WIDE_INT
11620                                    / HOST_BITS_PER_CHAR,
11621                                    NULL);
11622
11623             if (WORDS_BIG_ENDIAN)
11624               {
11625                 first = a->dw_attr_val.v.val_double.high;
11626                 second = a->dw_attr_val.v.val_double.low;
11627               }
11628             else
11629               {
11630                 first = a->dw_attr_val.v.val_double.low;
11631                 second = a->dw_attr_val.v.val_double.high;
11632               }
11633
11634             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11635                                  first, name);
11636             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11637                                  second, NULL);
11638           }
11639           break;
11640
11641         case dw_val_class_vec:
11642           {
11643             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11644             unsigned int len = a->dw_attr_val.v.val_vec.length;
11645             unsigned int i;
11646             unsigned char *p;
11647
11648             dw2_asm_output_data (constant_size (len * elt_size),
11649                                  len * elt_size, "%s", name);
11650             if (elt_size > sizeof (HOST_WIDE_INT))
11651               {
11652                 elt_size /= 2;
11653                 len *= 2;
11654               }
11655             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11656                  i < len;
11657                  i++, p += elt_size)
11658               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11659                                    "fp or vector constant word %u", i);
11660             break;
11661           }
11662
11663         case dw_val_class_flag:
11664           if (dwarf_version >= 4)
11665             {
11666               /* Currently all add_AT_flag calls pass in 1 as last argument,
11667                  so DW_FORM_flag_present can be used.  If that ever changes,
11668                  we'll need to use DW_FORM_flag and have some optimization
11669                  in build_abbrev_table that will change those to
11670                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11671                  the same abbrev entry.  */
11672               gcc_assert (AT_flag (a) == 1);
11673               if (flag_debug_asm)
11674                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11675                          ASM_COMMENT_START, name);
11676               break;
11677             }
11678           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11679           break;
11680
11681         case dw_val_class_loc_list:
11682           {
11683             char *sym = AT_loc_list (a)->ll_symbol;
11684
11685             gcc_assert (sym);
11686             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11687                                    "%s", name);
11688           }
11689           break;
11690
11691         case dw_val_class_die_ref:
11692           if (AT_ref_external (a))
11693             {
11694               if (use_debug_types)
11695                 {
11696                   comdat_type_node_ref type_node =
11697                     AT_ref (a)->die_id.die_type_node;
11698
11699                   gcc_assert (type_node);
11700                   output_signature (type_node->signature, name);
11701                 }
11702               else
11703                 {
11704                   char *sym = AT_ref (a)->die_id.die_symbol;
11705                   int size;
11706
11707                   gcc_assert (sym);
11708                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11709                      length, whereas in DWARF3 it's always sized as an
11710                      offset.  */
11711                   if (dwarf_version == 2)
11712                     size = DWARF2_ADDR_SIZE;
11713                   else
11714                     size = DWARF_OFFSET_SIZE;
11715                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11716                                          name);
11717                 }
11718             }
11719           else
11720             {
11721               gcc_assert (AT_ref (a)->die_offset);
11722               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11723                                    "%s", name);
11724             }
11725           break;
11726
11727         case dw_val_class_fde_ref:
11728           {
11729             char l1[20];
11730
11731             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11732                                          a->dw_attr_val.v.val_fde_index * 2);
11733             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11734                                    "%s", name);
11735           }
11736           break;
11737
11738         case dw_val_class_vms_delta:
11739           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11740                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11741                                     "%s", name);
11742           break;
11743
11744         case dw_val_class_lbl_id:
11745           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11746           break;
11747
11748         case dw_val_class_lineptr:
11749           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11750                                  debug_line_section, "%s", name);
11751           break;
11752
11753         case dw_val_class_macptr:
11754           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11755                                  debug_macinfo_section, "%s", name);
11756           break;
11757
11758         case dw_val_class_str:
11759           if (AT_string_form (a) == DW_FORM_strp)
11760             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11761                                    a->dw_attr_val.v.val_str->label,
11762                                    debug_str_section,
11763                                    "%s: \"%s\"", name, AT_string (a));
11764           else
11765             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11766           break;
11767
11768         case dw_val_class_file:
11769           {
11770             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11771
11772             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11773                                  a->dw_attr_val.v.val_file->filename);
11774             break;
11775           }
11776
11777         case dw_val_class_data8:
11778           {
11779             int i;
11780
11781             for (i = 0; i < 8; i++)
11782               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11783                                    i == 0 ? "%s" : NULL, name);
11784             break;
11785           }
11786
11787         default:
11788           gcc_unreachable ();
11789         }
11790     }
11791
11792   FOR_EACH_CHILD (die, c, output_die (c));
11793
11794   /* Add null byte to terminate sibling list.  */
11795   if (die->die_child != NULL)
11796     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11797                          (unsigned long) die->die_offset);
11798 }
11799
11800 /* Output the compilation unit that appears at the beginning of the
11801    .debug_info section, and precedes the DIE descriptions.  */
11802
11803 static void
11804 output_compilation_unit_header (void)
11805 {
11806   int ver = dwarf_version;
11807
11808   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11809     dw2_asm_output_data (4, 0xffffffff,
11810       "Initial length escape value indicating 64-bit DWARF extension");
11811   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11812                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11813                        "Length of Compilation Unit Info");
11814   dw2_asm_output_data (2, ver, "DWARF version number");
11815   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11816                          debug_abbrev_section,
11817                          "Offset Into Abbrev. Section");
11818   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11819 }
11820
11821 /* Output the compilation unit DIE and its children.  */
11822
11823 static void
11824 output_comp_unit (dw_die_ref die, int output_if_empty)
11825 {
11826   const char *secname;
11827   char *oldsym, *tmp;
11828
11829   /* Unless we are outputting main CU, we may throw away empty ones.  */
11830   if (!output_if_empty && die->die_child == NULL)
11831     return;
11832
11833   /* Even if there are no children of this DIE, we must output the information
11834      about the compilation unit.  Otherwise, on an empty translation unit, we
11835      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11836      will then complain when examining the file.  First mark all the DIEs in
11837      this CU so we know which get local refs.  */
11838   mark_dies (die);
11839
11840   build_abbrev_table (die);
11841
11842   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11843   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11844   calc_die_sizes (die);
11845
11846   oldsym = die->die_id.die_symbol;
11847   if (oldsym)
11848     {
11849       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11850
11851       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11852       secname = tmp;
11853       die->die_id.die_symbol = NULL;
11854       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11855     }
11856   else
11857     {
11858       switch_to_section (debug_info_section);
11859       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11860       info_section_emitted = true;
11861     }
11862
11863   /* Output debugging information.  */
11864   output_compilation_unit_header ();
11865   output_die (die);
11866
11867   /* Leave the marks on the main CU, so we can check them in
11868      output_pubnames.  */
11869   if (oldsym)
11870     {
11871       unmark_dies (die);
11872       die->die_id.die_symbol = oldsym;
11873     }
11874 }
11875
11876 /* Output a comdat type unit DIE and its children.  */
11877
11878 static void
11879 output_comdat_type_unit (comdat_type_node *node)
11880 {
11881   const char *secname;
11882   char *tmp;
11883   int i;
11884 #if defined (OBJECT_FORMAT_ELF)
11885   tree comdat_key;
11886 #endif
11887
11888   /* First mark all the DIEs in this CU so we know which get local refs.  */
11889   mark_dies (node->root_die);
11890
11891   build_abbrev_table (node->root_die);
11892
11893   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11894   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11895   calc_die_sizes (node->root_die);
11896
11897 #if defined (OBJECT_FORMAT_ELF)
11898   secname = ".debug_types";
11899   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11900   sprintf (tmp, "wt.");
11901   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11902     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11903   comdat_key = get_identifier (tmp);
11904   targetm.asm_out.named_section (secname,
11905                                  SECTION_DEBUG | SECTION_LINKONCE,
11906                                  comdat_key);
11907 #else
11908   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11909   sprintf (tmp, ".gnu.linkonce.wt.");
11910   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11911     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11912   secname = tmp;
11913   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11914 #endif
11915
11916   /* Output debugging information.  */
11917   output_compilation_unit_header ();
11918   output_signature (node->signature, "Type Signature");
11919   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11920                        "Offset to Type DIE");
11921   output_die (node->root_die);
11922
11923   unmark_dies (node->root_die);
11924 }
11925
11926 /* Return the DWARF2/3 pubname associated with a decl.  */
11927
11928 static const char *
11929 dwarf2_name (tree decl, int scope)
11930 {
11931   if (DECL_NAMELESS (decl))
11932     return NULL;
11933   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11934 }
11935
11936 /* Add a new entry to .debug_pubnames if appropriate.  */
11937
11938 static void
11939 add_pubname_string (const char *str, dw_die_ref die)
11940 {
11941   if (targetm.want_debug_pub_sections)
11942     {
11943       pubname_entry e;
11944
11945       e.die = die;
11946       e.name = xstrdup (str);
11947       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11948     }
11949 }
11950
11951 static void
11952 add_pubname (tree decl, dw_die_ref die)
11953 {
11954   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11955     {
11956       const char *name = dwarf2_name (decl, 1);
11957       if (name)
11958         add_pubname_string (name, die);
11959     }
11960 }
11961
11962 /* Add a new entry to .debug_pubtypes if appropriate.  */
11963
11964 static void
11965 add_pubtype (tree decl, dw_die_ref die)
11966 {
11967   pubname_entry e;
11968
11969   if (!targetm.want_debug_pub_sections)
11970     return;
11971
11972   e.name = NULL;
11973   if ((TREE_PUBLIC (decl)
11974        || is_cu_die (die->die_parent))
11975       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11976     {
11977       e.die = die;
11978       if (TYPE_P (decl))
11979         {
11980           if (TYPE_NAME (decl))
11981             {
11982               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11983                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11984               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11985                        && DECL_NAME (TYPE_NAME (decl)))
11986                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11987               else
11988                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11989             }
11990         }
11991       else
11992         {
11993           e.name = dwarf2_name (decl, 1);
11994           if (e.name)
11995             e.name = xstrdup (e.name);
11996         }
11997
11998       /* If we don't have a name for the type, there's no point in adding
11999          it to the table.  */
12000       if (e.name && e.name[0] != '\0')
12001         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
12002     }
12003 }
12004
12005 /* Output the public names table used to speed up access to externally
12006    visible names; or the public types table used to find type definitions.  */
12007
12008 static void
12009 output_pubnames (VEC (pubname_entry, gc) * names)
12010 {
12011   unsigned i;
12012   unsigned long pubnames_length = size_of_pubnames (names);
12013   pubname_ref pub;
12014
12015   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12016     dw2_asm_output_data (4, 0xffffffff,
12017       "Initial length escape value indicating 64-bit DWARF extension");
12018   if (names == pubname_table)
12019     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
12020                          "Length of Public Names Info");
12021   else
12022     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
12023                          "Length of Public Type Names Info");
12024   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
12025   dw2_asm_output_data (2, 2, "DWARF Version");
12026   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12027                          debug_info_section,
12028                          "Offset of Compilation Unit Info");
12029   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
12030                        "Compilation Unit Length");
12031
12032   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
12033     {
12034       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
12035       if (names == pubname_table)
12036         gcc_assert (pub->die->die_mark);
12037
12038       if (names != pubtype_table
12039           || pub->die->die_offset != 0
12040           || !flag_eliminate_unused_debug_types)
12041         {
12042           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
12043                                "DIE offset");
12044
12045           dw2_asm_output_nstring (pub->name, -1, "external name");
12046         }
12047     }
12048
12049   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
12050 }
12051
12052 /* Output the information that goes into the .debug_aranges table.
12053    Namely, define the beginning and ending address range of the
12054    text section generated for this compilation unit.  */
12055
12056 static void
12057 output_aranges (unsigned long aranges_length)
12058 {
12059   unsigned i;
12060
12061   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12062     dw2_asm_output_data (4, 0xffffffff,
12063       "Initial length escape value indicating 64-bit DWARF extension");
12064   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
12065                        "Length of Address Ranges Info");
12066   /* Version number for aranges is still 2, even in DWARF3.  */
12067   dw2_asm_output_data (2, 2, "DWARF Version");
12068   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12069                          debug_info_section,
12070                          "Offset of Compilation Unit Info");
12071   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
12072   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
12073
12074   /* We need to align to twice the pointer size here.  */
12075   if (DWARF_ARANGES_PAD_SIZE)
12076     {
12077       /* Pad using a 2 byte words so that padding is correct for any
12078          pointer size.  */
12079       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
12080                            2 * DWARF2_ADDR_SIZE);
12081       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
12082         dw2_asm_output_data (2, 0, NULL);
12083     }
12084
12085   /* It is necessary not to output these entries if the sections were
12086      not used; if the sections were not used, the length will be 0 and
12087      the address may end up as 0 if the section is discarded by ld
12088      --gc-sections, leaving an invalid (0, 0) entry that can be
12089      confused with the terminator.  */
12090   if (text_section_used)
12091     {
12092       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
12093       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
12094                             text_section_label, "Length");
12095     }
12096   if (cold_text_section_used)
12097     {
12098       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
12099                            "Address");
12100       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
12101                             cold_text_section_label, "Length");
12102     }
12103
12104   if (have_multiple_function_sections)
12105     {
12106       unsigned fde_idx = 0;
12107
12108       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
12109         {
12110           dw_fde_ref fde = &fde_table[fde_idx];
12111
12112           if (!fde->in_std_section)
12113             {
12114               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
12115                                    "Address");
12116               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
12117                                     fde->dw_fde_begin, "Length");
12118             }
12119           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
12120             {
12121               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
12122                                    "Address");
12123               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
12124                                     fde->dw_fde_second_begin, "Length");
12125             }
12126         }
12127     }
12128
12129   /* Output the terminator words.  */
12130   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12131   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12132 }
12133
12134 /* Add a new entry to .debug_ranges.  Return the offset at which it
12135    was placed.  */
12136
12137 static unsigned int
12138 add_ranges_num (int num)
12139 {
12140   unsigned int in_use = ranges_table_in_use;
12141
12142   if (in_use == ranges_table_allocated)
12143     {
12144       ranges_table_allocated += RANGES_TABLE_INCREMENT;
12145       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
12146                                     ranges_table_allocated);
12147       memset (ranges_table + ranges_table_in_use, 0,
12148               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
12149     }
12150
12151   ranges_table[in_use].num = num;
12152   ranges_table_in_use = in_use + 1;
12153
12154   return in_use * 2 * DWARF2_ADDR_SIZE;
12155 }
12156
12157 /* Add a new entry to .debug_ranges corresponding to a block, or a
12158    range terminator if BLOCK is NULL.  */
12159
12160 static unsigned int
12161 add_ranges (const_tree block)
12162 {
12163   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
12164 }
12165
12166 /* Add a new entry to .debug_ranges corresponding to a pair of
12167    labels.  */
12168
12169 static void
12170 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
12171                       bool *added)
12172 {
12173   unsigned int in_use = ranges_by_label_in_use;
12174   unsigned int offset;
12175
12176   if (in_use == ranges_by_label_allocated)
12177     {
12178       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
12179       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
12180                                        ranges_by_label,
12181                                        ranges_by_label_allocated);
12182       memset (ranges_by_label + ranges_by_label_in_use, 0,
12183               RANGES_TABLE_INCREMENT
12184               * sizeof (struct dw_ranges_by_label_struct));
12185     }
12186
12187   ranges_by_label[in_use].begin = begin;
12188   ranges_by_label[in_use].end = end;
12189   ranges_by_label_in_use = in_use + 1;
12190
12191   offset = add_ranges_num (-(int)in_use - 1);
12192   if (!*added)
12193     {
12194       add_AT_range_list (die, DW_AT_ranges, offset);
12195       *added = true;
12196     }
12197 }
12198
12199 static void
12200 output_ranges (void)
12201 {
12202   unsigned i;
12203   static const char *const start_fmt = "Offset %#x";
12204   const char *fmt = start_fmt;
12205
12206   for (i = 0; i < ranges_table_in_use; i++)
12207     {
12208       int block_num = ranges_table[i].num;
12209
12210       if (block_num > 0)
12211         {
12212           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12213           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12214
12215           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12216           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12217
12218           /* If all code is in the text section, then the compilation
12219              unit base address defaults to DW_AT_low_pc, which is the
12220              base of the text section.  */
12221           if (!have_multiple_function_sections)
12222             {
12223               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12224                                     text_section_label,
12225                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12226               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12227                                     text_section_label, NULL);
12228             }
12229
12230           /* Otherwise, the compilation unit base address is zero,
12231              which allows us to use absolute addresses, and not worry
12232              about whether the target supports cross-section
12233              arithmetic.  */
12234           else
12235             {
12236               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12237                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12238               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12239             }
12240
12241           fmt = NULL;
12242         }
12243
12244       /* Negative block_num stands for an index into ranges_by_label.  */
12245       else if (block_num < 0)
12246         {
12247           int lab_idx = - block_num - 1;
12248
12249           if (!have_multiple_function_sections)
12250             {
12251               gcc_unreachable ();
12252 #if 0
12253               /* If we ever use add_ranges_by_labels () for a single
12254                  function section, all we have to do is to take out
12255                  the #if 0 above.  */
12256               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12257                                     ranges_by_label[lab_idx].begin,
12258                                     text_section_label,
12259                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12260               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12261                                     ranges_by_label[lab_idx].end,
12262                                     text_section_label, NULL);
12263 #endif
12264             }
12265           else
12266             {
12267               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12268                                    ranges_by_label[lab_idx].begin,
12269                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12270               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12271                                    ranges_by_label[lab_idx].end,
12272                                    NULL);
12273             }
12274         }
12275       else
12276         {
12277           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12278           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12279           fmt = start_fmt;
12280         }
12281     }
12282 }
12283
12284 /* Data structure containing information about input files.  */
12285 struct file_info
12286 {
12287   const char *path;     /* Complete file name.  */
12288   const char *fname;    /* File name part.  */
12289   int length;           /* Length of entire string.  */
12290   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
12291   int dir_idx;          /* Index in directory table.  */
12292 };
12293
12294 /* Data structure containing information about directories with source
12295    files.  */
12296 struct dir_info
12297 {
12298   const char *path;     /* Path including directory name.  */
12299   int length;           /* Path length.  */
12300   int prefix;           /* Index of directory entry which is a prefix.  */
12301   int count;            /* Number of files in this directory.  */
12302   int dir_idx;          /* Index of directory used as base.  */
12303 };
12304
12305 /* Callback function for file_info comparison.  We sort by looking at
12306    the directories in the path.  */
12307
12308 static int
12309 file_info_cmp (const void *p1, const void *p2)
12310 {
12311   const struct file_info *const s1 = (const struct file_info *) p1;
12312   const struct file_info *const s2 = (const struct file_info *) p2;
12313   const unsigned char *cp1;
12314   const unsigned char *cp2;
12315
12316   /* Take care of file names without directories.  We need to make sure that
12317      we return consistent values to qsort since some will get confused if
12318      we return the same value when identical operands are passed in opposite
12319      orders.  So if neither has a directory, return 0 and otherwise return
12320      1 or -1 depending on which one has the directory.  */
12321   if ((s1->path == s1->fname || s2->path == s2->fname))
12322     return (s2->path == s2->fname) - (s1->path == s1->fname);
12323
12324   cp1 = (const unsigned char *) s1->path;
12325   cp2 = (const unsigned char *) s2->path;
12326
12327   while (1)
12328     {
12329       ++cp1;
12330       ++cp2;
12331       /* Reached the end of the first path?  If so, handle like above.  */
12332       if ((cp1 == (const unsigned char *) s1->fname)
12333           || (cp2 == (const unsigned char *) s2->fname))
12334         return ((cp2 == (const unsigned char *) s2->fname)
12335                 - (cp1 == (const unsigned char *) s1->fname));
12336
12337       /* Character of current path component the same?  */
12338       else if (*cp1 != *cp2)
12339         return *cp1 - *cp2;
12340     }
12341 }
12342
12343 struct file_name_acquire_data
12344 {
12345   struct file_info *files;
12346   int used_files;
12347   int max_files;
12348 };
12349
12350 /* Traversal function for the hash table.  */
12351
12352 static int
12353 file_name_acquire (void ** slot, void *data)
12354 {
12355   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12356   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12357   struct file_info *fi;
12358   const char *f;
12359
12360   gcc_assert (fnad->max_files >= d->emitted_number);
12361
12362   if (! d->emitted_number)
12363     return 1;
12364
12365   gcc_assert (fnad->max_files != fnad->used_files);
12366
12367   fi = fnad->files + fnad->used_files++;
12368
12369   /* Skip all leading "./".  */
12370   f = d->filename;
12371   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12372     f += 2;
12373
12374   /* Create a new array entry.  */
12375   fi->path = f;
12376   fi->length = strlen (f);
12377   fi->file_idx = d;
12378
12379   /* Search for the file name part.  */
12380   f = strrchr (f, DIR_SEPARATOR);
12381 #if defined (DIR_SEPARATOR_2)
12382   {
12383     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12384
12385     if (g != NULL)
12386       {
12387         if (f == NULL || f < g)
12388           f = g;
12389       }
12390   }
12391 #endif
12392
12393   fi->fname = f == NULL ? fi->path : f + 1;
12394   return 1;
12395 }
12396
12397 /* Output the directory table and the file name table.  We try to minimize
12398    the total amount of memory needed.  A heuristic is used to avoid large
12399    slowdowns with many input files.  */
12400
12401 static void
12402 output_file_names (void)
12403 {
12404   struct file_name_acquire_data fnad;
12405   int numfiles;
12406   struct file_info *files;
12407   struct dir_info *dirs;
12408   int *saved;
12409   int *savehere;
12410   int *backmap;
12411   int ndirs;
12412   int idx_offset;
12413   int i;
12414
12415   if (!last_emitted_file)
12416     {
12417       dw2_asm_output_data (1, 0, "End directory table");
12418       dw2_asm_output_data (1, 0, "End file name table");
12419       return;
12420     }
12421
12422   numfiles = last_emitted_file->emitted_number;
12423
12424   /* Allocate the various arrays we need.  */
12425   files = XALLOCAVEC (struct file_info, numfiles);
12426   dirs = XALLOCAVEC (struct dir_info, numfiles);
12427
12428   fnad.files = files;
12429   fnad.used_files = 0;
12430   fnad.max_files = numfiles;
12431   htab_traverse (file_table, file_name_acquire, &fnad);
12432   gcc_assert (fnad.used_files == fnad.max_files);
12433
12434   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12435
12436   /* Find all the different directories used.  */
12437   dirs[0].path = files[0].path;
12438   dirs[0].length = files[0].fname - files[0].path;
12439   dirs[0].prefix = -1;
12440   dirs[0].count = 1;
12441   dirs[0].dir_idx = 0;
12442   files[0].dir_idx = 0;
12443   ndirs = 1;
12444
12445   for (i = 1; i < numfiles; i++)
12446     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12447         && memcmp (dirs[ndirs - 1].path, files[i].path,
12448                    dirs[ndirs - 1].length) == 0)
12449       {
12450         /* Same directory as last entry.  */
12451         files[i].dir_idx = ndirs - 1;
12452         ++dirs[ndirs - 1].count;
12453       }
12454     else
12455       {
12456         int j;
12457
12458         /* This is a new directory.  */
12459         dirs[ndirs].path = files[i].path;
12460         dirs[ndirs].length = files[i].fname - files[i].path;
12461         dirs[ndirs].count = 1;
12462         dirs[ndirs].dir_idx = ndirs;
12463         files[i].dir_idx = ndirs;
12464
12465         /* Search for a prefix.  */
12466         dirs[ndirs].prefix = -1;
12467         for (j = 0; j < ndirs; j++)
12468           if (dirs[j].length < dirs[ndirs].length
12469               && dirs[j].length > 1
12470               && (dirs[ndirs].prefix == -1
12471                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12472               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12473             dirs[ndirs].prefix = j;
12474
12475         ++ndirs;
12476       }
12477
12478   /* Now to the actual work.  We have to find a subset of the directories which
12479      allow expressing the file name using references to the directory table
12480      with the least amount of characters.  We do not do an exhaustive search
12481      where we would have to check out every combination of every single
12482      possible prefix.  Instead we use a heuristic which provides nearly optimal
12483      results in most cases and never is much off.  */
12484   saved = XALLOCAVEC (int, ndirs);
12485   savehere = XALLOCAVEC (int, ndirs);
12486
12487   memset (saved, '\0', ndirs * sizeof (saved[0]));
12488   for (i = 0; i < ndirs; i++)
12489     {
12490       int j;
12491       int total;
12492
12493       /* We can always save some space for the current directory.  But this
12494          does not mean it will be enough to justify adding the directory.  */
12495       savehere[i] = dirs[i].length;
12496       total = (savehere[i] - saved[i]) * dirs[i].count;
12497
12498       for (j = i + 1; j < ndirs; j++)
12499         {
12500           savehere[j] = 0;
12501           if (saved[j] < dirs[i].length)
12502             {
12503               /* Determine whether the dirs[i] path is a prefix of the
12504                  dirs[j] path.  */
12505               int k;
12506
12507               k = dirs[j].prefix;
12508               while (k != -1 && k != (int) i)
12509                 k = dirs[k].prefix;
12510
12511               if (k == (int) i)
12512                 {
12513                   /* Yes it is.  We can possibly save some memory by
12514                      writing the filenames in dirs[j] relative to
12515                      dirs[i].  */
12516                   savehere[j] = dirs[i].length;
12517                   total += (savehere[j] - saved[j]) * dirs[j].count;
12518                 }
12519             }
12520         }
12521
12522       /* Check whether we can save enough to justify adding the dirs[i]
12523          directory.  */
12524       if (total > dirs[i].length + 1)
12525         {
12526           /* It's worthwhile adding.  */
12527           for (j = i; j < ndirs; j++)
12528             if (savehere[j] > 0)
12529               {
12530                 /* Remember how much we saved for this directory so far.  */
12531                 saved[j] = savehere[j];
12532
12533                 /* Remember the prefix directory.  */
12534                 dirs[j].dir_idx = i;
12535               }
12536         }
12537     }
12538
12539   /* Emit the directory name table.  */
12540   idx_offset = dirs[0].length > 0 ? 1 : 0;
12541   for (i = 1 - idx_offset; i < ndirs; i++)
12542     dw2_asm_output_nstring (dirs[i].path,
12543                             dirs[i].length
12544                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12545                             "Directory Entry: %#x", i + idx_offset);
12546
12547   dw2_asm_output_data (1, 0, "End directory table");
12548
12549   /* We have to emit them in the order of emitted_number since that's
12550      used in the debug info generation.  To do this efficiently we
12551      generate a back-mapping of the indices first.  */
12552   backmap = XALLOCAVEC (int, numfiles);
12553   for (i = 0; i < numfiles; i++)
12554     backmap[files[i].file_idx->emitted_number - 1] = i;
12555
12556   /* Now write all the file names.  */
12557   for (i = 0; i < numfiles; i++)
12558     {
12559       int file_idx = backmap[i];
12560       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12561
12562 #ifdef VMS_DEBUGGING_INFO
12563 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12564
12565       /* Setting these fields can lead to debugger miscomparisons,
12566          but VMS Debug requires them to be set correctly.  */
12567
12568       int ver;
12569       long long cdt;
12570       long siz;
12571       int maxfilelen = strlen (files[file_idx].path)
12572                                + dirs[dir_idx].length
12573                                + MAX_VMS_VERSION_LEN + 1;
12574       char *filebuf = XALLOCAVEC (char, maxfilelen);
12575
12576       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12577       snprintf (filebuf, maxfilelen, "%s;%d",
12578                 files[file_idx].path + dirs[dir_idx].length, ver);
12579
12580       dw2_asm_output_nstring
12581         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12582
12583       /* Include directory index.  */
12584       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12585
12586       /* Modification time.  */
12587       dw2_asm_output_data_uleb128
12588         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12589           ? cdt : 0,
12590          NULL);
12591
12592       /* File length in bytes.  */
12593       dw2_asm_output_data_uleb128
12594         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12595           ? siz : 0,
12596          NULL);
12597 #else
12598       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12599                               "File Entry: %#x", (unsigned) i + 1);
12600
12601       /* Include directory index.  */
12602       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12603
12604       /* Modification time.  */
12605       dw2_asm_output_data_uleb128 (0, NULL);
12606
12607       /* File length in bytes.  */
12608       dw2_asm_output_data_uleb128 (0, NULL);
12609 #endif /* VMS_DEBUGGING_INFO */
12610     }
12611
12612   dw2_asm_output_data (1, 0, "End file name table");
12613 }
12614
12615
12616 /* Output one line number table into the .debug_line section.  */
12617
12618 static void
12619 output_one_line_info_table (dw_line_info_table *table)
12620 {
12621   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12622   unsigned int current_line = 1;
12623   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12624   dw_line_info_entry *ent;
12625   size_t i;
12626
12627   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
12628     {
12629       switch (ent->opcode)
12630         {
12631         case LI_set_address:
12632           /* ??? Unfortunately, we have little choice here currently, and
12633              must always use the most general form.  GCC does not know the
12634              address delta itself, so we can't use DW_LNS_advance_pc.  Many
12635              ports do have length attributes which will give an upper bound
12636              on the address range.  We could perhaps use length attributes
12637              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12638           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12639
12640           /* This can handle any delta.  This takes
12641              4+DWARF2_ADDR_SIZE bytes.  */
12642           dw2_asm_output_data (1, 0, "set address %s", line_label);
12643           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12644           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12645           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12646           break;
12647
12648         case LI_set_line:
12649           if (ent->val == current_line)
12650             {
12651               /* We still need to start a new row, so output a copy insn.  */
12652               dw2_asm_output_data (1, DW_LNS_copy,
12653                                    "copy line %u", current_line);
12654             }
12655           else
12656             {
12657               int line_offset = ent->val - current_line;
12658               int line_delta = line_offset - DWARF_LINE_BASE;
12659
12660               current_line = ent->val;
12661               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12662                 {
12663                   /* This can handle deltas from -10 to 234, using the current
12664                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12665                      This takes 1 byte.  */
12666                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12667                                        "line %u", current_line);
12668                 }
12669               else
12670                 {
12671                   /* This can handle any delta.  This takes at least 4 bytes,
12672                      depending on the value being encoded.  */
12673                   dw2_asm_output_data (1, DW_LNS_advance_line,
12674                                        "advance to line %u", current_line);
12675                   dw2_asm_output_data_sleb128 (line_offset, NULL);
12676                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
12677                 }
12678             }
12679           break;
12680
12681         case LI_set_file:
12682           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12683           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12684           break;
12685
12686         case LI_set_column:
12687           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12688           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12689           break;
12690
12691         case LI_negate_stmt:
12692           current_is_stmt = !current_is_stmt;
12693           dw2_asm_output_data (1, DW_LNS_negate_stmt,
12694                                "is_stmt %d", current_is_stmt);
12695           break;
12696
12697         case LI_set_prologue_end:
12698           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12699                                "set prologue end");
12700           break;
12701           
12702         case LI_set_epilogue_begin:
12703           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12704                                "set epilogue begin");
12705           break;
12706
12707         case LI_set_discriminator:
12708           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12709           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12710           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12711           dw2_asm_output_data_uleb128 (ent->val, NULL);
12712           break;
12713         }
12714     }
12715
12716   /* Emit debug info for the address of the end of the table.  */
12717   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12718   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12719   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12720   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12721
12722   dw2_asm_output_data (1, 0, "end sequence");
12723   dw2_asm_output_data_uleb128 (1, NULL);
12724   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12725 }
12726
12727 /* Output the source line number correspondence information.  This
12728    information goes into the .debug_line section.  */
12729
12730 static void
12731 output_line_info (void)
12732 {
12733   char l1[20], l2[20], p1[20], p2[20];
12734   int ver = dwarf_version;
12735   bool saw_one = false;
12736   int opc;
12737
12738   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12739   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12740   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12741   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12742
12743   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12744     dw2_asm_output_data (4, 0xffffffff,
12745       "Initial length escape value indicating 64-bit DWARF extension");
12746   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12747                         "Length of Source Line Info");
12748   ASM_OUTPUT_LABEL (asm_out_file, l1);
12749
12750   dw2_asm_output_data (2, ver, "DWARF Version");
12751   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12752   ASM_OUTPUT_LABEL (asm_out_file, p1);
12753
12754   /* Define the architecture-dependent minimum instruction length (in bytes).
12755      In this implementation of DWARF, this field is used for information
12756      purposes only.  Since GCC generates assembly language, we have no
12757      a priori knowledge of how many instruction bytes are generated for each
12758      source line, and therefore can use only the DW_LNE_set_address and
12759      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12760      this as '1', which is "correct enough" for all architectures,
12761      and don't let the target override.  */
12762   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12763
12764   if (ver >= 4)
12765     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12766                          "Maximum Operations Per Instruction");
12767   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12768                        "Default is_stmt_start flag");
12769   dw2_asm_output_data (1, DWARF_LINE_BASE,
12770                        "Line Base Value (Special Opcodes)");
12771   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12772                        "Line Range Value (Special Opcodes)");
12773   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12774                        "Special Opcode Base");
12775
12776   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12777     {
12778       int n_op_args;
12779       switch (opc)
12780         {
12781         case DW_LNS_advance_pc:
12782         case DW_LNS_advance_line:
12783         case DW_LNS_set_file:
12784         case DW_LNS_set_column:
12785         case DW_LNS_fixed_advance_pc:
12786         case DW_LNS_set_isa:
12787           n_op_args = 1;
12788           break;
12789         default:
12790           n_op_args = 0;
12791           break;
12792         }
12793
12794       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12795                            opc, n_op_args);
12796     }
12797
12798   /* Write out the information about the files we use.  */
12799   output_file_names ();
12800   ASM_OUTPUT_LABEL (asm_out_file, p2);
12801
12802   if (separate_line_info)
12803     {
12804       dw_line_info_table *table;
12805       size_t i;
12806
12807       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
12808         if (table->in_use)
12809           {
12810             output_one_line_info_table (table);
12811             saw_one = true;
12812           }
12813     }
12814   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12815     {
12816       output_one_line_info_table (cold_text_section_line_info);
12817       saw_one = true;
12818     }
12819
12820   /* ??? Some Darwin linkers crash on a .debug_line section with no
12821      sequences.  Further, merely a DW_LNE_end_sequence entry is not
12822      sufficient -- the address column must also be initialized.
12823      Make sure to output at least one set_address/end_sequence pair,
12824      choosing .text since that section is always present.  */
12825   if (text_section_line_info->in_use || !saw_one)
12826     output_one_line_info_table (text_section_line_info);
12827
12828   /* Output the marker for the end of the line number info.  */
12829   ASM_OUTPUT_LABEL (asm_out_file, l2);
12830 }
12831 \f
12832 /* Given a pointer to a tree node for some base type, return a pointer to
12833    a DIE that describes the given type.
12834
12835    This routine must only be called for GCC type nodes that correspond to
12836    Dwarf base (fundamental) types.  */
12837
12838 static dw_die_ref
12839 base_type_die (tree type)
12840 {
12841   dw_die_ref base_type_result;
12842   enum dwarf_type encoding;
12843
12844   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12845     return 0;
12846
12847   /* If this is a subtype that should not be emitted as a subrange type,
12848      use the base type.  See subrange_type_for_debug_p.  */
12849   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12850     type = TREE_TYPE (type);
12851
12852   switch (TREE_CODE (type))
12853     {
12854     case INTEGER_TYPE:
12855       if ((dwarf_version >= 4 || !dwarf_strict)
12856           && TYPE_NAME (type)
12857           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12858           && DECL_IS_BUILTIN (TYPE_NAME (type))
12859           && DECL_NAME (TYPE_NAME (type)))
12860         {
12861           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12862           if (strcmp (name, "char16_t") == 0
12863               || strcmp (name, "char32_t") == 0)
12864             {
12865               encoding = DW_ATE_UTF;
12866               break;
12867             }
12868         }
12869       if (TYPE_STRING_FLAG (type))
12870         {
12871           if (TYPE_UNSIGNED (type))
12872             encoding = DW_ATE_unsigned_char;
12873           else
12874             encoding = DW_ATE_signed_char;
12875         }
12876       else if (TYPE_UNSIGNED (type))
12877         encoding = DW_ATE_unsigned;
12878       else
12879         encoding = DW_ATE_signed;
12880       break;
12881
12882     case REAL_TYPE:
12883       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12884         {
12885           if (dwarf_version >= 3 || !dwarf_strict)
12886             encoding = DW_ATE_decimal_float;
12887           else
12888             encoding = DW_ATE_lo_user;
12889         }
12890       else
12891         encoding = DW_ATE_float;
12892       break;
12893
12894     case FIXED_POINT_TYPE:
12895       if (!(dwarf_version >= 3 || !dwarf_strict))
12896         encoding = DW_ATE_lo_user;
12897       else if (TYPE_UNSIGNED (type))
12898         encoding = DW_ATE_unsigned_fixed;
12899       else
12900         encoding = DW_ATE_signed_fixed;
12901       break;
12902
12903       /* Dwarf2 doesn't know anything about complex ints, so use
12904          a user defined type for it.  */
12905     case COMPLEX_TYPE:
12906       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12907         encoding = DW_ATE_complex_float;
12908       else
12909         encoding = DW_ATE_lo_user;
12910       break;
12911
12912     case BOOLEAN_TYPE:
12913       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12914       encoding = DW_ATE_boolean;
12915       break;
12916
12917     default:
12918       /* No other TREE_CODEs are Dwarf fundamental types.  */
12919       gcc_unreachable ();
12920     }
12921
12922   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12923
12924   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12925                    int_size_in_bytes (type));
12926   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12927
12928   return base_type_result;
12929 }
12930
12931 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12932    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12933
12934 static inline int
12935 is_base_type (tree type)
12936 {
12937   switch (TREE_CODE (type))
12938     {
12939     case ERROR_MARK:
12940     case VOID_TYPE:
12941     case INTEGER_TYPE:
12942     case REAL_TYPE:
12943     case FIXED_POINT_TYPE:
12944     case COMPLEX_TYPE:
12945     case BOOLEAN_TYPE:
12946       return 1;
12947
12948     case ARRAY_TYPE:
12949     case RECORD_TYPE:
12950     case UNION_TYPE:
12951     case QUAL_UNION_TYPE:
12952     case ENUMERAL_TYPE:
12953     case FUNCTION_TYPE:
12954     case METHOD_TYPE:
12955     case POINTER_TYPE:
12956     case REFERENCE_TYPE:
12957     case NULLPTR_TYPE:
12958     case OFFSET_TYPE:
12959     case LANG_TYPE:
12960     case VECTOR_TYPE:
12961       return 0;
12962
12963     default:
12964       gcc_unreachable ();
12965     }
12966
12967   return 0;
12968 }
12969
12970 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12971    node, return the size in bits for the type if it is a constant, or else
12972    return the alignment for the type if the type's size is not constant, or
12973    else return BITS_PER_WORD if the type actually turns out to be an
12974    ERROR_MARK node.  */
12975
12976 static inline unsigned HOST_WIDE_INT
12977 simple_type_size_in_bits (const_tree type)
12978 {
12979   if (TREE_CODE (type) == ERROR_MARK)
12980     return BITS_PER_WORD;
12981   else if (TYPE_SIZE (type) == NULL_TREE)
12982     return 0;
12983   else if (host_integerp (TYPE_SIZE (type), 1))
12984     return tree_low_cst (TYPE_SIZE (type), 1);
12985   else
12986     return TYPE_ALIGN (type);
12987 }
12988
12989 /* Similarly, but return a double_int instead of UHWI.  */
12990
12991 static inline double_int
12992 double_int_type_size_in_bits (const_tree type)
12993 {
12994   if (TREE_CODE (type) == ERROR_MARK)
12995     return uhwi_to_double_int (BITS_PER_WORD);
12996   else if (TYPE_SIZE (type) == NULL_TREE)
12997     return double_int_zero;
12998   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12999     return tree_to_double_int (TYPE_SIZE (type));
13000   else
13001     return uhwi_to_double_int (TYPE_ALIGN (type));
13002 }
13003
13004 /*  Given a pointer to a tree node for a subrange type, return a pointer
13005     to a DIE that describes the given type.  */
13006
13007 static dw_die_ref
13008 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
13009 {
13010   dw_die_ref subrange_die;
13011   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
13012
13013   if (context_die == NULL)
13014     context_die = comp_unit_die ();
13015
13016   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
13017
13018   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
13019     {
13020       /* The size of the subrange type and its base type do not match,
13021          so we need to generate a size attribute for the subrange type.  */
13022       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
13023     }
13024
13025   if (low)
13026     add_bound_info (subrange_die, DW_AT_lower_bound, low);
13027   if (high)
13028     add_bound_info (subrange_die, DW_AT_upper_bound, high);
13029
13030   return subrange_die;
13031 }
13032
13033 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
13034    entry that chains various modifiers in front of the given type.  */
13035
13036 static dw_die_ref
13037 modified_type_die (tree type, int is_const_type, int is_volatile_type,
13038                    dw_die_ref context_die)
13039 {
13040   enum tree_code code = TREE_CODE (type);
13041   dw_die_ref mod_type_die;
13042   dw_die_ref sub_die = NULL;
13043   tree item_type = NULL;
13044   tree qualified_type;
13045   tree name, low, high;
13046
13047   if (code == ERROR_MARK)
13048     return NULL;
13049
13050   /* See if we already have the appropriately qualified variant of
13051      this type.  */
13052   qualified_type
13053     = get_qualified_type (type,
13054                           ((is_const_type ? TYPE_QUAL_CONST : 0)
13055                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
13056
13057   if (qualified_type == sizetype
13058       && TYPE_NAME (qualified_type)
13059       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
13060     {
13061       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
13062
13063       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
13064                            && TYPE_PRECISION (t)
13065                            == TYPE_PRECISION (qualified_type)
13066                            && TYPE_UNSIGNED (t)
13067                            == TYPE_UNSIGNED (qualified_type));
13068       qualified_type = t;
13069     }
13070
13071   /* If we do, then we can just use its DIE, if it exists.  */
13072   if (qualified_type)
13073     {
13074       mod_type_die = lookup_type_die (qualified_type);
13075       if (mod_type_die)
13076         return mod_type_die;
13077     }
13078
13079   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
13080
13081   /* Handle C typedef types.  */
13082   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
13083       && !DECL_ARTIFICIAL (name))
13084     {
13085       tree dtype = TREE_TYPE (name);
13086
13087       if (qualified_type == dtype)
13088         {
13089           /* For a named type, use the typedef.  */
13090           gen_type_die (qualified_type, context_die);
13091           return lookup_type_die (qualified_type);
13092         }
13093       else if (is_const_type < TYPE_READONLY (dtype)
13094                || is_volatile_type < TYPE_VOLATILE (dtype)
13095                || (is_const_type <= TYPE_READONLY (dtype)
13096                    && is_volatile_type <= TYPE_VOLATILE (dtype)
13097                    && DECL_ORIGINAL_TYPE (name) != type))
13098         /* cv-unqualified version of named type.  Just use the unnamed
13099            type to which it refers.  */
13100         return modified_type_die (DECL_ORIGINAL_TYPE (name),
13101                                   is_const_type, is_volatile_type,
13102                                   context_die);
13103       /* Else cv-qualified version of named type; fall through.  */
13104     }
13105
13106   if (is_const_type
13107       /* If both is_const_type and is_volatile_type, prefer the path
13108          which leads to a qualified type.  */
13109       && (!is_volatile_type
13110           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
13111           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
13112     {
13113       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
13114       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
13115     }
13116   else if (is_volatile_type)
13117     {
13118       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
13119       sub_die = modified_type_die (type, is_const_type, 0, context_die);
13120     }
13121   else if (code == POINTER_TYPE)
13122     {
13123       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
13124       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13125                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13126       item_type = TREE_TYPE (type);
13127       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13128         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13129                          TYPE_ADDR_SPACE (item_type));
13130     }
13131   else if (code == REFERENCE_TYPE)
13132     {
13133       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13134         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
13135                                 type);
13136       else
13137         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
13138       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13139                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13140       item_type = TREE_TYPE (type);
13141       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13142         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13143                          TYPE_ADDR_SPACE (item_type));
13144     }
13145   else if (code == INTEGER_TYPE
13146            && TREE_TYPE (type) != NULL_TREE
13147            && subrange_type_for_debug_p (type, &low, &high))
13148     {
13149       mod_type_die = subrange_type_die (type, low, high, context_die);
13150       item_type = TREE_TYPE (type);
13151     }
13152   else if (is_base_type (type))
13153     mod_type_die = base_type_die (type);
13154   else
13155     {
13156       gen_type_die (type, context_die);
13157
13158       /* We have to get the type_main_variant here (and pass that to the
13159          `lookup_type_die' routine) because the ..._TYPE node we have
13160          might simply be a *copy* of some original type node (where the
13161          copy was created to help us keep track of typedef names) and
13162          that copy might have a different TYPE_UID from the original
13163          ..._TYPE node.  */
13164       if (TREE_CODE (type) != VECTOR_TYPE)
13165         return lookup_type_die (type_main_variant (type));
13166       else
13167         /* Vectors have the debugging information in the type,
13168            not the main variant.  */
13169         return lookup_type_die (type);
13170     }
13171
13172   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13173      don't output a DW_TAG_typedef, since there isn't one in the
13174      user's program; just attach a DW_AT_name to the type.
13175      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13176      if the base type already has the same name.  */
13177   if (name
13178       && ((TREE_CODE (name) != TYPE_DECL
13179            && (qualified_type == TYPE_MAIN_VARIANT (type)
13180                || (!is_const_type && !is_volatile_type)))
13181           || (TREE_CODE (name) == TYPE_DECL
13182               && TREE_TYPE (name) == qualified_type
13183               && DECL_NAME (name))))
13184     {
13185       if (TREE_CODE (name) == TYPE_DECL)
13186         /* Could just call add_name_and_src_coords_attributes here,
13187            but since this is a builtin type it doesn't have any
13188            useful source coordinates anyway.  */
13189         name = DECL_NAME (name);
13190       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13191       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13192     }
13193   /* This probably indicates a bug.  */
13194   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13195     add_name_attribute (mod_type_die, "__unknown__");
13196
13197   if (qualified_type)
13198     equate_type_number_to_die (qualified_type, mod_type_die);
13199
13200   if (item_type)
13201     /* We must do this after the equate_type_number_to_die call, in case
13202        this is a recursive type.  This ensures that the modified_type_die
13203        recursion will terminate even if the type is recursive.  Recursive
13204        types are possible in Ada.  */
13205     sub_die = modified_type_die (item_type,
13206                                  TYPE_READONLY (item_type),
13207                                  TYPE_VOLATILE (item_type),
13208                                  context_die);
13209
13210   if (sub_die != NULL)
13211     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13212
13213   return mod_type_die;
13214 }
13215
13216 /* Generate DIEs for the generic parameters of T.
13217    T must be either a generic type or a generic function.
13218    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13219
13220 static void
13221 gen_generic_params_dies (tree t)
13222 {
13223   tree parms, args;
13224   int parms_num, i;
13225   dw_die_ref die = NULL;
13226
13227   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13228     return;
13229
13230   if (TYPE_P (t))
13231     die = lookup_type_die (t);
13232   else if (DECL_P (t))
13233     die = lookup_decl_die (t);
13234
13235   gcc_assert (die);
13236
13237   parms = lang_hooks.get_innermost_generic_parms (t);
13238   if (!parms)
13239     /* T has no generic parameter. It means T is neither a generic type
13240        or function. End of story.  */
13241     return;
13242
13243   parms_num = TREE_VEC_LENGTH (parms);
13244   args = lang_hooks.get_innermost_generic_args (t);
13245   for (i = 0; i < parms_num; i++)
13246     {
13247       tree parm, arg, arg_pack_elems;
13248
13249       parm = TREE_VEC_ELT (parms, i);
13250       arg = TREE_VEC_ELT (args, i);
13251       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13252       gcc_assert (parm && TREE_VALUE (parm) && arg);
13253
13254       if (parm && TREE_VALUE (parm) && arg)
13255         {
13256           /* If PARM represents a template parameter pack,
13257              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13258              by DW_TAG_template_*_parameter DIEs for the argument
13259              pack elements of ARG. Note that ARG would then be
13260              an argument pack.  */
13261           if (arg_pack_elems)
13262             template_parameter_pack_die (TREE_VALUE (parm),
13263                                          arg_pack_elems,
13264                                          die);
13265           else
13266             generic_parameter_die (TREE_VALUE (parm), arg,
13267                                    true /* Emit DW_AT_name */, die);
13268         }
13269     }
13270 }
13271
13272 /* Create and return a DIE for PARM which should be
13273    the representation of a generic type parameter.
13274    For instance, in the C++ front end, PARM would be a template parameter.
13275    ARG is the argument to PARM.
13276    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13277    name of the PARM.
13278    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13279    as a child node.  */
13280
13281 static dw_die_ref
13282 generic_parameter_die (tree parm, tree arg,
13283                        bool emit_name_p,
13284                        dw_die_ref parent_die)
13285 {
13286   dw_die_ref tmpl_die = NULL;
13287   const char *name = NULL;
13288
13289   if (!parm || !DECL_NAME (parm) || !arg)
13290     return NULL;
13291
13292   /* We support non-type generic parameters and arguments,
13293      type generic parameters and arguments, as well as
13294      generic generic parameters (a.k.a. template template parameters in C++)
13295      and arguments.  */
13296   if (TREE_CODE (parm) == PARM_DECL)
13297     /* PARM is a nontype generic parameter  */
13298     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13299   else if (TREE_CODE (parm) == TYPE_DECL)
13300     /* PARM is a type generic parameter.  */
13301     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13302   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13303     /* PARM is a generic generic parameter.
13304        Its DIE is a GNU extension. It shall have a
13305        DW_AT_name attribute to represent the name of the template template
13306        parameter, and a DW_AT_GNU_template_name attribute to represent the
13307        name of the template template argument.  */
13308     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13309                         parent_die, parm);
13310   else
13311     gcc_unreachable ();
13312
13313   if (tmpl_die)
13314     {
13315       tree tmpl_type;
13316
13317       /* If PARM is a generic parameter pack, it means we are
13318          emitting debug info for a template argument pack element.
13319          In other terms, ARG is a template argument pack element.
13320          In that case, we don't emit any DW_AT_name attribute for
13321          the die.  */
13322       if (emit_name_p)
13323         {
13324           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13325           gcc_assert (name);
13326           add_AT_string (tmpl_die, DW_AT_name, name);
13327         }
13328
13329       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13330         {
13331           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13332              TMPL_DIE should have a child DW_AT_type attribute that is set
13333              to the type of the argument to PARM, which is ARG.
13334              If PARM is a type generic parameter, TMPL_DIE should have a
13335              child DW_AT_type that is set to ARG.  */
13336           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13337           add_type_attribute (tmpl_die, tmpl_type, 0,
13338                               TREE_THIS_VOLATILE (tmpl_type),
13339                               parent_die);
13340         }
13341       else
13342         {
13343           /* So TMPL_DIE is a DIE representing a
13344              a generic generic template parameter, a.k.a template template
13345              parameter in C++ and arg is a template.  */
13346
13347           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13348              to the name of the argument.  */
13349           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13350           if (name)
13351             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13352         }
13353
13354       if (TREE_CODE (parm) == PARM_DECL)
13355         /* So PARM is a non-type generic parameter.
13356            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13357            attribute of TMPL_DIE which value represents the value
13358            of ARG.
13359            We must be careful here:
13360            The value of ARG might reference some function decls.
13361            We might currently be emitting debug info for a generic
13362            type and types are emitted before function decls, we don't
13363            know if the function decls referenced by ARG will actually be
13364            emitted after cgraph computations.
13365            So must defer the generation of the DW_AT_const_value to
13366            after cgraph is ready.  */
13367         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13368     }
13369
13370   return tmpl_die;
13371 }
13372
13373 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13374    PARM_PACK must be a template parameter pack. The returned DIE
13375    will be child DIE of PARENT_DIE.  */
13376
13377 static dw_die_ref
13378 template_parameter_pack_die (tree parm_pack,
13379                              tree parm_pack_args,
13380                              dw_die_ref parent_die)
13381 {
13382   dw_die_ref die;
13383   int j;
13384
13385   gcc_assert (parent_die && parm_pack);
13386
13387   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13388   add_name_and_src_coords_attributes (die, parm_pack);
13389   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13390     generic_parameter_die (parm_pack,
13391                            TREE_VEC_ELT (parm_pack_args, j),
13392                            false /* Don't emit DW_AT_name */,
13393                            die);
13394   return die;
13395 }
13396
13397 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13398    an enumerated type.  */
13399
13400 static inline int
13401 type_is_enum (const_tree type)
13402 {
13403   return TREE_CODE (type) == ENUMERAL_TYPE;
13404 }
13405
13406 /* Return the DBX register number described by a given RTL node.  */
13407
13408 static unsigned int
13409 dbx_reg_number (const_rtx rtl)
13410 {
13411   unsigned regno = REGNO (rtl);
13412
13413   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13414
13415 #ifdef LEAF_REG_REMAP
13416   if (current_function_uses_only_leaf_regs)
13417     {
13418       int leaf_reg = LEAF_REG_REMAP (regno);
13419       if (leaf_reg != -1)
13420         regno = (unsigned) leaf_reg;
13421     }
13422 #endif
13423
13424   return DBX_REGISTER_NUMBER (regno);
13425 }
13426
13427 /* Optionally add a DW_OP_piece term to a location description expression.
13428    DW_OP_piece is only added if the location description expression already
13429    doesn't end with DW_OP_piece.  */
13430
13431 static void
13432 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13433 {
13434   dw_loc_descr_ref loc;
13435
13436   if (*list_head != NULL)
13437     {
13438       /* Find the end of the chain.  */
13439       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13440         ;
13441
13442       if (loc->dw_loc_opc != DW_OP_piece)
13443         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13444     }
13445 }
13446
13447 /* Return a location descriptor that designates a machine register or
13448    zero if there is none.  */
13449
13450 static dw_loc_descr_ref
13451 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13452 {
13453   rtx regs;
13454
13455   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13456     return 0;
13457
13458   /* We only use "frame base" when we're sure we're talking about the
13459      post-prologue local stack frame.  We do this by *not* running
13460      register elimination until this point, and recognizing the special
13461      argument pointer and soft frame pointer rtx's.
13462      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13463   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13464       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13465     {
13466       dw_loc_descr_ref result = NULL;
13467
13468       if (dwarf_version >= 4 || !dwarf_strict)
13469         {
13470           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13471                                        initialized);
13472           if (result)
13473             add_loc_descr (&result,
13474                            new_loc_descr (DW_OP_stack_value, 0, 0));
13475         }
13476       return result;
13477     }
13478
13479   regs = targetm.dwarf_register_span (rtl);
13480
13481   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13482     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13483   else
13484     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13485 }
13486
13487 /* Return a location descriptor that designates a machine register for
13488    a given hard register number.  */
13489
13490 static dw_loc_descr_ref
13491 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13492 {
13493   dw_loc_descr_ref reg_loc_descr;
13494
13495   if (regno <= 31)
13496     reg_loc_descr
13497       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13498   else
13499     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13500
13501   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13502     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13503
13504   return reg_loc_descr;
13505 }
13506
13507 /* Given an RTL of a register, return a location descriptor that
13508    designates a value that spans more than one register.  */
13509
13510 static dw_loc_descr_ref
13511 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13512                              enum var_init_status initialized)
13513 {
13514   int nregs, size, i;
13515   unsigned reg;
13516   dw_loc_descr_ref loc_result = NULL;
13517
13518   reg = REGNO (rtl);
13519 #ifdef LEAF_REG_REMAP
13520   if (current_function_uses_only_leaf_regs)
13521     {
13522       int leaf_reg = LEAF_REG_REMAP (reg);
13523       if (leaf_reg != -1)
13524         reg = (unsigned) leaf_reg;
13525     }
13526 #endif
13527   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13528   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13529
13530   /* Simple, contiguous registers.  */
13531   if (regs == NULL_RTX)
13532     {
13533       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13534
13535       loc_result = NULL;
13536       while (nregs--)
13537         {
13538           dw_loc_descr_ref t;
13539
13540           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13541                                       VAR_INIT_STATUS_INITIALIZED);
13542           add_loc_descr (&loc_result, t);
13543           add_loc_descr_op_piece (&loc_result, size);
13544           ++reg;
13545         }
13546       return loc_result;
13547     }
13548
13549   /* Now onto stupid register sets in non contiguous locations.  */
13550
13551   gcc_assert (GET_CODE (regs) == PARALLEL);
13552
13553   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13554   loc_result = NULL;
13555
13556   for (i = 0; i < XVECLEN (regs, 0); ++i)
13557     {
13558       dw_loc_descr_ref t;
13559
13560       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13561                                   VAR_INIT_STATUS_INITIALIZED);
13562       add_loc_descr (&loc_result, t);
13563       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13564       add_loc_descr_op_piece (&loc_result, size);
13565     }
13566
13567   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13568     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13569   return loc_result;
13570 }
13571
13572 /* Return a location descriptor that designates a constant.  */
13573
13574 static dw_loc_descr_ref
13575 int_loc_descriptor (HOST_WIDE_INT i)
13576 {
13577   enum dwarf_location_atom op;
13578
13579   /* Pick the smallest representation of a constant, rather than just
13580      defaulting to the LEB encoding.  */
13581   if (i >= 0)
13582     {
13583       if (i <= 31)
13584         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13585       else if (i <= 0xff)
13586         op = DW_OP_const1u;
13587       else if (i <= 0xffff)
13588         op = DW_OP_const2u;
13589       else if (HOST_BITS_PER_WIDE_INT == 32
13590                || i <= 0xffffffff)
13591         op = DW_OP_const4u;
13592       else
13593         op = DW_OP_constu;
13594     }
13595   else
13596     {
13597       if (i >= -0x80)
13598         op = DW_OP_const1s;
13599       else if (i >= -0x8000)
13600         op = DW_OP_const2s;
13601       else if (HOST_BITS_PER_WIDE_INT == 32
13602                || i >= -0x80000000)
13603         op = DW_OP_const4s;
13604       else
13605         op = DW_OP_consts;
13606     }
13607
13608   return new_loc_descr (op, i, 0);
13609 }
13610
13611 /* Return loc description representing "address" of integer value.
13612    This can appear only as toplevel expression.  */
13613
13614 static dw_loc_descr_ref
13615 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13616 {
13617   int litsize;
13618   dw_loc_descr_ref loc_result = NULL;
13619
13620   if (!(dwarf_version >= 4 || !dwarf_strict))
13621     return NULL;
13622
13623   if (i >= 0)
13624     {
13625       if (i <= 31)
13626         litsize = 1;
13627       else if (i <= 0xff)
13628         litsize = 2;
13629       else if (i <= 0xffff)
13630         litsize = 3;
13631       else if (HOST_BITS_PER_WIDE_INT == 32
13632                || i <= 0xffffffff)
13633         litsize = 5;
13634       else
13635         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13636     }
13637   else
13638     {
13639       if (i >= -0x80)
13640         litsize = 2;
13641       else if (i >= -0x8000)
13642         litsize = 3;
13643       else if (HOST_BITS_PER_WIDE_INT == 32
13644                || i >= -0x80000000)
13645         litsize = 5;
13646       else
13647         litsize = 1 + size_of_sleb128 (i);
13648     }
13649   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13650      is more compact.  For DW_OP_stack_value we need:
13651      litsize + 1 (DW_OP_stack_value)
13652      and for DW_OP_implicit_value:
13653      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13654   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13655     {
13656       loc_result = int_loc_descriptor (i);
13657       add_loc_descr (&loc_result,
13658                      new_loc_descr (DW_OP_stack_value, 0, 0));
13659       return loc_result;
13660     }
13661
13662   loc_result = new_loc_descr (DW_OP_implicit_value,
13663                               size, 0);
13664   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13665   loc_result->dw_loc_oprnd2.v.val_int = i;
13666   return loc_result;
13667 }
13668
13669 /* Return a location descriptor that designates a base+offset location.  */
13670
13671 static dw_loc_descr_ref
13672 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13673                  enum var_init_status initialized)
13674 {
13675   unsigned int regno;
13676   dw_loc_descr_ref result;
13677   dw_fde_ref fde = current_fde ();
13678
13679   /* We only use "frame base" when we're sure we're talking about the
13680      post-prologue local stack frame.  We do this by *not* running
13681      register elimination until this point, and recognizing the special
13682      argument pointer and soft frame pointer rtx's.  */
13683   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13684     {
13685       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13686
13687       if (elim != reg)
13688         {
13689           if (GET_CODE (elim) == PLUS)
13690             {
13691               offset += INTVAL (XEXP (elim, 1));
13692               elim = XEXP (elim, 0);
13693             }
13694           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13695                        && (elim == hard_frame_pointer_rtx
13696                            || elim == stack_pointer_rtx))
13697                       || elim == (frame_pointer_needed
13698                                   ? hard_frame_pointer_rtx
13699                                   : stack_pointer_rtx));
13700
13701           /* If drap register is used to align stack, use frame
13702              pointer + offset to access stack variables.  If stack
13703              is aligned without drap, use stack pointer + offset to
13704              access stack variables.  */
13705           if (crtl->stack_realign_tried
13706               && reg == frame_pointer_rtx)
13707             {
13708               int base_reg
13709                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13710                                       ? HARD_FRAME_POINTER_REGNUM
13711                                       : REGNO (elim));
13712               return new_reg_loc_descr (base_reg, offset);
13713             }
13714
13715           gcc_assert (frame_pointer_fb_offset_valid);
13716           offset += frame_pointer_fb_offset;
13717           return new_loc_descr (DW_OP_fbreg, offset, 0);
13718         }
13719     }
13720   else if (!optimize
13721            && fde
13722            && (fde->drap_reg == REGNO (reg)
13723                || fde->vdrap_reg == REGNO (reg)))
13724     {
13725       /* Use cfa+offset to represent the location of arguments passed
13726          on the stack when drap is used to align stack.
13727          Only do this when not optimizing, for optimized code var-tracking
13728          is supposed to track where the arguments live and the register
13729          used as vdrap or drap in some spot might be used for something
13730          else in other part of the routine.  */
13731       return new_loc_descr (DW_OP_fbreg, offset, 0);
13732     }
13733
13734   regno = dbx_reg_number (reg);
13735   if (regno <= 31)
13736     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13737                             offset, 0);
13738   else
13739     result = new_loc_descr (DW_OP_bregx, regno, offset);
13740
13741   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13742     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13743
13744   return result;
13745 }
13746
13747 /* Return true if this RTL expression describes a base+offset calculation.  */
13748
13749 static inline int
13750 is_based_loc (const_rtx rtl)
13751 {
13752   return (GET_CODE (rtl) == PLUS
13753           && ((REG_P (XEXP (rtl, 0))
13754                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13755                && CONST_INT_P (XEXP (rtl, 1)))));
13756 }
13757
13758 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13759    failed.  */
13760
13761 static dw_loc_descr_ref
13762 tls_mem_loc_descriptor (rtx mem)
13763 {
13764   tree base;
13765   dw_loc_descr_ref loc_result;
13766
13767   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13768     return NULL;
13769
13770   base = get_base_address (MEM_EXPR (mem));
13771   if (base == NULL
13772       || TREE_CODE (base) != VAR_DECL
13773       || !DECL_THREAD_LOCAL_P (base))
13774     return NULL;
13775
13776   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13777   if (loc_result == NULL)
13778     return NULL;
13779
13780   if (INTVAL (MEM_OFFSET (mem)))
13781     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13782
13783   return loc_result;
13784 }
13785
13786 /* Output debug info about reason why we failed to expand expression as dwarf
13787    expression.  */
13788
13789 static void
13790 expansion_failed (tree expr, rtx rtl, char const *reason)
13791 {
13792   if (dump_file && (dump_flags & TDF_DETAILS))
13793     {
13794       fprintf (dump_file, "Failed to expand as dwarf: ");
13795       if (expr)
13796         print_generic_expr (dump_file, expr, dump_flags);
13797       if (rtl)
13798         {
13799           fprintf (dump_file, "\n");
13800           print_rtl (dump_file, rtl);
13801         }
13802       fprintf (dump_file, "\nReason: %s\n", reason);
13803     }
13804 }
13805
13806 /* Helper function for const_ok_for_output, called either directly
13807    or via for_each_rtx.  */
13808
13809 static int
13810 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13811 {
13812   rtx rtl = *rtlp;
13813
13814   if (GET_CODE (rtl) == UNSPEC)
13815     {
13816       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13817          we can't express it in the debug info.  */
13818 #ifdef ENABLE_CHECKING
13819       /* Don't complain about TLS UNSPECs, those are just too hard to
13820          delegitimize.  */
13821       if (XVECLEN (rtl, 0) != 1
13822           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13823           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13824           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13825           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13826         inform (current_function_decl
13827                 ? DECL_SOURCE_LOCATION (current_function_decl)
13828                 : UNKNOWN_LOCATION,
13829 #if NUM_UNSPEC_VALUES > 0
13830                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13831                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13832                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13833                 XINT (rtl, 1));
13834 #else
13835                 "non-delegitimized UNSPEC %d found in variable location",
13836                 XINT (rtl, 1));
13837 #endif
13838 #endif
13839       expansion_failed (NULL_TREE, rtl,
13840                         "UNSPEC hasn't been delegitimized.\n");
13841       return 1;
13842     }
13843
13844   if (GET_CODE (rtl) != SYMBOL_REF)
13845     return 0;
13846
13847   if (CONSTANT_POOL_ADDRESS_P (rtl))
13848     {
13849       bool marked;
13850       get_pool_constant_mark (rtl, &marked);
13851       /* If all references to this pool constant were optimized away,
13852          it was not output and thus we can't represent it.  */
13853       if (!marked)
13854         {
13855           expansion_failed (NULL_TREE, rtl,
13856                             "Constant was removed from constant pool.\n");
13857           return 1;
13858         }
13859     }
13860
13861   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13862     return 1;
13863
13864   /* Avoid references to external symbols in debug info, on several targets
13865      the linker might even refuse to link when linking a shared library,
13866      and in many other cases the relocations for .debug_info/.debug_loc are
13867      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13868      to be defined within the same shared library or executable are fine.  */
13869   if (SYMBOL_REF_EXTERNAL_P (rtl))
13870     {
13871       tree decl = SYMBOL_REF_DECL (rtl);
13872
13873       if (decl == NULL || !targetm.binds_local_p (decl))
13874         {
13875           expansion_failed (NULL_TREE, rtl,
13876                             "Symbol not defined in current TU.\n");
13877           return 1;
13878         }
13879     }
13880
13881   return 0;
13882 }
13883
13884 /* Return true if constant RTL can be emitted in DW_OP_addr or
13885    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13886    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13887
13888 static bool
13889 const_ok_for_output (rtx rtl)
13890 {
13891   if (GET_CODE (rtl) == SYMBOL_REF)
13892     return const_ok_for_output_1 (&rtl, NULL) == 0;
13893
13894   if (GET_CODE (rtl) == CONST)
13895     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13896
13897   return true;
13898 }
13899
13900 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13901    if possible, NULL otherwise.  */
13902
13903 static dw_die_ref
13904 base_type_for_mode (enum machine_mode mode, bool unsignedp)
13905 {
13906   dw_die_ref type_die;
13907   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13908
13909   if (type == NULL)
13910     return NULL;
13911   switch (TREE_CODE (type))
13912     {
13913     case INTEGER_TYPE:
13914     case REAL_TYPE:
13915       break;
13916     default:
13917       return NULL;
13918     }
13919   type_die = lookup_type_die (type);
13920   if (!type_die)
13921     type_die = modified_type_die (type, false, false, comp_unit_die ());
13922   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13923     return NULL;
13924   return type_die;
13925 }
13926
13927 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
13928    type matching MODE, or, if MODE is narrower than or as wide as
13929    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
13930    possible.  */
13931
13932 static dw_loc_descr_ref
13933 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
13934 {
13935   enum machine_mode outer_mode = mode;
13936   dw_die_ref type_die;
13937   dw_loc_descr_ref cvt;
13938
13939   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13940     {
13941       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
13942       return op;
13943     }
13944   type_die = base_type_for_mode (outer_mode, 1);
13945   if (type_die == NULL)
13946     return NULL;
13947   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13948   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13949   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13950   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13951   add_loc_descr (&op, cvt);
13952   return op;
13953 }
13954
13955 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
13956
13957 static dw_loc_descr_ref
13958 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
13959                         dw_loc_descr_ref op1)
13960 {
13961   dw_loc_descr_ref ret = op0;
13962   add_loc_descr (&ret, op1);
13963   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
13964   if (STORE_FLAG_VALUE != 1)
13965     {
13966       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
13967       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
13968     }
13969   return ret;
13970 }
13971
13972 /* Return location descriptor for signed comparison OP RTL.  */
13973
13974 static dw_loc_descr_ref
13975 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
13976                          enum machine_mode mem_mode)
13977 {
13978   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13979   dw_loc_descr_ref op0, op1;
13980   int shift;
13981
13982   if (op_mode == VOIDmode)
13983     op_mode = GET_MODE (XEXP (rtl, 1));
13984   if (op_mode == VOIDmode)
13985     return NULL;
13986
13987   if (dwarf_strict
13988       && (GET_MODE_CLASS (op_mode) != MODE_INT
13989           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
13990     return NULL;
13991
13992   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
13993                             VAR_INIT_STATUS_INITIALIZED);
13994   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
13995                             VAR_INIT_STATUS_INITIALIZED);
13996
13997   if (op0 == NULL || op1 == NULL)
13998     return NULL;
13999
14000   if (GET_MODE_CLASS (op_mode) != MODE_INT
14001       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14002     return compare_loc_descriptor (op, op0, op1);
14003
14004   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14005     {
14006       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
14007       dw_loc_descr_ref cvt;
14008
14009       if (type_die == NULL)
14010         return NULL;
14011       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14012       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14013       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14014       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14015       add_loc_descr (&op0, cvt);
14016       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14017       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14018       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14019       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14020       add_loc_descr (&op1, cvt);
14021       return compare_loc_descriptor (op, op0, op1);
14022     }
14023
14024   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
14025   /* For eq/ne, if the operands are known to be zero-extended,
14026      there is no need to do the fancy shifting up.  */
14027   if (op == DW_OP_eq || op == DW_OP_ne)
14028     {
14029       dw_loc_descr_ref last0, last1;
14030       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14031         ;
14032       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14033         ;
14034       /* deref_size zero extends, and for constants we can check
14035          whether they are zero extended or not.  */
14036       if (((last0->dw_loc_opc == DW_OP_deref_size
14037             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14038            || (CONST_INT_P (XEXP (rtl, 0))
14039                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14040                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
14041           && ((last1->dw_loc_opc == DW_OP_deref_size
14042                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
14043               || (CONST_INT_P (XEXP (rtl, 1))
14044                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
14045                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
14046         return compare_loc_descriptor (op, op0, op1);
14047     }
14048   add_loc_descr (&op0, int_loc_descriptor (shift));
14049   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14050   if (CONST_INT_P (XEXP (rtl, 1)))
14051     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14052   else
14053     {
14054       add_loc_descr (&op1, int_loc_descriptor (shift));
14055       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14056     }
14057   return compare_loc_descriptor (op, op0, op1);
14058 }
14059
14060 /* Return location descriptor for unsigned comparison OP RTL.  */
14061
14062 static dw_loc_descr_ref
14063 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
14064                          enum machine_mode mem_mode)
14065 {
14066   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14067   dw_loc_descr_ref op0, op1;
14068
14069   if (op_mode == VOIDmode)
14070     op_mode = GET_MODE (XEXP (rtl, 1));
14071   if (op_mode == VOIDmode)
14072     return NULL;
14073   if (GET_MODE_CLASS (op_mode) != MODE_INT)
14074     return NULL;
14075
14076   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14077     return NULL;
14078
14079   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14080                             VAR_INIT_STATUS_INITIALIZED);
14081   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14082                             VAR_INIT_STATUS_INITIALIZED);
14083
14084   if (op0 == NULL || op1 == NULL)
14085     return NULL;
14086
14087   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14088     {
14089       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14090       dw_loc_descr_ref last0, last1;
14091       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
14092         ;
14093       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
14094         ;
14095       if (CONST_INT_P (XEXP (rtl, 0)))
14096         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14097       /* deref_size zero extends, so no need to mask it again.  */
14098       else if (last0->dw_loc_opc != DW_OP_deref_size
14099                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14100         {
14101           add_loc_descr (&op0, int_loc_descriptor (mask));
14102           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14103         }
14104       if (CONST_INT_P (XEXP (rtl, 1)))
14105         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14106       /* deref_size zero extends, so no need to mask it again.  */
14107       else if (last1->dw_loc_opc != DW_OP_deref_size
14108                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
14109         {
14110           add_loc_descr (&op1, int_loc_descriptor (mask));
14111           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14112         }
14113     }
14114   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14115     {
14116       HOST_WIDE_INT bias = 1;
14117       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14118       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14119       if (CONST_INT_P (XEXP (rtl, 1)))
14120         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14121                                   + INTVAL (XEXP (rtl, 1)));
14122       else
14123         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14124                                             bias, 0));
14125     }
14126   return compare_loc_descriptor (op, op0, op1);
14127 }
14128
14129 /* Return location descriptor for {U,S}{MIN,MAX}.  */
14130
14131 static dw_loc_descr_ref
14132 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
14133                        enum machine_mode mem_mode)
14134 {
14135   enum dwarf_location_atom op;
14136   dw_loc_descr_ref op0, op1, ret;
14137   dw_loc_descr_ref bra_node, drop_node;
14138
14139   if (dwarf_strict
14140       && (GET_MODE_CLASS (mode) != MODE_INT
14141           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14142     return NULL;
14143
14144   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14145                             VAR_INIT_STATUS_INITIALIZED);
14146   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14147                             VAR_INIT_STATUS_INITIALIZED);
14148
14149   if (op0 == NULL || op1 == NULL)
14150     return NULL;
14151
14152   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14153   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14154   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14155   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14156     {
14157       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14158         {
14159           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14160           add_loc_descr (&op0, int_loc_descriptor (mask));
14161           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14162           add_loc_descr (&op1, int_loc_descriptor (mask));
14163           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14164         }
14165       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14166         {
14167           HOST_WIDE_INT bias = 1;
14168           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14169           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14170           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14171         }
14172     }
14173   else if (GET_MODE_CLASS (mode) == MODE_INT
14174            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14175     {
14176       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
14177       add_loc_descr (&op0, int_loc_descriptor (shift));
14178       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14179       add_loc_descr (&op1, int_loc_descriptor (shift));
14180       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14181     }
14182   else if (GET_MODE_CLASS (mode) == MODE_INT
14183            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14184     {
14185       dw_die_ref type_die = base_type_for_mode (mode, 0);
14186       dw_loc_descr_ref cvt;
14187       if (type_die == NULL)
14188         return NULL;
14189       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14190       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14191       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14192       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14193       add_loc_descr (&op0, cvt);
14194       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14195       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14196       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14197       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14198       add_loc_descr (&op1, cvt);
14199     }
14200
14201   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14202     op = DW_OP_lt;
14203   else
14204     op = DW_OP_gt;
14205   ret = op0;
14206   add_loc_descr (&ret, op1);
14207   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
14208   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14209   add_loc_descr (&ret, bra_node);
14210   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14211   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14212   add_loc_descr (&ret, drop_node);
14213   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14214   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14215   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
14216       && GET_MODE_CLASS (mode) == MODE_INT
14217       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14218     ret = convert_descriptor_to_mode (mode, ret);
14219   return ret;
14220 }
14221
14222 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
14223    but after converting arguments to type_die, afterwards
14224    convert back to unsigned.  */
14225
14226 static dw_loc_descr_ref
14227 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
14228              enum machine_mode mode, enum machine_mode mem_mode)
14229 {
14230   dw_loc_descr_ref cvt, op0, op1;
14231
14232   if (type_die == NULL)
14233     return NULL;
14234   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14235                             VAR_INIT_STATUS_INITIALIZED);
14236   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14237                             VAR_INIT_STATUS_INITIALIZED);
14238   if (op0 == NULL || op1 == NULL)
14239     return NULL;
14240   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14241   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14242   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14243   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14244   add_loc_descr (&op0, cvt);
14245   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14246   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14247   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14248   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14249   add_loc_descr (&op1, cvt);
14250   add_loc_descr (&op0, op1);
14251   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
14252   return convert_descriptor_to_mode (mode, op0);
14253 }
14254
14255 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
14256    const0 is DW_OP_lit0 or corresponding typed constant,
14257    const1 is DW_OP_lit1 or corresponding typed constant
14258    and constMSB is constant with just the MSB bit set
14259    for the mode):
14260        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14261    L1: const0 DW_OP_swap
14262    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
14263        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14264    L3: DW_OP_drop
14265    L4: DW_OP_nop
14266
14267    CTZ is similar:
14268        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
14269    L1: const0 DW_OP_swap
14270    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14271        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14272    L3: DW_OP_drop
14273    L4: DW_OP_nop
14274
14275    FFS is similar:
14276        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
14277    L1: const1 DW_OP_swap
14278    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
14279        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
14280    L3: DW_OP_drop
14281    L4: DW_OP_nop  */
14282
14283 static dw_loc_descr_ref
14284 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
14285                     enum machine_mode mem_mode)
14286 {
14287   dw_loc_descr_ref op0, ret, tmp;
14288   HOST_WIDE_INT valv;
14289   dw_loc_descr_ref l1jump, l1label;
14290   dw_loc_descr_ref l2jump, l2label;
14291   dw_loc_descr_ref l3jump, l3label;
14292   dw_loc_descr_ref l4jump, l4label;
14293   rtx msb;
14294
14295   if (GET_MODE_CLASS (mode) != MODE_INT
14296       || GET_MODE (XEXP (rtl, 0)) != mode
14297       || (GET_CODE (rtl) == CLZ
14298           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
14299     return NULL;
14300
14301   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14302                             VAR_INIT_STATUS_INITIALIZED);
14303   if (op0 == NULL)
14304     return NULL;
14305   ret = op0;
14306   if (GET_CODE (rtl) == CLZ)
14307     {
14308       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14309         valv = GET_MODE_BITSIZE (mode);
14310     }
14311   else if (GET_CODE (rtl) == FFS)
14312     valv = 0;
14313   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
14314     valv = GET_MODE_BITSIZE (mode);
14315   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14316   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
14317   add_loc_descr (&ret, l1jump);
14318   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14319   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
14320                             VAR_INIT_STATUS_INITIALIZED);
14321   if (tmp == NULL)
14322     return NULL;
14323   add_loc_descr (&ret, tmp);
14324   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
14325   add_loc_descr (&ret, l4jump);
14326   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
14327                                 ? const1_rtx : const0_rtx,
14328                                 mode, mem_mode,
14329                                 VAR_INIT_STATUS_INITIALIZED);
14330   if (l1label == NULL)
14331     return NULL;
14332   add_loc_descr (&ret, l1label);
14333   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14334   l2label = new_loc_descr (DW_OP_dup, 0, 0);
14335   add_loc_descr (&ret, l2label);
14336   if (GET_CODE (rtl) != CLZ)
14337     msb = const1_rtx;
14338   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
14339     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
14340                    << (GET_MODE_BITSIZE (mode) - 1));
14341   else
14342     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
14343                                   << (GET_MODE_BITSIZE (mode)
14344                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
14345   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
14346     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14347                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
14348                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
14349   else
14350     tmp = mem_loc_descriptor (msb, mode, mem_mode,
14351                               VAR_INIT_STATUS_INITIALIZED);
14352   if (tmp == NULL)
14353     return NULL;
14354   add_loc_descr (&ret, tmp);
14355   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14356   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
14357   add_loc_descr (&ret, l3jump);
14358   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14359                             VAR_INIT_STATUS_INITIALIZED);
14360   if (tmp == NULL)
14361     return NULL;
14362   add_loc_descr (&ret, tmp);
14363   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
14364                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
14365   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14366   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
14367   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14368   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
14369   add_loc_descr (&ret, l2jump);
14370   l3label = new_loc_descr (DW_OP_drop, 0, 0);
14371   add_loc_descr (&ret, l3label);
14372   l4label = new_loc_descr (DW_OP_nop, 0, 0);
14373   add_loc_descr (&ret, l4label);
14374   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14375   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14376   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14377   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14378   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14379   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
14380   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14381   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
14382   return ret;
14383 }
14384
14385 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
14386    const1 is DW_OP_lit1 or corresponding typed constant):
14387        const0 DW_OP_swap
14388    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14389        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14390    L2: DW_OP_drop
14391
14392    PARITY is similar:
14393    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
14394        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
14395    L2: DW_OP_drop  */
14396
14397 static dw_loc_descr_ref
14398 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
14399                          enum machine_mode mem_mode)
14400 {
14401   dw_loc_descr_ref op0, ret, tmp;
14402   dw_loc_descr_ref l1jump, l1label;
14403   dw_loc_descr_ref l2jump, l2label;
14404
14405   if (GET_MODE_CLASS (mode) != MODE_INT
14406       || GET_MODE (XEXP (rtl, 0)) != mode)
14407     return NULL;
14408
14409   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14410                             VAR_INIT_STATUS_INITIALIZED);
14411   if (op0 == NULL)
14412     return NULL;
14413   ret = op0;
14414   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14415                             VAR_INIT_STATUS_INITIALIZED);
14416   if (tmp == NULL)
14417     return NULL;
14418   add_loc_descr (&ret, tmp);
14419   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14420   l1label = new_loc_descr (DW_OP_dup, 0, 0);
14421   add_loc_descr (&ret, l1label);
14422   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14423   add_loc_descr (&ret, l2jump);
14424   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14425   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14426   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14427                             VAR_INIT_STATUS_INITIALIZED);
14428   if (tmp == NULL)
14429     return NULL;
14430   add_loc_descr (&ret, tmp);
14431   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14432   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
14433                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
14434   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14435   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
14436                             VAR_INIT_STATUS_INITIALIZED);
14437   add_loc_descr (&ret, tmp);
14438   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14439   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14440   add_loc_descr (&ret, l1jump);
14441   l2label = new_loc_descr (DW_OP_drop, 0, 0);
14442   add_loc_descr (&ret, l2label);
14443   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14444   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14445   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14446   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14447   return ret;
14448 }
14449
14450 /* BSWAP (constS is initial shift count, either 56 or 24):
14451        constS const0
14452    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
14453        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
14454        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
14455        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
14456    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
14457
14458 static dw_loc_descr_ref
14459 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
14460                       enum machine_mode mem_mode)
14461 {
14462   dw_loc_descr_ref op0, ret, tmp;
14463   dw_loc_descr_ref l1jump, l1label;
14464   dw_loc_descr_ref l2jump, l2label;
14465
14466   if (GET_MODE_CLASS (mode) != MODE_INT
14467       || BITS_PER_UNIT != 8
14468       || (GET_MODE_BITSIZE (mode) != 32
14469           &&  GET_MODE_BITSIZE (mode) != 64))
14470     return NULL;
14471
14472   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14473                             VAR_INIT_STATUS_INITIALIZED);
14474   if (op0 == NULL)
14475     return NULL;
14476
14477   ret = op0;
14478   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14479                             mode, mem_mode,
14480                             VAR_INIT_STATUS_INITIALIZED);
14481   if (tmp == NULL)
14482     return NULL;
14483   add_loc_descr (&ret, tmp);
14484   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14485                             VAR_INIT_STATUS_INITIALIZED);
14486   if (tmp == NULL)
14487     return NULL;
14488   add_loc_descr (&ret, tmp);
14489   l1label = new_loc_descr (DW_OP_pick, 2, 0);
14490   add_loc_descr (&ret, l1label);
14491   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
14492                             mode, mem_mode,
14493                             VAR_INIT_STATUS_INITIALIZED);
14494   add_loc_descr (&ret, tmp);
14495   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
14496   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14497   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14498   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
14499                             VAR_INIT_STATUS_INITIALIZED);
14500   if (tmp == NULL)
14501     return NULL;
14502   add_loc_descr (&ret, tmp);
14503   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
14504   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
14505   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14506   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14507   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14508   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
14509   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
14510                             VAR_INIT_STATUS_INITIALIZED);
14511   add_loc_descr (&ret, tmp);
14512   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
14513   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
14514   add_loc_descr (&ret, l2jump);
14515   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
14516                             VAR_INIT_STATUS_INITIALIZED);
14517   add_loc_descr (&ret, tmp);
14518   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
14519   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14520   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
14521   add_loc_descr (&ret, l1jump);
14522   l2label = new_loc_descr (DW_OP_drop, 0, 0);
14523   add_loc_descr (&ret, l2label);
14524   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14525   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
14526   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14527   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
14528   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
14529   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
14530   return ret;
14531 }
14532
14533 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
14534    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14535    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
14536    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
14537
14538    ROTATERT is similar:
14539    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
14540    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
14541    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
14542
14543 static dw_loc_descr_ref
14544 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
14545                        enum machine_mode mem_mode)
14546 {
14547   rtx rtlop1 = XEXP (rtl, 1);
14548   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
14549   int i;
14550
14551   if (GET_MODE_CLASS (mode) != MODE_INT)
14552     return NULL;
14553
14554   if (GET_MODE (rtlop1) != VOIDmode
14555       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
14556     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
14557   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14558                             VAR_INIT_STATUS_INITIALIZED);
14559   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
14560                             VAR_INIT_STATUS_INITIALIZED);
14561   if (op0 == NULL || op1 == NULL)
14562     return NULL;
14563   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14564     for (i = 0; i < 2; i++)
14565       {
14566         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
14567           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
14568                                         mode, mem_mode,
14569                                         VAR_INIT_STATUS_INITIALIZED);
14570         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14571           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
14572                                    ? DW_OP_const4u
14573                                    : HOST_BITS_PER_WIDE_INT == 64
14574                                    ? DW_OP_const8u : DW_OP_constu,
14575                                    GET_MODE_MASK (mode), 0);
14576         else
14577           mask[i] = NULL;
14578         if (mask[i] == NULL)
14579           return NULL;
14580         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
14581       }
14582   ret = op0;
14583   add_loc_descr (&ret, op1);
14584   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14585   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
14586   if (GET_CODE (rtl) == ROTATERT)
14587     {
14588       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14589       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14590                                           GET_MODE_BITSIZE (mode), 0));
14591     }
14592   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
14593   if (mask[0] != NULL)
14594     add_loc_descr (&ret, mask[0]);
14595   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
14596   if (mask[1] != NULL)
14597     {
14598       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14599       add_loc_descr (&ret, mask[1]);
14600       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
14601     }
14602   if (GET_CODE (rtl) == ROTATE)
14603     {
14604       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
14605       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
14606                                           GET_MODE_BITSIZE (mode), 0));
14607     }
14608   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
14609   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
14610   return ret;
14611 }
14612
14613 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
14614    for DEBUG_PARAMETER_REF RTL.  */
14615
14616 static dw_loc_descr_ref
14617 parameter_ref_descriptor (rtx rtl)
14618 {
14619   dw_loc_descr_ref ret;
14620   dw_die_ref ref;
14621
14622   if (dwarf_strict)
14623     return NULL;
14624   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
14625   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
14626   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
14627   if (ref)
14628     {
14629       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14630       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14631       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14632     }
14633   else
14634     {
14635       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14636       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
14637     }
14638   return ret;
14639 }
14640
14641 /* The following routine converts the RTL for a variable or parameter
14642    (resident in memory) into an equivalent Dwarf representation of a
14643    mechanism for getting the address of that same variable onto the top of a
14644    hypothetical "address evaluation" stack.
14645
14646    When creating memory location descriptors, we are effectively transforming
14647    the RTL for a memory-resident object into its Dwarf postfix expression
14648    equivalent.  This routine recursively descends an RTL tree, turning
14649    it into Dwarf postfix code as it goes.
14650
14651    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
14652
14653    MEM_MODE is the mode of the memory reference, needed to handle some
14654    autoincrement addressing modes.
14655
14656    Return 0 if we can't represent the location.  */
14657
14658 static dw_loc_descr_ref
14659 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
14660                     enum machine_mode mem_mode,
14661                     enum var_init_status initialized)
14662 {
14663   dw_loc_descr_ref mem_loc_result = NULL;
14664   enum dwarf_location_atom op;
14665   dw_loc_descr_ref op0, op1;
14666
14667   if (mode == VOIDmode)
14668     mode = GET_MODE (rtl);
14669
14670   /* Note that for a dynamically sized array, the location we will generate a
14671      description of here will be the lowest numbered location which is
14672      actually within the array.  That's *not* necessarily the same as the
14673      zeroth element of the array.  */
14674
14675   rtl = targetm.delegitimize_address (rtl);
14676
14677   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
14678     return NULL;
14679
14680   switch (GET_CODE (rtl))
14681     {
14682     case POST_INC:
14683     case POST_DEC:
14684     case POST_MODIFY:
14685       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
14686
14687     case SUBREG:
14688       /* The case of a subreg may arise when we have a local (register)
14689          variable or a formal (register) parameter which doesn't quite fill
14690          up an entire register.  For now, just assume that it is
14691          legitimate to make the Dwarf info refer to the whole register which
14692          contains the given subreg.  */
14693       if (!subreg_lowpart_p (rtl))
14694         break;
14695       if (GET_MODE_CLASS (mode) == MODE_INT
14696           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
14697           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14698 #ifdef POINTERS_EXTEND_UNSIGNED
14699               || (mode == Pmode && mem_mode != VOIDmode)
14700 #endif
14701              )
14702           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
14703         {
14704           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
14705                                                GET_MODE (SUBREG_REG (rtl)),
14706                                                mem_mode, initialized);
14707           break;
14708         }
14709       if (dwarf_strict)
14710         break;
14711       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
14712         break;
14713       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
14714           && (GET_MODE_CLASS (mode) != MODE_INT
14715               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
14716         break;
14717       else
14718         {
14719           dw_die_ref type_die;
14720           dw_loc_descr_ref cvt;
14721
14722           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
14723                                                GET_MODE (SUBREG_REG (rtl)),
14724                                                mem_mode, initialized);
14725           if (mem_loc_result == NULL)
14726             break;
14727           type_die = base_type_for_mode (mode,
14728                                          GET_MODE_CLASS (mode) == MODE_INT);
14729           if (type_die == NULL)
14730             {
14731               mem_loc_result = NULL;
14732               break;
14733             }
14734           if (GET_MODE_SIZE (mode)
14735               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
14736             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14737           else
14738             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
14739           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14740           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14741           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14742           add_loc_descr (&mem_loc_result, cvt);
14743         }
14744       break;
14745
14746     case REG:
14747       if (GET_MODE_CLASS (mode) != MODE_INT
14748           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14749 #ifdef POINTERS_EXTEND_UNSIGNED
14750               && (mode != Pmode || mem_mode == VOIDmode)
14751 #endif
14752               ))
14753         {
14754           dw_die_ref type_die;
14755
14756           if (dwarf_strict)
14757             break;
14758           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
14759             break;
14760           type_die = base_type_for_mode (mode,
14761                                          GET_MODE_CLASS (mode) == MODE_INT);
14762           if (type_die == NULL)
14763             break;
14764           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
14765                                           dbx_reg_number (rtl), 0);
14766           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14767           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14768           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
14769           break;
14770         }
14771       /* Whenever a register number forms a part of the description of the
14772          method for calculating the (dynamic) address of a memory resident
14773          object, DWARF rules require the register number be referred to as
14774          a "base register".  This distinction is not based in any way upon
14775          what category of register the hardware believes the given register
14776          belongs to.  This is strictly DWARF terminology we're dealing with
14777          here. Note that in cases where the location of a memory-resident
14778          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
14779          OP_CONST (0)) the actual DWARF location descriptor that we generate
14780          may just be OP_BASEREG (basereg).  This may look deceptively like
14781          the object in question was allocated to a register (rather than in
14782          memory) so DWARF consumers need to be aware of the subtle
14783          distinction between OP_REG and OP_BASEREG.  */
14784       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
14785         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
14786       else if (stack_realign_drap
14787                && crtl->drap_reg
14788                && crtl->args.internal_arg_pointer == rtl
14789                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
14790         {
14791           /* If RTL is internal_arg_pointer, which has been optimized
14792              out, use DRAP instead.  */
14793           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
14794                                             VAR_INIT_STATUS_INITIALIZED);
14795         }
14796       break;
14797
14798     case SIGN_EXTEND:
14799     case ZERO_EXTEND:
14800       if (GET_MODE_CLASS (mode) != MODE_INT)
14801         break;
14802       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14803                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
14804       if (op0 == 0)
14805         break;
14806       else if (GET_CODE (rtl) == ZERO_EXTEND
14807                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14808                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14809                   < HOST_BITS_PER_WIDE_INT
14810                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
14811                   to expand zero extend as two shifts instead of
14812                   masking.  */
14813                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
14814         {
14815           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
14816           mem_loc_result = op0;
14817           add_loc_descr (&mem_loc_result,
14818                          int_loc_descriptor (GET_MODE_MASK (imode)));
14819           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
14820         }
14821       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14822         {
14823           int shift = DWARF2_ADDR_SIZE
14824                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14825           shift *= BITS_PER_UNIT;
14826           if (GET_CODE (rtl) == SIGN_EXTEND)
14827             op = DW_OP_shra;
14828           else
14829             op = DW_OP_shr;
14830           mem_loc_result = op0;
14831           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14832           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14833           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14834           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14835         }
14836       else if (!dwarf_strict)
14837         {
14838           dw_die_ref type_die1, type_die2;
14839           dw_loc_descr_ref cvt;
14840
14841           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14842                                           GET_CODE (rtl) == ZERO_EXTEND);
14843           if (type_die1 == NULL)
14844             break;
14845           type_die2 = base_type_for_mode (mode, 1);
14846           if (type_die2 == NULL)
14847             break;
14848           mem_loc_result = op0;
14849           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14850           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14851           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14852           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14853           add_loc_descr (&mem_loc_result, cvt);
14854           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14855           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14856           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14857           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14858           add_loc_descr (&mem_loc_result, cvt);
14859         }
14860       break;
14861
14862     case MEM:
14863       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14864                                            get_address_mode (rtl), mode,
14865                                            VAR_INIT_STATUS_INITIALIZED);
14866       if (mem_loc_result == NULL)
14867         mem_loc_result = tls_mem_loc_descriptor (rtl);
14868       if (mem_loc_result != 0)
14869         {
14870           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14871               || GET_MODE_CLASS (mode) != MODE_INT)
14872             {
14873               dw_die_ref type_die;
14874               dw_loc_descr_ref deref;
14875
14876               if (dwarf_strict)
14877                 return NULL;
14878               type_die
14879                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
14880               if (type_die == NULL)
14881                 return NULL;
14882               deref = new_loc_descr (DW_OP_GNU_deref_type,
14883                                      GET_MODE_SIZE (mode), 0);
14884               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14885               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14886               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14887               add_loc_descr (&mem_loc_result, deref);
14888             }
14889           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14890             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14891           else
14892             add_loc_descr (&mem_loc_result,
14893                            new_loc_descr (DW_OP_deref_size,
14894                                           GET_MODE_SIZE (mode), 0));
14895         }
14896       else
14897         {
14898           rtx new_rtl = avoid_constant_pool_reference (rtl);
14899           if (new_rtl != rtl)
14900             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
14901         }
14902       break;
14903
14904     case LO_SUM:
14905       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14906
14907     case LABEL_REF:
14908       /* Some ports can transform a symbol ref into a label ref, because
14909          the symbol ref is too far away and has to be dumped into a constant
14910          pool.  */
14911     case CONST:
14912     case SYMBOL_REF:
14913       if (GET_MODE_CLASS (mode) != MODE_INT
14914           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14915 #ifdef POINTERS_EXTEND_UNSIGNED
14916               && (mode != Pmode || mem_mode == VOIDmode)
14917 #endif
14918               ))
14919         break;
14920       if (GET_CODE (rtl) == SYMBOL_REF
14921           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14922         {
14923           dw_loc_descr_ref temp;
14924
14925           /* If this is not defined, we have no way to emit the data.  */
14926           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14927             break;
14928
14929           /* We used to emit DW_OP_addr here, but that's wrong, since
14930              DW_OP_addr should be relocated by the debug info consumer,
14931              while DW_OP_GNU_push_tls_address operand should not.  */
14932           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
14933                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
14934           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
14935           temp->dw_loc_oprnd1.v.val_addr = rtl;
14936           temp->dtprel = true;
14937
14938           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
14939           add_loc_descr (&mem_loc_result, temp);
14940
14941           break;
14942         }
14943
14944       if (!const_ok_for_output (rtl))
14945         break;
14946
14947     symref:
14948       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14949       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14950       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14951       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14952       break;
14953
14954     case CONCAT:
14955     case CONCATN:
14956     case VAR_LOCATION:
14957     case DEBUG_IMPLICIT_PTR:
14958       expansion_failed (NULL_TREE, rtl,
14959                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14960       return 0;
14961
14962     case ENTRY_VALUE:
14963       if (dwarf_strict)
14964         return NULL;
14965       if (REG_P (ENTRY_VALUE_EXP (rtl)))
14966         {
14967           if (GET_MODE_CLASS (mode) != MODE_INT
14968               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14969             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14970                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14971           else
14972             op0
14973               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
14974                                         VAR_INIT_STATUS_INITIALIZED);
14975         }
14976       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14977                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14978         {
14979           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14980                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14981           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14982             return NULL;
14983         }
14984       else
14985         gcc_unreachable ();
14986       if (op0 == NULL)
14987         return NULL;
14988       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
14989       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14990       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14991       break;
14992
14993     case DEBUG_PARAMETER_REF:
14994       mem_loc_result = parameter_ref_descriptor (rtl);
14995       break;
14996
14997     case PRE_MODIFY:
14998       /* Extract the PLUS expression nested inside and fall into
14999          PLUS code below.  */
15000       rtl = XEXP (rtl, 1);
15001       goto plus;
15002
15003     case PRE_INC:
15004     case PRE_DEC:
15005       /* Turn these into a PLUS expression and fall into the PLUS code
15006          below.  */
15007       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
15008                           GEN_INT (GET_CODE (rtl) == PRE_INC
15009                                    ? GET_MODE_UNIT_SIZE (mem_mode)
15010                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
15011
15012       /* ... fall through ...  */
15013
15014     case PLUS:
15015     plus:
15016       if (is_based_loc (rtl)
15017           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15018           && GET_MODE_CLASS (mode) == MODE_INT)
15019         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
15020                                           INTVAL (XEXP (rtl, 1)),
15021                                           VAR_INIT_STATUS_INITIALIZED);
15022       else
15023         {
15024           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15025                                                VAR_INIT_STATUS_INITIALIZED);
15026           if (mem_loc_result == 0)
15027             break;
15028
15029           if (CONST_INT_P (XEXP (rtl, 1))
15030               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
15031             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
15032           else
15033             {
15034               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15035                                         VAR_INIT_STATUS_INITIALIZED);
15036               if (op1 == 0)
15037                 break;
15038               add_loc_descr (&mem_loc_result, op1);
15039               add_loc_descr (&mem_loc_result,
15040                              new_loc_descr (DW_OP_plus, 0, 0));
15041             }
15042         }
15043       break;
15044
15045     /* If a pseudo-reg is optimized away, it is possible for it to
15046        be replaced with a MEM containing a multiply or shift.  */
15047     case MINUS:
15048       op = DW_OP_minus;
15049       goto do_binop;
15050
15051     case MULT:
15052       op = DW_OP_mul;
15053       goto do_binop;
15054
15055     case DIV:
15056       if (!dwarf_strict
15057           && GET_MODE_CLASS (mode) == MODE_INT
15058           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
15059         {
15060           mem_loc_result = typed_binop (DW_OP_div, rtl,
15061                                         base_type_for_mode (mode, 0),
15062                                         mode, mem_mode);
15063           break;
15064         }
15065       op = DW_OP_div;
15066       goto do_binop;
15067
15068     case UMOD:
15069       op = DW_OP_mod;
15070       goto do_binop;
15071
15072     case ASHIFT:
15073       op = DW_OP_shl;
15074       goto do_shift;
15075
15076     case ASHIFTRT:
15077       op = DW_OP_shra;
15078       goto do_shift;
15079
15080     case LSHIFTRT:
15081       op = DW_OP_shr;
15082       goto do_shift;
15083
15084     do_shift:
15085       if (GET_MODE_CLASS (mode) != MODE_INT)
15086         break;
15087       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15088                                 VAR_INIT_STATUS_INITIALIZED);
15089       {
15090         rtx rtlop1 = XEXP (rtl, 1);
15091         if (GET_MODE (rtlop1) != VOIDmode
15092             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
15093                < GET_MODE_BITSIZE (mode))
15094           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
15095         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
15096                                   VAR_INIT_STATUS_INITIALIZED);
15097       }
15098
15099       if (op0 == 0 || op1 == 0)
15100         break;
15101
15102       mem_loc_result = op0;
15103       add_loc_descr (&mem_loc_result, op1);
15104       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15105       break;
15106
15107     case AND:
15108       op = DW_OP_and;
15109       goto do_binop;
15110
15111     case IOR:
15112       op = DW_OP_or;
15113       goto do_binop;
15114
15115     case XOR:
15116       op = DW_OP_xor;
15117       goto do_binop;
15118
15119     do_binop:
15120       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15121                                 VAR_INIT_STATUS_INITIALIZED);
15122       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15123                                 VAR_INIT_STATUS_INITIALIZED);
15124
15125       if (op0 == 0 || op1 == 0)
15126         break;
15127
15128       mem_loc_result = op0;
15129       add_loc_descr (&mem_loc_result, op1);
15130       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15131       break;
15132
15133     case MOD:
15134       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
15135         {
15136           mem_loc_result = typed_binop (DW_OP_mod, rtl,
15137                                         base_type_for_mode (mode, 0),
15138                                         mode, mem_mode);
15139           break;
15140         }
15141
15142       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15143                                 VAR_INIT_STATUS_INITIALIZED);
15144       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15145                                 VAR_INIT_STATUS_INITIALIZED);
15146
15147       if (op0 == 0 || op1 == 0)
15148         break;
15149
15150       mem_loc_result = op0;
15151       add_loc_descr (&mem_loc_result, op1);
15152       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15153       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
15154       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
15155       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
15156       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
15157       break;
15158
15159     case UDIV:
15160       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
15161         {
15162           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
15163             {
15164               op = DW_OP_div;
15165               goto do_binop;
15166             }
15167           mem_loc_result = typed_binop (DW_OP_div, rtl,
15168                                         base_type_for_mode (mode, 1),
15169                                         mode, mem_mode);
15170         }
15171       break;
15172
15173     case NOT:
15174       op = DW_OP_not;
15175       goto do_unop;
15176
15177     case ABS:
15178       op = DW_OP_abs;
15179       goto do_unop;
15180
15181     case NEG:
15182       op = DW_OP_neg;
15183       goto do_unop;
15184
15185     do_unop:
15186       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
15187                                 VAR_INIT_STATUS_INITIALIZED);
15188
15189       if (op0 == 0)
15190         break;
15191
15192       mem_loc_result = op0;
15193       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15194       break;
15195
15196     case CONST_INT:
15197       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15198 #ifdef POINTERS_EXTEND_UNSIGNED
15199           || (mode == Pmode
15200               && mem_mode != VOIDmode
15201               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
15202 #endif
15203           )
15204         {
15205           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
15206           break;
15207         }
15208       if (!dwarf_strict
15209           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
15210               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
15211         {
15212           dw_die_ref type_die = base_type_for_mode (mode, 1);
15213           if (type_die == NULL)
15214             return NULL;
15215           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
15216                                           INTVAL (rtl));
15217           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15218           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15219           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15220           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
15221             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
15222           else
15223             {
15224               mem_loc_result->dw_loc_oprnd2.val_class
15225                 = dw_val_class_const_double;
15226               mem_loc_result->dw_loc_oprnd2.v.val_double
15227                 = shwi_to_double_int (INTVAL (rtl));
15228             }
15229         }
15230       break;
15231
15232     case CONST_DOUBLE:
15233       if (!dwarf_strict)
15234         {
15235           dw_die_ref type_die;
15236
15237           /* Note that a CONST_DOUBLE rtx could represent either an integer
15238              or a floating-point constant.  A CONST_DOUBLE is used whenever
15239              the constant requires more than one word in order to be
15240              adequately represented.  We output CONST_DOUBLEs as blocks.  */
15241           if (mode == VOIDmode
15242               || (GET_MODE (rtl) == VOIDmode
15243                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
15244             break;
15245           type_die = base_type_for_mode (mode,
15246                                          GET_MODE_CLASS (mode) == MODE_INT);
15247           if (type_die == NULL)
15248             return NULL;
15249           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
15250           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15251           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15252           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
15253           if (SCALAR_FLOAT_MODE_P (mode))
15254             {
15255               unsigned int length = GET_MODE_SIZE (mode);
15256               unsigned char *array
15257                   = (unsigned char*) ggc_alloc_atomic (length);
15258
15259               insert_float (rtl, array);
15260               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15261               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15262               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15263               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15264             }
15265           else
15266             {
15267               mem_loc_result->dw_loc_oprnd2.val_class
15268                 = dw_val_class_const_double;
15269               mem_loc_result->dw_loc_oprnd2.v.val_double
15270                 = rtx_to_double_int (rtl);
15271             }
15272         }
15273       break;
15274
15275     case EQ:
15276       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
15277       break;
15278
15279     case GE:
15280       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15281       break;
15282
15283     case GT:
15284       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15285       break;
15286
15287     case LE:
15288       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15289       break;
15290
15291     case LT:
15292       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15293       break;
15294
15295     case NE:
15296       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
15297       break;
15298
15299     case GEU:
15300       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
15301       break;
15302
15303     case GTU:
15304       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
15305       break;
15306
15307     case LEU:
15308       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
15309       break;
15310
15311     case LTU:
15312       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
15313       break;
15314
15315     case UMIN:
15316     case UMAX:
15317       if (GET_MODE_CLASS (mode) != MODE_INT)
15318         break;
15319       /* FALLTHRU */
15320     case SMIN:
15321     case SMAX:
15322       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
15323       break;
15324
15325     case ZERO_EXTRACT:
15326     case SIGN_EXTRACT:
15327       if (CONST_INT_P (XEXP (rtl, 1))
15328           && CONST_INT_P (XEXP (rtl, 2))
15329           && ((unsigned) INTVAL (XEXP (rtl, 1))
15330               + (unsigned) INTVAL (XEXP (rtl, 2))
15331               <= GET_MODE_BITSIZE (mode))
15332           && GET_MODE_CLASS (mode) == MODE_INT
15333           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
15334           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
15335         {
15336           int shift, size;
15337           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15338                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
15339           if (op0 == 0)
15340             break;
15341           if (GET_CODE (rtl) == SIGN_EXTRACT)
15342             op = DW_OP_shra;
15343           else
15344             op = DW_OP_shr;
15345           mem_loc_result = op0;
15346           size = INTVAL (XEXP (rtl, 1));
15347           shift = INTVAL (XEXP (rtl, 2));
15348           if (BITS_BIG_ENDIAN)
15349             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
15350                     - shift - size;
15351           if (shift + size != (int) DWARF2_ADDR_SIZE)
15352             {
15353               add_loc_descr (&mem_loc_result,
15354                              int_loc_descriptor (DWARF2_ADDR_SIZE
15355                                                  - shift - size));
15356               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
15357             }
15358           if (size != (int) DWARF2_ADDR_SIZE)
15359             {
15360               add_loc_descr (&mem_loc_result,
15361                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
15362               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
15363             }
15364         }
15365       break;
15366
15367     case IF_THEN_ELSE:
15368       {
15369         dw_loc_descr_ref op2, bra_node, drop_node;
15370         op0 = mem_loc_descriptor (XEXP (rtl, 0),
15371                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
15372                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
15373                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
15374         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
15375                                   VAR_INIT_STATUS_INITIALIZED);
15376         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
15377                                   VAR_INIT_STATUS_INITIALIZED);
15378         if (op0 == NULL || op1 == NULL || op2 == NULL)
15379           break;
15380
15381         mem_loc_result = op1;
15382         add_loc_descr (&mem_loc_result, op2);
15383         add_loc_descr (&mem_loc_result, op0);
15384         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15385         add_loc_descr (&mem_loc_result, bra_node);
15386         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
15387         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
15388         add_loc_descr (&mem_loc_result, drop_node);
15389         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15390         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
15391       }
15392       break;
15393
15394     case FLOAT_EXTEND:
15395     case FLOAT_TRUNCATE:
15396     case FLOAT:
15397     case UNSIGNED_FLOAT:
15398     case FIX:
15399     case UNSIGNED_FIX:
15400       if (!dwarf_strict)
15401         {
15402           dw_die_ref type_die;
15403           dw_loc_descr_ref cvt;
15404
15405           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
15406                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
15407           if (op0 == NULL)
15408             break;
15409           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
15410               && (GET_CODE (rtl) == FLOAT
15411                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
15412                      <= DWARF2_ADDR_SIZE))
15413             {
15414               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
15415                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
15416               if (type_die == NULL)
15417                 break;
15418               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
15419               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15420               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15421               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15422               add_loc_descr (&op0, cvt);
15423             }
15424           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
15425           if (type_die == NULL)
15426             break;
15427           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
15428           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15429           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
15430           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
15431           add_loc_descr (&op0, cvt);
15432           if (GET_MODE_CLASS (mode) == MODE_INT
15433               && (GET_CODE (rtl) == FIX
15434                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
15435             {
15436               op0 = convert_descriptor_to_mode (mode, op0);
15437               if (op0 == NULL)
15438                 break;
15439             }
15440           mem_loc_result = op0;
15441         }
15442       break;
15443
15444     case CLZ:
15445     case CTZ:
15446     case FFS:
15447       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
15448       break;
15449
15450     case POPCOUNT:
15451     case PARITY:
15452       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
15453       break;
15454
15455     case BSWAP:
15456       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
15457       break;
15458
15459     case ROTATE:
15460     case ROTATERT:
15461       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
15462       break;
15463
15464     case COMPARE:
15465     case TRUNCATE:
15466       /* In theory, we could implement the above.  */
15467       /* DWARF cannot represent the unsigned compare operations
15468          natively.  */
15469     case SS_MULT:
15470     case US_MULT:
15471     case SS_DIV:
15472     case US_DIV:
15473     case SS_PLUS:
15474     case US_PLUS:
15475     case SS_MINUS:
15476     case US_MINUS:
15477     case SS_NEG:
15478     case US_NEG:
15479     case SS_ABS:
15480     case SS_ASHIFT:
15481     case US_ASHIFT:
15482     case SS_TRUNCATE:
15483     case US_TRUNCATE:
15484     case UNORDERED:
15485     case ORDERED:
15486     case UNEQ:
15487     case UNGE:
15488     case UNGT:
15489     case UNLE:
15490     case UNLT:
15491     case LTGT:
15492     case FRACT_CONVERT:
15493     case UNSIGNED_FRACT_CONVERT:
15494     case SAT_FRACT:
15495     case UNSIGNED_SAT_FRACT:
15496     case SQRT:
15497     case ASM_OPERANDS:
15498     case VEC_MERGE:
15499     case VEC_SELECT:
15500     case VEC_CONCAT:
15501     case VEC_DUPLICATE:
15502     case UNSPEC:
15503     case HIGH:
15504     case FMA:
15505     case STRICT_LOW_PART:
15506     case CONST_VECTOR:
15507     case CONST_FIXED:
15508     case CLRSB:
15509       /* If delegitimize_address couldn't do anything with the UNSPEC, we
15510          can't express it in the debug info.  This can happen e.g. with some
15511          TLS UNSPECs.  */
15512       break;
15513
15514     case CONST_STRING:
15515       resolve_one_addr (&rtl, NULL);
15516       goto symref;
15517
15518     default:
15519 #ifdef ENABLE_CHECKING
15520       print_rtl (stderr, rtl);
15521       gcc_unreachable ();
15522 #else
15523       break;
15524 #endif
15525     }
15526
15527   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15528     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15529
15530   return mem_loc_result;
15531 }
15532
15533 /* Return a descriptor that describes the concatenation of two locations.
15534    This is typically a complex variable.  */
15535
15536 static dw_loc_descr_ref
15537 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
15538 {
15539   dw_loc_descr_ref cc_loc_result = NULL;
15540   dw_loc_descr_ref x0_ref
15541     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15542   dw_loc_descr_ref x1_ref
15543     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15544
15545   if (x0_ref == 0 || x1_ref == 0)
15546     return 0;
15547
15548   cc_loc_result = x0_ref;
15549   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
15550
15551   add_loc_descr (&cc_loc_result, x1_ref);
15552   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
15553
15554   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
15555     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15556
15557   return cc_loc_result;
15558 }
15559
15560 /* Return a descriptor that describes the concatenation of N
15561    locations.  */
15562
15563 static dw_loc_descr_ref
15564 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
15565 {
15566   unsigned int i;
15567   dw_loc_descr_ref cc_loc_result = NULL;
15568   unsigned int n = XVECLEN (concatn, 0);
15569
15570   for (i = 0; i < n; ++i)
15571     {
15572       dw_loc_descr_ref ref;
15573       rtx x = XVECEXP (concatn, 0, i);
15574
15575       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
15576       if (ref == NULL)
15577         return NULL;
15578
15579       add_loc_descr (&cc_loc_result, ref);
15580       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
15581     }
15582
15583   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
15584     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
15585
15586   return cc_loc_result;
15587 }
15588
15589 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
15590    for DEBUG_IMPLICIT_PTR RTL.  */
15591
15592 static dw_loc_descr_ref
15593 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
15594 {
15595   dw_loc_descr_ref ret;
15596   dw_die_ref ref;
15597
15598   if (dwarf_strict)
15599     return NULL;
15600   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
15601               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
15602               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
15603   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
15604   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
15605   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
15606   if (ref)
15607     {
15608       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
15609       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
15610       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
15611     }
15612   else
15613     {
15614       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
15615       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
15616     }
15617   return ret;
15618 }
15619
15620 /* Output a proper Dwarf location descriptor for a variable or parameter
15621    which is either allocated in a register or in a memory location.  For a
15622    register, we just generate an OP_REG and the register number.  For a
15623    memory location we provide a Dwarf postfix expression describing how to
15624    generate the (dynamic) address of the object onto the address stack.
15625
15626    MODE is mode of the decl if this loc_descriptor is going to be used in
15627    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15628    allowed, VOIDmode otherwise.
15629
15630    If we don't know how to describe it, return 0.  */
15631
15632 static dw_loc_descr_ref
15633 loc_descriptor (rtx rtl, enum machine_mode mode,
15634                 enum var_init_status initialized)
15635 {
15636   dw_loc_descr_ref loc_result = NULL;
15637
15638   switch (GET_CODE (rtl))
15639     {
15640     case SUBREG:
15641       /* The case of a subreg may arise when we have a local (register)
15642          variable or a formal (register) parameter which doesn't quite fill
15643          up an entire register.  For now, just assume that it is
15644          legitimate to make the Dwarf info refer to the whole register which
15645          contains the given subreg.  */
15646       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15647         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
15648       else
15649         goto do_default;
15650       break;
15651
15652     case REG:
15653       loc_result = reg_loc_descriptor (rtl, initialized);
15654       break;
15655
15656     case MEM:
15657       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15658                                        GET_MODE (rtl), initialized);
15659       if (loc_result == NULL)
15660         loc_result = tls_mem_loc_descriptor (rtl);
15661       if (loc_result == NULL)
15662         {
15663           rtx new_rtl = avoid_constant_pool_reference (rtl);
15664           if (new_rtl != rtl)
15665             loc_result = loc_descriptor (new_rtl, mode, initialized);
15666         }
15667       break;
15668
15669     case CONCAT:
15670       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15671                                           initialized);
15672       break;
15673
15674     case CONCATN:
15675       loc_result = concatn_loc_descriptor (rtl, initialized);
15676       break;
15677
15678     case VAR_LOCATION:
15679       /* Single part.  */
15680       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15681         {
15682           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15683           if (GET_CODE (loc) == EXPR_LIST)
15684             loc = XEXP (loc, 0);
15685           loc_result = loc_descriptor (loc, mode, initialized);
15686           break;
15687         }
15688
15689       rtl = XEXP (rtl, 1);
15690       /* FALLTHRU */
15691
15692     case PARALLEL:
15693       {
15694         rtvec par_elems = XVEC (rtl, 0);
15695         int num_elem = GET_NUM_ELEM (par_elems);
15696         enum machine_mode mode;
15697         int i;
15698
15699         /* Create the first one, so we have something to add to.  */
15700         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15701                                      VOIDmode, initialized);
15702         if (loc_result == NULL)
15703           return NULL;
15704         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15705         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15706         for (i = 1; i < num_elem; i++)
15707           {
15708             dw_loc_descr_ref temp;
15709
15710             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15711                                    VOIDmode, initialized);
15712             if (temp == NULL)
15713               return NULL;
15714             add_loc_descr (&loc_result, temp);
15715             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15716             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15717           }
15718       }
15719       break;
15720
15721     case CONST_INT:
15722       if (mode != VOIDmode && mode != BLKmode)
15723         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15724                                                     INTVAL (rtl));
15725       break;
15726
15727     case CONST_DOUBLE:
15728       if (mode == VOIDmode)
15729         mode = GET_MODE (rtl);
15730
15731       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15732         {
15733           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15734
15735           /* Note that a CONST_DOUBLE rtx could represent either an integer
15736              or a floating-point constant.  A CONST_DOUBLE is used whenever
15737              the constant requires more than one word in order to be
15738              adequately represented.  We output CONST_DOUBLEs as blocks.  */
15739           loc_result = new_loc_descr (DW_OP_implicit_value,
15740                                       GET_MODE_SIZE (mode), 0);
15741           if (SCALAR_FLOAT_MODE_P (mode))
15742             {
15743               unsigned int length = GET_MODE_SIZE (mode);
15744               unsigned char *array
15745                   = (unsigned char*) ggc_alloc_atomic (length);
15746
15747               insert_float (rtl, array);
15748               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15749               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15750               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15751               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15752             }
15753           else
15754             {
15755               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15756               loc_result->dw_loc_oprnd2.v.val_double
15757                 = rtx_to_double_int (rtl);
15758             }
15759         }
15760       break;
15761
15762     case CONST_VECTOR:
15763       if (mode == VOIDmode)
15764         mode = GET_MODE (rtl);
15765
15766       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15767         {
15768           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15769           unsigned int length = CONST_VECTOR_NUNITS (rtl);
15770           unsigned char *array = (unsigned char *)
15771             ggc_alloc_atomic (length * elt_size);
15772           unsigned int i;
15773           unsigned char *p;
15774
15775           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15776           switch (GET_MODE_CLASS (mode))
15777             {
15778             case MODE_VECTOR_INT:
15779               for (i = 0, p = array; i < length; i++, p += elt_size)
15780                 {
15781                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15782                   double_int val = rtx_to_double_int (elt);
15783
15784                   if (elt_size <= sizeof (HOST_WIDE_INT))
15785                     insert_int (double_int_to_shwi (val), elt_size, p);
15786                   else
15787                     {
15788                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15789                       insert_double (val, p);
15790                     }
15791                 }
15792               break;
15793
15794             case MODE_VECTOR_FLOAT:
15795               for (i = 0, p = array; i < length; i++, p += elt_size)
15796                 {
15797                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15798                   insert_float (elt, p);
15799                 }
15800               break;
15801
15802             default:
15803               gcc_unreachable ();
15804             }
15805
15806           loc_result = new_loc_descr (DW_OP_implicit_value,
15807                                       length * elt_size, 0);
15808           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15809           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15810           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15811           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15812         }
15813       break;
15814
15815     case CONST:
15816       if (mode == VOIDmode
15817           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
15818           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
15819           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15820         {
15821           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15822           break;
15823         }
15824       /* FALLTHROUGH */
15825     case SYMBOL_REF:
15826       if (!const_ok_for_output (rtl))
15827         break;
15828     case LABEL_REF:
15829       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15830           && (dwarf_version >= 4 || !dwarf_strict))
15831         {
15832           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
15833           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
15834           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
15835           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15836           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15837         }
15838       break;
15839
15840     case DEBUG_IMPLICIT_PTR:
15841       loc_result = implicit_ptr_descriptor (rtl, 0);
15842       break;
15843
15844     case PLUS:
15845       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15846           && CONST_INT_P (XEXP (rtl, 1)))
15847         {
15848           loc_result
15849             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15850           break;
15851         }
15852       /* FALLTHRU */
15853     do_default:
15854     default:
15855       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
15856            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15857            && dwarf_version >= 4)
15858           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15859         {
15860           /* Value expression.  */
15861           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15862           if (loc_result)
15863             add_loc_descr (&loc_result,
15864                            new_loc_descr (DW_OP_stack_value, 0, 0));
15865         }
15866       break;
15867     }
15868
15869   return loc_result;
15870 }
15871
15872 /* We need to figure out what section we should use as the base for the
15873    address ranges where a given location is valid.
15874    1. If this particular DECL has a section associated with it, use that.
15875    2. If this function has a section associated with it, use that.
15876    3. Otherwise, use the text section.
15877    XXX: If you split a variable across multiple sections, we won't notice.  */
15878
15879 static const char *
15880 secname_for_decl (const_tree decl)
15881 {
15882   const char *secname;
15883
15884   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
15885     {
15886       tree sectree = DECL_SECTION_NAME (decl);
15887       secname = TREE_STRING_POINTER (sectree);
15888     }
15889   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15890     {
15891       tree sectree = DECL_SECTION_NAME (current_function_decl);
15892       secname = TREE_STRING_POINTER (sectree);
15893     }
15894   else if (cfun && in_cold_section_p)
15895     secname = crtl->subsections.cold_section_label;
15896   else
15897     secname = text_section_label;
15898
15899   return secname;
15900 }
15901
15902 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
15903
15904 static bool
15905 decl_by_reference_p (tree decl)
15906 {
15907   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15908            || TREE_CODE (decl) == VAR_DECL)
15909           && DECL_BY_REFERENCE (decl));
15910 }
15911
15912 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15913    for VARLOC.  */
15914
15915 static dw_loc_descr_ref
15916 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15917                enum var_init_status initialized)
15918 {
15919   int have_address = 0;
15920   dw_loc_descr_ref descr;
15921   enum machine_mode mode;
15922
15923   if (want_address != 2)
15924     {
15925       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15926       /* Single part.  */
15927       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15928         {
15929           varloc = PAT_VAR_LOCATION_LOC (varloc);
15930           if (GET_CODE (varloc) == EXPR_LIST)
15931             varloc = XEXP (varloc, 0);
15932           mode = GET_MODE (varloc);
15933           if (MEM_P (varloc))
15934             {
15935               rtx addr = XEXP (varloc, 0);
15936               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15937                                           mode, initialized);
15938               if (descr)
15939                 have_address = 1;
15940               else
15941                 {
15942                   rtx x = avoid_constant_pool_reference (varloc);
15943                   if (x != varloc)
15944                     descr = mem_loc_descriptor (x, mode, VOIDmode,
15945                                                 initialized);
15946                 }
15947             }
15948           else
15949             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15950         }
15951       else
15952         return 0;
15953     }
15954   else
15955     {
15956       if (GET_CODE (varloc) == VAR_LOCATION)
15957         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15958       else
15959         mode = DECL_MODE (loc);
15960       descr = loc_descriptor (varloc, mode, initialized);
15961       have_address = 1;
15962     }
15963
15964   if (!descr)
15965     return 0;
15966
15967   if (want_address == 2 && !have_address
15968       && (dwarf_version >= 4 || !dwarf_strict))
15969     {
15970       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15971         {
15972           expansion_failed (loc, NULL_RTX,
15973                             "DWARF address size mismatch");
15974           return 0;
15975         }
15976       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15977       have_address = 1;
15978     }
15979   /* Show if we can't fill the request for an address.  */
15980   if (want_address && !have_address)
15981     {
15982       expansion_failed (loc, NULL_RTX,
15983                         "Want address and only have value");
15984       return 0;
15985     }
15986
15987   /* If we've got an address and don't want one, dereference.  */
15988   if (!want_address && have_address)
15989     {
15990       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15991       enum dwarf_location_atom op;
15992
15993       if (size > DWARF2_ADDR_SIZE || size == -1)
15994         {
15995           expansion_failed (loc, NULL_RTX,
15996                             "DWARF address size mismatch");
15997           return 0;
15998         }
15999       else if (size == DWARF2_ADDR_SIZE)
16000         op = DW_OP_deref;
16001       else
16002         op = DW_OP_deref_size;
16003
16004       add_loc_descr (&descr, new_loc_descr (op, size, 0));
16005     }
16006
16007   return descr;
16008 }
16009
16010 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
16011    if it is not possible.  */
16012
16013 static dw_loc_descr_ref
16014 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
16015 {
16016   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
16017     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
16018   else if (dwarf_version >= 3 || !dwarf_strict)
16019     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
16020   else
16021     return NULL;
16022 }
16023
16024 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
16025    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
16026
16027 static dw_loc_descr_ref
16028 dw_sra_loc_expr (tree decl, rtx loc)
16029 {
16030   rtx p;
16031   unsigned int padsize = 0;
16032   dw_loc_descr_ref descr, *descr_tail;
16033   unsigned HOST_WIDE_INT decl_size;
16034   rtx varloc;
16035   enum var_init_status initialized;
16036
16037   if (DECL_SIZE (decl) == NULL
16038       || !host_integerp (DECL_SIZE (decl), 1))
16039     return NULL;
16040
16041   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
16042   descr = NULL;
16043   descr_tail = &descr;
16044
16045   for (p = loc; p; p = XEXP (p, 1))
16046     {
16047       unsigned int bitsize = decl_piece_bitsize (p);
16048       rtx loc_note = *decl_piece_varloc_ptr (p);
16049       dw_loc_descr_ref cur_descr;
16050       dw_loc_descr_ref *tail, last = NULL;
16051       unsigned int opsize = 0;
16052
16053       if (loc_note == NULL_RTX
16054           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
16055         {
16056           padsize += bitsize;
16057           continue;
16058         }
16059       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
16060       varloc = NOTE_VAR_LOCATION (loc_note);
16061       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
16062       if (cur_descr == NULL)
16063         {
16064           padsize += bitsize;
16065           continue;
16066         }
16067
16068       /* Check that cur_descr either doesn't use
16069          DW_OP_*piece operations, or their sum is equal
16070          to bitsize.  Otherwise we can't embed it.  */
16071       for (tail = &cur_descr; *tail != NULL;
16072            tail = &(*tail)->dw_loc_next)
16073         if ((*tail)->dw_loc_opc == DW_OP_piece)
16074           {
16075             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
16076                       * BITS_PER_UNIT;
16077             last = *tail;
16078           }
16079         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
16080           {
16081             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
16082             last = *tail;
16083           }
16084
16085       if (last != NULL && opsize != bitsize)
16086         {
16087           padsize += bitsize;
16088           continue;
16089         }
16090
16091       /* If there is a hole, add DW_OP_*piece after empty DWARF
16092          expression, which means that those bits are optimized out.  */
16093       if (padsize)
16094         {
16095           if (padsize > decl_size)
16096             return NULL;
16097           decl_size -= padsize;
16098           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
16099           if (*descr_tail == NULL)
16100             return NULL;
16101           descr_tail = &(*descr_tail)->dw_loc_next;
16102           padsize = 0;
16103         }
16104       *descr_tail = cur_descr;
16105       descr_tail = tail;
16106       if (bitsize > decl_size)
16107         return NULL;
16108       decl_size -= bitsize;
16109       if (last == NULL)
16110         {
16111           HOST_WIDE_INT offset = 0;
16112           if (GET_CODE (varloc) == VAR_LOCATION
16113               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
16114             {
16115               varloc = PAT_VAR_LOCATION_LOC (varloc);
16116               if (GET_CODE (varloc) == EXPR_LIST)
16117                 varloc = XEXP (varloc, 0);
16118             }
16119           do 
16120             {
16121               if (GET_CODE (varloc) == CONST
16122                   || GET_CODE (varloc) == SIGN_EXTEND
16123                   || GET_CODE (varloc) == ZERO_EXTEND)
16124                 varloc = XEXP (varloc, 0);
16125               else if (GET_CODE (varloc) == SUBREG)
16126                 varloc = SUBREG_REG (varloc);
16127               else
16128                 break;
16129             }
16130           while (1);
16131           /* DW_OP_bit_size offset should be zero for register
16132              or implicit location descriptions and empty location
16133              descriptions, but for memory addresses needs big endian
16134              adjustment.  */
16135           if (MEM_P (varloc))
16136             {
16137               unsigned HOST_WIDE_INT memsize
16138                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
16139               if (memsize != bitsize)
16140                 {
16141                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
16142                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
16143                     return NULL;
16144                   if (memsize < bitsize)
16145                     return NULL;
16146                   if (BITS_BIG_ENDIAN)
16147                     offset = memsize - bitsize;
16148                 }
16149             }
16150
16151           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
16152           if (*descr_tail == NULL)
16153             return NULL;
16154           descr_tail = &(*descr_tail)->dw_loc_next;
16155         }
16156     }
16157
16158   /* If there were any non-empty expressions, add padding till the end of
16159      the decl.  */
16160   if (descr != NULL && decl_size != 0)
16161     {
16162       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
16163       if (*descr_tail == NULL)
16164         return NULL;
16165     }
16166   return descr;
16167 }
16168
16169 /* Return the dwarf representation of the location list LOC_LIST of
16170    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
16171    function.  */
16172
16173 static dw_loc_list_ref
16174 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
16175 {
16176   const char *endname, *secname;
16177   rtx varloc;
16178   enum var_init_status initialized;
16179   struct var_loc_node *node;
16180   dw_loc_descr_ref descr;
16181   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
16182   dw_loc_list_ref list = NULL;
16183   dw_loc_list_ref *listp = &list;
16184
16185   /* Now that we know what section we are using for a base,
16186      actually construct the list of locations.
16187      The first location information is what is passed to the
16188      function that creates the location list, and the remaining
16189      locations just get added on to that list.
16190      Note that we only know the start address for a location
16191      (IE location changes), so to build the range, we use
16192      the range [current location start, next location start].
16193      This means we have to special case the last node, and generate
16194      a range of [last location start, end of function label].  */
16195
16196   secname = secname_for_decl (decl);
16197
16198   for (node = loc_list->first; node; node = node->next)
16199     if (GET_CODE (node->loc) == EXPR_LIST
16200         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
16201       {
16202         if (GET_CODE (node->loc) == EXPR_LIST)
16203           {
16204             /* This requires DW_OP_{,bit_}piece, which is not usable
16205                inside DWARF expressions.  */
16206             if (want_address != 2)
16207               continue;
16208             descr = dw_sra_loc_expr (decl, node->loc);
16209             if (descr == NULL)
16210               continue;
16211           }
16212         else
16213           {
16214             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16215             varloc = NOTE_VAR_LOCATION (node->loc);
16216             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
16217           }
16218         if (descr)
16219           {
16220             bool range_across_switch = false;
16221             /* If section switch happens in between node->label
16222                and node->next->label (or end of function) and
16223                we can't emit it as a single entry list,
16224                emit two ranges, first one ending at the end
16225                of first partition and second one starting at the
16226                beginning of second partition.  */
16227             if (node == loc_list->last_before_switch
16228                 && (node != loc_list->first || loc_list->first->next)
16229                 && current_function_decl)
16230               {
16231                 endname = current_fde ()->dw_fde_end;
16232                 range_across_switch = true;
16233               }
16234             /* The variable has a location between NODE->LABEL and
16235                NODE->NEXT->LABEL.  */
16236             else if (node->next)
16237               endname = node->next->label;
16238             /* If the variable has a location at the last label
16239                it keeps its location until the end of function.  */
16240             else if (!current_function_decl)
16241               endname = text_end_label;
16242             else
16243               {
16244                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16245                                              current_function_funcdef_no);
16246                 endname = ggc_strdup (label_id);
16247               }
16248
16249             *listp = new_loc_list (descr, node->label, endname, secname);
16250             if (TREE_CODE (decl) == PARM_DECL
16251                 && node == loc_list->first
16252                 && GET_CODE (node->loc) == NOTE
16253                 && strcmp (node->label, endname) == 0)
16254               (*listp)->force = true;
16255             listp = &(*listp)->dw_loc_next;
16256
16257             if (range_across_switch)
16258               {
16259                 if (GET_CODE (node->loc) == EXPR_LIST)
16260                   descr = dw_sra_loc_expr (decl, node->loc);
16261                 else
16262                   {
16263                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
16264                     varloc = NOTE_VAR_LOCATION (node->loc);
16265                     descr = dw_loc_list_1 (decl, varloc, want_address,
16266                                            initialized);
16267                   }
16268                 gcc_assert (descr);
16269                 /* The variable has a location between NODE->LABEL and
16270                    NODE->NEXT->LABEL.  */
16271                 if (node->next)
16272                   endname = node->next->label;
16273                 else
16274                   endname = current_fde ()->dw_fde_second_end;
16275                 *listp = new_loc_list (descr,
16276                                        current_fde ()->dw_fde_second_begin,
16277                                        endname, secname);
16278                 listp = &(*listp)->dw_loc_next;
16279               }
16280           }
16281       }
16282
16283   /* Try to avoid the overhead of a location list emitting a location
16284      expression instead, but only if we didn't have more than one
16285      location entry in the first place.  If some entries were not
16286      representable, we don't want to pretend a single entry that was
16287      applies to the entire scope in which the variable is
16288      available.  */
16289   if (list && loc_list->first->next)
16290     gen_llsym (list);
16291
16292   return list;
16293 }
16294
16295 /* Return if the loc_list has only single element and thus can be represented
16296    as location description.   */
16297
16298 static bool
16299 single_element_loc_list_p (dw_loc_list_ref list)
16300 {
16301   gcc_assert (!list->dw_loc_next || list->ll_symbol);
16302   return !list->ll_symbol;
16303 }
16304
16305 /* To each location in list LIST add loc descr REF.  */
16306
16307 static void
16308 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
16309 {
16310   dw_loc_descr_ref copy;
16311   add_loc_descr (&list->expr, ref);
16312   list = list->dw_loc_next;
16313   while (list)
16314     {
16315       copy = ggc_alloc_dw_loc_descr_node ();
16316       memcpy (copy, ref, sizeof (dw_loc_descr_node));
16317       add_loc_descr (&list->expr, copy);
16318       while (copy->dw_loc_next)
16319         {
16320           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
16321           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
16322           copy->dw_loc_next = new_copy;
16323           copy = new_copy;
16324         }
16325       list = list->dw_loc_next;
16326     }
16327 }
16328
16329 /* Given two lists RET and LIST
16330    produce location list that is result of adding expression in LIST
16331    to expression in RET on each possition in program.
16332    Might be destructive on both RET and LIST.
16333
16334    TODO: We handle only simple cases of RET or LIST having at most one
16335    element. General case would inolve sorting the lists in program order
16336    and merging them that will need some additional work.
16337    Adding that will improve quality of debug info especially for SRA-ed
16338    structures.  */
16339
16340 static void
16341 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
16342 {
16343   if (!list)
16344     return;
16345   if (!*ret)
16346     {
16347       *ret = list;
16348       return;
16349     }
16350   if (!list->dw_loc_next)
16351     {
16352       add_loc_descr_to_each (*ret, list->expr);
16353       return;
16354     }
16355   if (!(*ret)->dw_loc_next)
16356     {
16357       add_loc_descr_to_each (list, (*ret)->expr);
16358       *ret = list;
16359       return;
16360     }
16361   expansion_failed (NULL_TREE, NULL_RTX,
16362                     "Don't know how to merge two non-trivial"
16363                     " location lists.\n");
16364   *ret = NULL;
16365   return;
16366 }
16367
16368 /* LOC is constant expression.  Try a luck, look it up in constant
16369    pool and return its loc_descr of its address.  */
16370
16371 static dw_loc_descr_ref
16372 cst_pool_loc_descr (tree loc)
16373 {
16374   /* Get an RTL for this, if something has been emitted.  */
16375   rtx rtl = lookup_constant_def (loc);
16376
16377   if (!rtl || !MEM_P (rtl))
16378     {
16379       gcc_assert (!rtl);
16380       return 0;
16381     }
16382   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
16383
16384   /* TODO: We might get more coverage if we was actually delaying expansion
16385      of all expressions till end of compilation when constant pools are fully
16386      populated.  */
16387   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
16388     {
16389       expansion_failed (loc, NULL_RTX,
16390                         "CST value in contant pool but not marked.");
16391       return 0;
16392     }
16393   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
16394                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
16395 }
16396
16397 /* Return dw_loc_list representing address of addr_expr LOC
16398    by looking for innder INDIRECT_REF expression and turing it
16399    into simple arithmetics.  */
16400
16401 static dw_loc_list_ref
16402 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
16403 {
16404   tree obj, offset;
16405   HOST_WIDE_INT bitsize, bitpos, bytepos;
16406   enum machine_mode mode;
16407   int volatilep;
16408   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
16409   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16410
16411   obj = get_inner_reference (TREE_OPERAND (loc, 0),
16412                              &bitsize, &bitpos, &offset, &mode,
16413                              &unsignedp, &volatilep, false);
16414   STRIP_NOPS (obj);
16415   if (bitpos % BITS_PER_UNIT)
16416     {
16417       expansion_failed (loc, NULL_RTX, "bitfield access");
16418       return 0;
16419     }
16420   if (!INDIRECT_REF_P (obj))
16421     {
16422       expansion_failed (obj,
16423                         NULL_RTX, "no indirect ref in inner refrence");
16424       return 0;
16425     }
16426   if (!offset && !bitpos)
16427     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
16428   else if (toplev
16429            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
16430            && (dwarf_version >= 4 || !dwarf_strict))
16431     {
16432       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
16433       if (!list_ret)
16434         return 0;
16435       if (offset)
16436         {
16437           /* Variable offset.  */
16438           list_ret1 = loc_list_from_tree (offset, 0);
16439           if (list_ret1 == 0)
16440             return 0;
16441           add_loc_list (&list_ret, list_ret1);
16442           if (!list_ret)
16443             return 0;
16444           add_loc_descr_to_each (list_ret,
16445                                  new_loc_descr (DW_OP_plus, 0, 0));
16446         }
16447       bytepos = bitpos / BITS_PER_UNIT;
16448       if (bytepos > 0)
16449         add_loc_descr_to_each (list_ret,
16450                                new_loc_descr (DW_OP_plus_uconst,
16451                                               bytepos, 0));
16452       else if (bytepos < 0)
16453         loc_list_plus_const (list_ret, bytepos);
16454       add_loc_descr_to_each (list_ret,
16455                              new_loc_descr (DW_OP_stack_value, 0, 0));
16456     }
16457   return list_ret;
16458 }
16459
16460
16461 /* Generate Dwarf location list representing LOC.
16462    If WANT_ADDRESS is false, expression computing LOC will be computed
16463    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
16464    if WANT_ADDRESS is 2, expression computing address useable in location
16465      will be returned (i.e. DW_OP_reg can be used
16466      to refer to register values).  */
16467
16468 static dw_loc_list_ref
16469 loc_list_from_tree (tree loc, int want_address)
16470 {
16471   dw_loc_descr_ref ret = NULL, ret1 = NULL;
16472   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
16473   int have_address = 0;
16474   enum dwarf_location_atom op;
16475
16476   /* ??? Most of the time we do not take proper care for sign/zero
16477      extending the values properly.  Hopefully this won't be a real
16478      problem...  */
16479
16480   switch (TREE_CODE (loc))
16481     {
16482     case ERROR_MARK:
16483       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
16484       return 0;
16485
16486     case PLACEHOLDER_EXPR:
16487       /* This case involves extracting fields from an object to determine the
16488          position of other fields.  We don't try to encode this here.  The
16489          only user of this is Ada, which encodes the needed information using
16490          the names of types.  */
16491       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
16492       return 0;
16493
16494     case CALL_EXPR:
16495       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
16496       /* There are no opcodes for these operations.  */
16497       return 0;
16498
16499     case PREINCREMENT_EXPR:
16500     case PREDECREMENT_EXPR:
16501     case POSTINCREMENT_EXPR:
16502     case POSTDECREMENT_EXPR:
16503       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
16504       /* There are no opcodes for these operations.  */
16505       return 0;
16506
16507     case ADDR_EXPR:
16508       /* If we already want an address, see if there is INDIRECT_REF inside
16509          e.g. for &this->field.  */
16510       if (want_address)
16511         {
16512           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
16513                        (loc, want_address == 2);
16514           if (list_ret)
16515             have_address = 1;
16516           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
16517                    && (ret = cst_pool_loc_descr (loc)))
16518             have_address = 1;
16519         }
16520         /* Otherwise, process the argument and look for the address.  */
16521       if (!list_ret && !ret)
16522         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
16523       else
16524         {
16525           if (want_address)
16526             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
16527           return NULL;
16528         }
16529       break;
16530
16531     case VAR_DECL:
16532       if (DECL_THREAD_LOCAL_P (loc))
16533         {
16534           rtx rtl;
16535           enum dwarf_location_atom first_op;
16536           enum dwarf_location_atom second_op;
16537           bool dtprel = false;
16538
16539           if (targetm.have_tls)
16540             {
16541               /* If this is not defined, we have no way to emit the
16542                  data.  */
16543               if (!targetm.asm_out.output_dwarf_dtprel)
16544                 return 0;
16545
16546                /* The way DW_OP_GNU_push_tls_address is specified, we
16547                   can only look up addresses of objects in the current
16548                   module.  We used DW_OP_addr as first op, but that's
16549                   wrong, because DW_OP_addr is relocated by the debug
16550                   info consumer, while DW_OP_GNU_push_tls_address
16551                   operand shouldn't be.  */
16552               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
16553                 return 0;
16554               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
16555               dtprel = true;
16556               second_op = DW_OP_GNU_push_tls_address;
16557             }
16558           else
16559             {
16560               if (!targetm.emutls.debug_form_tls_address
16561                   || !(dwarf_version >= 3 || !dwarf_strict))
16562                 return 0;
16563               /* We stuffed the control variable into the DECL_VALUE_EXPR
16564                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
16565                  no longer appear in gimple code.  We used the control
16566                  variable in specific so that we could pick it up here.  */
16567               loc = DECL_VALUE_EXPR (loc);
16568               first_op = DW_OP_addr;
16569               second_op = DW_OP_form_tls_address;
16570             }
16571
16572           rtl = rtl_for_decl_location (loc);
16573           if (rtl == NULL_RTX)
16574             return 0;
16575
16576           if (!MEM_P (rtl))
16577             return 0;
16578           rtl = XEXP (rtl, 0);
16579           if (! CONSTANT_P (rtl))
16580             return 0;
16581
16582           ret = new_loc_descr (first_op, 0, 0);
16583           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
16584           ret->dw_loc_oprnd1.v.val_addr = rtl;
16585           ret->dtprel = dtprel;
16586
16587           ret1 = new_loc_descr (second_op, 0, 0);
16588           add_loc_descr (&ret, ret1);
16589
16590           have_address = 1;
16591           break;
16592         }
16593       /* FALLTHRU */
16594
16595     case PARM_DECL:
16596     case RESULT_DECL:
16597       if (DECL_HAS_VALUE_EXPR_P (loc))
16598         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
16599                                    want_address);
16600       /* FALLTHRU */
16601
16602     case FUNCTION_DECL:
16603       {
16604         rtx rtl;
16605         var_loc_list *loc_list = lookup_decl_loc (loc);
16606
16607         if (loc_list && loc_list->first)
16608           {
16609             list_ret = dw_loc_list (loc_list, loc, want_address);
16610             have_address = want_address != 0;
16611             break;
16612           }
16613         rtl = rtl_for_decl_location (loc);
16614         if (rtl == NULL_RTX)
16615           {
16616             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
16617             return 0;
16618           }
16619         else if (CONST_INT_P (rtl))
16620           {
16621             HOST_WIDE_INT val = INTVAL (rtl);
16622             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16623               val &= GET_MODE_MASK (DECL_MODE (loc));
16624             ret = int_loc_descriptor (val);
16625           }
16626         else if (GET_CODE (rtl) == CONST_STRING)
16627           {
16628             expansion_failed (loc, NULL_RTX, "CONST_STRING");
16629             return 0;
16630           }
16631         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16632           {
16633             ret = new_loc_descr (DW_OP_addr, 0, 0);
16634             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
16635             ret->dw_loc_oprnd1.v.val_addr = rtl;
16636           }
16637         else
16638           {
16639             enum machine_mode mode, mem_mode;
16640
16641             /* Certain constructs can only be represented at top-level.  */
16642             if (want_address == 2)
16643               {
16644                 ret = loc_descriptor (rtl, VOIDmode,
16645                                       VAR_INIT_STATUS_INITIALIZED);
16646                 have_address = 1;
16647               }
16648             else
16649               {
16650                 mode = GET_MODE (rtl);
16651                 mem_mode = VOIDmode;
16652                 if (MEM_P (rtl))
16653                   {
16654                     mem_mode = mode;
16655                     mode = get_address_mode (rtl);
16656                     rtl = XEXP (rtl, 0);
16657                     have_address = 1;
16658                   }
16659                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16660                                           VAR_INIT_STATUS_INITIALIZED);
16661               }
16662             if (!ret)
16663               expansion_failed (loc, rtl,
16664                                 "failed to produce loc descriptor for rtl");
16665           }
16666       }
16667       break;
16668
16669     case MEM_REF:
16670       /* ??? FIXME.  */
16671       if (!integer_zerop (TREE_OPERAND (loc, 1)))
16672         return 0;
16673       /* Fallthru.  */
16674     case INDIRECT_REF:
16675       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16676       have_address = 1;
16677       break;
16678
16679     case COMPOUND_EXPR:
16680       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
16681
16682     CASE_CONVERT:
16683     case VIEW_CONVERT_EXPR:
16684     case SAVE_EXPR:
16685     case MODIFY_EXPR:
16686       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
16687
16688     case COMPONENT_REF:
16689     case BIT_FIELD_REF:
16690     case ARRAY_REF:
16691     case ARRAY_RANGE_REF:
16692     case REALPART_EXPR:
16693     case IMAGPART_EXPR:
16694       {
16695         tree obj, offset;
16696         HOST_WIDE_INT bitsize, bitpos, bytepos;
16697         enum machine_mode mode;
16698         int volatilep;
16699         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
16700
16701         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16702                                    &unsignedp, &volatilep, false);
16703
16704         gcc_assert (obj != loc);
16705
16706         list_ret = loc_list_from_tree (obj,
16707                                        want_address == 2
16708                                        && !bitpos && !offset ? 2 : 1);
16709         /* TODO: We can extract value of the small expression via shifting even
16710            for nonzero bitpos.  */
16711         if (list_ret == 0)
16712           return 0;
16713         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16714           {
16715             expansion_failed (loc, NULL_RTX,
16716                               "bitfield access");
16717             return 0;
16718           }
16719
16720         if (offset != NULL_TREE)
16721           {
16722             /* Variable offset.  */
16723             list_ret1 = loc_list_from_tree (offset, 0);
16724             if (list_ret1 == 0)
16725               return 0;
16726             add_loc_list (&list_ret, list_ret1);
16727             if (!list_ret)
16728               return 0;
16729             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16730           }
16731
16732         bytepos = bitpos / BITS_PER_UNIT;
16733         if (bytepos > 0)
16734           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16735         else if (bytepos < 0)
16736           loc_list_plus_const (list_ret, bytepos);
16737
16738         have_address = 1;
16739         break;
16740       }
16741
16742     case INTEGER_CST:
16743       if ((want_address || !host_integerp (loc, 0))
16744           && (ret = cst_pool_loc_descr (loc)))
16745         have_address = 1;
16746       else if (want_address == 2
16747                && host_integerp (loc, 0)
16748                && (ret = address_of_int_loc_descriptor
16749                            (int_size_in_bytes (TREE_TYPE (loc)),
16750                             tree_low_cst (loc, 0))))
16751         have_address = 1;
16752       else if (host_integerp (loc, 0))
16753         ret = int_loc_descriptor (tree_low_cst (loc, 0));
16754       else
16755         {
16756           expansion_failed (loc, NULL_RTX,
16757                             "Integer operand is not host integer");
16758           return 0;
16759         }
16760       break;
16761
16762     case CONSTRUCTOR:
16763     case REAL_CST:
16764     case STRING_CST:
16765     case COMPLEX_CST:
16766       if ((ret = cst_pool_loc_descr (loc)))
16767         have_address = 1;
16768       else
16769       /* We can construct small constants here using int_loc_descriptor.  */
16770         expansion_failed (loc, NULL_RTX,
16771                           "constructor or constant not in constant pool");
16772       break;
16773
16774     case TRUTH_AND_EXPR:
16775     case TRUTH_ANDIF_EXPR:
16776     case BIT_AND_EXPR:
16777       op = DW_OP_and;
16778       goto do_binop;
16779
16780     case TRUTH_XOR_EXPR:
16781     case BIT_XOR_EXPR:
16782       op = DW_OP_xor;
16783       goto do_binop;
16784
16785     case TRUTH_OR_EXPR:
16786     case TRUTH_ORIF_EXPR:
16787     case BIT_IOR_EXPR:
16788       op = DW_OP_or;
16789       goto do_binop;
16790
16791     case FLOOR_DIV_EXPR:
16792     case CEIL_DIV_EXPR:
16793     case ROUND_DIV_EXPR:
16794     case TRUNC_DIV_EXPR:
16795       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16796         return 0;
16797       op = DW_OP_div;
16798       goto do_binop;
16799
16800     case MINUS_EXPR:
16801       op = DW_OP_minus;
16802       goto do_binop;
16803
16804     case FLOOR_MOD_EXPR:
16805     case CEIL_MOD_EXPR:
16806     case ROUND_MOD_EXPR:
16807     case TRUNC_MOD_EXPR:
16808       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16809         {
16810           op = DW_OP_mod;
16811           goto do_binop;
16812         }
16813       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16814       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16815       if (list_ret == 0 || list_ret1 == 0)
16816         return 0;
16817
16818       add_loc_list (&list_ret, list_ret1);
16819       if (list_ret == 0)
16820         return 0;
16821       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16822       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16823       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16824       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16825       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16826       break;
16827
16828     case MULT_EXPR:
16829       op = DW_OP_mul;
16830       goto do_binop;
16831
16832     case LSHIFT_EXPR:
16833       op = DW_OP_shl;
16834       goto do_binop;
16835
16836     case RSHIFT_EXPR:
16837       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16838       goto do_binop;
16839
16840     case POINTER_PLUS_EXPR:
16841     case PLUS_EXPR:
16842       if (host_integerp (TREE_OPERAND (loc, 1), 0))
16843         {
16844           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16845           if (list_ret == 0)
16846             return 0;
16847
16848           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
16849           break;
16850         }
16851
16852       op = DW_OP_plus;
16853       goto do_binop;
16854
16855     case LE_EXPR:
16856       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16857         return 0;
16858
16859       op = DW_OP_le;
16860       goto do_binop;
16861
16862     case GE_EXPR:
16863       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16864         return 0;
16865
16866       op = DW_OP_ge;
16867       goto do_binop;
16868
16869     case LT_EXPR:
16870       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16871         return 0;
16872
16873       op = DW_OP_lt;
16874       goto do_binop;
16875
16876     case GT_EXPR:
16877       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16878         return 0;
16879
16880       op = DW_OP_gt;
16881       goto do_binop;
16882
16883     case EQ_EXPR:
16884       op = DW_OP_eq;
16885       goto do_binop;
16886
16887     case NE_EXPR:
16888       op = DW_OP_ne;
16889       goto do_binop;
16890
16891     do_binop:
16892       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16893       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16894       if (list_ret == 0 || list_ret1 == 0)
16895         return 0;
16896
16897       add_loc_list (&list_ret, list_ret1);
16898       if (list_ret == 0)
16899         return 0;
16900       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16901       break;
16902
16903     case TRUTH_NOT_EXPR:
16904     case BIT_NOT_EXPR:
16905       op = DW_OP_not;
16906       goto do_unop;
16907
16908     case ABS_EXPR:
16909       op = DW_OP_abs;
16910       goto do_unop;
16911
16912     case NEGATE_EXPR:
16913       op = DW_OP_neg;
16914       goto do_unop;
16915
16916     do_unop:
16917       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16918       if (list_ret == 0)
16919         return 0;
16920
16921       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16922       break;
16923
16924     case MIN_EXPR:
16925     case MAX_EXPR:
16926       {
16927         const enum tree_code code =
16928           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16929
16930         loc = build3 (COND_EXPR, TREE_TYPE (loc),
16931                       build2 (code, integer_type_node,
16932                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16933                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16934       }
16935
16936       /* ... fall through ...  */
16937
16938     case COND_EXPR:
16939       {
16940         dw_loc_descr_ref lhs
16941           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
16942         dw_loc_list_ref rhs
16943           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
16944         dw_loc_descr_ref bra_node, jump_node, tmp;
16945
16946         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16947         if (list_ret == 0 || lhs == 0 || rhs == 0)
16948           return 0;
16949
16950         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16951         add_loc_descr_to_each (list_ret, bra_node);
16952
16953         add_loc_list (&list_ret, rhs);
16954         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16955         add_loc_descr_to_each (list_ret, jump_node);
16956
16957         add_loc_descr_to_each (list_ret, lhs);
16958         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16959         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16960
16961         /* ??? Need a node to point the skip at.  Use a nop.  */
16962         tmp = new_loc_descr (DW_OP_nop, 0, 0);
16963         add_loc_descr_to_each (list_ret, tmp);
16964         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16965         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16966       }
16967       break;
16968
16969     case FIX_TRUNC_EXPR:
16970       return 0;
16971
16972     default:
16973       /* Leave front-end specific codes as simply unknown.  This comes
16974          up, for instance, with the C STMT_EXPR.  */
16975       if ((unsigned int) TREE_CODE (loc)
16976           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16977         {
16978           expansion_failed (loc, NULL_RTX,
16979                             "language specific tree node");
16980           return 0;
16981         }
16982
16983 #ifdef ENABLE_CHECKING
16984       /* Otherwise this is a generic code; we should just lists all of
16985          these explicitly.  We forgot one.  */
16986       gcc_unreachable ();
16987 #else
16988       /* In a release build, we want to degrade gracefully: better to
16989          generate incomplete debugging information than to crash.  */
16990       return NULL;
16991 #endif
16992     }
16993
16994   if (!ret && !list_ret)
16995     return 0;
16996
16997   if (want_address == 2 && !have_address
16998       && (dwarf_version >= 4 || !dwarf_strict))
16999     {
17000       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
17001         {
17002           expansion_failed (loc, NULL_RTX,
17003                             "DWARF address size mismatch");
17004           return 0;
17005         }
17006       if (ret)
17007         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
17008       else
17009         add_loc_descr_to_each (list_ret,
17010                                new_loc_descr (DW_OP_stack_value, 0, 0));
17011       have_address = 1;
17012     }
17013   /* Show if we can't fill the request for an address.  */
17014   if (want_address && !have_address)
17015     {
17016       expansion_failed (loc, NULL_RTX,
17017                         "Want address and only have value");
17018       return 0;
17019     }
17020
17021   gcc_assert (!ret || !list_ret);
17022
17023   /* If we've got an address and don't want one, dereference.  */
17024   if (!want_address && have_address)
17025     {
17026       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
17027
17028       if (size > DWARF2_ADDR_SIZE || size == -1)
17029         {
17030           expansion_failed (loc, NULL_RTX,
17031                             "DWARF address size mismatch");
17032           return 0;
17033         }
17034       else if (size == DWARF2_ADDR_SIZE)
17035         op = DW_OP_deref;
17036       else
17037         op = DW_OP_deref_size;
17038
17039       if (ret)
17040         add_loc_descr (&ret, new_loc_descr (op, size, 0));
17041       else
17042         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
17043     }
17044   if (ret)
17045     list_ret = new_loc_list (ret, NULL, NULL, NULL);
17046
17047   return list_ret;
17048 }
17049
17050 /* Same as above but return only single location expression.  */
17051 static dw_loc_descr_ref
17052 loc_descriptor_from_tree (tree loc, int want_address)
17053 {
17054   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
17055   if (!ret)
17056     return NULL;
17057   if (ret->dw_loc_next)
17058     {
17059       expansion_failed (loc, NULL_RTX,
17060                         "Location list where only loc descriptor needed");
17061       return NULL;
17062     }
17063   return ret->expr;
17064 }
17065
17066 /* Given a value, round it up to the lowest multiple of `boundary'
17067    which is not less than the value itself.  */
17068
17069 static inline HOST_WIDE_INT
17070 ceiling (HOST_WIDE_INT value, unsigned int boundary)
17071 {
17072   return (((value + boundary - 1) / boundary) * boundary);
17073 }
17074
17075 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
17076    pointer to the declared type for the relevant field variable, or return
17077    `integer_type_node' if the given node turns out to be an
17078    ERROR_MARK node.  */
17079
17080 static inline tree
17081 field_type (const_tree decl)
17082 {
17083   tree type;
17084
17085   if (TREE_CODE (decl) == ERROR_MARK)
17086     return integer_type_node;
17087
17088   type = DECL_BIT_FIELD_TYPE (decl);
17089   if (type == NULL_TREE)
17090     type = TREE_TYPE (decl);
17091
17092   return type;
17093 }
17094
17095 /* Given a pointer to a tree node, return the alignment in bits for
17096    it, or else return BITS_PER_WORD if the node actually turns out to
17097    be an ERROR_MARK node.  */
17098
17099 static inline unsigned
17100 simple_type_align_in_bits (const_tree type)
17101 {
17102   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
17103 }
17104
17105 static inline unsigned
17106 simple_decl_align_in_bits (const_tree decl)
17107 {
17108   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
17109 }
17110
17111 /* Return the result of rounding T up to ALIGN.  */
17112
17113 static inline double_int
17114 round_up_to_align (double_int t, unsigned int align)
17115 {
17116   double_int alignd = uhwi_to_double_int (align);
17117   t = double_int_add (t, alignd);
17118   t = double_int_add (t, double_int_minus_one);
17119   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
17120   t = double_int_mul (t, alignd);
17121   return t;
17122 }
17123
17124 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
17125    lowest addressed byte of the "containing object" for the given FIELD_DECL,
17126    or return 0 if we are unable to determine what that offset is, either
17127    because the argument turns out to be a pointer to an ERROR_MARK node, or
17128    because the offset is actually variable.  (We can't handle the latter case
17129    just yet).  */
17130
17131 static HOST_WIDE_INT
17132 field_byte_offset (const_tree decl)
17133 {
17134   double_int object_offset_in_bits;
17135   double_int object_offset_in_bytes;
17136   double_int bitpos_int;
17137
17138   if (TREE_CODE (decl) == ERROR_MARK)
17139     return 0;
17140
17141   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
17142
17143   /* We cannot yet cope with fields whose positions are variable, so
17144      for now, when we see such things, we simply return 0.  Someday, we may
17145      be able to handle such cases, but it will be damn difficult.  */
17146   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
17147     return 0;
17148
17149   bitpos_int = tree_to_double_int (bit_position (decl));
17150
17151 #ifdef PCC_BITFIELD_TYPE_MATTERS
17152   if (PCC_BITFIELD_TYPE_MATTERS)
17153     {
17154       tree type;
17155       tree field_size_tree;
17156       double_int deepest_bitpos;
17157       double_int field_size_in_bits;
17158       unsigned int type_align_in_bits;
17159       unsigned int decl_align_in_bits;
17160       double_int type_size_in_bits;
17161
17162       type = field_type (decl);
17163       type_size_in_bits = double_int_type_size_in_bits (type);
17164       type_align_in_bits = simple_type_align_in_bits (type);
17165
17166       field_size_tree = DECL_SIZE (decl);
17167
17168       /* The size could be unspecified if there was an error, or for
17169          a flexible array member.  */
17170       if (!field_size_tree)
17171         field_size_tree = bitsize_zero_node;
17172
17173       /* If the size of the field is not constant, use the type size.  */
17174       if (TREE_CODE (field_size_tree) == INTEGER_CST)
17175         field_size_in_bits = tree_to_double_int (field_size_tree);
17176       else
17177         field_size_in_bits = type_size_in_bits;
17178
17179       decl_align_in_bits = simple_decl_align_in_bits (decl);
17180
17181       /* The GCC front-end doesn't make any attempt to keep track of the
17182          starting bit offset (relative to the start of the containing
17183          structure type) of the hypothetical "containing object" for a
17184          bit-field.  Thus, when computing the byte offset value for the
17185          start of the "containing object" of a bit-field, we must deduce
17186          this information on our own. This can be rather tricky to do in
17187          some cases.  For example, handling the following structure type
17188          definition when compiling for an i386/i486 target (which only
17189          aligns long long's to 32-bit boundaries) can be very tricky:
17190
17191          struct S { int field1; long long field2:31; };
17192
17193          Fortunately, there is a simple rule-of-thumb which can be used
17194          in such cases.  When compiling for an i386/i486, GCC will
17195          allocate 8 bytes for the structure shown above.  It decides to
17196          do this based upon one simple rule for bit-field allocation.
17197          GCC allocates each "containing object" for each bit-field at
17198          the first (i.e. lowest addressed) legitimate alignment boundary
17199          (based upon the required minimum alignment for the declared
17200          type of the field) which it can possibly use, subject to the
17201          condition that there is still enough available space remaining
17202          in the containing object (when allocated at the selected point)
17203          to fully accommodate all of the bits of the bit-field itself.
17204
17205          This simple rule makes it obvious why GCC allocates 8 bytes for
17206          each object of the structure type shown above.  When looking
17207          for a place to allocate the "containing object" for `field2',
17208          the compiler simply tries to allocate a 64-bit "containing
17209          object" at each successive 32-bit boundary (starting at zero)
17210          until it finds a place to allocate that 64- bit field such that
17211          at least 31 contiguous (and previously unallocated) bits remain
17212          within that selected 64 bit field.  (As it turns out, for the
17213          example above, the compiler finds it is OK to allocate the
17214          "containing object" 64-bit field at bit-offset zero within the
17215          structure type.)
17216
17217          Here we attempt to work backwards from the limited set of facts
17218          we're given, and we try to deduce from those facts, where GCC
17219          must have believed that the containing object started (within
17220          the structure type). The value we deduce is then used (by the
17221          callers of this routine) to generate DW_AT_location and
17222          DW_AT_bit_offset attributes for fields (both bit-fields and, in
17223          the case of DW_AT_location, regular fields as well).  */
17224
17225       /* Figure out the bit-distance from the start of the structure to
17226          the "deepest" bit of the bit-field.  */
17227       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
17228
17229       /* This is the tricky part.  Use some fancy footwork to deduce
17230          where the lowest addressed bit of the containing object must
17231          be.  */
17232       object_offset_in_bits
17233         = double_int_sub (deepest_bitpos, type_size_in_bits);
17234
17235       /* Round up to type_align by default.  This works best for
17236          bitfields.  */
17237       object_offset_in_bits
17238         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
17239
17240       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
17241         {
17242           object_offset_in_bits
17243             = double_int_sub (deepest_bitpos, type_size_in_bits);
17244
17245           /* Round up to decl_align instead.  */
17246           object_offset_in_bits
17247             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
17248         }
17249     }
17250   else
17251 #endif /* PCC_BITFIELD_TYPE_MATTERS */
17252     object_offset_in_bits = bitpos_int;
17253
17254   object_offset_in_bytes
17255     = double_int_div (object_offset_in_bits,
17256                       uhwi_to_double_int (BITS_PER_UNIT), true,
17257                       TRUNC_DIV_EXPR);
17258   return double_int_to_shwi (object_offset_in_bytes);
17259 }
17260 \f
17261 /* The following routines define various Dwarf attributes and any data
17262    associated with them.  */
17263
17264 /* Add a location description attribute value to a DIE.
17265
17266    This emits location attributes suitable for whole variables and
17267    whole parameters.  Note that the location attributes for struct fields are
17268    generated by the routine `data_member_location_attribute' below.  */
17269
17270 static inline void
17271 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
17272                              dw_loc_list_ref descr)
17273 {
17274   if (descr == 0)
17275     return;
17276   if (single_element_loc_list_p (descr))
17277     add_AT_loc (die, attr_kind, descr->expr);
17278   else
17279     add_AT_loc_list (die, attr_kind, descr);
17280 }
17281
17282 /* Add DW_AT_accessibility attribute to DIE if needed.  */
17283
17284 static void
17285 add_accessibility_attribute (dw_die_ref die, tree decl)
17286 {
17287   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
17288      children, otherwise the default is DW_ACCESS_public.  In DWARF2
17289      the default has always been DW_ACCESS_public.  */
17290   if (TREE_PROTECTED (decl))
17291     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
17292   else if (TREE_PRIVATE (decl))
17293     {
17294       if (dwarf_version == 2
17295           || die->die_parent == NULL
17296           || die->die_parent->die_tag != DW_TAG_class_type)
17297         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
17298     }
17299   else if (dwarf_version > 2
17300            && die->die_parent
17301            && die->die_parent->die_tag == DW_TAG_class_type)
17302     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
17303 }
17304
17305 /* Attach the specialized form of location attribute used for data members of
17306    struct and union types.  In the special case of a FIELD_DECL node which
17307    represents a bit-field, the "offset" part of this special location
17308    descriptor must indicate the distance in bytes from the lowest-addressed
17309    byte of the containing struct or union type to the lowest-addressed byte of
17310    the "containing object" for the bit-field.  (See the `field_byte_offset'
17311    function above).
17312
17313    For any given bit-field, the "containing object" is a hypothetical object
17314    (of some integral or enum type) within which the given bit-field lives.  The
17315    type of this hypothetical "containing object" is always the same as the
17316    declared type of the individual bit-field itself (for GCC anyway... the
17317    DWARF spec doesn't actually mandate this).  Note that it is the size (in
17318    bytes) of the hypothetical "containing object" which will be given in the
17319    DW_AT_byte_size attribute for this bit-field.  (See the
17320    `byte_size_attribute' function below.)  It is also used when calculating the
17321    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
17322    function below.)  */
17323
17324 static void
17325 add_data_member_location_attribute (dw_die_ref die, tree decl)
17326 {
17327   HOST_WIDE_INT offset;
17328   dw_loc_descr_ref loc_descr = 0;
17329
17330   if (TREE_CODE (decl) == TREE_BINFO)
17331     {
17332       /* We're working on the TAG_inheritance for a base class.  */
17333       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
17334         {
17335           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
17336              aren't at a fixed offset from all (sub)objects of the same
17337              type.  We need to extract the appropriate offset from our
17338              vtable.  The following dwarf expression means
17339
17340                BaseAddr = ObAddr + *((*ObAddr) - Offset)
17341
17342              This is specific to the V3 ABI, of course.  */
17343
17344           dw_loc_descr_ref tmp;
17345
17346           /* Make a copy of the object address.  */
17347           tmp = new_loc_descr (DW_OP_dup, 0, 0);
17348           add_loc_descr (&loc_descr, tmp);
17349
17350           /* Extract the vtable address.  */
17351           tmp = new_loc_descr (DW_OP_deref, 0, 0);
17352           add_loc_descr (&loc_descr, tmp);
17353
17354           /* Calculate the address of the offset.  */
17355           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
17356           gcc_assert (offset < 0);
17357
17358           tmp = int_loc_descriptor (-offset);
17359           add_loc_descr (&loc_descr, tmp);
17360           tmp = new_loc_descr (DW_OP_minus, 0, 0);
17361           add_loc_descr (&loc_descr, tmp);
17362
17363           /* Extract the offset.  */
17364           tmp = new_loc_descr (DW_OP_deref, 0, 0);
17365           add_loc_descr (&loc_descr, tmp);
17366
17367           /* Add it to the object address.  */
17368           tmp = new_loc_descr (DW_OP_plus, 0, 0);
17369           add_loc_descr (&loc_descr, tmp);
17370         }
17371       else
17372         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
17373     }
17374   else
17375     offset = field_byte_offset (decl);
17376
17377   if (! loc_descr)
17378     {
17379       if (dwarf_version > 2)
17380         {
17381           /* Don't need to output a location expression, just the constant. */
17382           if (offset < 0)
17383             add_AT_int (die, DW_AT_data_member_location, offset);
17384           else
17385             add_AT_unsigned (die, DW_AT_data_member_location, offset);
17386           return;
17387         }
17388       else
17389         {
17390           enum dwarf_location_atom op;
17391
17392           /* The DWARF2 standard says that we should assume that the structure
17393              address is already on the stack, so we can specify a structure
17394              field address by using DW_OP_plus_uconst.  */
17395
17396 #ifdef MIPS_DEBUGGING_INFO
17397           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
17398              operator correctly.  It works only if we leave the offset on the
17399              stack.  */
17400           op = DW_OP_constu;
17401 #else
17402           op = DW_OP_plus_uconst;
17403 #endif
17404
17405           loc_descr = new_loc_descr (op, offset, 0);
17406         }
17407     }
17408
17409   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
17410 }
17411
17412 /* Writes integer values to dw_vec_const array.  */
17413
17414 static void
17415 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
17416 {
17417   while (size != 0)
17418     {
17419       *dest++ = val & 0xff;
17420       val >>= 8;
17421       --size;
17422     }
17423 }
17424
17425 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
17426
17427 static HOST_WIDE_INT
17428 extract_int (const unsigned char *src, unsigned int size)
17429 {
17430   HOST_WIDE_INT val = 0;
17431
17432   src += size;
17433   while (size != 0)
17434     {
17435       val <<= 8;
17436       val |= *--src & 0xff;
17437       --size;
17438     }
17439   return val;
17440 }
17441
17442 /* Writes double_int values to dw_vec_const array.  */
17443
17444 static void
17445 insert_double (double_int val, unsigned char *dest)
17446 {
17447   unsigned char *p0 = dest;
17448   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
17449
17450   if (WORDS_BIG_ENDIAN)
17451     {
17452       p0 = p1;
17453       p1 = dest;
17454     }
17455
17456   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
17457   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
17458 }
17459
17460 /* Writes floating point values to dw_vec_const array.  */
17461
17462 static void
17463 insert_float (const_rtx rtl, unsigned char *array)
17464 {
17465   REAL_VALUE_TYPE rv;
17466   long val[4];
17467   int i;
17468
17469   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
17470   real_to_target (val, &rv, GET_MODE (rtl));
17471
17472   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
17473   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
17474     {
17475       insert_int (val[i], 4, array);
17476       array += 4;
17477     }
17478 }
17479
17480 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
17481    does not have a "location" either in memory or in a register.  These
17482    things can arise in GNU C when a constant is passed as an actual parameter
17483    to an inlined function.  They can also arise in C++ where declared
17484    constants do not necessarily get memory "homes".  */
17485
17486 static bool
17487 add_const_value_attribute (dw_die_ref die, rtx rtl)
17488 {
17489   switch (GET_CODE (rtl))
17490     {
17491     case CONST_INT:
17492       {
17493         HOST_WIDE_INT val = INTVAL (rtl);
17494
17495         if (val < 0)
17496           add_AT_int (die, DW_AT_const_value, val);
17497         else
17498           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
17499       }
17500       return true;
17501
17502     case CONST_DOUBLE:
17503       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
17504          floating-point constant.  A CONST_DOUBLE is used whenever the
17505          constant requires more than one word in order to be adequately
17506          represented.  */
17507       {
17508         enum machine_mode mode = GET_MODE (rtl);
17509
17510         if (SCALAR_FLOAT_MODE_P (mode))
17511           {
17512             unsigned int length = GET_MODE_SIZE (mode);
17513             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
17514
17515             insert_float (rtl, array);
17516             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
17517           }
17518         else
17519           add_AT_double (die, DW_AT_const_value,
17520                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
17521       }
17522       return true;
17523
17524     case CONST_VECTOR:
17525       {
17526         enum machine_mode mode = GET_MODE (rtl);
17527         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
17528         unsigned int length = CONST_VECTOR_NUNITS (rtl);
17529         unsigned char *array = (unsigned char *) ggc_alloc_atomic
17530           (length * elt_size);
17531         unsigned int i;
17532         unsigned char *p;
17533
17534         switch (GET_MODE_CLASS (mode))
17535           {
17536           case MODE_VECTOR_INT:
17537             for (i = 0, p = array; i < length; i++, p += elt_size)
17538               {
17539                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17540                 double_int val = rtx_to_double_int (elt);
17541
17542                 if (elt_size <= sizeof (HOST_WIDE_INT))
17543                   insert_int (double_int_to_shwi (val), elt_size, p);
17544                 else
17545                   {
17546                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
17547                     insert_double (val, p);
17548                   }
17549               }
17550             break;
17551
17552           case MODE_VECTOR_FLOAT:
17553             for (i = 0, p = array; i < length; i++, p += elt_size)
17554               {
17555                 rtx elt = CONST_VECTOR_ELT (rtl, i);
17556                 insert_float (elt, p);
17557               }
17558             break;
17559
17560           default:
17561             gcc_unreachable ();
17562           }
17563
17564         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
17565       }
17566       return true;
17567
17568     case CONST_STRING:
17569       if (dwarf_version >= 4 || !dwarf_strict)
17570         {
17571           dw_loc_descr_ref loc_result;
17572           resolve_one_addr (&rtl, NULL);
17573         rtl_addr:
17574           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
17575           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
17576           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
17577           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
17578           add_AT_loc (die, DW_AT_location, loc_result);
17579           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
17580           return true;
17581         }
17582       return false;
17583
17584     case CONST:
17585       if (CONSTANT_P (XEXP (rtl, 0)))
17586         return add_const_value_attribute (die, XEXP (rtl, 0));
17587       /* FALLTHROUGH */
17588     case SYMBOL_REF:
17589       if (!const_ok_for_output (rtl))
17590         return false;
17591     case LABEL_REF:
17592       if (dwarf_version >= 4 || !dwarf_strict)
17593         goto rtl_addr;
17594       return false;
17595
17596     case PLUS:
17597       /* In cases where an inlined instance of an inline function is passed
17598          the address of an `auto' variable (which is local to the caller) we
17599          can get a situation where the DECL_RTL of the artificial local
17600          variable (for the inlining) which acts as a stand-in for the
17601          corresponding formal parameter (of the inline function) will look
17602          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
17603          exactly a compile-time constant expression, but it isn't the address
17604          of the (artificial) local variable either.  Rather, it represents the
17605          *value* which the artificial local variable always has during its
17606          lifetime.  We currently have no way to represent such quasi-constant
17607          values in Dwarf, so for now we just punt and generate nothing.  */
17608       return false;
17609
17610     case HIGH:
17611     case CONST_FIXED:
17612       return false;
17613
17614     case MEM:
17615       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
17616           && MEM_READONLY_P (rtl)
17617           && GET_MODE (rtl) == BLKmode)
17618         {
17619           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
17620           return true;
17621         }
17622       return false;
17623
17624     default:
17625       /* No other kinds of rtx should be possible here.  */
17626       gcc_unreachable ();
17627     }
17628   return false;
17629 }
17630
17631 /* Determine whether the evaluation of EXPR references any variables
17632    or functions which aren't otherwise used (and therefore may not be
17633    output).  */
17634 static tree
17635 reference_to_unused (tree * tp, int * walk_subtrees,
17636                      void * data ATTRIBUTE_UNUSED)
17637 {
17638   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17639     *walk_subtrees = 0;
17640
17641   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17642       && ! TREE_ASM_WRITTEN (*tp))
17643     return *tp;
17644   /* ???  The C++ FE emits debug information for using decls, so
17645      putting gcc_unreachable here falls over.  See PR31899.  For now
17646      be conservative.  */
17647   else if (!cgraph_global_info_ready
17648            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17649     return *tp;
17650   else if (TREE_CODE (*tp) == VAR_DECL)
17651     {
17652       struct varpool_node *node = varpool_get_node (*tp);
17653       if (!node || !node->needed)
17654         return *tp;
17655     }
17656   else if (TREE_CODE (*tp) == FUNCTION_DECL
17657            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17658     {
17659       /* The call graph machinery must have finished analyzing,
17660          optimizing and gimplifying the CU by now.
17661          So if *TP has no call graph node associated
17662          to it, it means *TP will not be emitted.  */
17663       if (!cgraph_get_node (*tp))
17664         return *tp;
17665     }
17666   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17667     return *tp;
17668
17669   return NULL_TREE;
17670 }
17671
17672 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17673    for use in a later add_const_value_attribute call.  */
17674
17675 static rtx
17676 rtl_for_decl_init (tree init, tree type)
17677 {
17678   rtx rtl = NULL_RTX;
17679
17680   STRIP_NOPS (init);
17681
17682   /* If a variable is initialized with a string constant without embedded
17683      zeros, build CONST_STRING.  */
17684   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17685     {
17686       tree enttype = TREE_TYPE (type);
17687       tree domain = TYPE_DOMAIN (type);
17688       enum machine_mode mode = TYPE_MODE (enttype);
17689
17690       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17691           && domain
17692           && integer_zerop (TYPE_MIN_VALUE (domain))
17693           && compare_tree_int (TYPE_MAX_VALUE (domain),
17694                                TREE_STRING_LENGTH (init) - 1) == 0
17695           && ((size_t) TREE_STRING_LENGTH (init)
17696               == strlen (TREE_STRING_POINTER (init)) + 1))
17697         {
17698           rtl = gen_rtx_CONST_STRING (VOIDmode,
17699                                       ggc_strdup (TREE_STRING_POINTER (init)));
17700           rtl = gen_rtx_MEM (BLKmode, rtl);
17701           MEM_READONLY_P (rtl) = 1;
17702         }
17703     }
17704   /* Other aggregates, and complex values, could be represented using
17705      CONCAT: FIXME!  */
17706   else if (AGGREGATE_TYPE_P (type)
17707            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17708                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17709            || TREE_CODE (type) == COMPLEX_TYPE)
17710     ;
17711   /* Vectors only work if their mode is supported by the target.
17712      FIXME: generic vectors ought to work too.  */
17713   else if (TREE_CODE (type) == VECTOR_TYPE
17714            && !VECTOR_MODE_P (TYPE_MODE (type)))
17715     ;
17716   /* If the initializer is something that we know will expand into an
17717      immediate RTL constant, expand it now.  We must be careful not to
17718      reference variables which won't be output.  */
17719   else if (initializer_constant_valid_p (init, type)
17720            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17721     {
17722       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17723          possible.  */
17724       if (TREE_CODE (type) == VECTOR_TYPE)
17725         switch (TREE_CODE (init))
17726           {
17727           case VECTOR_CST:
17728             break;
17729           case CONSTRUCTOR:
17730             if (TREE_CONSTANT (init))
17731               {
17732                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
17733                 bool constant_p = true;
17734                 tree value;
17735                 unsigned HOST_WIDE_INT ix;
17736
17737                 /* Even when ctor is constant, it might contain non-*_CST
17738                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17739                    belong into VECTOR_CST nodes.  */
17740                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17741                   if (!CONSTANT_CLASS_P (value))
17742                     {
17743                       constant_p = false;
17744                       break;
17745                     }
17746
17747                 if (constant_p)
17748                   {
17749                     init = build_vector_from_ctor (type, elts);
17750                     break;
17751                   }
17752               }
17753             /* FALLTHRU */
17754
17755           default:
17756             return NULL;
17757           }
17758
17759       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17760
17761       /* If expand_expr returns a MEM, it wasn't immediate.  */
17762       gcc_assert (!rtl || !MEM_P (rtl));
17763     }
17764
17765   return rtl;
17766 }
17767
17768 /* Generate RTL for the variable DECL to represent its location.  */
17769
17770 static rtx
17771 rtl_for_decl_location (tree decl)
17772 {
17773   rtx rtl;
17774
17775   /* Here we have to decide where we are going to say the parameter "lives"
17776      (as far as the debugger is concerned).  We only have a couple of
17777      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17778
17779      DECL_RTL normally indicates where the parameter lives during most of the
17780      activation of the function.  If optimization is enabled however, this
17781      could be either NULL or else a pseudo-reg.  Both of those cases indicate
17782      that the parameter doesn't really live anywhere (as far as the code
17783      generation parts of GCC are concerned) during most of the function's
17784      activation.  That will happen (for example) if the parameter is never
17785      referenced within the function.
17786
17787      We could just generate a location descriptor here for all non-NULL
17788      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17789      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17790      where DECL_RTL is NULL or is a pseudo-reg.
17791
17792      Note however that we can only get away with using DECL_INCOMING_RTL as
17793      a backup substitute for DECL_RTL in certain limited cases.  In cases
17794      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17795      we can be sure that the parameter was passed using the same type as it is
17796      declared to have within the function, and that its DECL_INCOMING_RTL
17797      points us to a place where a value of that type is passed.
17798
17799      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17800      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17801      because in these cases DECL_INCOMING_RTL points us to a value of some
17802      type which is *different* from the type of the parameter itself.  Thus,
17803      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17804      such cases, the debugger would end up (for example) trying to fetch a
17805      `float' from a place which actually contains the first part of a
17806      `double'.  That would lead to really incorrect and confusing
17807      output at debug-time.
17808
17809      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17810      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
17811      are a couple of exceptions however.  On little-endian machines we can
17812      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17813      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17814      an integral type that is smaller than TREE_TYPE (decl). These cases arise
17815      when (on a little-endian machine) a non-prototyped function has a
17816      parameter declared to be of type `short' or `char'.  In such cases,
17817      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17818      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17819      passed `int' value.  If the debugger then uses that address to fetch
17820      a `short' or a `char' (on a little-endian machine) the result will be
17821      the correct data, so we allow for such exceptional cases below.
17822
17823      Note that our goal here is to describe the place where the given formal
17824      parameter lives during most of the function's activation (i.e. between the
17825      end of the prologue and the start of the epilogue).  We'll do that as best
17826      as we can. Note however that if the given formal parameter is modified
17827      sometime during the execution of the function, then a stack backtrace (at
17828      debug-time) will show the function as having been called with the *new*
17829      value rather than the value which was originally passed in.  This happens
17830      rarely enough that it is not a major problem, but it *is* a problem, and
17831      I'd like to fix it.
17832
17833      A future version of dwarf2out.c may generate two additional attributes for
17834      any given DW_TAG_formal_parameter DIE which will describe the "passed
17835      type" and the "passed location" for the given formal parameter in addition
17836      to the attributes we now generate to indicate the "declared type" and the
17837      "active location" for each parameter.  This additional set of attributes
17838      could be used by debuggers for stack backtraces. Separately, note that
17839      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17840      This happens (for example) for inlined-instances of inline function formal
17841      parameters which are never referenced.  This really shouldn't be
17842      happening.  All PARM_DECL nodes should get valid non-NULL
17843      DECL_INCOMING_RTL values.  FIXME.  */
17844
17845   /* Use DECL_RTL as the "location" unless we find something better.  */
17846   rtl = DECL_RTL_IF_SET (decl);
17847
17848   /* When generating abstract instances, ignore everything except
17849      constants, symbols living in memory, and symbols living in
17850      fixed registers.  */
17851   if (! reload_completed)
17852     {
17853       if (rtl
17854           && (CONSTANT_P (rtl)
17855               || (MEM_P (rtl)
17856                   && CONSTANT_P (XEXP (rtl, 0)))
17857               || (REG_P (rtl)
17858                   && TREE_CODE (decl) == VAR_DECL
17859                   && TREE_STATIC (decl))))
17860         {
17861           rtl = targetm.delegitimize_address (rtl);
17862           return rtl;
17863         }
17864       rtl = NULL_RTX;
17865     }
17866   else if (TREE_CODE (decl) == PARM_DECL)
17867     {
17868       if (rtl == NULL_RTX
17869           || is_pseudo_reg (rtl)
17870           || (MEM_P (rtl)
17871               && is_pseudo_reg (XEXP (rtl, 0))
17872               && DECL_INCOMING_RTL (decl)
17873               && MEM_P (DECL_INCOMING_RTL (decl))
17874               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17875         {
17876           tree declared_type = TREE_TYPE (decl);
17877           tree passed_type = DECL_ARG_TYPE (decl);
17878           enum machine_mode dmode = TYPE_MODE (declared_type);
17879           enum machine_mode pmode = TYPE_MODE (passed_type);
17880
17881           /* This decl represents a formal parameter which was optimized out.
17882              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17883              all cases where (rtl == NULL_RTX) just below.  */
17884           if (dmode == pmode)
17885             rtl = DECL_INCOMING_RTL (decl);
17886           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17887                    && SCALAR_INT_MODE_P (dmode)
17888                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17889                    && DECL_INCOMING_RTL (decl))
17890             {
17891               rtx inc = DECL_INCOMING_RTL (decl);
17892               if (REG_P (inc))
17893                 rtl = inc;
17894               else if (MEM_P (inc))
17895                 {
17896                   if (BYTES_BIG_ENDIAN)
17897                     rtl = adjust_address_nv (inc, dmode,
17898                                              GET_MODE_SIZE (pmode)
17899                                              - GET_MODE_SIZE (dmode));
17900                   else
17901                     rtl = inc;
17902                 }
17903             }
17904         }
17905
17906       /* If the parm was passed in registers, but lives on the stack, then
17907          make a big endian correction if the mode of the type of the
17908          parameter is not the same as the mode of the rtl.  */
17909       /* ??? This is the same series of checks that are made in dbxout.c before
17910          we reach the big endian correction code there.  It isn't clear if all
17911          of these checks are necessary here, but keeping them all is the safe
17912          thing to do.  */
17913       else if (MEM_P (rtl)
17914                && XEXP (rtl, 0) != const0_rtx
17915                && ! CONSTANT_P (XEXP (rtl, 0))
17916                /* Not passed in memory.  */
17917                && !MEM_P (DECL_INCOMING_RTL (decl))
17918                /* Not passed by invisible reference.  */
17919                && (!REG_P (XEXP (rtl, 0))
17920                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17921                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17922 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17923                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17924 #endif
17925                      )
17926                /* Big endian correction check.  */
17927                && BYTES_BIG_ENDIAN
17928                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17929                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17930                    < UNITS_PER_WORD))
17931         {
17932           int offset = (UNITS_PER_WORD
17933                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17934
17935           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17936                              plus_constant (XEXP (rtl, 0), offset));
17937         }
17938     }
17939   else if (TREE_CODE (decl) == VAR_DECL
17940            && rtl
17941            && MEM_P (rtl)
17942            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17943            && BYTES_BIG_ENDIAN)
17944     {
17945       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17946       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17947
17948       /* If a variable is declared "register" yet is smaller than
17949          a register, then if we store the variable to memory, it
17950          looks like we're storing a register-sized value, when in
17951          fact we are not.  We need to adjust the offset of the
17952          storage location to reflect the actual value's bytes,
17953          else gdb will not be able to display it.  */
17954       if (rsize > dsize)
17955         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17956                            plus_constant (XEXP (rtl, 0), rsize-dsize));
17957     }
17958
17959   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17960      and will have been substituted directly into all expressions that use it.
17961      C does not have such a concept, but C++ and other languages do.  */
17962   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17963     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17964
17965   if (rtl)
17966     rtl = targetm.delegitimize_address (rtl);
17967
17968   /* If we don't look past the constant pool, we risk emitting a
17969      reference to a constant pool entry that isn't referenced from
17970      code, and thus is not emitted.  */
17971   if (rtl)
17972     rtl = avoid_constant_pool_reference (rtl);
17973
17974   /* Try harder to get a rtl.  If this symbol ends up not being emitted
17975      in the current CU, resolve_addr will remove the expression referencing
17976      it.  */
17977   if (rtl == NULL_RTX
17978       && TREE_CODE (decl) == VAR_DECL
17979       && !DECL_EXTERNAL (decl)
17980       && TREE_STATIC (decl)
17981       && DECL_NAME (decl)
17982       && !DECL_HARD_REGISTER (decl)
17983       && DECL_MODE (decl) != VOIDmode)
17984     {
17985       rtl = make_decl_rtl_for_debug (decl);
17986       if (!MEM_P (rtl)
17987           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17988           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17989         rtl = NULL_RTX;
17990     }
17991
17992   return rtl;
17993 }
17994
17995 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17996    returned.  If so, the decl for the COMMON block is returned, and the
17997    value is the offset into the common block for the symbol.  */
17998
17999 static tree
18000 fortran_common (tree decl, HOST_WIDE_INT *value)
18001 {
18002   tree val_expr, cvar;
18003   enum machine_mode mode;
18004   HOST_WIDE_INT bitsize, bitpos;
18005   tree offset;
18006   int volatilep = 0, unsignedp = 0;
18007
18008   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
18009      it does not have a value (the offset into the common area), or if it
18010      is thread local (as opposed to global) then it isn't common, and shouldn't
18011      be handled as such.  */
18012   if (TREE_CODE (decl) != VAR_DECL
18013       || !TREE_STATIC (decl)
18014       || !DECL_HAS_VALUE_EXPR_P (decl)
18015       || !is_fortran ())
18016     return NULL_TREE;
18017
18018   val_expr = DECL_VALUE_EXPR (decl);
18019   if (TREE_CODE (val_expr) != COMPONENT_REF)
18020     return NULL_TREE;
18021
18022   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
18023                               &mode, &unsignedp, &volatilep, true);
18024
18025   if (cvar == NULL_TREE
18026       || TREE_CODE (cvar) != VAR_DECL
18027       || DECL_ARTIFICIAL (cvar)
18028       || !TREE_PUBLIC (cvar))
18029     return NULL_TREE;
18030
18031   *value = 0;
18032   if (offset != NULL)
18033     {
18034       if (!host_integerp (offset, 0))
18035         return NULL_TREE;
18036       *value = tree_low_cst (offset, 0);
18037     }
18038   if (bitpos != 0)
18039     *value += bitpos / BITS_PER_UNIT;
18040
18041   return cvar;
18042 }
18043
18044 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
18045    data attribute for a variable or a parameter.  We generate the
18046    DW_AT_const_value attribute only in those cases where the given variable
18047    or parameter does not have a true "location" either in memory or in a
18048    register.  This can happen (for example) when a constant is passed as an
18049    actual argument in a call to an inline function.  (It's possible that
18050    these things can crop up in other ways also.)  Note that one type of
18051    constant value which can be passed into an inlined function is a constant
18052    pointer.  This can happen for example if an actual argument in an inlined
18053    function call evaluates to a compile-time constant address.
18054
18055    CACHE_P is true if it is worth caching the location list for DECL,
18056    so that future calls can reuse it rather than regenerate it from scratch.
18057    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
18058    since we will need to refer to them each time the function is inlined.  */
18059
18060 static bool
18061 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
18062                                        enum dwarf_attribute attr)
18063 {
18064   rtx rtl;
18065   dw_loc_list_ref list;
18066   var_loc_list *loc_list;
18067   cached_dw_loc_list *cache;
18068   void **slot;
18069
18070   if (TREE_CODE (decl) == ERROR_MARK)
18071     return false;
18072
18073   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
18074               || TREE_CODE (decl) == RESULT_DECL);
18075
18076   /* Try to get some constant RTL for this decl, and use that as the value of
18077      the location.  */
18078
18079   rtl = rtl_for_decl_location (decl);
18080   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18081       && add_const_value_attribute (die, rtl))
18082     return true;
18083
18084   /* See if we have single element location list that is equivalent to
18085      a constant value.  That way we are better to use add_const_value_attribute
18086      rather than expanding constant value equivalent.  */
18087   loc_list = lookup_decl_loc (decl);
18088   if (loc_list
18089       && loc_list->first
18090       && loc_list->first->next == NULL
18091       && NOTE_P (loc_list->first->loc)
18092       && NOTE_VAR_LOCATION (loc_list->first->loc)
18093       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
18094     {
18095       struct var_loc_node *node;
18096
18097       node = loc_list->first;
18098       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
18099       if (GET_CODE (rtl) == EXPR_LIST)
18100         rtl = XEXP (rtl, 0);
18101       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
18102           && add_const_value_attribute (die, rtl))
18103          return true;
18104     }
18105   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
18106      list several times.  See if we've already cached the contents.  */
18107   list = NULL;
18108   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
18109     cache_p = false;
18110   if (cache_p)
18111     {
18112       cache = (cached_dw_loc_list *)
18113         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
18114       if (cache)
18115         list = cache->loc_list;
18116     }
18117   if (list == NULL)
18118     {
18119       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
18120       /* It is usually worth caching this result if the decl is from
18121          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
18122       if (cache_p && list && list->dw_loc_next)
18123         {
18124           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
18125                                            DECL_UID (decl), INSERT);
18126           cache = ggc_alloc_cleared_cached_dw_loc_list ();
18127           cache->decl_id = DECL_UID (decl);
18128           cache->loc_list = list;
18129           *slot = cache;
18130         }
18131     }
18132   if (list)
18133     {
18134       add_AT_location_description (die, attr, list);
18135       return true;
18136     }
18137   /* None of that worked, so it must not really have a location;
18138      try adding a constant value attribute from the DECL_INITIAL.  */
18139   return tree_add_const_value_attribute_for_decl (die, decl);
18140 }
18141
18142 /* Add VARIABLE and DIE into deferred locations list.  */
18143
18144 static void
18145 defer_location (tree variable, dw_die_ref die)
18146 {
18147   deferred_locations entry;
18148   entry.variable = variable;
18149   entry.die = die;
18150   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
18151 }
18152
18153 /* Helper function for tree_add_const_value_attribute.  Natively encode
18154    initializer INIT into an array.  Return true if successful.  */
18155
18156 static bool
18157 native_encode_initializer (tree init, unsigned char *array, int size)
18158 {
18159   tree type;
18160
18161   if (init == NULL_TREE)
18162     return false;
18163
18164   STRIP_NOPS (init);
18165   switch (TREE_CODE (init))
18166     {
18167     case STRING_CST:
18168       type = TREE_TYPE (init);
18169       if (TREE_CODE (type) == ARRAY_TYPE)
18170         {
18171           tree enttype = TREE_TYPE (type);
18172           enum machine_mode mode = TYPE_MODE (enttype);
18173
18174           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
18175             return false;
18176           if (int_size_in_bytes (type) != size)
18177             return false;
18178           if (size > TREE_STRING_LENGTH (init))
18179             {
18180               memcpy (array, TREE_STRING_POINTER (init),
18181                       TREE_STRING_LENGTH (init));
18182               memset (array + TREE_STRING_LENGTH (init),
18183                       '\0', size - TREE_STRING_LENGTH (init));
18184             }
18185           else
18186             memcpy (array, TREE_STRING_POINTER (init), size);
18187           return true;
18188         }
18189       return false;
18190     case CONSTRUCTOR:
18191       type = TREE_TYPE (init);
18192       if (int_size_in_bytes (type) != size)
18193         return false;
18194       if (TREE_CODE (type) == ARRAY_TYPE)
18195         {
18196           HOST_WIDE_INT min_index;
18197           unsigned HOST_WIDE_INT cnt;
18198           int curpos = 0, fieldsize;
18199           constructor_elt *ce;
18200
18201           if (TYPE_DOMAIN (type) == NULL_TREE
18202               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
18203             return false;
18204
18205           fieldsize = int_size_in_bytes (TREE_TYPE (type));
18206           if (fieldsize <= 0)
18207             return false;
18208
18209           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
18210           memset (array, '\0', size);
18211           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
18212             {
18213               tree val = ce->value;
18214               tree index = ce->index;
18215               int pos = curpos;
18216               if (index && TREE_CODE (index) == RANGE_EXPR)
18217                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
18218                       * fieldsize;
18219               else if (index)
18220                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
18221
18222               if (val)
18223                 {
18224                   STRIP_NOPS (val);
18225                   if (!native_encode_initializer (val, array + pos, fieldsize))
18226                     return false;
18227                 }
18228               curpos = pos + fieldsize;
18229               if (index && TREE_CODE (index) == RANGE_EXPR)
18230                 {
18231                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
18232                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
18233                   while (count-- > 0)
18234                     {
18235                       if (val)
18236                         memcpy (array + curpos, array + pos, fieldsize);
18237                       curpos += fieldsize;
18238                     }
18239                 }
18240               gcc_assert (curpos <= size);
18241             }
18242           return true;
18243         }
18244       else if (TREE_CODE (type) == RECORD_TYPE
18245                || TREE_CODE (type) == UNION_TYPE)
18246         {
18247           tree field = NULL_TREE;
18248           unsigned HOST_WIDE_INT cnt;
18249           constructor_elt *ce;
18250
18251           if (int_size_in_bytes (type) != size)
18252             return false;
18253
18254           if (TREE_CODE (type) == RECORD_TYPE)
18255             field = TYPE_FIELDS (type);
18256
18257           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
18258             {
18259               tree val = ce->value;
18260               int pos, fieldsize;
18261
18262               if (ce->index != 0)
18263                 field = ce->index;
18264
18265               if (val)
18266                 STRIP_NOPS (val);
18267
18268               if (field == NULL_TREE || DECL_BIT_FIELD (field))
18269                 return false;
18270
18271               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
18272                   && TYPE_DOMAIN (TREE_TYPE (field))
18273                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
18274                 return false;
18275               else if (DECL_SIZE_UNIT (field) == NULL_TREE
18276                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
18277                 return false;
18278               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
18279               pos = int_byte_position (field);
18280               gcc_assert (pos + fieldsize <= size);
18281               if (val
18282                   && !native_encode_initializer (val, array + pos, fieldsize))
18283                 return false;
18284             }
18285           return true;
18286         }
18287       return false;
18288     case VIEW_CONVERT_EXPR:
18289     case NON_LVALUE_EXPR:
18290       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
18291     default:
18292       return native_encode_expr (init, array, size) == size;
18293     }
18294 }
18295
18296 /* Attach a DW_AT_const_value attribute to DIE. The value of the
18297    attribute is the const value T.  */
18298
18299 static bool
18300 tree_add_const_value_attribute (dw_die_ref die, tree t)
18301 {
18302   tree init;
18303   tree type = TREE_TYPE (t);
18304   rtx rtl;
18305
18306   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
18307     return false;
18308
18309   init = t;
18310   gcc_assert (!DECL_P (init));
18311
18312   rtl = rtl_for_decl_init (init, type);
18313   if (rtl)
18314     return add_const_value_attribute (die, rtl);
18315   /* If the host and target are sane, try harder.  */
18316   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
18317            && initializer_constant_valid_p (init, type))
18318     {
18319       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
18320       if (size > 0 && (int) size == size)
18321         {
18322           unsigned char *array = (unsigned char *)
18323             ggc_alloc_cleared_atomic (size);
18324
18325           if (native_encode_initializer (init, array, size))
18326             {
18327               add_AT_vec (die, DW_AT_const_value, size, 1, array);
18328               return true;
18329             }
18330         }
18331     }
18332   return false;
18333 }
18334
18335 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
18336    attribute is the const value of T, where T is an integral constant
18337    variable with static storage duration
18338    (so it can't be a PARM_DECL or a RESULT_DECL).  */
18339
18340 static bool
18341 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
18342 {
18343
18344   if (!decl
18345       || (TREE_CODE (decl) != VAR_DECL
18346           && TREE_CODE (decl) != CONST_DECL)
18347       || (TREE_CODE (decl) == VAR_DECL
18348           && !TREE_STATIC (decl)))
18349     return false;
18350
18351     if (TREE_READONLY (decl)
18352         && ! TREE_THIS_VOLATILE (decl)
18353         && DECL_INITIAL (decl))
18354       /* OK */;
18355     else
18356       return false;
18357
18358   /* Don't add DW_AT_const_value if abstract origin already has one.  */
18359   if (get_AT (var_die, DW_AT_const_value))
18360     return false;
18361
18362   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
18363 }
18364
18365 /* Convert the CFI instructions for the current function into a
18366    location list.  This is used for DW_AT_frame_base when we targeting
18367    a dwarf2 consumer that does not support the dwarf3
18368    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
18369    expressions.  */
18370
18371 static dw_loc_list_ref
18372 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
18373 {
18374   int ix;
18375   dw_fde_ref fde;
18376   dw_loc_list_ref list, *list_tail;
18377   dw_cfi_ref cfi;
18378   dw_cfa_location last_cfa, next_cfa;
18379   const char *start_label, *last_label, *section;
18380   dw_cfa_location remember;
18381
18382   fde = current_fde ();
18383   gcc_assert (fde != NULL);
18384
18385   section = secname_for_decl (current_function_decl);
18386   list_tail = &list;
18387   list = NULL;
18388
18389   memset (&next_cfa, 0, sizeof (next_cfa));
18390   next_cfa.reg = INVALID_REGNUM;
18391   remember = next_cfa;
18392
18393   start_label = fde->dw_fde_begin;
18394
18395   /* ??? Bald assumption that the CIE opcode list does not contain
18396      advance opcodes.  */
18397   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
18398     lookup_cfa_1 (cfi, &next_cfa, &remember);
18399
18400   last_cfa = next_cfa;
18401   last_label = start_label;
18402
18403   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
18404     {
18405       /* If the first partition contained no CFI adjustments, the
18406          CIE opcodes apply to the whole first partition.  */
18407       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18408                                  fde->dw_fde_begin, fde->dw_fde_end, section);
18409       list_tail =&(*list_tail)->dw_loc_next;
18410       start_label = last_label = fde->dw_fde_second_begin;
18411     }
18412
18413   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
18414     {
18415       switch (cfi->dw_cfi_opc)
18416         {
18417         case DW_CFA_set_loc:
18418         case DW_CFA_advance_loc1:
18419         case DW_CFA_advance_loc2:
18420         case DW_CFA_advance_loc4:
18421           if (!cfa_equal_p (&last_cfa, &next_cfa))
18422             {
18423               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18424                                          start_label, last_label, section);
18425
18426               list_tail = &(*list_tail)->dw_loc_next;
18427               last_cfa = next_cfa;
18428               start_label = last_label;
18429             }
18430           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
18431           break;
18432
18433         case DW_CFA_advance_loc:
18434           /* The encoding is complex enough that we should never emit this.  */
18435           gcc_unreachable ();
18436
18437         default:
18438           lookup_cfa_1 (cfi, &next_cfa, &remember);
18439           break;
18440         }
18441       if (ix + 1 == fde->dw_fde_switch_cfi_index)
18442         {
18443           if (!cfa_equal_p (&last_cfa, &next_cfa))
18444             {
18445               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18446                                          start_label, last_label, section);
18447
18448               list_tail = &(*list_tail)->dw_loc_next;
18449               last_cfa = next_cfa;
18450               start_label = last_label;
18451             }
18452           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18453                                      start_label, fde->dw_fde_end, section);
18454           list_tail = &(*list_tail)->dw_loc_next;
18455           start_label = last_label = fde->dw_fde_second_begin;
18456         }
18457     }
18458
18459   if (!cfa_equal_p (&last_cfa, &next_cfa))
18460     {
18461       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
18462                                  start_label, last_label, section);
18463       list_tail = &(*list_tail)->dw_loc_next;
18464       start_label = last_label;
18465     }
18466
18467   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
18468                              start_label,
18469                              fde->dw_fde_second_begin
18470                              ? fde->dw_fde_second_end : fde->dw_fde_end,
18471                              section);
18472
18473   if (list && list->dw_loc_next)
18474     gen_llsym (list);
18475
18476   return list;
18477 }
18478
18479 /* Compute a displacement from the "steady-state frame pointer" to the
18480    frame base (often the same as the CFA), and store it in
18481    frame_pointer_fb_offset.  OFFSET is added to the displacement
18482    before the latter is negated.  */
18483
18484 static void
18485 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
18486 {
18487   rtx reg, elim;
18488
18489 #ifdef FRAME_POINTER_CFA_OFFSET
18490   reg = frame_pointer_rtx;
18491   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
18492 #else
18493   reg = arg_pointer_rtx;
18494   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
18495 #endif
18496
18497   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
18498   if (GET_CODE (elim) == PLUS)
18499     {
18500       offset += INTVAL (XEXP (elim, 1));
18501       elim = XEXP (elim, 0);
18502     }
18503
18504   frame_pointer_fb_offset = -offset;
18505
18506   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
18507      in which to eliminate.  This is because it's stack pointer isn't 
18508      directly accessible as a register within the ISA.  To work around
18509      this, assume that while we cannot provide a proper value for
18510      frame_pointer_fb_offset, we won't need one either.  */
18511   frame_pointer_fb_offset_valid
18512     = ((SUPPORTS_STACK_ALIGNMENT
18513         && (elim == hard_frame_pointer_rtx
18514             || elim == stack_pointer_rtx))
18515        || elim == (frame_pointer_needed
18516                    ? hard_frame_pointer_rtx
18517                    : stack_pointer_rtx));
18518 }
18519
18520 /* Generate a DW_AT_name attribute given some string value to be included as
18521    the value of the attribute.  */
18522
18523 static void
18524 add_name_attribute (dw_die_ref die, const char *name_string)
18525 {
18526   if (name_string != NULL && *name_string != 0)
18527     {
18528       if (demangle_name_func)
18529         name_string = (*demangle_name_func) (name_string);
18530
18531       add_AT_string (die, DW_AT_name, name_string);
18532     }
18533 }
18534
18535 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
18536    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
18537    of TYPE accordingly.
18538
18539    ??? This is a temporary measure until after we're able to generate
18540    regular DWARF for the complex Ada type system.  */
18541
18542 static void 
18543 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
18544                                      dw_die_ref context_die)
18545 {
18546   tree dtype;
18547   dw_die_ref dtype_die;
18548
18549   if (!lang_hooks.types.descriptive_type)
18550     return;
18551
18552   dtype = lang_hooks.types.descriptive_type (type);
18553   if (!dtype)
18554     return;
18555
18556   dtype_die = lookup_type_die (dtype);
18557   if (!dtype_die)
18558     {
18559       gen_type_die (dtype, context_die);
18560       dtype_die = lookup_type_die (dtype);
18561       gcc_assert (dtype_die);
18562     }
18563
18564   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
18565 }
18566
18567 /* Generate a DW_AT_comp_dir attribute for DIE.  */
18568
18569 static void
18570 add_comp_dir_attribute (dw_die_ref die)
18571 {
18572   const char *wd = get_src_pwd ();
18573   char *wd1;
18574
18575   if (wd == NULL)
18576     return;
18577
18578   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
18579     {
18580       int wdlen;
18581
18582       wdlen = strlen (wd);
18583       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
18584       strcpy (wd1, wd);
18585       wd1 [wdlen] = DIR_SEPARATOR;
18586       wd1 [wdlen + 1] = 0;
18587       wd = wd1;
18588     }
18589
18590     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
18591 }
18592
18593 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
18594    default.  */
18595
18596 static int
18597 lower_bound_default (void)
18598 {
18599   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
18600     {
18601     case DW_LANG_C:
18602     case DW_LANG_C89:
18603     case DW_LANG_C99:
18604     case DW_LANG_C_plus_plus:
18605     case DW_LANG_ObjC:
18606     case DW_LANG_ObjC_plus_plus:
18607     case DW_LANG_Java:
18608       return 0;
18609     case DW_LANG_Fortran77:
18610     case DW_LANG_Fortran90:
18611     case DW_LANG_Fortran95:
18612       return 1;
18613     case DW_LANG_UPC:
18614     case DW_LANG_D:
18615     case DW_LANG_Python:
18616       return dwarf_version >= 4 ? 0 : -1;
18617     case DW_LANG_Ada95:
18618     case DW_LANG_Ada83:
18619     case DW_LANG_Cobol74:
18620     case DW_LANG_Cobol85:
18621     case DW_LANG_Pascal83:
18622     case DW_LANG_Modula2:
18623     case DW_LANG_PLI:
18624       return dwarf_version >= 4 ? 1 : -1;
18625     default:
18626       return -1;
18627     }
18628 }
18629
18630 /* Given a tree node describing an array bound (either lower or upper) output
18631    a representation for that bound.  */
18632
18633 static void
18634 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
18635 {
18636   switch (TREE_CODE (bound))
18637     {
18638     case ERROR_MARK:
18639       return;
18640
18641     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
18642     case INTEGER_CST:
18643       {
18644         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
18645         int dflt;
18646
18647         /* Use the default if possible.  */
18648         if (bound_attr == DW_AT_lower_bound
18649             && host_integerp (bound, 0)
18650             && (dflt = lower_bound_default ()) != -1
18651             && tree_low_cst (bound, 0) == dflt)
18652           ;
18653
18654         /* Otherwise represent the bound as an unsigned value with the
18655            precision of its type.  The precision and signedness of the
18656            type will be necessary to re-interpret it unambiguously.  */
18657         else if (prec < HOST_BITS_PER_WIDE_INT)
18658           {
18659             unsigned HOST_WIDE_INT mask
18660               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
18661             add_AT_unsigned (subrange_die, bound_attr,
18662                              TREE_INT_CST_LOW (bound) & mask);
18663           }
18664         else if (prec == HOST_BITS_PER_WIDE_INT
18665                  || TREE_INT_CST_HIGH (bound) == 0)
18666           add_AT_unsigned (subrange_die, bound_attr,
18667                            TREE_INT_CST_LOW (bound));
18668         else
18669           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
18670                          TREE_INT_CST_LOW (bound));
18671       }
18672       break;
18673
18674     CASE_CONVERT:
18675     case VIEW_CONVERT_EXPR:
18676       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
18677       break;
18678
18679     case SAVE_EXPR:
18680       break;
18681
18682     case VAR_DECL:
18683     case PARM_DECL:
18684     case RESULT_DECL:
18685       {
18686         dw_die_ref decl_die = lookup_decl_die (bound);
18687
18688         /* ??? Can this happen, or should the variable have been bound
18689            first?  Probably it can, since I imagine that we try to create
18690            the types of parameters in the order in which they exist in
18691            the list, and won't have created a forward reference to a
18692            later parameter.  */
18693         if (decl_die != NULL)
18694           {
18695             add_AT_die_ref (subrange_die, bound_attr, decl_die);
18696             break;
18697           }
18698       }
18699       /* FALLTHRU */
18700
18701     default:
18702       {
18703         /* Otherwise try to create a stack operation procedure to
18704            evaluate the value of the array bound.  */
18705
18706         dw_die_ref ctx, decl_die;
18707         dw_loc_list_ref list;
18708
18709         list = loc_list_from_tree (bound, 2);
18710         if (list == NULL || single_element_loc_list_p (list))
18711           {
18712             /* If DW_AT_*bound is not a reference nor constant, it is
18713                a DWARF expression rather than location description.
18714                For that loc_list_from_tree (bound, 0) is needed.
18715                If that fails to give a single element list,
18716                fall back to outputting this as a reference anyway.  */
18717             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
18718             if (list2 && single_element_loc_list_p (list2))
18719               {
18720                 add_AT_loc (subrange_die, bound_attr, list2->expr);
18721                 break;
18722               }
18723           }
18724         if (list == NULL)
18725           break;
18726
18727         if (current_function_decl == 0)
18728           ctx = comp_unit_die ();
18729         else
18730           ctx = lookup_decl_die (current_function_decl);
18731
18732         decl_die = new_die (DW_TAG_variable, ctx, bound);
18733         add_AT_flag (decl_die, DW_AT_artificial, 1);
18734         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
18735         add_AT_location_description (decl_die, DW_AT_location, list);
18736         add_AT_die_ref (subrange_die, bound_attr, decl_die);
18737         break;
18738       }
18739     }
18740 }
18741
18742 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18743    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18744    Note that the block of subscript information for an array type also
18745    includes information about the element type of the given array type.  */
18746
18747 static void
18748 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18749 {
18750   unsigned dimension_number;
18751   tree lower, upper;
18752   dw_die_ref subrange_die;
18753
18754   for (dimension_number = 0;
18755        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18756        type = TREE_TYPE (type), dimension_number++)
18757     {
18758       tree domain = TYPE_DOMAIN (type);
18759
18760       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18761         break;
18762
18763       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18764          and (in GNU C only) variable bounds.  Handle all three forms
18765          here.  */
18766       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18767       if (domain)
18768         {
18769           /* We have an array type with specified bounds.  */
18770           lower = TYPE_MIN_VALUE (domain);
18771           upper = TYPE_MAX_VALUE (domain);
18772
18773           /* Define the index type.  */
18774           if (TREE_TYPE (domain))
18775             {
18776               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
18777                  TREE_TYPE field.  We can't emit debug info for this
18778                  because it is an unnamed integral type.  */
18779               if (TREE_CODE (domain) == INTEGER_TYPE
18780                   && TYPE_NAME (domain) == NULL_TREE
18781                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18782                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18783                 ;
18784               else
18785                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
18786                                     type_die);
18787             }
18788
18789           /* ??? If upper is NULL, the array has unspecified length,
18790              but it does have a lower bound.  This happens with Fortran
18791                dimension arr(N:*)
18792              Since the debugger is definitely going to need to know N
18793              to produce useful results, go ahead and output the lower
18794              bound solo, and hope the debugger can cope.  */
18795
18796           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
18797           if (upper)
18798             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
18799         }
18800
18801       /* Otherwise we have an array type with an unspecified length.  The
18802          DWARF-2 spec does not say how to handle this; let's just leave out the
18803          bounds.  */
18804     }
18805 }
18806
18807 static void
18808 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18809 {
18810   unsigned size;
18811
18812   switch (TREE_CODE (tree_node))
18813     {
18814     case ERROR_MARK:
18815       size = 0;
18816       break;
18817     case ENUMERAL_TYPE:
18818     case RECORD_TYPE:
18819     case UNION_TYPE:
18820     case QUAL_UNION_TYPE:
18821       size = int_size_in_bytes (tree_node);
18822       break;
18823     case FIELD_DECL:
18824       /* For a data member of a struct or union, the DW_AT_byte_size is
18825          generally given as the number of bytes normally allocated for an
18826          object of the *declared* type of the member itself.  This is true
18827          even for bit-fields.  */
18828       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
18829       break;
18830     default:
18831       gcc_unreachable ();
18832     }
18833
18834   /* Note that `size' might be -1 when we get to this point.  If it is, that
18835      indicates that the byte size of the entity in question is variable.  We
18836      have no good way of expressing this fact in Dwarf at the present time,
18837      so just let the -1 pass on through.  */
18838   add_AT_unsigned (die, DW_AT_byte_size, size);
18839 }
18840
18841 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18842    which specifies the distance in bits from the highest order bit of the
18843    "containing object" for the bit-field to the highest order bit of the
18844    bit-field itself.
18845
18846    For any given bit-field, the "containing object" is a hypothetical object
18847    (of some integral or enum type) within which the given bit-field lives.  The
18848    type of this hypothetical "containing object" is always the same as the
18849    declared type of the individual bit-field itself.  The determination of the
18850    exact location of the "containing object" for a bit-field is rather
18851    complicated.  It's handled by the `field_byte_offset' function (above).
18852
18853    Note that it is the size (in bytes) of the hypothetical "containing object"
18854    which will be given in the DW_AT_byte_size attribute for this bit-field.
18855    (See `byte_size_attribute' above).  */
18856
18857 static inline void
18858 add_bit_offset_attribute (dw_die_ref die, tree decl)
18859 {
18860   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
18861   tree type = DECL_BIT_FIELD_TYPE (decl);
18862   HOST_WIDE_INT bitpos_int;
18863   HOST_WIDE_INT highest_order_object_bit_offset;
18864   HOST_WIDE_INT highest_order_field_bit_offset;
18865   HOST_WIDE_INT bit_offset;
18866
18867   /* Must be a field and a bit field.  */
18868   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
18869
18870   /* We can't yet handle bit-fields whose offsets are variable, so if we
18871      encounter such things, just return without generating any attribute
18872      whatsoever.  Likewise for variable or too large size.  */
18873   if (! host_integerp (bit_position (decl), 0)
18874       || ! host_integerp (DECL_SIZE (decl), 1))
18875     return;
18876
18877   bitpos_int = int_bit_position (decl);
18878
18879   /* Note that the bit offset is always the distance (in bits) from the
18880      highest-order bit of the "containing object" to the highest-order bit of
18881      the bit-field itself.  Since the "high-order end" of any object or field
18882      is different on big-endian and little-endian machines, the computation
18883      below must take account of these differences.  */
18884   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18885   highest_order_field_bit_offset = bitpos_int;
18886
18887   if (! BYTES_BIG_ENDIAN)
18888     {
18889       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
18890       highest_order_object_bit_offset += simple_type_size_in_bits (type);
18891     }
18892
18893   bit_offset
18894     = (! BYTES_BIG_ENDIAN
18895        ? highest_order_object_bit_offset - highest_order_field_bit_offset
18896        : highest_order_field_bit_offset - highest_order_object_bit_offset);
18897
18898   if (bit_offset < 0)
18899     add_AT_int (die, DW_AT_bit_offset, bit_offset);
18900   else
18901     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18902 }
18903
18904 /* For a FIELD_DECL node which represents a bit field, output an attribute
18905    which specifies the length in bits of the given field.  */
18906
18907 static inline void
18908 add_bit_size_attribute (dw_die_ref die, tree decl)
18909 {
18910   /* Must be a field and a bit field.  */
18911   gcc_assert (TREE_CODE (decl) == FIELD_DECL
18912               && DECL_BIT_FIELD_TYPE (decl));
18913
18914   if (host_integerp (DECL_SIZE (decl), 1))
18915     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
18916 }
18917
18918 /* If the compiled language is ANSI C, then add a 'prototyped'
18919    attribute, if arg types are given for the parameters of a function.  */
18920
18921 static inline void
18922 add_prototyped_attribute (dw_die_ref die, tree func_type)
18923 {
18924   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
18925       && prototype_p (func_type))
18926     add_AT_flag (die, DW_AT_prototyped, 1);
18927 }
18928
18929 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
18930    by looking in either the type declaration or object declaration
18931    equate table.  */
18932
18933 static inline dw_die_ref
18934 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18935 {
18936   dw_die_ref origin_die = NULL;
18937
18938   if (TREE_CODE (origin) != FUNCTION_DECL)
18939     {
18940       /* We may have gotten separated from the block for the inlined
18941          function, if we're in an exception handler or some such; make
18942          sure that the abstract function has been written out.
18943
18944          Doing this for nested functions is wrong, however; functions are
18945          distinct units, and our context might not even be inline.  */
18946       tree fn = origin;
18947
18948       if (TYPE_P (fn))
18949         fn = TYPE_STUB_DECL (fn);
18950
18951       fn = decl_function_context (fn);
18952       if (fn)
18953         dwarf2out_abstract_function (fn);
18954     }
18955
18956   if (DECL_P (origin))
18957     origin_die = lookup_decl_die (origin);
18958   else if (TYPE_P (origin))
18959     origin_die = lookup_type_die (origin);
18960
18961   /* XXX: Functions that are never lowered don't always have correct block
18962      trees (in the case of java, they simply have no block tree, in some other
18963      languages).  For these functions, there is nothing we can really do to
18964      output correct debug info for inlined functions in all cases.  Rather
18965      than die, we'll just produce deficient debug info now, in that we will
18966      have variables without a proper abstract origin.  In the future, when all
18967      functions are lowered, we should re-add a gcc_assert (origin_die)
18968      here.  */
18969
18970   if (origin_die)
18971     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18972   return origin_die;
18973 }
18974
18975 /* We do not currently support the pure_virtual attribute.  */
18976
18977 static inline void
18978 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18979 {
18980   if (DECL_VINDEX (func_decl))
18981     {
18982       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18983
18984       if (host_integerp (DECL_VINDEX (func_decl), 0))
18985         add_AT_loc (die, DW_AT_vtable_elem_location,
18986                     new_loc_descr (DW_OP_constu,
18987                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
18988                                    0));
18989
18990       /* GNU extension: Record what type this method came from originally.  */
18991       if (debug_info_level > DINFO_LEVEL_TERSE
18992           && DECL_CONTEXT (func_decl))
18993         add_AT_die_ref (die, DW_AT_containing_type,
18994                         lookup_type_die (DECL_CONTEXT (func_decl)));
18995     }
18996 }
18997 \f
18998 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18999    given decl.  This used to be a vendor extension until after DWARF 4
19000    standardized it.  */
19001
19002 static void
19003 add_linkage_attr (dw_die_ref die, tree decl)
19004 {
19005   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
19006
19007   /* Mimic what assemble_name_raw does with a leading '*'.  */
19008   if (name[0] == '*')
19009     name = &name[1];
19010
19011   if (dwarf_version >= 4)
19012     add_AT_string (die, DW_AT_linkage_name, name);
19013   else
19014     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
19015 }
19016
19017 /* Add source coordinate attributes for the given decl.  */
19018
19019 static void
19020 add_src_coords_attributes (dw_die_ref die, tree decl)
19021 {
19022   expanded_location s;
19023
19024   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
19025     return;
19026   s = expand_location (DECL_SOURCE_LOCATION (decl));
19027   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
19028   add_AT_unsigned (die, DW_AT_decl_line, s.line);
19029 }
19030
19031 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
19032
19033 static void
19034 add_linkage_name (dw_die_ref die, tree decl)
19035 {
19036   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
19037        && TREE_PUBLIC (decl)
19038        && !DECL_ABSTRACT (decl)
19039        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
19040        && die->die_tag != DW_TAG_member)
19041     {
19042       /* Defer until we have an assembler name set.  */
19043       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
19044         {
19045           limbo_die_node *asm_name;
19046
19047           asm_name = ggc_alloc_cleared_limbo_die_node ();
19048           asm_name->die = die;
19049           asm_name->created_for = decl;
19050           asm_name->next = deferred_asm_name;
19051           deferred_asm_name = asm_name;
19052         }
19053       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
19054         add_linkage_attr (die, decl);
19055     }
19056 }
19057
19058 /* Add a DW_AT_name attribute and source coordinate attribute for the
19059    given decl, but only if it actually has a name.  */
19060
19061 static void
19062 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
19063 {
19064   tree decl_name;
19065
19066   decl_name = DECL_NAME (decl);
19067   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
19068     {
19069       const char *name = dwarf2_name (decl, 0);
19070       if (name)
19071         add_name_attribute (die, name);
19072       if (! DECL_ARTIFICIAL (decl))
19073         add_src_coords_attributes (die, decl);
19074
19075       add_linkage_name (die, decl);
19076     }
19077
19078 #ifdef VMS_DEBUGGING_INFO
19079   /* Get the function's name, as described by its RTL.  This may be different
19080      from the DECL_NAME name used in the source file.  */
19081   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
19082     {
19083       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
19084                    XEXP (DECL_RTL (decl), 0));
19085       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
19086     }
19087 #endif /* VMS_DEBUGGING_INFO */
19088 }
19089
19090 #ifdef VMS_DEBUGGING_INFO
19091 /* Output the debug main pointer die for VMS */
19092
19093 void
19094 dwarf2out_vms_debug_main_pointer (void)
19095 {
19096   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19097   dw_die_ref die;
19098
19099   /* Allocate the VMS debug main subprogram die.  */
19100   die = ggc_alloc_cleared_die_node ();
19101   die->die_tag = DW_TAG_subprogram;
19102   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
19103   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
19104                                current_function_funcdef_no);
19105   add_AT_lbl_id (die, DW_AT_entry_pc, label);
19106
19107   /* Make it the first child of comp_unit_die ().  */
19108   die->die_parent = comp_unit_die ();
19109   if (comp_unit_die ()->die_child)
19110     {
19111       die->die_sib = comp_unit_die ()->die_child->die_sib;
19112       comp_unit_die ()->die_child->die_sib = die;
19113     }
19114   else
19115     {
19116       die->die_sib = die;
19117       comp_unit_die ()->die_child = die;
19118     }
19119 }
19120 #endif /* VMS_DEBUGGING_INFO */
19121
19122 /* Push a new declaration scope.  */
19123
19124 static void
19125 push_decl_scope (tree scope)
19126 {
19127   VEC_safe_push (tree, gc, decl_scope_table, scope);
19128 }
19129
19130 /* Pop a declaration scope.  */
19131
19132 static inline void
19133 pop_decl_scope (void)
19134 {
19135   VEC_pop (tree, decl_scope_table);
19136 }
19137
19138 /* Return the DIE for the scope that immediately contains this type.
19139    Non-named types get global scope.  Named types nested in other
19140    types get their containing scope if it's open, or global scope
19141    otherwise.  All other types (i.e. function-local named types) get
19142    the current active scope.  */
19143
19144 static dw_die_ref
19145 scope_die_for (tree t, dw_die_ref context_die)
19146 {
19147   dw_die_ref scope_die = NULL;
19148   tree containing_scope;
19149   int i;
19150
19151   /* Non-types always go in the current scope.  */
19152   gcc_assert (TYPE_P (t));
19153
19154   containing_scope = TYPE_CONTEXT (t);
19155
19156   /* Use the containing namespace if it was passed in (for a declaration).  */
19157   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
19158     {
19159       if (context_die == lookup_decl_die (containing_scope))
19160         /* OK */;
19161       else
19162         containing_scope = NULL_TREE;
19163     }
19164
19165   /* Ignore function type "scopes" from the C frontend.  They mean that
19166      a tagged type is local to a parmlist of a function declarator, but
19167      that isn't useful to DWARF.  */
19168   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
19169     containing_scope = NULL_TREE;
19170
19171   if (SCOPE_FILE_SCOPE_P (containing_scope))
19172     scope_die = comp_unit_die ();
19173   else if (TYPE_P (containing_scope))
19174     {
19175       /* For types, we can just look up the appropriate DIE.  But
19176          first we check to see if we're in the middle of emitting it
19177          so we know where the new DIE should go.  */
19178       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
19179         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
19180           break;
19181
19182       if (i < 0)
19183         {
19184           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
19185                       || TREE_ASM_WRITTEN (containing_scope));
19186           /*We are not in the middle of emitting the type
19187             CONTAINING_SCOPE. Let's see if it's emitted already.  */
19188           scope_die = lookup_type_die (containing_scope);
19189
19190           /* If none of the current dies are suitable, we get file scope.  */
19191           if (scope_die == NULL)
19192             scope_die = comp_unit_die ();
19193         }
19194       else
19195         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
19196     }
19197   else
19198     scope_die = context_die;
19199
19200   return scope_die;
19201 }
19202
19203 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
19204
19205 static inline int
19206 local_scope_p (dw_die_ref context_die)
19207 {
19208   for (; context_die; context_die = context_die->die_parent)
19209     if (context_die->die_tag == DW_TAG_inlined_subroutine
19210         || context_die->die_tag == DW_TAG_subprogram)
19211       return 1;
19212
19213   return 0;
19214 }
19215
19216 /* Returns nonzero if CONTEXT_DIE is a class.  */
19217
19218 static inline int
19219 class_scope_p (dw_die_ref context_die)
19220 {
19221   return (context_die
19222           && (context_die->die_tag == DW_TAG_structure_type
19223               || context_die->die_tag == DW_TAG_class_type
19224               || context_die->die_tag == DW_TAG_interface_type
19225               || context_die->die_tag == DW_TAG_union_type));
19226 }
19227
19228 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
19229    whether or not to treat a DIE in this context as a declaration.  */
19230
19231 static inline int
19232 class_or_namespace_scope_p (dw_die_ref context_die)
19233 {
19234   return (class_scope_p (context_die)
19235           || (context_die && context_die->die_tag == DW_TAG_namespace));
19236 }
19237
19238 /* Many forms of DIEs require a "type description" attribute.  This
19239    routine locates the proper "type descriptor" die for the type given
19240    by 'type', and adds a DW_AT_type attribute below the given die.  */
19241
19242 static void
19243 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
19244                     int decl_volatile, dw_die_ref context_die)
19245 {
19246   enum tree_code code  = TREE_CODE (type);
19247   dw_die_ref type_die  = NULL;
19248
19249   /* ??? If this type is an unnamed subrange type of an integral, floating-point
19250      or fixed-point type, use the inner type.  This is because we have no
19251      support for unnamed types in base_type_die.  This can happen if this is
19252      an Ada subrange type.  Correct solution is emit a subrange type die.  */
19253   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
19254       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
19255     type = TREE_TYPE (type), code = TREE_CODE (type);
19256
19257   if (code == ERROR_MARK
19258       /* Handle a special case.  For functions whose return type is void, we
19259          generate *no* type attribute.  (Note that no object may have type
19260          `void', so this only applies to function return types).  */
19261       || code == VOID_TYPE)
19262     return;
19263
19264   type_die = modified_type_die (type,
19265                                 decl_const || TYPE_READONLY (type),
19266                                 decl_volatile || TYPE_VOLATILE (type),
19267                                 context_die);
19268
19269   if (type_die != NULL)
19270     add_AT_die_ref (object_die, DW_AT_type, type_die);
19271 }
19272
19273 /* Given an object die, add the calling convention attribute for the
19274    function call type.  */
19275 static void
19276 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
19277 {
19278   enum dwarf_calling_convention value = DW_CC_normal;
19279
19280   value = ((enum dwarf_calling_convention)
19281            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
19282
19283   if (is_fortran ()
19284       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
19285     {
19286       /* DWARF 2 doesn't provide a way to identify a program's source-level
19287         entry point.  DW_AT_calling_convention attributes are only meant
19288         to describe functions' calling conventions.  However, lacking a
19289         better way to signal the Fortran main program, we used this for 
19290         a long time, following existing custom.  Now, DWARF 4 has 
19291         DW_AT_main_subprogram, which we add below, but some tools still
19292         rely on the old way, which we thus keep.  */
19293       value = DW_CC_program;
19294
19295       if (dwarf_version >= 4 || !dwarf_strict)
19296         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
19297     }
19298
19299   /* Only add the attribute if the backend requests it, and
19300      is not DW_CC_normal.  */
19301   if (value && (value != DW_CC_normal))
19302     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
19303 }
19304
19305 /* Given a tree pointer to a struct, class, union, or enum type node, return
19306    a pointer to the (string) tag name for the given type, or zero if the type
19307    was declared without a tag.  */
19308
19309 static const char *
19310 type_tag (const_tree type)
19311 {
19312   const char *name = 0;
19313
19314   if (TYPE_NAME (type) != 0)
19315     {
19316       tree t = 0;
19317
19318       /* Find the IDENTIFIER_NODE for the type name.  */
19319       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
19320           && !TYPE_NAMELESS (type))
19321         t = TYPE_NAME (type);
19322
19323       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
19324          a TYPE_DECL node, regardless of whether or not a `typedef' was
19325          involved.  */
19326       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19327                && ! DECL_IGNORED_P (TYPE_NAME (type)))
19328         {
19329           /* We want to be extra verbose.  Don't call dwarf_name if
19330              DECL_NAME isn't set.  The default hook for decl_printable_name
19331              doesn't like that, and in this context it's correct to return
19332              0, instead of "<anonymous>" or the like.  */
19333           if (DECL_NAME (TYPE_NAME (type))
19334               && !DECL_NAMELESS (TYPE_NAME (type)))
19335             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
19336         }
19337
19338       /* Now get the name as a string, or invent one.  */
19339       if (!name && t != 0)
19340         name = IDENTIFIER_POINTER (t);
19341     }
19342
19343   return (name == 0 || *name == '\0') ? 0 : name;
19344 }
19345
19346 /* Return the type associated with a data member, make a special check
19347    for bit field types.  */
19348
19349 static inline tree
19350 member_declared_type (const_tree member)
19351 {
19352   return (DECL_BIT_FIELD_TYPE (member)
19353           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
19354 }
19355
19356 /* Get the decl's label, as described by its RTL. This may be different
19357    from the DECL_NAME name used in the source file.  */
19358
19359 #if 0
19360 static const char *
19361 decl_start_label (tree decl)
19362 {
19363   rtx x;
19364   const char *fnname;
19365
19366   x = DECL_RTL (decl);
19367   gcc_assert (MEM_P (x));
19368
19369   x = XEXP (x, 0);
19370   gcc_assert (GET_CODE (x) == SYMBOL_REF);
19371
19372   fnname = XSTR (x, 0);
19373   return fnname;
19374 }
19375 #endif
19376 \f
19377 /* These routines generate the internal representation of the DIE's for
19378    the compilation unit.  Debugging information is collected by walking
19379    the declaration trees passed in from dwarf2out_decl().  */
19380
19381 static void
19382 gen_array_type_die (tree type, dw_die_ref context_die)
19383 {
19384   dw_die_ref scope_die = scope_die_for (type, context_die);
19385   dw_die_ref array_die;
19386
19387   /* GNU compilers represent multidimensional array types as sequences of one
19388      dimensional array types whose element types are themselves array types.
19389      We sometimes squish that down to a single array_type DIE with multiple
19390      subscripts in the Dwarf debugging info.  The draft Dwarf specification
19391      say that we are allowed to do this kind of compression in C, because
19392      there is no difference between an array of arrays and a multidimensional
19393      array.  We don't do this for Ada to remain as close as possible to the
19394      actual representation, which is especially important against the language
19395      flexibilty wrt arrays of variable size.  */
19396
19397   bool collapse_nested_arrays = !is_ada ();
19398   tree element_type;
19399
19400   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
19401      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
19402   if (TYPE_STRING_FLAG (type)
19403       && TREE_CODE (type) == ARRAY_TYPE
19404       && is_fortran ()
19405       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
19406     {
19407       HOST_WIDE_INT size;
19408
19409       array_die = new_die (DW_TAG_string_type, scope_die, type);
19410       add_name_attribute (array_die, type_tag (type));
19411       equate_type_number_to_die (type, array_die);
19412       size = int_size_in_bytes (type);
19413       if (size >= 0)
19414         add_AT_unsigned (array_die, DW_AT_byte_size, size);
19415       else if (TYPE_DOMAIN (type) != NULL_TREE
19416                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
19417                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
19418         {
19419           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
19420           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
19421
19422           size = int_size_in_bytes (TREE_TYPE (szdecl));
19423           if (loc && size > 0)
19424             {
19425               add_AT_location_description (array_die, DW_AT_string_length, loc);
19426               if (size != DWARF2_ADDR_SIZE)
19427                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
19428             }
19429         }
19430       return;
19431     }
19432
19433   /* ??? The SGI dwarf reader fails for array of array of enum types
19434      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
19435      array type comes before the outer array type.  We thus call gen_type_die
19436      before we new_die and must prevent nested array types collapsing for this
19437      target.  */
19438
19439 #ifdef MIPS_DEBUGGING_INFO
19440   gen_type_die (TREE_TYPE (type), context_die);
19441   collapse_nested_arrays = false;
19442 #endif
19443
19444   array_die = new_die (DW_TAG_array_type, scope_die, type);
19445   add_name_attribute (array_die, type_tag (type));
19446   add_gnat_descriptive_type_attribute (array_die, type, context_die);
19447   equate_type_number_to_die (type, array_die);
19448
19449   if (TREE_CODE (type) == VECTOR_TYPE)
19450     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
19451
19452   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
19453   if (is_fortran ()
19454       && TREE_CODE (type) == ARRAY_TYPE
19455       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
19456       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
19457     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19458
19459 #if 0
19460   /* We default the array ordering.  SDB will probably do
19461      the right things even if DW_AT_ordering is not present.  It's not even
19462      an issue until we start to get into multidimensional arrays anyway.  If
19463      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
19464      then we'll have to put the DW_AT_ordering attribute back in.  (But if
19465      and when we find out that we need to put these in, we will only do so
19466      for multidimensional arrays.  */
19467   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
19468 #endif
19469
19470 #ifdef MIPS_DEBUGGING_INFO
19471   /* The SGI compilers handle arrays of unknown bound by setting
19472      AT_declaration and not emitting any subrange DIEs.  */
19473   if (TREE_CODE (type) == ARRAY_TYPE
19474       && ! TYPE_DOMAIN (type))
19475     add_AT_flag (array_die, DW_AT_declaration, 1);
19476   else
19477 #endif
19478   if (TREE_CODE (type) == VECTOR_TYPE)
19479     {
19480       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
19481       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
19482       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
19483       add_bound_info (subrange_die, DW_AT_upper_bound,
19484                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
19485     }
19486   else
19487     add_subscript_info (array_die, type, collapse_nested_arrays);
19488
19489   /* Add representation of the type of the elements of this array type and
19490      emit the corresponding DIE if we haven't done it already.  */
19491   element_type = TREE_TYPE (type);
19492   if (collapse_nested_arrays)
19493     while (TREE_CODE (element_type) == ARRAY_TYPE)
19494       {
19495         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
19496           break;
19497         element_type = TREE_TYPE (element_type);
19498       }
19499
19500 #ifndef MIPS_DEBUGGING_INFO
19501   gen_type_die (element_type, context_die);
19502 #endif
19503
19504   add_type_attribute (array_die, element_type, 0, 0, context_die);
19505
19506   if (get_AT (array_die, DW_AT_name))
19507     add_pubtype (type, array_die);
19508 }
19509
19510 static dw_loc_descr_ref
19511 descr_info_loc (tree val, tree base_decl)
19512 {
19513   HOST_WIDE_INT size;
19514   dw_loc_descr_ref loc, loc2;
19515   enum dwarf_location_atom op;
19516
19517   if (val == base_decl)
19518     return new_loc_descr (DW_OP_push_object_address, 0, 0);
19519
19520   switch (TREE_CODE (val))
19521     {
19522     CASE_CONVERT:
19523       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19524     case VAR_DECL:
19525       return loc_descriptor_from_tree (val, 0);
19526     case INTEGER_CST:
19527       if (host_integerp (val, 0))
19528         return int_loc_descriptor (tree_low_cst (val, 0));
19529       break;
19530     case INDIRECT_REF:
19531       size = int_size_in_bytes (TREE_TYPE (val));
19532       if (size < 0)
19533         break;
19534       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19535       if (!loc)
19536         break;
19537       if (size == DWARF2_ADDR_SIZE)
19538         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
19539       else
19540         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
19541       return loc;
19542     case POINTER_PLUS_EXPR:
19543     case PLUS_EXPR:
19544       if (host_integerp (TREE_OPERAND (val, 1), 1)
19545           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
19546              < 16384)
19547         {
19548           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19549           if (!loc)
19550             break;
19551           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
19552         }
19553       else
19554         {
19555           op = DW_OP_plus;
19556         do_binop:
19557           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
19558           if (!loc)
19559             break;
19560           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
19561           if (!loc2)
19562             break;
19563           add_loc_descr (&loc, loc2);
19564           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
19565         }
19566       return loc;
19567     case MINUS_EXPR:
19568       op = DW_OP_minus;
19569       goto do_binop;
19570     case MULT_EXPR:
19571       op = DW_OP_mul;
19572       goto do_binop;
19573     case EQ_EXPR:
19574       op = DW_OP_eq;
19575       goto do_binop;
19576     case NE_EXPR:
19577       op = DW_OP_ne;
19578       goto do_binop;
19579     default:
19580       break;
19581     }
19582   return NULL;
19583 }
19584
19585 static void
19586 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
19587                       tree val, tree base_decl)
19588 {
19589   dw_loc_descr_ref loc;
19590
19591   if (host_integerp (val, 0))
19592     {
19593       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
19594       return;
19595     }
19596
19597   loc = descr_info_loc (val, base_decl);
19598   if (!loc)
19599     return;
19600
19601   add_AT_loc (die, attr, loc);
19602 }
19603
19604 /* This routine generates DIE for array with hidden descriptor, details
19605    are filled into *info by a langhook.  */
19606
19607 static void
19608 gen_descr_array_type_die (tree type, struct array_descr_info *info,
19609                           dw_die_ref context_die)
19610 {
19611   dw_die_ref scope_die = scope_die_for (type, context_die);
19612   dw_die_ref array_die;
19613   int dim;
19614
19615   array_die = new_die (DW_TAG_array_type, scope_die, type);
19616   add_name_attribute (array_die, type_tag (type));
19617   equate_type_number_to_die (type, array_die);
19618
19619   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
19620   if (is_fortran ()
19621       && info->ndimensions >= 2)
19622     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
19623
19624   if (info->data_location)
19625     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
19626                           info->base_decl);
19627   if (info->associated)
19628     add_descr_info_field (array_die, DW_AT_associated, info->associated,
19629                           info->base_decl);
19630   if (info->allocated)
19631     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
19632                           info->base_decl);
19633
19634   for (dim = 0; dim < info->ndimensions; dim++)
19635     {
19636       dw_die_ref subrange_die
19637         = new_die (DW_TAG_subrange_type, array_die, NULL);
19638
19639       if (info->dimen[dim].lower_bound)
19640         {
19641           /* If it is the default value, omit it.  */
19642           int dflt;
19643
19644           if (host_integerp (info->dimen[dim].lower_bound, 0)
19645               && (dflt = lower_bound_default ()) != -1
19646               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
19647             ;
19648           else
19649             add_descr_info_field (subrange_die, DW_AT_lower_bound,
19650                                   info->dimen[dim].lower_bound,
19651                                   info->base_decl);
19652         }
19653       if (info->dimen[dim].upper_bound)
19654         add_descr_info_field (subrange_die, DW_AT_upper_bound,
19655                               info->dimen[dim].upper_bound,
19656                               info->base_decl);
19657       if (info->dimen[dim].stride)
19658         add_descr_info_field (subrange_die, DW_AT_byte_stride,
19659                               info->dimen[dim].stride,
19660                               info->base_decl);
19661     }
19662
19663   gen_type_die (info->element_type, context_die);
19664   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
19665
19666   if (get_AT (array_die, DW_AT_name))
19667     add_pubtype (type, array_die);
19668 }
19669
19670 #if 0
19671 static void
19672 gen_entry_point_die (tree decl, dw_die_ref context_die)
19673 {
19674   tree origin = decl_ultimate_origin (decl);
19675   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19676
19677   if (origin != NULL)
19678     add_abstract_origin_attribute (decl_die, origin);
19679   else
19680     {
19681       add_name_and_src_coords_attributes (decl_die, decl);
19682       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19683                           0, 0, context_die);
19684     }
19685
19686   if (DECL_ABSTRACT (decl))
19687     equate_decl_number_to_die (decl, decl_die);
19688   else
19689     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19690 }
19691 #endif
19692
19693 /* Walk through the list of incomplete types again, trying once more to
19694    emit full debugging info for them.  */
19695
19696 static void
19697 retry_incomplete_types (void)
19698 {
19699   int i;
19700
19701   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
19702     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
19703                                   DINFO_USAGE_DIR_USE))
19704       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
19705 }
19706
19707 /* Determine what tag to use for a record type.  */
19708
19709 static enum dwarf_tag
19710 record_type_tag (tree type)
19711 {
19712   if (! lang_hooks.types.classify_record)
19713     return DW_TAG_structure_type;
19714
19715   switch (lang_hooks.types.classify_record (type))
19716     {
19717     case RECORD_IS_STRUCT:
19718       return DW_TAG_structure_type;
19719
19720     case RECORD_IS_CLASS:
19721       return DW_TAG_class_type;
19722
19723     case RECORD_IS_INTERFACE:
19724       if (dwarf_version >= 3 || !dwarf_strict)
19725         return DW_TAG_interface_type;
19726       return DW_TAG_structure_type;
19727
19728     default:
19729       gcc_unreachable ();
19730     }
19731 }
19732
19733 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
19734    include all of the information about the enumeration values also. Each
19735    enumerated type name/value is listed as a child of the enumerated type
19736    DIE.  */
19737
19738 static dw_die_ref
19739 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19740 {
19741   dw_die_ref type_die = lookup_type_die (type);
19742
19743   if (type_die == NULL)
19744     {
19745       type_die = new_die (DW_TAG_enumeration_type,
19746                           scope_die_for (type, context_die), type);
19747       equate_type_number_to_die (type, type_die);
19748       add_name_attribute (type_die, type_tag (type));
19749       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19750       if (dwarf_version >= 4 || !dwarf_strict)
19751         {
19752           if (ENUM_IS_SCOPED (type))
19753             add_AT_flag (type_die, DW_AT_enum_class, 1);
19754           if (ENUM_IS_OPAQUE (type))
19755             add_AT_flag (type_die, DW_AT_declaration, 1);
19756         }
19757     }
19758   else if (! TYPE_SIZE (type))
19759     return type_die;
19760   else
19761     remove_AT (type_die, DW_AT_declaration);
19762
19763   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
19764      given enum type is incomplete, do not generate the DW_AT_byte_size
19765      attribute or the DW_AT_element_list attribute.  */
19766   if (TYPE_SIZE (type))
19767     {
19768       tree link;
19769
19770       TREE_ASM_WRITTEN (type) = 1;
19771       add_byte_size_attribute (type_die, type);
19772       if (TYPE_STUB_DECL (type) != NULL_TREE)
19773         {
19774           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19775           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19776         }
19777
19778       /* If the first reference to this type was as the return type of an
19779          inline function, then it may not have a parent.  Fix this now.  */
19780       if (type_die->die_parent == NULL)
19781         add_child_die (scope_die_for (type, context_die), type_die);
19782
19783       for (link = TYPE_VALUES (type);
19784            link != NULL; link = TREE_CHAIN (link))
19785         {
19786           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19787           tree value = TREE_VALUE (link);
19788
19789           add_name_attribute (enum_die,
19790                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19791
19792           if (TREE_CODE (value) == CONST_DECL)
19793             value = DECL_INITIAL (value);
19794
19795           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
19796             /* DWARF2 does not provide a way of indicating whether or
19797                not enumeration constants are signed or unsigned.  GDB
19798                always assumes the values are signed, so we output all
19799                values as if they were signed.  That means that
19800                enumeration constants with very large unsigned values
19801                will appear to have negative values in the debugger.  */
19802             add_AT_int (enum_die, DW_AT_const_value,
19803                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
19804         }
19805     }
19806   else
19807     add_AT_flag (type_die, DW_AT_declaration, 1);
19808
19809   if (get_AT (type_die, DW_AT_name))
19810     add_pubtype (type, type_die);
19811
19812   return type_die;
19813 }
19814
19815 /* Generate a DIE to represent either a real live formal parameter decl or to
19816    represent just the type of some formal parameter position in some function
19817    type.
19818
19819    Note that this routine is a bit unusual because its argument may be a
19820    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19821    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19822    node.  If it's the former then this function is being called to output a
19823    DIE to represent a formal parameter object (or some inlining thereof).  If
19824    it's the latter, then this function is only being called to output a
19825    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19826    argument type of some subprogram type.
19827    If EMIT_NAME_P is true, name and source coordinate attributes
19828    are emitted.  */
19829
19830 static dw_die_ref
19831 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19832                           dw_die_ref context_die)
19833 {
19834   tree node_or_origin = node ? node : origin;
19835   tree ultimate_origin;
19836   dw_die_ref parm_die
19837     = new_die (DW_TAG_formal_parameter, context_die, node);
19838
19839   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19840     {
19841     case tcc_declaration:
19842       ultimate_origin = decl_ultimate_origin (node_or_origin);
19843       if (node || ultimate_origin)
19844         origin = ultimate_origin;
19845       if (origin != NULL)
19846         add_abstract_origin_attribute (parm_die, origin);
19847       else if (emit_name_p)
19848         add_name_and_src_coords_attributes (parm_die, node);
19849       if (origin == NULL
19850           || (! DECL_ABSTRACT (node_or_origin)
19851               && variably_modified_type_p (TREE_TYPE (node_or_origin),
19852                                            decl_function_context
19853                                                             (node_or_origin))))
19854         {
19855           tree type = TREE_TYPE (node_or_origin);
19856           if (decl_by_reference_p (node_or_origin))
19857             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
19858                                 context_die);
19859           else
19860             add_type_attribute (parm_die, type,
19861                                 TREE_READONLY (node_or_origin),
19862                                 TREE_THIS_VOLATILE (node_or_origin),
19863                                 context_die);
19864         }
19865       if (origin == NULL && DECL_ARTIFICIAL (node))
19866         add_AT_flag (parm_die, DW_AT_artificial, 1);
19867
19868       if (node && node != origin)
19869         equate_decl_number_to_die (node, parm_die);
19870       if (! DECL_ABSTRACT (node_or_origin))
19871         add_location_or_const_value_attribute (parm_die, node_or_origin,
19872                                                node == NULL, DW_AT_location);
19873
19874       break;
19875
19876     case tcc_type:
19877       /* We were called with some kind of a ..._TYPE node.  */
19878       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
19879       break;
19880
19881     default:
19882       gcc_unreachable ();
19883     }
19884
19885   return parm_die;
19886 }
19887
19888 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19889    children DW_TAG_formal_parameter DIEs representing the arguments of the
19890    parameter pack.
19891
19892    PARM_PACK must be a function parameter pack.
19893    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19894    must point to the subsequent arguments of the function PACK_ARG belongs to.
19895    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19896    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19897    following the last one for which a DIE was generated.  */
19898
19899 static dw_die_ref
19900 gen_formal_parameter_pack_die  (tree parm_pack,
19901                                 tree pack_arg,
19902                                 dw_die_ref subr_die,
19903                                 tree *next_arg)
19904 {
19905   tree arg;
19906   dw_die_ref parm_pack_die;
19907
19908   gcc_assert (parm_pack
19909               && lang_hooks.function_parameter_pack_p (parm_pack)
19910               && subr_die);
19911
19912   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19913   add_src_coords_attributes (parm_pack_die, parm_pack);
19914
19915   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19916     {
19917       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19918                                                                  parm_pack))
19919         break;
19920       gen_formal_parameter_die (arg, NULL,
19921                                 false /* Don't emit name attribute.  */,
19922                                 parm_pack_die);
19923     }
19924   if (next_arg)
19925     *next_arg = arg;
19926   return parm_pack_die;
19927 }
19928
19929 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19930    at the end of an (ANSI prototyped) formal parameters list.  */
19931
19932 static void
19933 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19934 {
19935   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19936 }
19937
19938 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19939    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19940    parameters as specified in some function type specification (except for
19941    those which appear as part of a function *definition*).  */
19942
19943 static void
19944 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19945 {
19946   tree link;
19947   tree formal_type = NULL;
19948   tree first_parm_type;
19949   tree arg;
19950
19951   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19952     {
19953       arg = DECL_ARGUMENTS (function_or_method_type);
19954       function_or_method_type = TREE_TYPE (function_or_method_type);
19955     }
19956   else
19957     arg = NULL_TREE;
19958
19959   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19960
19961   /* Make our first pass over the list of formal parameter types and output a
19962      DW_TAG_formal_parameter DIE for each one.  */
19963   for (link = first_parm_type; link; )
19964     {
19965       dw_die_ref parm_die;
19966
19967       formal_type = TREE_VALUE (link);
19968       if (formal_type == void_type_node)
19969         break;
19970
19971       /* Output a (nameless) DIE to represent the formal parameter itself.  */
19972       parm_die = gen_formal_parameter_die (formal_type, NULL,
19973                                            true /* Emit name attribute.  */,
19974                                            context_die);
19975       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19976           && link == first_parm_type)
19977         {
19978           add_AT_flag (parm_die, DW_AT_artificial, 1);
19979           if (dwarf_version >= 3 || !dwarf_strict)
19980             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19981         }
19982       else if (arg && DECL_ARTIFICIAL (arg))
19983         add_AT_flag (parm_die, DW_AT_artificial, 1);
19984
19985       link = TREE_CHAIN (link);
19986       if (arg)
19987         arg = DECL_CHAIN (arg);
19988     }
19989
19990   /* If this function type has an ellipsis, add a
19991      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
19992   if (formal_type != void_type_node)
19993     gen_unspecified_parameters_die (function_or_method_type, context_die);
19994
19995   /* Make our second (and final) pass over the list of formal parameter types
19996      and output DIEs to represent those types (as necessary).  */
19997   for (link = TYPE_ARG_TYPES (function_or_method_type);
19998        link && TREE_VALUE (link);
19999        link = TREE_CHAIN (link))
20000     gen_type_die (TREE_VALUE (link), context_die);
20001 }
20002
20003 /* We want to generate the DIE for TYPE so that we can generate the
20004    die for MEMBER, which has been defined; we will need to refer back
20005    to the member declaration nested within TYPE.  If we're trying to
20006    generate minimal debug info for TYPE, processing TYPE won't do the
20007    trick; we need to attach the member declaration by hand.  */
20008
20009 static void
20010 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
20011 {
20012   gen_type_die (type, context_die);
20013
20014   /* If we're trying to avoid duplicate debug info, we may not have
20015      emitted the member decl for this function.  Emit it now.  */
20016   if (TYPE_STUB_DECL (type)
20017       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
20018       && ! lookup_decl_die (member))
20019     {
20020       dw_die_ref type_die;
20021       gcc_assert (!decl_ultimate_origin (member));
20022
20023       push_decl_scope (type);
20024       type_die = lookup_type_die_strip_naming_typedef (type);
20025       if (TREE_CODE (member) == FUNCTION_DECL)
20026         gen_subprogram_die (member, type_die);
20027       else if (TREE_CODE (member) == FIELD_DECL)
20028         {
20029           /* Ignore the nameless fields that are used to skip bits but handle
20030              C++ anonymous unions and structs.  */
20031           if (DECL_NAME (member) != NULL_TREE
20032               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
20033               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
20034             {
20035               gen_type_die (member_declared_type (member), type_die);
20036               gen_field_die (member, type_die);
20037             }
20038         }
20039       else
20040         gen_variable_die (member, NULL_TREE, type_die);
20041
20042       pop_decl_scope ();
20043     }
20044 }
20045
20046 /* Generate the DWARF2 info for the "abstract" instance of a function which we
20047    may later generate inlined and/or out-of-line instances of.  */
20048
20049 static void
20050 dwarf2out_abstract_function (tree decl)
20051 {
20052   dw_die_ref old_die;
20053   tree save_fn;
20054   tree context;
20055   int was_abstract;
20056   htab_t old_decl_loc_table;
20057   htab_t old_cached_dw_loc_list_table;
20058   int old_call_site_count, old_tail_call_site_count;
20059   struct call_arg_loc_node *old_call_arg_locations;
20060
20061   /* Make sure we have the actual abstract inline, not a clone.  */
20062   decl = DECL_ORIGIN (decl);
20063
20064   old_die = lookup_decl_die (decl);
20065   if (old_die && get_AT (old_die, DW_AT_inline))
20066     /* We've already generated the abstract instance.  */
20067     return;
20068
20069   /* We can be called while recursively when seeing block defining inlined subroutine
20070      DIE.  Be sure to not clobber the outer location table nor use it or we would
20071      get locations in abstract instantces.  */
20072   old_decl_loc_table = decl_loc_table;
20073   decl_loc_table = NULL;
20074   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
20075   cached_dw_loc_list_table = NULL;
20076   old_call_arg_locations = call_arg_locations;
20077   call_arg_locations = NULL;
20078   old_call_site_count = call_site_count;
20079   call_site_count = -1;
20080   old_tail_call_site_count = tail_call_site_count;
20081   tail_call_site_count = -1;
20082
20083   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
20084      we don't get confused by DECL_ABSTRACT.  */
20085   if (debug_info_level > DINFO_LEVEL_TERSE)
20086     {
20087       context = decl_class_context (decl);
20088       if (context)
20089         gen_type_die_for_member
20090           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
20091     }
20092
20093   /* Pretend we've just finished compiling this function.  */
20094   save_fn = current_function_decl;
20095   current_function_decl = decl;
20096   push_cfun (DECL_STRUCT_FUNCTION (decl));
20097
20098   was_abstract = DECL_ABSTRACT (decl);
20099   set_decl_abstract_flags (decl, 1);
20100   dwarf2out_decl (decl);
20101   if (! was_abstract)
20102     set_decl_abstract_flags (decl, 0);
20103
20104   current_function_decl = save_fn;
20105   decl_loc_table = old_decl_loc_table;
20106   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
20107   call_arg_locations = old_call_arg_locations;
20108   call_site_count = old_call_site_count;
20109   tail_call_site_count = old_tail_call_site_count;
20110   pop_cfun ();
20111 }
20112
20113 /* Helper function of premark_used_types() which gets called through
20114    htab_traverse.
20115
20116    Marks the DIE of a given type in *SLOT as perennial, so it never gets
20117    marked as unused by prune_unused_types.  */
20118
20119 static int
20120 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
20121 {
20122   tree type;
20123   dw_die_ref die;
20124
20125   type = (tree) *slot;
20126   die = lookup_type_die (type);
20127   if (die != NULL)
20128     die->die_perennial_p = 1;
20129   return 1;
20130 }
20131
20132 /* Helper function of premark_types_used_by_global_vars which gets called
20133    through htab_traverse.
20134
20135    Marks the DIE of a given type in *SLOT as perennial, so it never gets
20136    marked as unused by prune_unused_types. The DIE of the type is marked
20137    only if the global variable using the type will actually be emitted.  */
20138
20139 static int
20140 premark_types_used_by_global_vars_helper (void **slot,
20141                                           void *data ATTRIBUTE_UNUSED)
20142 {
20143   struct types_used_by_vars_entry *entry;
20144   dw_die_ref die;
20145
20146   entry = (struct types_used_by_vars_entry *) *slot;
20147   gcc_assert (entry->type != NULL
20148               && entry->var_decl != NULL);
20149   die = lookup_type_die (entry->type);
20150   if (die)
20151     {
20152       /* Ask cgraph if the global variable really is to be emitted.
20153          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
20154       struct varpool_node *node = varpool_get_node (entry->var_decl);
20155       if (node && node->needed)
20156         {
20157           die->die_perennial_p = 1;
20158           /* Keep the parent DIEs as well.  */
20159           while ((die = die->die_parent) && die->die_perennial_p == 0)
20160             die->die_perennial_p = 1;
20161         }
20162     }
20163   return 1;
20164 }
20165
20166 /* Mark all members of used_types_hash as perennial.  */
20167
20168 static void
20169 premark_used_types (void)
20170 {
20171   if (cfun && cfun->used_types_hash)
20172     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
20173 }
20174
20175 /* Mark all members of types_used_by_vars_entry as perennial.  */
20176
20177 static void
20178 premark_types_used_by_global_vars (void)
20179 {
20180   if (types_used_by_vars_hash)
20181     htab_traverse (types_used_by_vars_hash,
20182                    premark_types_used_by_global_vars_helper, NULL);
20183 }
20184
20185 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
20186    for CA_LOC call arg loc node.  */
20187
20188 static dw_die_ref
20189 gen_call_site_die (tree decl, dw_die_ref subr_die,
20190                    struct call_arg_loc_node *ca_loc)
20191 {
20192   dw_die_ref stmt_die = NULL, die;
20193   tree block = ca_loc->block;
20194
20195   while (block
20196          && block != DECL_INITIAL (decl)
20197          && TREE_CODE (block) == BLOCK)
20198     {
20199       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
20200         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
20201       if (stmt_die)
20202         break;
20203       block = BLOCK_SUPERCONTEXT (block);
20204     }
20205   if (stmt_die == NULL)
20206     stmt_die = subr_die;
20207   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
20208   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
20209   if (ca_loc->tail_call_p)
20210     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
20211   if (ca_loc->symbol_ref)
20212     {
20213       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
20214       if (tdie)
20215         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
20216       else
20217         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
20218     }
20219   return die;
20220 }
20221
20222 /* Generate a DIE to represent a declared function (either file-scope or
20223    block-local).  */
20224
20225 static void
20226 gen_subprogram_die (tree decl, dw_die_ref context_die)
20227 {
20228   tree origin = decl_ultimate_origin (decl);
20229   dw_die_ref subr_die;
20230   tree outer_scope;
20231   dw_die_ref old_die = lookup_decl_die (decl);
20232   int declaration = (current_function_decl != decl
20233                      || class_or_namespace_scope_p (context_die));
20234
20235   premark_used_types ();
20236
20237   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
20238      started to generate the abstract instance of an inline, decided to output
20239      its containing class, and proceeded to emit the declaration of the inline
20240      from the member list for the class.  If so, DECLARATION takes priority;
20241      we'll get back to the abstract instance when done with the class.  */
20242
20243   /* The class-scope declaration DIE must be the primary DIE.  */
20244   if (origin && declaration && class_or_namespace_scope_p (context_die))
20245     {
20246       origin = NULL;
20247       gcc_assert (!old_die);
20248     }
20249
20250   /* Now that the C++ front end lazily declares artificial member fns, we
20251      might need to retrofit the declaration into its class.  */
20252   if (!declaration && !origin && !old_die
20253       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
20254       && !class_or_namespace_scope_p (context_die)
20255       && debug_info_level > DINFO_LEVEL_TERSE)
20256     old_die = force_decl_die (decl);
20257
20258   if (origin != NULL)
20259     {
20260       gcc_assert (!declaration || local_scope_p (context_die));
20261
20262       /* Fixup die_parent for the abstract instance of a nested
20263          inline function.  */
20264       if (old_die && old_die->die_parent == NULL)
20265         add_child_die (context_die, old_die);
20266
20267       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20268       add_abstract_origin_attribute (subr_die, origin);
20269       /*  This is where the actual code for a cloned function is.
20270           Let's emit linkage name attribute for it.  This helps
20271           debuggers to e.g, set breakpoints into
20272           constructors/destructors when the user asks "break
20273           K::K".  */
20274       add_linkage_name (subr_die, decl);
20275     }
20276   else if (old_die)
20277     {
20278       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20279       struct dwarf_file_data * file_index = lookup_filename (s.file);
20280
20281       if (!get_AT_flag (old_die, DW_AT_declaration)
20282           /* We can have a normal definition following an inline one in the
20283              case of redefinition of GNU C extern inlines.
20284              It seems reasonable to use AT_specification in this case.  */
20285           && !get_AT (old_die, DW_AT_inline))
20286         {
20287           /* Detect and ignore this case, where we are trying to output
20288              something we have already output.  */
20289           return;
20290         }
20291
20292       /* If the definition comes from the same place as the declaration,
20293          maybe use the old DIE.  We always want the DIE for this function
20294          that has the *_pc attributes to be under comp_unit_die so the
20295          debugger can find it.  We also need to do this for abstract
20296          instances of inlines, since the spec requires the out-of-line copy
20297          to have the same parent.  For local class methods, this doesn't
20298          apply; we just use the old DIE.  */
20299       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
20300           && (DECL_ARTIFICIAL (decl)
20301               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
20302                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
20303                       == (unsigned) s.line))))
20304         {
20305           subr_die = old_die;
20306
20307           /* Clear out the declaration attribute and the formal parameters.
20308              Do not remove all children, because it is possible that this
20309              declaration die was forced using force_decl_die(). In such
20310              cases die that forced declaration die (e.g. TAG_imported_module)
20311              is one of the children that we do not want to remove.  */
20312           remove_AT (subr_die, DW_AT_declaration);
20313           remove_AT (subr_die, DW_AT_object_pointer);
20314           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
20315         }
20316       else
20317         {
20318           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20319           add_AT_specification (subr_die, old_die);
20320           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20321             add_AT_file (subr_die, DW_AT_decl_file, file_index);
20322           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20323             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
20324         }
20325     }
20326   else
20327     {
20328       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
20329
20330       if (TREE_PUBLIC (decl))
20331         add_AT_flag (subr_die, DW_AT_external, 1);
20332
20333       add_name_and_src_coords_attributes (subr_die, decl);
20334       if (debug_info_level > DINFO_LEVEL_TERSE)
20335         {
20336           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
20337           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
20338                               0, 0, context_die);
20339         }
20340
20341       add_pure_or_virtual_attribute (subr_die, decl);
20342       if (DECL_ARTIFICIAL (decl))
20343         add_AT_flag (subr_die, DW_AT_artificial, 1);
20344
20345       add_accessibility_attribute (subr_die, decl);
20346     }
20347
20348   if (declaration)
20349     {
20350       if (!old_die || !get_AT (old_die, DW_AT_inline))
20351         {
20352           add_AT_flag (subr_die, DW_AT_declaration, 1);
20353
20354           /* If this is an explicit function declaration then generate
20355              a DW_AT_explicit attribute.  */
20356           if (lang_hooks.decls.function_decl_explicit_p (decl)
20357               && (dwarf_version >= 3 || !dwarf_strict))
20358             add_AT_flag (subr_die, DW_AT_explicit, 1);
20359
20360           /* The first time we see a member function, it is in the context of
20361              the class to which it belongs.  We make sure of this by emitting
20362              the class first.  The next time is the definition, which is
20363              handled above.  The two may come from the same source text.
20364
20365              Note that force_decl_die() forces function declaration die. It is
20366              later reused to represent definition.  */
20367           equate_decl_number_to_die (decl, subr_die);
20368         }
20369     }
20370   else if (DECL_ABSTRACT (decl))
20371     {
20372       if (DECL_DECLARED_INLINE_P (decl))
20373         {
20374           if (cgraph_function_possibly_inlined_p (decl))
20375             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
20376           else
20377             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
20378         }
20379       else
20380         {
20381           if (cgraph_function_possibly_inlined_p (decl))
20382             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
20383           else
20384             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
20385         }
20386
20387       if (DECL_DECLARED_INLINE_P (decl)
20388           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
20389         add_AT_flag (subr_die, DW_AT_artificial, 1);
20390
20391       equate_decl_number_to_die (decl, subr_die);
20392     }
20393   else if (!DECL_EXTERNAL (decl))
20394     {
20395       HOST_WIDE_INT cfa_fb_offset;
20396
20397       if (!old_die || !get_AT (old_die, DW_AT_inline))
20398         equate_decl_number_to_die (decl, subr_die);
20399
20400       if (!flag_reorder_blocks_and_partition)
20401         {
20402           dw_fde_ref fde = &fde_table[current_funcdef_fde];
20403           if (fde->dw_fde_begin)
20404             {
20405               /* We have already generated the labels.  */
20406               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
20407               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
20408             }
20409           else
20410             {
20411               /* Create start/end labels and add the range.  */
20412               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
20413               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
20414                                            current_function_funcdef_no);
20415               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
20416               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
20417                                            current_function_funcdef_no);
20418               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
20419             }
20420
20421 #if VMS_DEBUGGING_INFO
20422       /* HP OpenVMS Industry Standard 64: DWARF Extensions
20423          Section 2.3 Prologue and Epilogue Attributes:
20424          When a breakpoint is set on entry to a function, it is generally
20425          desirable for execution to be suspended, not on the very first
20426          instruction of the function, but rather at a point after the
20427          function's frame has been set up, after any language defined local
20428          declaration processing has been completed, and before execution of
20429          the first statement of the function begins. Debuggers generally
20430          cannot properly determine where this point is.  Similarly for a
20431          breakpoint set on exit from a function. The prologue and epilogue
20432          attributes allow a compiler to communicate the location(s) to use.  */
20433
20434       {
20435         if (fde->dw_fde_vms_end_prologue)
20436           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
20437             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
20438
20439         if (fde->dw_fde_vms_begin_epilogue)
20440           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
20441             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
20442       }
20443 #endif
20444
20445           add_pubname (decl, subr_die);
20446         }
20447       else
20448         {  /* Generate pubnames entries for the split function code
20449               ranges.  */
20450           dw_fde_ref fde = &fde_table[current_funcdef_fde];
20451
20452           if (fde->dw_fde_second_begin)
20453             {
20454               if (dwarf_version >= 3 || !dwarf_strict)
20455                 {
20456                   /* We should use ranges for non-contiguous code section 
20457                      addresses.  Use the actual code range for the initial
20458                      section, since the HOT/COLD labels might precede an 
20459                      alignment offset.  */
20460                   bool range_list_added = false;
20461                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
20462                                         fde->dw_fde_end, &range_list_added);
20463                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
20464                                         fde->dw_fde_second_end,
20465                                         &range_list_added);
20466                   add_pubname (decl, subr_die);
20467                   if (range_list_added)
20468                     add_ranges (NULL);
20469                 }
20470               else
20471                 {
20472                   /* There is no real support in DW2 for this .. so we make
20473                      a work-around.  First, emit the pub name for the segment
20474                      containing the function label.  Then make and emit a
20475                      simplified subprogram DIE for the second segment with the
20476                      name pre-fixed by __hot/cold_sect_of_.  We use the same
20477                      linkage name for the second die so that gdb will find both
20478                      sections when given "b foo".  */
20479                   const char *name = NULL;
20480                   tree decl_name = DECL_NAME (decl);
20481                   dw_die_ref seg_die;
20482
20483                   /* Do the 'primary' section.   */
20484                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
20485                                  fde->dw_fde_begin);
20486                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
20487                                  fde->dw_fde_end);
20488                   /* Add it.   */
20489                   add_pubname (decl, subr_die);
20490
20491                   /* Build a minimal DIE for the secondary section.  */
20492                   seg_die = new_die (DW_TAG_subprogram,
20493                                      subr_die->die_parent, decl);
20494
20495                   if (TREE_PUBLIC (decl))
20496                     add_AT_flag (seg_die, DW_AT_external, 1);
20497
20498                   if (decl_name != NULL 
20499                       && IDENTIFIER_POINTER (decl_name) != NULL)
20500                     {
20501                       name = dwarf2_name (decl, 1);
20502                       if (! DECL_ARTIFICIAL (decl))
20503                         add_src_coords_attributes (seg_die, decl);
20504
20505                       add_linkage_name (seg_die, decl);
20506                     }
20507                   gcc_assert (name != NULL);
20508                   add_pure_or_virtual_attribute (seg_die, decl);
20509                   if (DECL_ARTIFICIAL (decl))
20510                     add_AT_flag (seg_die, DW_AT_artificial, 1);
20511
20512                   name = concat ("__second_sect_of_", name, NULL); 
20513                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
20514                                  fde->dw_fde_second_begin);
20515                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
20516                                  fde->dw_fde_second_end);
20517                   add_name_attribute (seg_die, name);
20518                   add_pubname_string (name, seg_die);
20519                 }
20520             }
20521           else
20522             {
20523               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
20524               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
20525               add_pubname (decl, subr_die);
20526             }
20527         }
20528
20529 #ifdef MIPS_DEBUGGING_INFO
20530       /* Add a reference to the FDE for this routine.  */
20531       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
20532 #endif
20533
20534       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
20535
20536       /* We define the "frame base" as the function's CFA.  This is more
20537          convenient for several reasons: (1) It's stable across the prologue
20538          and epilogue, which makes it better than just a frame pointer,
20539          (2) With dwarf3, there exists a one-byte encoding that allows us
20540          to reference the .debug_frame data by proxy, but failing that,
20541          (3) We can at least reuse the code inspection and interpretation
20542          code that determines the CFA position at various points in the
20543          function.  */
20544       if (dwarf_version >= 3)
20545         {
20546           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
20547           add_AT_loc (subr_die, DW_AT_frame_base, op);
20548         }
20549       else
20550         {
20551           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
20552           if (list->dw_loc_next)
20553             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
20554           else
20555             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
20556         }
20557
20558       /* Compute a displacement from the "steady-state frame pointer" to
20559          the CFA.  The former is what all stack slots and argument slots
20560          will reference in the rtl; the later is what we've told the
20561          debugger about.  We'll need to adjust all frame_base references
20562          by this displacement.  */
20563       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
20564
20565       if (cfun->static_chain_decl)
20566         add_AT_location_description (subr_die, DW_AT_static_link,
20567                  loc_list_from_tree (cfun->static_chain_decl, 2));
20568     }
20569
20570   /* Generate child dies for template paramaters.  */
20571   if (debug_info_level > DINFO_LEVEL_TERSE)
20572     gen_generic_params_dies (decl);
20573
20574   /* Now output descriptions of the arguments for this function. This gets
20575      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
20576      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
20577      `...' at the end of the formal parameter list.  In order to find out if
20578      there was a trailing ellipsis or not, we must instead look at the type
20579      associated with the FUNCTION_DECL.  This will be a node of type
20580      FUNCTION_TYPE. If the chain of type nodes hanging off of this
20581      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
20582      an ellipsis at the end.  */
20583
20584   /* In the case where we are describing a mere function declaration, all we
20585      need to do here (and all we *can* do here) is to describe the *types* of
20586      its formal parameters.  */
20587   if (debug_info_level <= DINFO_LEVEL_TERSE)
20588     ;
20589   else if (declaration)
20590     gen_formal_types_die (decl, subr_die);
20591   else
20592     {
20593       /* Generate DIEs to represent all known formal parameters.  */
20594       tree parm = DECL_ARGUMENTS (decl);
20595       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
20596       tree generic_decl_parm = generic_decl
20597                                 ? DECL_ARGUMENTS (generic_decl)
20598                                 : NULL;
20599
20600       /* Now we want to walk the list of parameters of the function and
20601          emit their relevant DIEs.
20602
20603          We consider the case of DECL being an instance of a generic function
20604          as well as it being a normal function.
20605
20606          If DECL is an instance of a generic function we walk the
20607          parameters of the generic function declaration _and_ the parameters of
20608          DECL itself. This is useful because we want to emit specific DIEs for
20609          function parameter packs and those are declared as part of the
20610          generic function declaration. In that particular case,
20611          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
20612          That DIE has children DIEs representing the set of arguments
20613          of the pack. Note that the set of pack arguments can be empty.
20614          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
20615          children DIE.
20616
20617          Otherwise, we just consider the parameters of DECL.  */
20618       while (generic_decl_parm || parm)
20619         {
20620           if (generic_decl_parm
20621               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
20622             gen_formal_parameter_pack_die (generic_decl_parm,
20623                                            parm, subr_die,
20624                                            &parm);
20625           else if (parm)
20626             {
20627               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
20628
20629               if (parm == DECL_ARGUMENTS (decl)
20630                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
20631                   && parm_die
20632                   && (dwarf_version >= 3 || !dwarf_strict))
20633                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
20634
20635               parm = DECL_CHAIN (parm);
20636             }
20637
20638           if (generic_decl_parm)
20639             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20640         }
20641
20642       /* Decide whether we need an unspecified_parameters DIE at the end.
20643          There are 2 more cases to do this for: 1) the ansi ... declaration -
20644          this is detectable when the end of the arg list is not a
20645          void_type_node 2) an unprototyped function declaration (not a
20646          definition).  This just means that we have no info about the
20647          parameters at all.  */
20648       if (prototype_p (TREE_TYPE (decl)))
20649         {
20650           /* This is the prototyped case, check for....  */
20651           if (stdarg_p (TREE_TYPE (decl)))
20652             gen_unspecified_parameters_die (decl, subr_die);
20653         }
20654       else if (DECL_INITIAL (decl) == NULL_TREE)
20655         gen_unspecified_parameters_die (decl, subr_die);
20656     }
20657
20658   /* Output Dwarf info for all of the stuff within the body of the function
20659      (if it has one - it may be just a declaration).  */
20660   outer_scope = DECL_INITIAL (decl);
20661
20662   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20663      a function.  This BLOCK actually represents the outermost binding contour
20664      for the function, i.e. the contour in which the function's formal
20665      parameters and labels get declared. Curiously, it appears that the front
20666      end doesn't actually put the PARM_DECL nodes for the current function onto
20667      the BLOCK_VARS list for this outer scope, but are strung off of the
20668      DECL_ARGUMENTS list for the function instead.
20669
20670      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20671      the LABEL_DECL nodes for the function however, and we output DWARF info
20672      for those in decls_for_scope.  Just within the `outer_scope' there will be
20673      a BLOCK node representing the function's outermost pair of curly braces,
20674      and any blocks used for the base and member initializers of a C++
20675      constructor function.  */
20676   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
20677     {
20678       int call_site_note_count = 0;
20679       int tail_call_site_note_count = 0;
20680
20681       /* Emit a DW_TAG_variable DIE for a named return value.  */
20682       if (DECL_NAME (DECL_RESULT (decl)))
20683         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
20684
20685       current_function_has_inlines = 0;
20686       decls_for_scope (outer_scope, subr_die, 0);
20687
20688       if (call_arg_locations && !dwarf_strict)
20689         {
20690           struct call_arg_loc_node *ca_loc;
20691           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20692             {
20693               dw_die_ref die = NULL;
20694               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20695               rtx arg, next_arg;
20696
20697               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
20698                    arg; arg = next_arg)
20699                 {
20700                   dw_loc_descr_ref reg, val;
20701                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20702                   dw_die_ref cdie, tdie = NULL;
20703
20704                   next_arg = XEXP (arg, 1);
20705                   if (REG_P (XEXP (XEXP (arg, 0), 0))
20706                       && next_arg
20707                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20708                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20709                       && REGNO (XEXP (XEXP (arg, 0), 0))
20710                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20711                     next_arg = XEXP (next_arg, 1);
20712                   if (mode == VOIDmode)
20713                     {
20714                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20715                       if (mode == VOIDmode)
20716                         mode = GET_MODE (XEXP (arg, 0));
20717                     }
20718                   if (mode == VOIDmode || mode == BLKmode)
20719                     continue;
20720                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20721                     {
20722                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20723                       tloc = XEXP (XEXP (arg, 0), 1);
20724                       continue;
20725                     }
20726                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20727                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20728                     {
20729                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20730                       tlocc = XEXP (XEXP (arg, 0), 1);
20731                       continue;
20732                     }
20733                   reg = NULL;
20734                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
20735                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20736                                               VAR_INIT_STATUS_INITIALIZED);
20737                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20738                     {
20739                       rtx mem = XEXP (XEXP (arg, 0), 0);
20740                       reg = mem_loc_descriptor (XEXP (mem, 0),
20741                                                 get_address_mode (mem),
20742                                                 GET_MODE (mem),
20743                                                 VAR_INIT_STATUS_INITIALIZED);
20744                     }
20745                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
20746                            == DEBUG_PARAMETER_REF)
20747                     {
20748                       tree tdecl
20749                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
20750                       tdie = lookup_decl_die (tdecl);
20751                       if (tdie == NULL)
20752                         continue;
20753                     }
20754                   else
20755                     continue;
20756                   if (reg == NULL
20757                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
20758                          != DEBUG_PARAMETER_REF)
20759                     continue;
20760                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20761                                             VOIDmode,
20762                                             VAR_INIT_STATUS_INITIALIZED);
20763                   if (val == NULL)
20764                     continue;
20765                   if (die == NULL)
20766                     die = gen_call_site_die (decl, subr_die, ca_loc);
20767                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20768                                   NULL_TREE);
20769                   if (reg != NULL)
20770                     add_AT_loc (cdie, DW_AT_location, reg);
20771                   else if (tdie != NULL)
20772                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
20773                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20774                   if (next_arg != XEXP (arg, 1))
20775                     {
20776                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20777                       if (mode == VOIDmode)
20778                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20779                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20780                                                             0), 1),
20781                                                 mode, VOIDmode,
20782                                                 VAR_INIT_STATUS_INITIALIZED);
20783                       if (val != NULL)
20784                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20785                     }
20786                 }
20787               if (die == NULL
20788                   && (ca_loc->symbol_ref || tloc))
20789                 die = gen_call_site_die (decl, subr_die, ca_loc);
20790               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20791                 {
20792                   dw_loc_descr_ref tval = NULL;
20793
20794                   if (tloc != NULL_RTX)
20795                     tval = mem_loc_descriptor (tloc,
20796                                                GET_MODE (tloc) == VOIDmode
20797                                                ? Pmode : GET_MODE (tloc),
20798                                                VOIDmode,
20799                                                VAR_INIT_STATUS_INITIALIZED);
20800                   if (tval)
20801                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
20802                   else if (tlocc != NULL_RTX)
20803                     {
20804                       tval = mem_loc_descriptor (tlocc,
20805                                                  GET_MODE (tlocc) == VOIDmode
20806                                                  ? Pmode : GET_MODE (tlocc),
20807                                                  VOIDmode,
20808                                                  VAR_INIT_STATUS_INITIALIZED);
20809                       if (tval)
20810                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
20811                                     tval);
20812                     }
20813                 }
20814               if (die != NULL)
20815                 {
20816                   call_site_note_count++;
20817                   if (ca_loc->tail_call_p)
20818                     tail_call_site_note_count++;
20819                 }
20820             }
20821         }
20822       call_arg_locations = NULL;
20823       call_arg_loc_last = NULL;
20824       if (tail_call_site_count >= 0
20825           && tail_call_site_count == tail_call_site_note_count
20826           && !dwarf_strict)
20827         {
20828           if (call_site_count >= 0
20829               && call_site_count == call_site_note_count)
20830             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
20831           else
20832             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
20833         }
20834       call_site_count = -1;
20835       tail_call_site_count = -1;
20836     }
20837   /* Add the calling convention attribute if requested.  */
20838   add_calling_convention_attribute (subr_die, decl);
20839
20840 }
20841
20842 /* Returns a hash value for X (which really is a die_struct).  */
20843
20844 static hashval_t
20845 common_block_die_table_hash (const void *x)
20846 {
20847   const_dw_die_ref d = (const_dw_die_ref) x;
20848   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
20849 }
20850
20851 /* Return nonzero if decl_id and die_parent of die_struct X is the same
20852    as decl_id and die_parent of die_struct Y.  */
20853
20854 static int
20855 common_block_die_table_eq (const void *x, const void *y)
20856 {
20857   const_dw_die_ref d = (const_dw_die_ref) x;
20858   const_dw_die_ref e = (const_dw_die_ref) y;
20859   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
20860 }
20861
20862 /* Generate a DIE to represent a declared data object.
20863    Either DECL or ORIGIN must be non-null.  */
20864
20865 static void
20866 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
20867 {
20868   HOST_WIDE_INT off;
20869   tree com_decl;
20870   tree decl_or_origin = decl ? decl : origin;
20871   tree ultimate_origin;
20872   dw_die_ref var_die;
20873   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
20874   dw_die_ref origin_die;
20875   bool declaration = (DECL_EXTERNAL (decl_or_origin)
20876                       || class_or_namespace_scope_p (context_die));
20877   bool specialization_p = false;
20878
20879   ultimate_origin = decl_ultimate_origin (decl_or_origin);
20880   if (decl || ultimate_origin)
20881     origin = ultimate_origin;
20882   com_decl = fortran_common (decl_or_origin, &off);
20883
20884   /* Symbol in common gets emitted as a child of the common block, in the form
20885      of a data member.  */
20886   if (com_decl)
20887     {
20888       dw_die_ref com_die;
20889       dw_loc_list_ref loc;
20890       die_node com_die_arg;
20891
20892       var_die = lookup_decl_die (decl_or_origin);
20893       if (var_die)
20894         {
20895           if (get_AT (var_die, DW_AT_location) == NULL)
20896             {
20897               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
20898               if (loc)
20899                 {
20900                   if (off)
20901                     {
20902                       /* Optimize the common case.  */
20903                       if (single_element_loc_list_p (loc)
20904                           && loc->expr->dw_loc_opc == DW_OP_addr
20905                           && loc->expr->dw_loc_next == NULL
20906                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
20907                              == SYMBOL_REF)
20908                         loc->expr->dw_loc_oprnd1.v.val_addr
20909                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20910                         else
20911                           loc_list_plus_const (loc, off);
20912                     }
20913                   add_AT_location_description (var_die, DW_AT_location, loc);
20914                   remove_AT (var_die, DW_AT_declaration);
20915                 }
20916             }
20917           return;
20918         }
20919
20920       if (common_block_die_table == NULL)
20921         common_block_die_table
20922           = htab_create_ggc (10, common_block_die_table_hash,
20923                              common_block_die_table_eq, NULL);
20924
20925       com_die_arg.decl_id = DECL_UID (com_decl);
20926       com_die_arg.die_parent = context_die;
20927       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
20928       loc = loc_list_from_tree (com_decl, 2);
20929       if (com_die == NULL)
20930         {
20931           const char *cnam
20932             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
20933           void **slot;
20934
20935           com_die = new_die (DW_TAG_common_block, context_die, decl);
20936           add_name_and_src_coords_attributes (com_die, com_decl);
20937           if (loc)
20938             {
20939               add_AT_location_description (com_die, DW_AT_location, loc);
20940               /* Avoid sharing the same loc descriptor between
20941                  DW_TAG_common_block and DW_TAG_variable.  */
20942               loc = loc_list_from_tree (com_decl, 2);
20943             }
20944           else if (DECL_EXTERNAL (decl))
20945             add_AT_flag (com_die, DW_AT_declaration, 1);
20946           add_pubname_string (cnam, com_die); /* ??? needed? */
20947           com_die->decl_id = DECL_UID (com_decl);
20948           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
20949           *slot = (void *) com_die;
20950         }
20951       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
20952         {
20953           add_AT_location_description (com_die, DW_AT_location, loc);
20954           loc = loc_list_from_tree (com_decl, 2);
20955           remove_AT (com_die, DW_AT_declaration);
20956         }
20957       var_die = new_die (DW_TAG_variable, com_die, decl);
20958       add_name_and_src_coords_attributes (var_die, decl);
20959       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
20960                           TREE_THIS_VOLATILE (decl), context_die);
20961       add_AT_flag (var_die, DW_AT_external, 1);
20962       if (loc)
20963         {
20964           if (off)
20965             {
20966               /* Optimize the common case.  */
20967               if (single_element_loc_list_p (loc)
20968                   && loc->expr->dw_loc_opc == DW_OP_addr
20969                   && loc->expr->dw_loc_next == NULL
20970                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
20971                 loc->expr->dw_loc_oprnd1.v.val_addr
20972                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20973               else
20974                 loc_list_plus_const (loc, off);
20975             }
20976           add_AT_location_description (var_die, DW_AT_location, loc);
20977         }
20978       else if (DECL_EXTERNAL (decl))
20979         add_AT_flag (var_die, DW_AT_declaration, 1);
20980       equate_decl_number_to_die (decl, var_die);
20981       return;
20982     }
20983
20984   /* If the compiler emitted a definition for the DECL declaration
20985      and if we already emitted a DIE for it, don't emit a second
20986      DIE for it again. Allow re-declarations of DECLs that are
20987      inside functions, though.  */
20988   if (old_die && declaration && !local_scope_p (context_die))
20989     return;
20990
20991   /* For static data members, the declaration in the class is supposed
20992      to have DW_TAG_member tag; the specification should still be
20993      DW_TAG_variable referencing the DW_TAG_member DIE.  */
20994   if (declaration && class_scope_p (context_die))
20995     var_die = new_die (DW_TAG_member, context_die, decl);
20996   else
20997     var_die = new_die (DW_TAG_variable, context_die, decl);
20998
20999   origin_die = NULL;
21000   if (origin != NULL)
21001     origin_die = add_abstract_origin_attribute (var_die, origin);
21002
21003   /* Loop unrolling can create multiple blocks that refer to the same
21004      static variable, so we must test for the DW_AT_declaration flag.
21005
21006      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
21007      copy decls and set the DECL_ABSTRACT flag on them instead of
21008      sharing them.
21009
21010      ??? Duplicated blocks have been rewritten to use .debug_ranges.
21011
21012      ??? The declare_in_namespace support causes us to get two DIEs for one
21013      variable, both of which are declarations.  We want to avoid considering
21014      one to be a specification, so we must test that this DIE is not a
21015      declaration.  */
21016   else if (old_die && TREE_STATIC (decl) && ! declaration
21017            && get_AT_flag (old_die, DW_AT_declaration) == 1)
21018     {
21019       /* This is a definition of a C++ class level static.  */
21020       add_AT_specification (var_die, old_die);
21021       specialization_p = true;
21022       if (DECL_NAME (decl))
21023         {
21024           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
21025           struct dwarf_file_data * file_index = lookup_filename (s.file);
21026
21027           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
21028             add_AT_file (var_die, DW_AT_decl_file, file_index);
21029
21030           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
21031             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
21032
21033           if (old_die->die_tag == DW_TAG_member)
21034             add_linkage_name (var_die, decl);
21035         }
21036     }
21037   else
21038     add_name_and_src_coords_attributes (var_die, decl);
21039
21040   if ((origin == NULL && !specialization_p)
21041       || (origin != NULL
21042           && !DECL_ABSTRACT (decl_or_origin)
21043           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
21044                                        decl_function_context
21045                                                         (decl_or_origin))))
21046     {
21047       tree type = TREE_TYPE (decl_or_origin);
21048
21049       if (decl_by_reference_p (decl_or_origin))
21050         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
21051       else
21052         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
21053                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
21054     }
21055
21056   if (origin == NULL && !specialization_p)
21057     {
21058       if (TREE_PUBLIC (decl))
21059         add_AT_flag (var_die, DW_AT_external, 1);
21060
21061       if (DECL_ARTIFICIAL (decl))
21062         add_AT_flag (var_die, DW_AT_artificial, 1);
21063
21064       add_accessibility_attribute (var_die, decl);
21065     }
21066
21067   if (declaration)
21068     add_AT_flag (var_die, DW_AT_declaration, 1);
21069
21070   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
21071     equate_decl_number_to_die (decl, var_die);
21072
21073   if (! declaration
21074       && (! DECL_ABSTRACT (decl_or_origin)
21075           /* Local static vars are shared between all clones/inlines,
21076              so emit DW_AT_location on the abstract DIE if DECL_RTL is
21077              already set.  */
21078           || (TREE_CODE (decl_or_origin) == VAR_DECL
21079               && TREE_STATIC (decl_or_origin)
21080               && DECL_RTL_SET_P (decl_or_origin)))
21081       /* When abstract origin already has DW_AT_location attribute, no need
21082          to add it again.  */
21083       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
21084     {
21085       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
21086           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
21087         defer_location (decl_or_origin, var_die);
21088       else
21089         add_location_or_const_value_attribute (var_die, decl_or_origin,
21090                                                decl == NULL, DW_AT_location);
21091       add_pubname (decl_or_origin, var_die);
21092     }
21093   else
21094     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
21095 }
21096
21097 /* Generate a DIE to represent a named constant.  */
21098
21099 static void
21100 gen_const_die (tree decl, dw_die_ref context_die)
21101 {
21102   dw_die_ref const_die;
21103   tree type = TREE_TYPE (decl);
21104
21105   const_die = new_die (DW_TAG_constant, context_die, decl);
21106   add_name_and_src_coords_attributes (const_die, decl);
21107   add_type_attribute (const_die, type, 1, 0, context_die);
21108   if (TREE_PUBLIC (decl))
21109     add_AT_flag (const_die, DW_AT_external, 1);
21110   if (DECL_ARTIFICIAL (decl))
21111     add_AT_flag (const_die, DW_AT_artificial, 1);
21112   tree_add_const_value_attribute_for_decl (const_die, decl);
21113 }
21114
21115 /* Generate a DIE to represent a label identifier.  */
21116
21117 static void
21118 gen_label_die (tree decl, dw_die_ref context_die)
21119 {
21120   tree origin = decl_ultimate_origin (decl);
21121   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
21122   rtx insn;
21123   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21124
21125   if (origin != NULL)
21126     add_abstract_origin_attribute (lbl_die, origin);
21127   else
21128     add_name_and_src_coords_attributes (lbl_die, decl);
21129
21130   if (DECL_ABSTRACT (decl))
21131     equate_decl_number_to_die (decl, lbl_die);
21132   else
21133     {
21134       insn = DECL_RTL_IF_SET (decl);
21135
21136       /* Deleted labels are programmer specified labels which have been
21137          eliminated because of various optimizations.  We still emit them
21138          here so that it is possible to put breakpoints on them.  */
21139       if (insn
21140           && (LABEL_P (insn)
21141               || ((NOTE_P (insn)
21142                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
21143         {
21144           /* When optimization is enabled (via -O) some parts of the compiler
21145              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
21146              represent source-level labels which were explicitly declared by
21147              the user.  This really shouldn't be happening though, so catch
21148              it if it ever does happen.  */
21149           gcc_assert (!INSN_DELETED_P (insn));
21150
21151           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
21152           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
21153         }
21154     }
21155 }
21156
21157 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
21158    attributes to the DIE for a block STMT, to describe where the inlined
21159    function was called from.  This is similar to add_src_coords_attributes.  */
21160
21161 static inline void
21162 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
21163 {
21164   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
21165
21166   if (dwarf_version >= 3 || !dwarf_strict)
21167     {
21168       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
21169       add_AT_unsigned (die, DW_AT_call_line, s.line);
21170     }
21171 }
21172
21173
21174 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
21175    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
21176
21177 static inline void
21178 add_high_low_attributes (tree stmt, dw_die_ref die)
21179 {
21180   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21181
21182   if (BLOCK_FRAGMENT_CHAIN (stmt)
21183       && (dwarf_version >= 3 || !dwarf_strict))
21184     {
21185       tree chain;
21186
21187       if (inlined_function_outer_scope_p (stmt))
21188         {
21189           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21190                                        BLOCK_NUMBER (stmt));
21191           add_AT_lbl_id (die, DW_AT_entry_pc, label);
21192         }
21193
21194       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
21195
21196       chain = BLOCK_FRAGMENT_CHAIN (stmt);
21197       do
21198         {
21199           add_ranges (chain);
21200           chain = BLOCK_FRAGMENT_CHAIN (chain);
21201         }
21202       while (chain);
21203       add_ranges (NULL);
21204     }
21205   else
21206     {
21207       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
21208                                    BLOCK_NUMBER (stmt));
21209       add_AT_lbl_id (die, DW_AT_low_pc, label);
21210       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
21211                                    BLOCK_NUMBER (stmt));
21212       add_AT_lbl_id (die, DW_AT_high_pc, label);
21213     }
21214 }
21215
21216 /* Generate a DIE for a lexical block.  */
21217
21218 static void
21219 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
21220 {
21221   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
21222
21223   if (call_arg_locations)
21224     {
21225       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
21226         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
21227                                BLOCK_NUMBER (stmt) + 1);
21228       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
21229     }
21230
21231   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
21232     add_high_low_attributes (stmt, stmt_die);
21233
21234   decls_for_scope (stmt, stmt_die, depth);
21235 }
21236
21237 /* Generate a DIE for an inlined subprogram.  */
21238
21239 static void
21240 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
21241 {
21242   tree decl;
21243
21244   /* The instance of function that is effectively being inlined shall not
21245      be abstract.  */
21246   gcc_assert (! BLOCK_ABSTRACT (stmt));
21247
21248   decl = block_ultimate_origin (stmt);
21249
21250   /* Emit info for the abstract instance first, if we haven't yet.  We
21251      must emit this even if the block is abstract, otherwise when we
21252      emit the block below (or elsewhere), we may end up trying to emit
21253      a die whose origin die hasn't been emitted, and crashing.  */
21254   dwarf2out_abstract_function (decl);
21255
21256   if (! BLOCK_ABSTRACT (stmt))
21257     {
21258       dw_die_ref subr_die
21259         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
21260
21261       if (call_arg_locations)
21262         {
21263           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
21264             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
21265                                    BLOCK_NUMBER (stmt) + 1);
21266           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
21267         }
21268       add_abstract_origin_attribute (subr_die, decl);
21269       if (TREE_ASM_WRITTEN (stmt))
21270         add_high_low_attributes (stmt, subr_die);
21271       add_call_src_coords_attributes (stmt, subr_die);
21272
21273       decls_for_scope (stmt, subr_die, depth);
21274       current_function_has_inlines = 1;
21275     }
21276 }
21277
21278 /* Generate a DIE for a field in a record, or structure.  */
21279
21280 static void
21281 gen_field_die (tree decl, dw_die_ref context_die)
21282 {
21283   dw_die_ref decl_die;
21284
21285   if (TREE_TYPE (decl) == error_mark_node)
21286     return;
21287
21288   decl_die = new_die (DW_TAG_member, context_die, decl);
21289   add_name_and_src_coords_attributes (decl_die, decl);
21290   add_type_attribute (decl_die, member_declared_type (decl),
21291                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
21292                       context_die);
21293
21294   if (DECL_BIT_FIELD_TYPE (decl))
21295     {
21296       add_byte_size_attribute (decl_die, decl);
21297       add_bit_size_attribute (decl_die, decl);
21298       add_bit_offset_attribute (decl_die, decl);
21299     }
21300
21301   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
21302     add_data_member_location_attribute (decl_die, decl);
21303
21304   if (DECL_ARTIFICIAL (decl))
21305     add_AT_flag (decl_die, DW_AT_artificial, 1);
21306
21307   add_accessibility_attribute (decl_die, decl);
21308
21309   /* Equate decl number to die, so that we can look up this decl later on.  */
21310   equate_decl_number_to_die (decl, decl_die);
21311 }
21312
21313 #if 0
21314 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21315    Use modified_type_die instead.
21316    We keep this code here just in case these types of DIEs may be needed to
21317    represent certain things in other languages (e.g. Pascal) someday.  */
21318
21319 static void
21320 gen_pointer_type_die (tree type, dw_die_ref context_die)
21321 {
21322   dw_die_ref ptr_die
21323     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
21324
21325   equate_type_number_to_die (type, ptr_die);
21326   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
21327   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21328 }
21329
21330 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
21331    Use modified_type_die instead.
21332    We keep this code here just in case these types of DIEs may be needed to
21333    represent certain things in other languages (e.g. Pascal) someday.  */
21334
21335 static void
21336 gen_reference_type_die (tree type, dw_die_ref context_die)
21337 {
21338   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
21339
21340   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
21341     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
21342   else
21343     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
21344
21345   equate_type_number_to_die (type, ref_die);
21346   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
21347   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
21348 }
21349 #endif
21350
21351 /* Generate a DIE for a pointer to a member type.  */
21352
21353 static void
21354 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
21355 {
21356   dw_die_ref ptr_die
21357     = new_die (DW_TAG_ptr_to_member_type,
21358                scope_die_for (type, context_die), type);
21359
21360   equate_type_number_to_die (type, ptr_die);
21361   add_AT_die_ref (ptr_die, DW_AT_containing_type,
21362                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
21363   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
21364 }
21365
21366 /* Generate the DIE for the compilation unit.  */
21367
21368 static dw_die_ref
21369 gen_compile_unit_die (const char *filename)
21370 {
21371   dw_die_ref die;
21372   char producer[250];
21373   const char *language_string = lang_hooks.name;
21374   int language;
21375
21376   die = new_die (DW_TAG_compile_unit, NULL, NULL);
21377
21378   if (filename)
21379     {
21380       add_name_attribute (die, filename);
21381       /* Don't add cwd for <built-in>.  */
21382       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
21383         add_comp_dir_attribute (die);
21384     }
21385
21386   sprintf (producer, "%s %s", language_string, version_string);
21387
21388 #ifdef MIPS_DEBUGGING_INFO
21389   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
21390      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
21391      not appear in the producer string, the debugger reaches the conclusion
21392      that the object file is stripped and has no debugging information.
21393      To get the MIPS/SGI debugger to believe that there is debugging
21394      information in the object file, we add a -g to the producer string.  */
21395   if (debug_info_level > DINFO_LEVEL_TERSE)
21396     strcat (producer, " -g");
21397 #endif
21398
21399   add_AT_string (die, DW_AT_producer, producer);
21400
21401   /* If our producer is LTO try to figure out a common language to use
21402      from the global list of translation units.  */
21403   if (strcmp (language_string, "GNU GIMPLE") == 0)
21404     {
21405       unsigned i;
21406       tree t;
21407       const char *common_lang = NULL;
21408
21409       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
21410         {
21411           if (!TRANSLATION_UNIT_LANGUAGE (t))
21412             continue;
21413           if (!common_lang)
21414             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
21415           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
21416             ;
21417           else if (strncmp (common_lang, "GNU C", 5) == 0
21418                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
21419             /* Mixing C and C++ is ok, use C++ in that case.  */
21420             common_lang = "GNU C++";
21421           else
21422             {
21423               /* Fall back to C.  */
21424               common_lang = NULL;
21425               break;
21426             }
21427         }
21428
21429       if (common_lang)
21430         language_string = common_lang;
21431     }
21432
21433   language = DW_LANG_C89;
21434   if (strcmp (language_string, "GNU C++") == 0)
21435     language = DW_LANG_C_plus_plus;
21436   else if (strcmp (language_string, "GNU F77") == 0)
21437     language = DW_LANG_Fortran77;
21438   else if (strcmp (language_string, "GNU Pascal") == 0)
21439     language = DW_LANG_Pascal83;
21440   else if (dwarf_version >= 3 || !dwarf_strict)
21441     {
21442       if (strcmp (language_string, "GNU Ada") == 0)
21443         language = DW_LANG_Ada95;
21444       else if (strcmp (language_string, "GNU Fortran") == 0)
21445         language = DW_LANG_Fortran95;
21446       else if (strcmp (language_string, "GNU Java") == 0)
21447         language = DW_LANG_Java;
21448       else if (strcmp (language_string, "GNU Objective-C") == 0)
21449         language = DW_LANG_ObjC;
21450       else if (strcmp (language_string, "GNU Objective-C++") == 0)
21451         language = DW_LANG_ObjC_plus_plus;
21452     }
21453
21454   add_AT_unsigned (die, DW_AT_language, language);
21455
21456   switch (language)
21457     {
21458     case DW_LANG_Fortran77:
21459     case DW_LANG_Fortran90:
21460     case DW_LANG_Fortran95:
21461       /* Fortran has case insensitive identifiers and the front-end
21462          lowercases everything.  */
21463       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
21464       break;
21465     default:
21466       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
21467       break;
21468     }
21469   return die;
21470 }
21471
21472 /* Generate the DIE for a base class.  */
21473
21474 static void
21475 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
21476 {
21477   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
21478
21479   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
21480   add_data_member_location_attribute (die, binfo);
21481
21482   if (BINFO_VIRTUAL_P (binfo))
21483     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
21484
21485   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
21486      children, otherwise the default is DW_ACCESS_public.  In DWARF2
21487      the default has always been DW_ACCESS_private.  */
21488   if (access == access_public_node)
21489     {
21490       if (dwarf_version == 2
21491           || context_die->die_tag == DW_TAG_class_type)
21492       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
21493     }
21494   else if (access == access_protected_node)
21495     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
21496   else if (dwarf_version > 2
21497            && context_die->die_tag != DW_TAG_class_type)
21498     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
21499 }
21500
21501 /* Generate a DIE for a class member.  */
21502
21503 static void
21504 gen_member_die (tree type, dw_die_ref context_die)
21505 {
21506   tree member;
21507   tree binfo = TYPE_BINFO (type);
21508   dw_die_ref child;
21509
21510   /* If this is not an incomplete type, output descriptions of each of its
21511      members. Note that as we output the DIEs necessary to represent the
21512      members of this record or union type, we will also be trying to output
21513      DIEs to represent the *types* of those members. However the `type'
21514      function (above) will specifically avoid generating type DIEs for member
21515      types *within* the list of member DIEs for this (containing) type except
21516      for those types (of members) which are explicitly marked as also being
21517      members of this (containing) type themselves.  The g++ front- end can
21518      force any given type to be treated as a member of some other (containing)
21519      type by setting the TYPE_CONTEXT of the given (member) type to point to
21520      the TREE node representing the appropriate (containing) type.  */
21521
21522   /* First output info about the base classes.  */
21523   if (binfo)
21524     {
21525       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
21526       int i;
21527       tree base;
21528
21529       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
21530         gen_inheritance_die (base,
21531                              (accesses ? VEC_index (tree, accesses, i)
21532                               : access_public_node), context_die);
21533     }
21534
21535   /* Now output info about the data members and type members.  */
21536   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
21537     {
21538       /* If we thought we were generating minimal debug info for TYPE
21539          and then changed our minds, some of the member declarations
21540          may have already been defined.  Don't define them again, but
21541          do put them in the right order.  */
21542
21543       child = lookup_decl_die (member);
21544       if (child)
21545         splice_child_die (context_die, child);
21546       else
21547         gen_decl_die (member, NULL, context_die);
21548     }
21549
21550   /* Now output info about the function members (if any).  */
21551   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
21552     {
21553       /* Don't include clones in the member list.  */
21554       if (DECL_ABSTRACT_ORIGIN (member))
21555         continue;
21556
21557       child = lookup_decl_die (member);
21558       if (child)
21559         splice_child_die (context_die, child);
21560       else
21561         gen_decl_die (member, NULL, context_die);
21562     }
21563 }
21564
21565 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
21566    is set, we pretend that the type was never defined, so we only get the
21567    member DIEs needed by later specification DIEs.  */
21568
21569 static void
21570 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
21571                                 enum debug_info_usage usage)
21572 {
21573   dw_die_ref type_die = lookup_type_die (type);
21574   dw_die_ref scope_die = 0;
21575   int nested = 0;
21576   int complete = (TYPE_SIZE (type)
21577                   && (! TYPE_STUB_DECL (type)
21578                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
21579   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
21580   complete = complete && should_emit_struct_debug (type, usage);
21581
21582   if (type_die && ! complete)
21583     return;
21584
21585   if (TYPE_CONTEXT (type) != NULL_TREE
21586       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
21587           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
21588     nested = 1;
21589
21590   scope_die = scope_die_for (type, context_die);
21591
21592   if (! type_die || (nested && is_cu_die (scope_die)))
21593     /* First occurrence of type or toplevel definition of nested class.  */
21594     {
21595       dw_die_ref old_die = type_die;
21596
21597       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
21598                           ? record_type_tag (type) : DW_TAG_union_type,
21599                           scope_die, type);
21600       equate_type_number_to_die (type, type_die);
21601       if (old_die)
21602         add_AT_specification (type_die, old_die);
21603       else
21604         {
21605           add_name_attribute (type_die, type_tag (type));
21606           add_gnat_descriptive_type_attribute (type_die, type, context_die);
21607         }
21608     }
21609   else
21610     remove_AT (type_die, DW_AT_declaration);
21611
21612   /* Generate child dies for template paramaters.  */
21613   if (debug_info_level > DINFO_LEVEL_TERSE
21614       && COMPLETE_TYPE_P (type))
21615     schedule_generic_params_dies_gen (type);
21616
21617   /* If this type has been completed, then give it a byte_size attribute and
21618      then give a list of members.  */
21619   if (complete && !ns_decl)
21620     {
21621       /* Prevent infinite recursion in cases where the type of some member of
21622          this type is expressed in terms of this type itself.  */
21623       TREE_ASM_WRITTEN (type) = 1;
21624       add_byte_size_attribute (type_die, type);
21625       if (TYPE_STUB_DECL (type) != NULL_TREE)
21626         {
21627           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
21628           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
21629         }
21630
21631       /* If the first reference to this type was as the return type of an
21632          inline function, then it may not have a parent.  Fix this now.  */
21633       if (type_die->die_parent == NULL)
21634         add_child_die (scope_die, type_die);
21635
21636       push_decl_scope (type);
21637       gen_member_die (type, type_die);
21638       pop_decl_scope ();
21639
21640       /* GNU extension: Record what type our vtable lives in.  */
21641       if (TYPE_VFIELD (type))
21642         {
21643           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
21644
21645           gen_type_die (vtype, context_die);
21646           add_AT_die_ref (type_die, DW_AT_containing_type,
21647                           lookup_type_die (vtype));
21648         }
21649     }
21650   else
21651     {
21652       add_AT_flag (type_die, DW_AT_declaration, 1);
21653
21654       /* We don't need to do this for function-local types.  */
21655       if (TYPE_STUB_DECL (type)
21656           && ! decl_function_context (TYPE_STUB_DECL (type)))
21657         VEC_safe_push (tree, gc, incomplete_types, type);
21658     }
21659
21660   if (get_AT (type_die, DW_AT_name))
21661     add_pubtype (type, type_die);
21662 }
21663
21664 /* Generate a DIE for a subroutine _type_.  */
21665
21666 static void
21667 gen_subroutine_type_die (tree type, dw_die_ref context_die)
21668 {
21669   tree return_type = TREE_TYPE (type);
21670   dw_die_ref subr_die
21671     = new_die (DW_TAG_subroutine_type,
21672                scope_die_for (type, context_die), type);
21673
21674   equate_type_number_to_die (type, subr_die);
21675   add_prototyped_attribute (subr_die, type);
21676   add_type_attribute (subr_die, return_type, 0, 0, context_die);
21677   gen_formal_types_die (type, subr_die);
21678
21679   if (get_AT (subr_die, DW_AT_name))
21680     add_pubtype (type, subr_die);
21681 }
21682
21683 /* Generate a DIE for a type definition.  */
21684
21685 static void
21686 gen_typedef_die (tree decl, dw_die_ref context_die)
21687 {
21688   dw_die_ref type_die;
21689   tree origin;
21690
21691   if (TREE_ASM_WRITTEN (decl))
21692     return;
21693
21694   TREE_ASM_WRITTEN (decl) = 1;
21695   type_die = new_die (DW_TAG_typedef, context_die, decl);
21696   origin = decl_ultimate_origin (decl);
21697   if (origin != NULL)
21698     add_abstract_origin_attribute (type_die, origin);
21699   else
21700     {
21701       tree type;
21702
21703       add_name_and_src_coords_attributes (type_die, decl);
21704       if (DECL_ORIGINAL_TYPE (decl))
21705         {
21706           type = DECL_ORIGINAL_TYPE (decl);
21707
21708           gcc_assert (type != TREE_TYPE (decl));
21709           equate_type_number_to_die (TREE_TYPE (decl), type_die);
21710         }
21711       else
21712         {
21713           type = TREE_TYPE (decl);
21714
21715           if (is_naming_typedef_decl (TYPE_NAME (type)))
21716             {
21717               /* Here, we are in the case of decl being a typedef naming
21718                  an anonymous type, e.g:
21719                      typedef struct {...} foo;
21720                  In that case TREE_TYPE (decl) is not a typedef variant
21721                  type and TYPE_NAME of the anonymous type is set to the
21722                  TYPE_DECL of the typedef. This construct is emitted by
21723                  the C++ FE.
21724
21725                  TYPE is the anonymous struct named by the typedef
21726                  DECL. As we need the DW_AT_type attribute of the
21727                  DW_TAG_typedef to point to the DIE of TYPE, let's
21728                  generate that DIE right away. add_type_attribute
21729                  called below will then pick (via lookup_type_die) that
21730                  anonymous struct DIE.  */
21731               if (!TREE_ASM_WRITTEN (type))
21732                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
21733
21734               /* This is a GNU Extension.  We are adding a
21735                  DW_AT_linkage_name attribute to the DIE of the
21736                  anonymous struct TYPE.  The value of that attribute
21737                  is the name of the typedef decl naming the anonymous
21738                  struct.  This greatly eases the work of consumers of
21739                  this debug info.  */
21740               add_linkage_attr (lookup_type_die (type), decl);
21741             }
21742         }
21743
21744       add_type_attribute (type_die, type, TREE_READONLY (decl),
21745                           TREE_THIS_VOLATILE (decl), context_die);
21746
21747       if (is_naming_typedef_decl (decl))
21748         /* We want that all subsequent calls to lookup_type_die with
21749            TYPE in argument yield the DW_TAG_typedef we have just
21750            created.  */
21751         equate_type_number_to_die (type, type_die);
21752
21753       add_accessibility_attribute (type_die, decl);
21754     }
21755
21756   if (DECL_ABSTRACT (decl))
21757     equate_decl_number_to_die (decl, type_die);
21758
21759   if (get_AT (type_die, DW_AT_name))
21760     add_pubtype (decl, type_die);
21761 }
21762
21763 /* Generate a DIE for a struct, class, enum or union type.  */
21764
21765 static void
21766 gen_tagged_type_die (tree type,
21767                      dw_die_ref context_die,
21768                      enum debug_info_usage usage)
21769 {
21770   int need_pop;
21771
21772   if (type == NULL_TREE
21773       || !is_tagged_type (type))
21774     return;
21775
21776   /* If this is a nested type whose containing class hasn't been written
21777      out yet, writing it out will cover this one, too.  This does not apply
21778      to instantiations of member class templates; they need to be added to
21779      the containing class as they are generated.  FIXME: This hurts the
21780      idea of combining type decls from multiple TUs, since we can't predict
21781      what set of template instantiations we'll get.  */
21782   if (TYPE_CONTEXT (type)
21783       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
21784       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
21785     {
21786       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
21787
21788       if (TREE_ASM_WRITTEN (type))
21789         return;
21790
21791       /* If that failed, attach ourselves to the stub.  */
21792       push_decl_scope (TYPE_CONTEXT (type));
21793       context_die = lookup_type_die (TYPE_CONTEXT (type));
21794       need_pop = 1;
21795     }
21796   else if (TYPE_CONTEXT (type) != NULL_TREE
21797            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
21798     {
21799       /* If this type is local to a function that hasn't been written
21800          out yet, use a NULL context for now; it will be fixed up in
21801          decls_for_scope.  */
21802       context_die = lookup_decl_die (TYPE_CONTEXT (type));
21803       /* A declaration DIE doesn't count; nested types need to go in the
21804          specification.  */
21805       if (context_die && is_declaration_die (context_die))
21806         context_die = NULL;
21807       need_pop = 0;
21808     }
21809   else
21810     {
21811       context_die = declare_in_namespace (type, context_die);
21812       need_pop = 0;
21813     }
21814
21815   if (TREE_CODE (type) == ENUMERAL_TYPE)
21816     {
21817       /* This might have been written out by the call to
21818          declare_in_namespace.  */
21819       if (!TREE_ASM_WRITTEN (type))
21820         gen_enumeration_type_die (type, context_die);
21821     }
21822   else
21823     gen_struct_or_union_type_die (type, context_die, usage);
21824
21825   if (need_pop)
21826     pop_decl_scope ();
21827
21828   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
21829      it up if it is ever completed.  gen_*_type_die will set it for us
21830      when appropriate.  */
21831 }
21832
21833 /* Generate a type description DIE.  */
21834
21835 static void
21836 gen_type_die_with_usage (tree type, dw_die_ref context_die,
21837                          enum debug_info_usage usage)
21838 {
21839   struct array_descr_info info;
21840
21841   if (type == NULL_TREE || type == error_mark_node)
21842     return;
21843
21844   if (TYPE_NAME (type) != NULL_TREE
21845       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21846       && is_redundant_typedef (TYPE_NAME (type))
21847       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
21848     /* The DECL of this type is a typedef we don't want to emit debug
21849        info for but we want debug info for its underlying typedef.
21850        This can happen for e.g, the injected-class-name of a C++
21851        type.  */
21852     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
21853
21854   /* If TYPE is a typedef type variant, let's generate debug info
21855      for the parent typedef which TYPE is a type of.  */
21856   if (typedef_variant_p (type))
21857     {
21858       if (TREE_ASM_WRITTEN (type))
21859         return;
21860
21861       /* Prevent broken recursion; we can't hand off to the same type.  */
21862       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
21863
21864       /* Use the DIE of the containing namespace as the parent DIE of
21865          the type description DIE we want to generate.  */
21866       if (DECL_CONTEXT (TYPE_NAME (type))
21867           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21868         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21869
21870       TREE_ASM_WRITTEN (type) = 1;
21871
21872       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21873       return;
21874     }
21875
21876   /* If type is an anonymous tagged type named by a typedef, let's
21877      generate debug info for the typedef.  */
21878   if (is_naming_typedef_decl (TYPE_NAME (type)))
21879     {
21880       /* Use the DIE of the containing namespace as the parent DIE of
21881          the type description DIE we want to generate.  */
21882       if (DECL_CONTEXT (TYPE_NAME (type))
21883           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21884         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21885       
21886       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21887       return;
21888     }
21889
21890   /* If this is an array type with hidden descriptor, handle it first.  */
21891   if (!TREE_ASM_WRITTEN (type)
21892       && lang_hooks.types.get_array_descr_info
21893       && lang_hooks.types.get_array_descr_info (type, &info)
21894       && (dwarf_version >= 3 || !dwarf_strict))
21895     {
21896       gen_descr_array_type_die (type, &info, context_die);
21897       TREE_ASM_WRITTEN (type) = 1;
21898       return;
21899     }
21900
21901   /* We are going to output a DIE to represent the unqualified version
21902      of this type (i.e. without any const or volatile qualifiers) so
21903      get the main variant (i.e. the unqualified version) of this type
21904      now.  (Vectors are special because the debugging info is in the
21905      cloned type itself).  */
21906   if (TREE_CODE (type) != VECTOR_TYPE)
21907     type = type_main_variant (type);
21908
21909   if (TREE_ASM_WRITTEN (type))
21910     return;
21911
21912   switch (TREE_CODE (type))
21913     {
21914     case ERROR_MARK:
21915       break;
21916
21917     case POINTER_TYPE:
21918     case REFERENCE_TYPE:
21919       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
21920          ensures that the gen_type_die recursion will terminate even if the
21921          type is recursive.  Recursive types are possible in Ada.  */
21922       /* ??? We could perhaps do this for all types before the switch
21923          statement.  */
21924       TREE_ASM_WRITTEN (type) = 1;
21925
21926       /* For these types, all that is required is that we output a DIE (or a
21927          set of DIEs) to represent the "basis" type.  */
21928       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21929                                 DINFO_USAGE_IND_USE);
21930       break;
21931
21932     case OFFSET_TYPE:
21933       /* This code is used for C++ pointer-to-data-member types.
21934          Output a description of the relevant class type.  */
21935       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
21936                                         DINFO_USAGE_IND_USE);
21937
21938       /* Output a description of the type of the object pointed to.  */
21939       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21940                                         DINFO_USAGE_IND_USE);
21941
21942       /* Now output a DIE to represent this pointer-to-data-member type
21943          itself.  */
21944       gen_ptr_to_mbr_type_die (type, context_die);
21945       break;
21946
21947     case FUNCTION_TYPE:
21948       /* Force out return type (in case it wasn't forced out already).  */
21949       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21950                                         DINFO_USAGE_DIR_USE);
21951       gen_subroutine_type_die (type, context_die);
21952       break;
21953
21954     case METHOD_TYPE:
21955       /* Force out return type (in case it wasn't forced out already).  */
21956       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21957                                         DINFO_USAGE_DIR_USE);
21958       gen_subroutine_type_die (type, context_die);
21959       break;
21960
21961     case ARRAY_TYPE:
21962       gen_array_type_die (type, context_die);
21963       break;
21964
21965     case VECTOR_TYPE:
21966       gen_array_type_die (type, context_die);
21967       break;
21968
21969     case ENUMERAL_TYPE:
21970     case RECORD_TYPE:
21971     case UNION_TYPE:
21972     case QUAL_UNION_TYPE:
21973       gen_tagged_type_die (type, context_die, usage);
21974       return;
21975
21976     case VOID_TYPE:
21977     case INTEGER_TYPE:
21978     case REAL_TYPE:
21979     case FIXED_POINT_TYPE:
21980     case COMPLEX_TYPE:
21981     case BOOLEAN_TYPE:
21982       /* No DIEs needed for fundamental types.  */
21983       break;
21984
21985     case NULLPTR_TYPE:
21986     case LANG_TYPE:
21987       /* Just use DW_TAG_unspecified_type.  */
21988       {
21989         dw_die_ref type_die = lookup_type_die (type);
21990         if (type_die == NULL)
21991           {
21992             tree name = TYPE_NAME (type);
21993             if (TREE_CODE (name) == TYPE_DECL)
21994               name = DECL_NAME (name);
21995             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
21996             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
21997             equate_type_number_to_die (type, type_die);
21998           }
21999       }
22000       break;
22001
22002     default:
22003       gcc_unreachable ();
22004     }
22005
22006   TREE_ASM_WRITTEN (type) = 1;
22007 }
22008
22009 static void
22010 gen_type_die (tree type, dw_die_ref context_die)
22011 {
22012   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
22013 }
22014
22015 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
22016    things which are local to the given block.  */
22017
22018 static void
22019 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
22020 {
22021   int must_output_die = 0;
22022   bool inlined_func;
22023
22024   /* Ignore blocks that are NULL.  */
22025   if (stmt == NULL_TREE)
22026     return;
22027
22028   inlined_func = inlined_function_outer_scope_p (stmt);
22029
22030   /* If the block is one fragment of a non-contiguous block, do not
22031      process the variables, since they will have been done by the
22032      origin block.  Do process subblocks.  */
22033   if (BLOCK_FRAGMENT_ORIGIN (stmt))
22034     {
22035       tree sub;
22036
22037       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
22038         gen_block_die (sub, context_die, depth + 1);
22039
22040       return;
22041     }
22042
22043   /* Determine if we need to output any Dwarf DIEs at all to represent this
22044      block.  */
22045   if (inlined_func)
22046     /* The outer scopes for inlinings *must* always be represented.  We
22047        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
22048     must_output_die = 1;
22049   else
22050     {
22051       /* Determine if this block directly contains any "significant"
22052          local declarations which we will need to output DIEs for.  */
22053       if (debug_info_level > DINFO_LEVEL_TERSE)
22054         /* We are not in terse mode so *any* local declaration counts
22055            as being a "significant" one.  */
22056         must_output_die = ((BLOCK_VARS (stmt) != NULL
22057                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
22058                            && (TREE_USED (stmt)
22059                                || TREE_ASM_WRITTEN (stmt)
22060                                || BLOCK_ABSTRACT (stmt)));
22061       else if ((TREE_USED (stmt)
22062                 || TREE_ASM_WRITTEN (stmt)
22063                 || BLOCK_ABSTRACT (stmt))
22064                && !dwarf2out_ignore_block (stmt))
22065         must_output_die = 1;
22066     }
22067
22068   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
22069      DIE for any block which contains no significant local declarations at
22070      all.  Rather, in such cases we just call `decls_for_scope' so that any
22071      needed Dwarf info for any sub-blocks will get properly generated. Note
22072      that in terse mode, our definition of what constitutes a "significant"
22073      local declaration gets restricted to include only inlined function
22074      instances and local (nested) function definitions.  */
22075   if (must_output_die)
22076     {
22077       if (inlined_func)
22078         {
22079           /* If STMT block is abstract, that means we have been called
22080              indirectly from dwarf2out_abstract_function.
22081              That function rightfully marks the descendent blocks (of
22082              the abstract function it is dealing with) as being abstract,
22083              precisely to prevent us from emitting any
22084              DW_TAG_inlined_subroutine DIE as a descendent
22085              of an abstract function instance. So in that case, we should
22086              not call gen_inlined_subroutine_die.
22087
22088              Later though, when cgraph asks dwarf2out to emit info
22089              for the concrete instance of the function decl into which
22090              the concrete instance of STMT got inlined, the later will lead
22091              to the generation of a DW_TAG_inlined_subroutine DIE.  */
22092           if (! BLOCK_ABSTRACT (stmt))
22093             gen_inlined_subroutine_die (stmt, context_die, depth);
22094         }
22095       else
22096         gen_lexical_block_die (stmt, context_die, depth);
22097     }
22098   else
22099     decls_for_scope (stmt, context_die, depth);
22100 }
22101
22102 /* Process variable DECL (or variable with origin ORIGIN) within
22103    block STMT and add it to CONTEXT_DIE.  */
22104 static void
22105 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
22106 {
22107   dw_die_ref die;
22108   tree decl_or_origin = decl ? decl : origin;
22109
22110   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
22111     die = lookup_decl_die (decl_or_origin);
22112   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
22113            && TYPE_DECL_IS_STUB (decl_or_origin))
22114     die = lookup_type_die (TREE_TYPE (decl_or_origin));
22115   else
22116     die = NULL;
22117
22118   if (die != NULL && die->die_parent == NULL)
22119     add_child_die (context_die, die);
22120   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
22121     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
22122                                          stmt, context_die);
22123   else
22124     gen_decl_die (decl, origin, context_die);
22125 }
22126
22127 /* Generate all of the decls declared within a given scope and (recursively)
22128    all of its sub-blocks.  */
22129
22130 static void
22131 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
22132 {
22133   tree decl;
22134   unsigned int i;
22135   tree subblocks;
22136
22137   /* Ignore NULL blocks.  */
22138   if (stmt == NULL_TREE)
22139     return;
22140
22141   /* Output the DIEs to represent all of the data objects and typedefs
22142      declared directly within this block but not within any nested
22143      sub-blocks.  Also, nested function and tag DIEs have been
22144      generated with a parent of NULL; fix that up now.  */
22145   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
22146     process_scope_var (stmt, decl, NULL_TREE, context_die);
22147   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
22148     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
22149                        context_die);
22150
22151   /* If we're at -g1, we're not interested in subblocks.  */
22152   if (debug_info_level <= DINFO_LEVEL_TERSE)
22153     return;
22154
22155   /* Output the DIEs to represent all sub-blocks (and the items declared
22156      therein) of this block.  */
22157   for (subblocks = BLOCK_SUBBLOCKS (stmt);
22158        subblocks != NULL;
22159        subblocks = BLOCK_CHAIN (subblocks))
22160     gen_block_die (subblocks, context_die, depth + 1);
22161 }
22162
22163 /* Is this a typedef we can avoid emitting?  */
22164
22165 static inline int
22166 is_redundant_typedef (const_tree decl)
22167 {
22168   if (TYPE_DECL_IS_STUB (decl))
22169     return 1;
22170
22171   if (DECL_ARTIFICIAL (decl)
22172       && DECL_CONTEXT (decl)
22173       && is_tagged_type (DECL_CONTEXT (decl))
22174       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
22175       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
22176     /* Also ignore the artificial member typedef for the class name.  */
22177     return 1;
22178
22179   return 0;
22180 }
22181
22182 /* Return TRUE if TYPE is a typedef that names a type for linkage
22183    purposes. This kind of typedefs is produced by the C++ FE for
22184    constructs like:
22185
22186    typedef struct {...} foo;
22187
22188    In that case, there is no typedef variant type produced for foo.
22189    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
22190    struct type.  */
22191
22192 static bool
22193 is_naming_typedef_decl (const_tree decl)
22194 {
22195   if (decl == NULL_TREE
22196       || TREE_CODE (decl) != TYPE_DECL
22197       || !is_tagged_type (TREE_TYPE (decl))
22198       || DECL_IS_BUILTIN (decl)
22199       || is_redundant_typedef (decl)
22200       /* It looks like Ada produces TYPE_DECLs that are very similar
22201          to C++ naming typedefs but that have different
22202          semantics. Let's be specific to c++ for now.  */
22203       || !is_cxx ())
22204     return FALSE;
22205
22206   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
22207           && TYPE_NAME (TREE_TYPE (decl)) == decl
22208           && (TYPE_STUB_DECL (TREE_TYPE (decl))
22209               != TYPE_NAME (TREE_TYPE (decl))));
22210 }
22211
22212 /* Returns the DIE for a context.  */
22213
22214 static inline dw_die_ref
22215 get_context_die (tree context)
22216 {
22217   if (context)
22218     {
22219       /* Find die that represents this context.  */
22220       if (TYPE_P (context))
22221         {
22222           context = TYPE_MAIN_VARIANT (context);
22223           return strip_naming_typedef (context, force_type_die (context));
22224         }
22225       else
22226         return force_decl_die (context);
22227     }
22228   return comp_unit_die ();
22229 }
22230
22231 /* Returns the DIE for decl.  A DIE will always be returned.  */
22232
22233 static dw_die_ref
22234 force_decl_die (tree decl)
22235 {
22236   dw_die_ref decl_die;
22237   unsigned saved_external_flag;
22238   tree save_fn = NULL_TREE;
22239   decl_die = lookup_decl_die (decl);
22240   if (!decl_die)
22241     {
22242       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
22243
22244       decl_die = lookup_decl_die (decl);
22245       if (decl_die)
22246         return decl_die;
22247
22248       switch (TREE_CODE (decl))
22249         {
22250         case FUNCTION_DECL:
22251           /* Clear current_function_decl, so that gen_subprogram_die thinks
22252              that this is a declaration. At this point, we just want to force
22253              declaration die.  */
22254           save_fn = current_function_decl;
22255           current_function_decl = NULL_TREE;
22256           gen_subprogram_die (decl, context_die);
22257           current_function_decl = save_fn;
22258           break;
22259
22260         case VAR_DECL:
22261           /* Set external flag to force declaration die. Restore it after
22262            gen_decl_die() call.  */
22263           saved_external_flag = DECL_EXTERNAL (decl);
22264           DECL_EXTERNAL (decl) = 1;
22265           gen_decl_die (decl, NULL, context_die);
22266           DECL_EXTERNAL (decl) = saved_external_flag;
22267           break;
22268
22269         case NAMESPACE_DECL:
22270           if (dwarf_version >= 3 || !dwarf_strict)
22271             dwarf2out_decl (decl);
22272           else
22273             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
22274             decl_die = comp_unit_die ();
22275           break;
22276
22277         case TRANSLATION_UNIT_DECL:
22278           decl_die = comp_unit_die ();
22279           break;
22280
22281         default:
22282           gcc_unreachable ();
22283         }
22284
22285       /* We should be able to find the DIE now.  */
22286       if (!decl_die)
22287         decl_die = lookup_decl_die (decl);
22288       gcc_assert (decl_die);
22289     }
22290
22291   return decl_die;
22292 }
22293
22294 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
22295    always returned.  */
22296
22297 static dw_die_ref
22298 force_type_die (tree type)
22299 {
22300   dw_die_ref type_die;
22301
22302   type_die = lookup_type_die (type);
22303   if (!type_die)
22304     {
22305       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
22306
22307       type_die = modified_type_die (type, TYPE_READONLY (type),
22308                                     TYPE_VOLATILE (type), context_die);
22309       gcc_assert (type_die);
22310     }
22311   return type_die;
22312 }
22313
22314 /* Force out any required namespaces to be able to output DECL,
22315    and return the new context_die for it, if it's changed.  */
22316
22317 static dw_die_ref
22318 setup_namespace_context (tree thing, dw_die_ref context_die)
22319 {
22320   tree context = (DECL_P (thing)
22321                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
22322   if (context && TREE_CODE (context) == NAMESPACE_DECL)
22323     /* Force out the namespace.  */
22324     context_die = force_decl_die (context);
22325
22326   return context_die;
22327 }
22328
22329 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
22330    type) within its namespace, if appropriate.
22331
22332    For compatibility with older debuggers, namespace DIEs only contain
22333    declarations; all definitions are emitted at CU scope.  */
22334
22335 static dw_die_ref
22336 declare_in_namespace (tree thing, dw_die_ref context_die)
22337 {
22338   dw_die_ref ns_context;
22339
22340   if (debug_info_level <= DINFO_LEVEL_TERSE)
22341     return context_die;
22342
22343   /* If this decl is from an inlined function, then don't try to emit it in its
22344      namespace, as we will get confused.  It would have already been emitted
22345      when the abstract instance of the inline function was emitted anyways.  */
22346   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
22347     return context_die;
22348
22349   ns_context = setup_namespace_context (thing, context_die);
22350
22351   if (ns_context != context_die)
22352     {
22353       if (is_fortran ())
22354         return ns_context;
22355       if (DECL_P (thing))
22356         gen_decl_die (thing, NULL, ns_context);
22357       else
22358         gen_type_die (thing, ns_context);
22359     }
22360   return context_die;
22361 }
22362
22363 /* Generate a DIE for a namespace or namespace alias.  */
22364
22365 static void
22366 gen_namespace_die (tree decl, dw_die_ref context_die)
22367 {
22368   dw_die_ref namespace_die;
22369
22370   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
22371      they are an alias of.  */
22372   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
22373     {
22374       /* Output a real namespace or module.  */
22375       context_die = setup_namespace_context (decl, comp_unit_die ());
22376       namespace_die = new_die (is_fortran ()
22377                                ? DW_TAG_module : DW_TAG_namespace,
22378                                context_die, decl);
22379       /* For Fortran modules defined in different CU don't add src coords.  */
22380       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
22381         {
22382           const char *name = dwarf2_name (decl, 0);
22383           if (name)
22384             add_name_attribute (namespace_die, name);
22385         }
22386       else
22387         add_name_and_src_coords_attributes (namespace_die, decl);
22388       if (DECL_EXTERNAL (decl))
22389         add_AT_flag (namespace_die, DW_AT_declaration, 1);
22390       equate_decl_number_to_die (decl, namespace_die);
22391     }
22392   else
22393     {
22394       /* Output a namespace alias.  */
22395
22396       /* Force out the namespace we are an alias of, if necessary.  */
22397       dw_die_ref origin_die
22398         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
22399
22400       if (DECL_FILE_SCOPE_P (decl)
22401           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
22402         context_die = setup_namespace_context (decl, comp_unit_die ());
22403       /* Now create the namespace alias DIE.  */
22404       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
22405       add_name_and_src_coords_attributes (namespace_die, decl);
22406       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
22407       equate_decl_number_to_die (decl, namespace_die);
22408     }
22409 }
22410
22411 /* Generate Dwarf debug information for a decl described by DECL.
22412    The return value is currently only meaningful for PARM_DECLs,
22413    for all other decls it returns NULL.  */
22414
22415 static dw_die_ref
22416 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
22417 {
22418   tree decl_or_origin = decl ? decl : origin;
22419   tree class_origin = NULL, ultimate_origin;
22420
22421   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
22422     return NULL;
22423
22424   switch (TREE_CODE (decl_or_origin))
22425     {
22426     case ERROR_MARK:
22427       break;
22428
22429     case CONST_DECL:
22430       if (!is_fortran () && !is_ada ())
22431         {
22432           /* The individual enumerators of an enum type get output when we output
22433              the Dwarf representation of the relevant enum type itself.  */
22434           break;
22435         }
22436
22437       /* Emit its type.  */
22438       gen_type_die (TREE_TYPE (decl), context_die);
22439
22440       /* And its containing namespace.  */
22441       context_die = declare_in_namespace (decl, context_die);
22442
22443       gen_const_die (decl, context_die);
22444       break;
22445
22446     case FUNCTION_DECL:
22447       /* Don't output any DIEs to represent mere function declarations,
22448          unless they are class members or explicit block externs.  */
22449       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
22450           && DECL_FILE_SCOPE_P (decl_or_origin)
22451           && (current_function_decl == NULL_TREE
22452               || DECL_ARTIFICIAL (decl_or_origin)))
22453         break;
22454
22455 #if 0
22456       /* FIXME */
22457       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
22458          on local redeclarations of global functions.  That seems broken.  */
22459       if (current_function_decl != decl)
22460         /* This is only a declaration.  */;
22461 #endif
22462
22463       /* If we're emitting a clone, emit info for the abstract instance.  */
22464       if (origin || DECL_ORIGIN (decl) != decl)
22465         dwarf2out_abstract_function (origin
22466                                      ? DECL_ORIGIN (origin)
22467                                      : DECL_ABSTRACT_ORIGIN (decl));
22468
22469       /* If we're emitting an out-of-line copy of an inline function,
22470          emit info for the abstract instance and set up to refer to it.  */
22471       else if (cgraph_function_possibly_inlined_p (decl)
22472                && ! DECL_ABSTRACT (decl)
22473                && ! class_or_namespace_scope_p (context_die)
22474                /* dwarf2out_abstract_function won't emit a die if this is just
22475                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
22476                   that case, because that works only if we have a die.  */
22477                && DECL_INITIAL (decl) != NULL_TREE)
22478         {
22479           dwarf2out_abstract_function (decl);
22480           set_decl_origin_self (decl);
22481         }
22482
22483       /* Otherwise we're emitting the primary DIE for this decl.  */
22484       else if (debug_info_level > DINFO_LEVEL_TERSE)
22485         {
22486           /* Before we describe the FUNCTION_DECL itself, make sure that we
22487              have its containing type.  */
22488           if (!origin)
22489             origin = decl_class_context (decl);
22490           if (origin != NULL_TREE)
22491             gen_type_die (origin, context_die);
22492
22493           /* And its return type.  */
22494           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
22495
22496           /* And its virtual context.  */
22497           if (DECL_VINDEX (decl) != NULL_TREE)
22498             gen_type_die (DECL_CONTEXT (decl), context_die);
22499
22500           /* Make sure we have a member DIE for decl.  */
22501           if (origin != NULL_TREE)
22502             gen_type_die_for_member (origin, decl, context_die);
22503
22504           /* And its containing namespace.  */
22505           context_die = declare_in_namespace (decl, context_die);
22506         }
22507
22508       /* Now output a DIE to represent the function itself.  */
22509       if (decl)
22510         gen_subprogram_die (decl, context_die);
22511       break;
22512
22513     case TYPE_DECL:
22514       /* If we are in terse mode, don't generate any DIEs to represent any
22515          actual typedefs.  */
22516       if (debug_info_level <= DINFO_LEVEL_TERSE)
22517         break;
22518
22519       /* In the special case of a TYPE_DECL node representing the declaration
22520          of some type tag, if the given TYPE_DECL is marked as having been
22521          instantiated from some other (original) TYPE_DECL node (e.g. one which
22522          was generated within the original definition of an inline function) we
22523          used to generate a special (abbreviated) DW_TAG_structure_type,
22524          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
22525          should be actually referencing those DIEs, as variable DIEs with that
22526          type would be emitted already in the abstract origin, so it was always
22527          removed during unused type prunning.  Don't add anything in this
22528          case.  */
22529       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
22530         break;
22531
22532       if (is_redundant_typedef (decl))
22533         gen_type_die (TREE_TYPE (decl), context_die);
22534       else
22535         /* Output a DIE to represent the typedef itself.  */
22536         gen_typedef_die (decl, context_die);
22537       break;
22538
22539     case LABEL_DECL:
22540       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22541         gen_label_die (decl, context_die);
22542       break;
22543
22544     case VAR_DECL:
22545     case RESULT_DECL:
22546       /* If we are in terse mode, don't generate any DIEs to represent any
22547          variable declarations or definitions.  */
22548       if (debug_info_level <= DINFO_LEVEL_TERSE)
22549         break;
22550
22551       /* Output any DIEs that are needed to specify the type of this data
22552          object.  */
22553       if (decl_by_reference_p (decl_or_origin))
22554         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
22555       else
22556         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
22557
22558       /* And its containing type.  */
22559       class_origin = decl_class_context (decl_or_origin);
22560       if (class_origin != NULL_TREE)
22561         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
22562
22563       /* And its containing namespace.  */
22564       context_die = declare_in_namespace (decl_or_origin, context_die);
22565
22566       /* Now output the DIE to represent the data object itself.  This gets
22567          complicated because of the possibility that the VAR_DECL really
22568          represents an inlined instance of a formal parameter for an inline
22569          function.  */
22570       ultimate_origin = decl_ultimate_origin (decl_or_origin);
22571       if (ultimate_origin != NULL_TREE
22572           && TREE_CODE (ultimate_origin) == PARM_DECL)
22573         gen_formal_parameter_die (decl, origin,
22574                                   true /* Emit name attribute.  */,
22575                                   context_die);
22576       else
22577         gen_variable_die (decl, origin, context_die);
22578       break;
22579
22580     case FIELD_DECL:
22581       /* Ignore the nameless fields that are used to skip bits but handle C++
22582          anonymous unions and structs.  */
22583       if (DECL_NAME (decl) != NULL_TREE
22584           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
22585           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
22586         {
22587           gen_type_die (member_declared_type (decl), context_die);
22588           gen_field_die (decl, context_die);
22589         }
22590       break;
22591
22592     case PARM_DECL:
22593       if (DECL_BY_REFERENCE (decl_or_origin))
22594         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
22595       else
22596         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
22597       return gen_formal_parameter_die (decl, origin,
22598                                        true /* Emit name attribute.  */,
22599                                        context_die);
22600
22601     case NAMESPACE_DECL:
22602     case IMPORTED_DECL:
22603       if (dwarf_version >= 3 || !dwarf_strict)
22604         gen_namespace_die (decl, context_die);
22605       break;
22606
22607     default:
22608       /* Probably some frontend-internal decl.  Assume we don't care.  */
22609       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
22610       break;
22611     }
22612
22613   return NULL;
22614 }
22615 \f
22616 /* Output debug information for global decl DECL.  Called from toplev.c after
22617    compilation proper has finished.  */
22618
22619 static void
22620 dwarf2out_global_decl (tree decl)
22621 {
22622   /* Output DWARF2 information for file-scope tentative data object
22623      declarations, file-scope (extern) function declarations (which
22624      had no corresponding body) and file-scope tagged type declarations
22625      and definitions which have not yet been forced out.  */
22626   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
22627     dwarf2out_decl (decl);
22628 }
22629
22630 /* Output debug information for type decl DECL.  Called from toplev.c
22631    and from language front ends (to record built-in types).  */
22632 static void
22633 dwarf2out_type_decl (tree decl, int local)
22634 {
22635   if (!local)
22636     dwarf2out_decl (decl);
22637 }
22638
22639 /* Output debug information for imported module or decl DECL.
22640    NAME is non-NULL name in the lexical block if the decl has been renamed.
22641    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
22642    that DECL belongs to.
22643    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
22644 static void
22645 dwarf2out_imported_module_or_decl_1 (tree decl,
22646                                      tree name,
22647                                      tree lexical_block,
22648                                      dw_die_ref lexical_block_die)
22649 {
22650   expanded_location xloc;
22651   dw_die_ref imported_die = NULL;
22652   dw_die_ref at_import_die;
22653
22654   if (TREE_CODE (decl) == IMPORTED_DECL)
22655     {
22656       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
22657       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
22658       gcc_assert (decl);
22659     }
22660   else
22661     xloc = expand_location (input_location);
22662
22663   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
22664     {
22665       at_import_die = force_type_die (TREE_TYPE (decl));
22666       /* For namespace N { typedef void T; } using N::T; base_type_die
22667          returns NULL, but DW_TAG_imported_declaration requires
22668          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
22669       if (!at_import_die)
22670         {
22671           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
22672           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
22673           at_import_die = lookup_type_die (TREE_TYPE (decl));
22674           gcc_assert (at_import_die);
22675         }
22676     }
22677   else
22678     {
22679       at_import_die = lookup_decl_die (decl);
22680       if (!at_import_die)
22681         {
22682           /* If we're trying to avoid duplicate debug info, we may not have
22683              emitted the member decl for this field.  Emit it now.  */
22684           if (TREE_CODE (decl) == FIELD_DECL)
22685             {
22686               tree type = DECL_CONTEXT (decl);
22687
22688               if (TYPE_CONTEXT (type)
22689                   && TYPE_P (TYPE_CONTEXT (type))
22690                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
22691                                                 DINFO_USAGE_DIR_USE))
22692                 return;
22693               gen_type_die_for_member (type, decl,
22694                                        get_context_die (TYPE_CONTEXT (type)));
22695             }
22696           at_import_die = force_decl_die (decl);
22697         }
22698     }
22699
22700   if (TREE_CODE (decl) == NAMESPACE_DECL)
22701     {
22702       if (dwarf_version >= 3 || !dwarf_strict)
22703         imported_die = new_die (DW_TAG_imported_module,
22704                                 lexical_block_die,
22705                                 lexical_block);
22706       else
22707         return;
22708     }
22709   else
22710     imported_die = new_die (DW_TAG_imported_declaration,
22711                             lexical_block_die,
22712                             lexical_block);
22713
22714   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
22715   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
22716   if (name)
22717     add_AT_string (imported_die, DW_AT_name,
22718                    IDENTIFIER_POINTER (name));
22719   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
22720 }
22721
22722 /* Output debug information for imported module or decl DECL.
22723    NAME is non-NULL name in context if the decl has been renamed.
22724    CHILD is true if decl is one of the renamed decls as part of
22725    importing whole module.  */
22726
22727 static void
22728 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
22729                                    bool child)
22730 {
22731   /* dw_die_ref at_import_die;  */
22732   dw_die_ref scope_die;
22733
22734   if (debug_info_level <= DINFO_LEVEL_TERSE)
22735     return;
22736
22737   gcc_assert (decl);
22738
22739   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
22740      We need decl DIE for reference and scope die. First, get DIE for the decl
22741      itself.  */
22742
22743   /* Get the scope die for decl context. Use comp_unit_die for global module
22744      or decl. If die is not found for non globals, force new die.  */
22745   if (context
22746       && TYPE_P (context)
22747       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
22748     return;
22749
22750   if (!(dwarf_version >= 3 || !dwarf_strict))
22751     return;
22752
22753   scope_die = get_context_die (context);
22754
22755   if (child)
22756     {
22757       gcc_assert (scope_die->die_child);
22758       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
22759       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
22760       scope_die = scope_die->die_child;
22761     }
22762
22763   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
22764   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
22765
22766 }
22767
22768 /* Write the debugging output for DECL.  */
22769
22770 void
22771 dwarf2out_decl (tree decl)
22772 {
22773   dw_die_ref context_die = comp_unit_die ();
22774
22775   switch (TREE_CODE (decl))
22776     {
22777     case ERROR_MARK:
22778       return;
22779
22780     case FUNCTION_DECL:
22781       /* What we would really like to do here is to filter out all mere
22782          file-scope declarations of file-scope functions which are never
22783          referenced later within this translation unit (and keep all of ones
22784          that *are* referenced later on) but we aren't clairvoyant, so we have
22785          no idea which functions will be referenced in the future (i.e. later
22786          on within the current translation unit). So here we just ignore all
22787          file-scope function declarations which are not also definitions.  If
22788          and when the debugger needs to know something about these functions,
22789          it will have to hunt around and find the DWARF information associated
22790          with the definition of the function.
22791
22792          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
22793          nodes represent definitions and which ones represent mere
22794          declarations.  We have to check DECL_INITIAL instead. That's because
22795          the C front-end supports some weird semantics for "extern inline"
22796          function definitions.  These can get inlined within the current
22797          translation unit (and thus, we need to generate Dwarf info for their
22798          abstract instances so that the Dwarf info for the concrete inlined
22799          instances can have something to refer to) but the compiler never
22800          generates any out-of-lines instances of such things (despite the fact
22801          that they *are* definitions).
22802
22803          The important point is that the C front-end marks these "extern
22804          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
22805          them anyway. Note that the C++ front-end also plays some similar games
22806          for inline function definitions appearing within include files which
22807          also contain `#pragma interface' pragmas.  */
22808       if (DECL_INITIAL (decl) == NULL_TREE)
22809         return;
22810
22811       /* If we're a nested function, initially use a parent of NULL; if we're
22812          a plain function, this will be fixed up in decls_for_scope.  If
22813          we're a method, it will be ignored, since we already have a DIE.  */
22814       if (decl_function_context (decl)
22815           /* But if we're in terse mode, we don't care about scope.  */
22816           && debug_info_level > DINFO_LEVEL_TERSE)
22817         context_die = NULL;
22818       break;
22819
22820     case VAR_DECL:
22821       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
22822          declaration and if the declaration was never even referenced from
22823          within this entire compilation unit.  We suppress these DIEs in
22824          order to save space in the .debug section (by eliminating entries
22825          which are probably useless).  Note that we must not suppress
22826          block-local extern declarations (whether used or not) because that
22827          would screw-up the debugger's name lookup mechanism and cause it to
22828          miss things which really ought to be in scope at a given point.  */
22829       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
22830         return;
22831
22832       /* For local statics lookup proper context die.  */
22833       if (TREE_STATIC (decl) && decl_function_context (decl))
22834         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22835
22836       /* If we are in terse mode, don't generate any DIEs to represent any
22837          variable declarations or definitions.  */
22838       if (debug_info_level <= DINFO_LEVEL_TERSE)
22839         return;
22840       break;
22841
22842     case CONST_DECL:
22843       if (debug_info_level <= DINFO_LEVEL_TERSE)
22844         return;
22845       if (!is_fortran () && !is_ada ())
22846         return;
22847       if (TREE_STATIC (decl) && decl_function_context (decl))
22848         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22849       break;
22850
22851     case NAMESPACE_DECL:
22852     case IMPORTED_DECL:
22853       if (debug_info_level <= DINFO_LEVEL_TERSE)
22854         return;
22855       if (lookup_decl_die (decl) != NULL)
22856         return;
22857       break;
22858
22859     case TYPE_DECL:
22860       /* Don't emit stubs for types unless they are needed by other DIEs.  */
22861       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
22862         return;
22863
22864       /* Don't bother trying to generate any DIEs to represent any of the
22865          normal built-in types for the language we are compiling.  */
22866       if (DECL_IS_BUILTIN (decl))
22867         return;
22868
22869       /* If we are in terse mode, don't generate any DIEs for types.  */
22870       if (debug_info_level <= DINFO_LEVEL_TERSE)
22871         return;
22872
22873       /* If we're a function-scope tag, initially use a parent of NULL;
22874          this will be fixed up in decls_for_scope.  */
22875       if (decl_function_context (decl))
22876         context_die = NULL;
22877
22878       break;
22879
22880     default:
22881       return;
22882     }
22883
22884   gen_decl_die (decl, NULL, context_die);
22885 }
22886
22887 /* Write the debugging output for DECL.  */
22888
22889 static void
22890 dwarf2out_function_decl (tree decl)
22891 {
22892   dwarf2out_decl (decl);
22893   call_arg_locations = NULL;
22894   call_arg_loc_last = NULL;
22895   call_site_count = -1;
22896   tail_call_site_count = -1;
22897   VEC_free (dw_die_ref, heap, block_map);
22898   htab_empty (decl_loc_table);
22899   htab_empty (cached_dw_loc_list_table);
22900 }
22901
22902 /* Output a marker (i.e. a label) for the beginning of the generated code for
22903    a lexical block.  */
22904
22905 static void
22906 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
22907                        unsigned int blocknum)
22908 {
22909   switch_to_section (current_function_section ());
22910   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
22911 }
22912
22913 /* Output a marker (i.e. a label) for the end of the generated code for a
22914    lexical block.  */
22915
22916 static void
22917 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22918 {
22919   switch_to_section (current_function_section ());
22920   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22921 }
22922
22923 /* Returns nonzero if it is appropriate not to emit any debugging
22924    information for BLOCK, because it doesn't contain any instructions.
22925
22926    Don't allow this for blocks with nested functions or local classes
22927    as we would end up with orphans, and in the presence of scheduling
22928    we may end up calling them anyway.  */
22929
22930 static bool
22931 dwarf2out_ignore_block (const_tree block)
22932 {
22933   tree decl;
22934   unsigned int i;
22935
22936   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22937     if (TREE_CODE (decl) == FUNCTION_DECL
22938         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22939       return 0;
22940   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22941     {
22942       decl = BLOCK_NONLOCALIZED_VAR (block, i);
22943       if (TREE_CODE (decl) == FUNCTION_DECL
22944           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22945       return 0;
22946     }
22947
22948   return 1;
22949 }
22950
22951 /* Hash table routines for file_hash.  */
22952
22953 static int
22954 file_table_eq (const void *p1_p, const void *p2_p)
22955 {
22956   const struct dwarf_file_data *const p1 =
22957     (const struct dwarf_file_data *) p1_p;
22958   const char *const p2 = (const char *) p2_p;
22959   return filename_cmp (p1->filename, p2) == 0;
22960 }
22961
22962 static hashval_t
22963 file_table_hash (const void *p_p)
22964 {
22965   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
22966   return htab_hash_string (p->filename);
22967 }
22968
22969 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22970    dwarf2out.c) and return its "index".  The index of each (known) filename is
22971    just a unique number which is associated with only that one filename.  We
22972    need such numbers for the sake of generating labels (in the .debug_sfnames
22973    section) and references to those files numbers (in the .debug_srcinfo
22974    and.debug_macinfo sections).  If the filename given as an argument is not
22975    found in our current list, add it to the list and assign it the next
22976    available unique index number.  In order to speed up searches, we remember
22977    the index of the filename was looked up last.  This handles the majority of
22978    all searches.  */
22979
22980 static struct dwarf_file_data *
22981 lookup_filename (const char *file_name)
22982 {
22983   void ** slot;
22984   struct dwarf_file_data * created;
22985
22986   /* Check to see if the file name that was searched on the previous
22987      call matches this file name.  If so, return the index.  */
22988   if (file_table_last_lookup
22989       && (file_name == file_table_last_lookup->filename
22990           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
22991     return file_table_last_lookup;
22992
22993   /* Didn't match the previous lookup, search the table.  */
22994   slot = htab_find_slot_with_hash (file_table, file_name,
22995                                    htab_hash_string (file_name), INSERT);
22996   if (*slot)
22997     return (struct dwarf_file_data *) *slot;
22998
22999   created = ggc_alloc_dwarf_file_data ();
23000   created->filename = file_name;
23001   created->emitted_number = 0;
23002   *slot = created;
23003   return created;
23004 }
23005
23006 /* If the assembler will construct the file table, then translate the compiler
23007    internal file table number into the assembler file table number, and emit
23008    a .file directive if we haven't already emitted one yet.  The file table
23009    numbers are different because we prune debug info for unused variables and
23010    types, which may include filenames.  */
23011
23012 static int
23013 maybe_emit_file (struct dwarf_file_data * fd)
23014 {
23015   if (! fd->emitted_number)
23016     {
23017       if (last_emitted_file)
23018         fd->emitted_number = last_emitted_file->emitted_number + 1;
23019       else
23020         fd->emitted_number = 1;
23021       last_emitted_file = fd;
23022
23023       if (DWARF2_ASM_LINE_DEBUG_INFO)
23024         {
23025           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
23026           output_quoted_string (asm_out_file,
23027                                 remap_debug_filename (fd->filename));
23028           fputc ('\n', asm_out_file);
23029         }
23030     }
23031
23032   return fd->emitted_number;
23033 }
23034
23035 /* Schedule generation of a DW_AT_const_value attribute to DIE.
23036    That generation should happen after function debug info has been
23037    generated. The value of the attribute is the constant value of ARG.  */
23038
23039 static void
23040 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
23041 {
23042   die_arg_entry entry;
23043
23044   if (!die || !arg)
23045     return;
23046
23047   if (!tmpl_value_parm_die_table)
23048     tmpl_value_parm_die_table
23049       = VEC_alloc (die_arg_entry, gc, 32);
23050
23051   entry.die = die;
23052   entry.arg = arg;
23053   VEC_safe_push (die_arg_entry, gc,
23054                  tmpl_value_parm_die_table,
23055                  &entry);
23056 }
23057
23058 /* Return TRUE if T is an instance of generic type, FALSE
23059    otherwise.  */
23060
23061 static bool
23062 generic_type_p (tree t)
23063 {
23064   if (t == NULL_TREE || !TYPE_P (t))
23065     return false;
23066   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
23067 }
23068
23069 /* Schedule the generation of the generic parameter dies for the
23070   instance of generic type T. The proper generation itself is later
23071   done by gen_scheduled_generic_parms_dies. */
23072
23073 static void
23074 schedule_generic_params_dies_gen (tree t)
23075 {
23076   if (!generic_type_p (t))
23077     return;
23078
23079   if (generic_type_instances == NULL)
23080     generic_type_instances = VEC_alloc (tree, gc, 256);
23081
23082   VEC_safe_push (tree, gc, generic_type_instances, t);
23083 }
23084
23085 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
23086    by append_entry_to_tmpl_value_parm_die_table. This function must
23087    be called after function DIEs have been generated.  */
23088
23089 static void
23090 gen_remaining_tmpl_value_param_die_attribute (void)
23091 {
23092   if (tmpl_value_parm_die_table)
23093     {
23094       unsigned i;
23095       die_arg_entry *e;
23096
23097       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
23098         tree_add_const_value_attribute (e->die, e->arg);
23099     }
23100 }
23101
23102 /* Generate generic parameters DIEs for instances of generic types
23103    that have been previously scheduled by
23104    schedule_generic_params_dies_gen. This function must be called
23105    after all the types of the CU have been laid out.  */
23106
23107 static void
23108 gen_scheduled_generic_parms_dies (void)
23109 {
23110   unsigned i;
23111   tree t;
23112
23113   if (generic_type_instances == NULL)
23114     return;
23115   
23116   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
23117     gen_generic_params_dies (t);
23118 }
23119
23120
23121 /* Replace DW_AT_name for the decl with name.  */
23122
23123 static void
23124 dwarf2out_set_name (tree decl, tree name)
23125 {
23126   dw_die_ref die;
23127   dw_attr_ref attr;
23128   const char *dname;
23129
23130   die = TYPE_SYMTAB_DIE (decl);
23131   if (!die)
23132     return;
23133
23134   dname = dwarf2_name (name, 0);
23135   if (!dname)
23136     return;
23137
23138   attr = get_AT (die, DW_AT_name);
23139   if (attr)
23140     {
23141       struct indirect_string_node *node;
23142
23143       node = find_AT_string (dname);
23144       /* replace the string.  */
23145       attr->dw_attr_val.v.val_str = node;
23146     }
23147
23148   else
23149     add_name_attribute (die, dname);
23150 }
23151
23152 /* Called by the final INSN scan whenever we see a var location.  We
23153    use it to drop labels in the right places, and throw the location in
23154    our lookup table.  */
23155
23156 static void
23157 dwarf2out_var_location (rtx loc_note)
23158 {
23159   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
23160   struct var_loc_node *newloc;
23161   rtx next_real;
23162   static const char *last_label;
23163   static const char *last_postcall_label;
23164   static bool last_in_cold_section_p;
23165   tree decl;
23166   bool var_loc_p;
23167
23168   if (!NOTE_P (loc_note))
23169     {
23170       if (CALL_P (loc_note))
23171         {
23172           call_site_count++;
23173           if (SIBLING_CALL_P (loc_note))
23174             tail_call_site_count++;
23175         }
23176       return;
23177     }
23178
23179   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
23180   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
23181     return;
23182
23183   next_real = next_real_insn (loc_note);
23184
23185   /* If there are no instructions which would be affected by this note,
23186      don't do anything.  */
23187   if (var_loc_p
23188       && next_real == NULL_RTX
23189       && !NOTE_DURING_CALL_P (loc_note))
23190     return;
23191
23192   if (next_real == NULL_RTX)
23193     next_real = get_last_insn ();
23194
23195   /* If there were any real insns between note we processed last time
23196      and this note (or if it is the first note), clear
23197      last_{,postcall_}label so that they are not reused this time.  */
23198   if (last_var_location_insn == NULL_RTX
23199       || last_var_location_insn != next_real
23200       || last_in_cold_section_p != in_cold_section_p)
23201     {
23202       last_label = NULL;
23203       last_postcall_label = NULL;
23204     }
23205
23206   if (var_loc_p)
23207     {
23208       decl = NOTE_VAR_LOCATION_DECL (loc_note);
23209       newloc = add_var_loc_to_decl (decl, loc_note,
23210                                     NOTE_DURING_CALL_P (loc_note)
23211                                     ? last_postcall_label : last_label);
23212       if (newloc == NULL)
23213         return;
23214     }
23215   else
23216     {
23217       decl = NULL_TREE;
23218       newloc = NULL;
23219     }
23220
23221   /* If there were no real insns between note we processed last time
23222      and this note, use the label we emitted last time.  Otherwise
23223      create a new label and emit it.  */
23224   if (last_label == NULL)
23225     {
23226       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
23227       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
23228       loclabel_num++;
23229       last_label = ggc_strdup (loclabel);
23230     }
23231
23232   if (!var_loc_p)
23233     {
23234       struct call_arg_loc_node *ca_loc
23235         = ggc_alloc_cleared_call_arg_loc_node ();
23236       rtx prev = prev_real_insn (loc_note), x;
23237       ca_loc->call_arg_loc_note = loc_note;
23238       ca_loc->next = NULL;
23239       ca_loc->label = last_label;
23240       gcc_assert (prev
23241                   && (CALL_P (prev)
23242                       || (NONJUMP_INSN_P (prev)
23243                           && GET_CODE (PATTERN (prev)) == SEQUENCE
23244                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
23245       if (!CALL_P (prev))
23246         prev = XVECEXP (PATTERN (prev), 0, 0);
23247       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
23248       x = PATTERN (prev);
23249       if (GET_CODE (x) == PARALLEL)
23250         x = XVECEXP (x, 0, 0);
23251       if (GET_CODE (x) == SET)
23252         x = SET_SRC (x);
23253       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
23254         {
23255           x = XEXP (XEXP (x, 0), 0);
23256           if (GET_CODE (x) == SYMBOL_REF
23257               && SYMBOL_REF_DECL (x)
23258               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
23259             ca_loc->symbol_ref = x;
23260         }
23261       ca_loc->block = insn_scope (prev);
23262       if (call_arg_locations)
23263         call_arg_loc_last->next = ca_loc;
23264       else
23265         call_arg_locations = ca_loc;
23266       call_arg_loc_last = ca_loc;
23267     }
23268   else if (!NOTE_DURING_CALL_P (loc_note))
23269     newloc->label = last_label;
23270   else
23271     {
23272       if (!last_postcall_label)
23273         {
23274           sprintf (loclabel, "%s-1", last_label);
23275           last_postcall_label = ggc_strdup (loclabel);
23276         }
23277       newloc->label = last_postcall_label;
23278     }
23279
23280   last_var_location_insn = next_real;
23281   last_in_cold_section_p = in_cold_section_p;
23282 }
23283
23284 /* Note in one location list that text section has changed.  */
23285
23286 static int
23287 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
23288 {
23289   var_loc_list *list = (var_loc_list *) *slot;
23290   if (list->first)
23291     list->last_before_switch
23292       = list->last->next ? list->last->next : list->last;
23293   return 1;
23294 }
23295
23296 /* Note in all location lists that text section has changed.  */
23297
23298 static void
23299 var_location_switch_text_section (void)
23300 {
23301   if (decl_loc_table == NULL)
23302     return;
23303
23304   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
23305 }
23306
23307 /* Create a new line number table.  */
23308
23309 static dw_line_info_table *
23310 new_line_info_table (void)
23311 {
23312   dw_line_info_table *table;
23313
23314   table = ggc_alloc_cleared_dw_line_info_table_struct ();
23315   table->file_num = 1;
23316   table->line_num = 1;
23317   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
23318
23319   return table;
23320 }
23321
23322 /* Lookup the "current" table into which we emit line info, so
23323    that we don't have to do it for every source line.  */
23324
23325 static void
23326 set_cur_line_info_table (section *sec)
23327 {
23328   dw_line_info_table *table;
23329
23330   if (sec == text_section)
23331     table = text_section_line_info;
23332   else if (sec == cold_text_section)
23333     {
23334       table = cold_text_section_line_info;
23335       if (!table)
23336         {
23337           cold_text_section_line_info = table = new_line_info_table ();
23338           table->end_label = cold_end_label;
23339         }
23340     }
23341   else
23342     {
23343       const char *end_label;
23344
23345       if (flag_reorder_blocks_and_partition)
23346         {
23347           if (in_cold_section_p)
23348             end_label = crtl->subsections.cold_section_end_label;
23349           else
23350             end_label = crtl->subsections.hot_section_end_label;
23351         }
23352       else
23353         {
23354           char label[MAX_ARTIFICIAL_LABEL_BYTES];
23355           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
23356                                        current_function_funcdef_no);
23357           end_label = ggc_strdup (label);
23358         }
23359
23360       table = new_line_info_table ();
23361       table->end_label = end_label;
23362
23363       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
23364     }
23365
23366   cur_line_info_table = table;
23367 }
23368
23369
23370 /* We need to reset the locations at the beginning of each
23371    function. We can't do this in the end_function hook, because the
23372    declarations that use the locations won't have been output when
23373    that hook is called.  Also compute have_multiple_function_sections here.  */
23374
23375 static void
23376 dwarf2out_begin_function (tree fun)
23377 {
23378   section *sec = function_section (fun);
23379
23380   if (sec != text_section)
23381     have_multiple_function_sections = true;
23382
23383   if (flag_reorder_blocks_and_partition && !cold_text_section)
23384     {
23385       gcc_assert (current_function_decl == fun);
23386       cold_text_section = unlikely_text_section ();
23387       switch_to_section (cold_text_section);
23388       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
23389       switch_to_section (sec);
23390     }
23391
23392   dwarf2out_note_section_used ();
23393   call_site_count = 0;
23394   tail_call_site_count = 0;
23395
23396   set_cur_line_info_table (sec);
23397 }
23398
23399 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
23400
23401 static void
23402 push_dw_line_info_entry (dw_line_info_table *table,
23403                          enum dw_line_info_opcode opcode, unsigned int val)
23404 {
23405   dw_line_info_entry e;
23406   e.opcode = opcode;
23407   e.val = val;
23408   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
23409 }
23410
23411 /* Output a label to mark the beginning of a source code line entry
23412    and record information relating to this source line, in
23413    'line_info_table' for later output of the .debug_line section.  */
23414 /* ??? The discriminator parameter ought to be unsigned.  */
23415
23416 static void
23417 dwarf2out_source_line (unsigned int line, const char *filename,
23418                        int discriminator, bool is_stmt)
23419 {
23420   unsigned int file_num;
23421   dw_line_info_table *table;
23422
23423   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
23424     return;
23425
23426   /* The discriminator column was added in dwarf4.  Simplify the below
23427      by simply removing it if we're not supposed to output it.  */
23428   if (dwarf_version < 4 && dwarf_strict)
23429     discriminator = 0;
23430
23431   table = cur_line_info_table;
23432   file_num = maybe_emit_file (lookup_filename (filename));
23433
23434   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
23435      the debugger has used the second (possibly duplicate) line number
23436      at the beginning of the function to mark the end of the prologue.
23437      We could eliminate any other duplicates within the function.  For
23438      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
23439      that second line number entry.  */
23440   /* Recall that this end-of-prologue indication is *not* the same thing
23441      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
23442      to which the hook corresponds, follows the last insn that was 
23443      emitted by gen_prologue.  What we need is to preceed the first insn
23444      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
23445      insn that corresponds to something the user wrote.  These may be
23446      very different locations once scheduling is enabled.  */
23447
23448   if (0 && file_num == table->file_num
23449       && line == table->line_num
23450       && discriminator == table->discrim_num
23451       && is_stmt == table->is_stmt)
23452     return;
23453
23454   switch_to_section (current_function_section ());
23455
23456   /* If requested, emit something human-readable.  */
23457   if (flag_debug_asm)
23458     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
23459
23460   if (DWARF2_ASM_LINE_DEBUG_INFO)
23461     {
23462       /* Emit the .loc directive understood by GNU as.  */
23463       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
23464       if (is_stmt != table->is_stmt)
23465         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
23466       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
23467         fprintf (asm_out_file, " discriminator %d", discriminator);
23468       fputc ('\n', asm_out_file);
23469     }
23470   else
23471     {
23472       unsigned int label_num = ++line_info_label_num;
23473
23474       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
23475
23476       push_dw_line_info_entry (table, LI_set_address, label_num);
23477       if (file_num != table->file_num)
23478         push_dw_line_info_entry (table, LI_set_file, file_num);
23479       if (discriminator != table->discrim_num)
23480         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
23481       if (is_stmt != table->is_stmt)
23482         push_dw_line_info_entry (table, LI_negate_stmt, 0);
23483       push_dw_line_info_entry (table, LI_set_line, line);
23484     }
23485
23486   table->file_num = file_num;
23487   table->line_num = line;
23488   table->discrim_num = discriminator;
23489   table->is_stmt = is_stmt;
23490   table->in_use = true;
23491 }
23492
23493 /* Record the beginning of a new source file.  */
23494
23495 static void
23496 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
23497 {
23498   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23499     {
23500       /* Record the beginning of the file for break_out_includes.  */
23501       dw_die_ref bincl_die;
23502
23503       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
23504       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
23505     }
23506
23507   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23508     {
23509       macinfo_entry e;
23510       e.code = DW_MACINFO_start_file;
23511       e.lineno = lineno;
23512       e.info = xstrdup (filename);
23513       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23514     }
23515 }
23516
23517 /* Record the end of a source file.  */
23518
23519 static void
23520 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
23521 {
23522   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
23523     /* Record the end of the file for break_out_includes.  */
23524     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
23525
23526   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23527     {
23528       macinfo_entry e;
23529       e.code = DW_MACINFO_end_file;
23530       e.lineno = lineno;
23531       e.info = NULL;
23532       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23533     }
23534 }
23535
23536 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
23537    the tail part of the directive line, i.e. the part which is past the
23538    initial whitespace, #, whitespace, directive-name, whitespace part.  */
23539
23540 static void
23541 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
23542                   const char *buffer ATTRIBUTE_UNUSED)
23543 {
23544   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23545     {
23546       macinfo_entry e;
23547       e.code = DW_MACINFO_define;
23548       e.lineno = lineno;
23549       e.info = xstrdup (buffer);;
23550       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23551     }
23552 }
23553
23554 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
23555    the tail part of the directive line, i.e. the part which is past the
23556    initial whitespace, #, whitespace, directive-name, whitespace part.  */
23557
23558 static void
23559 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
23560                  const char *buffer ATTRIBUTE_UNUSED)
23561 {
23562   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23563     {
23564       macinfo_entry e;
23565       e.code = DW_MACINFO_undef;
23566       e.lineno = lineno;
23567       e.info = xstrdup (buffer);;
23568       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
23569     }
23570 }
23571
23572 static void
23573 output_macinfo (void)
23574 {
23575   unsigned i;
23576   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
23577   macinfo_entry *ref;
23578
23579   if (! length)
23580     return;
23581
23582   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
23583     {
23584       switch (ref->code)
23585         {
23586           case DW_MACINFO_start_file:
23587             {
23588               int file_num = maybe_emit_file (lookup_filename (ref->info));
23589               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
23590               dw2_asm_output_data_uleb128 
23591                         (ref->lineno, "Included from line number %lu", 
23592                                                 (unsigned long)ref->lineno);
23593               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
23594             }
23595             break;
23596           case DW_MACINFO_end_file:
23597             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
23598             break;
23599           case DW_MACINFO_define:
23600             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
23601             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
23602                                                 (unsigned long)ref->lineno);
23603             dw2_asm_output_nstring (ref->info, -1, "The macro");
23604             break;
23605           case DW_MACINFO_undef:
23606             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
23607             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
23608                                                 (unsigned long)ref->lineno);
23609             dw2_asm_output_nstring (ref->info, -1, "The macro");
23610             break;
23611           default:
23612            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
23613              ASM_COMMENT_START, (unsigned long)ref->code);
23614           break;
23615         }
23616     }
23617 }
23618
23619 /* Set up for Dwarf output at the start of compilation.  */
23620
23621 static void
23622 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
23623 {
23624   /* Allocate the file_table.  */
23625   file_table = htab_create_ggc (50, file_table_hash,
23626                                 file_table_eq, NULL);
23627
23628   /* Allocate the decl_die_table.  */
23629   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
23630                                     decl_die_table_eq, NULL);
23631
23632   /* Allocate the decl_loc_table.  */
23633   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
23634                                     decl_loc_table_eq, NULL);
23635
23636   /* Allocate the cached_dw_loc_list_table.  */
23637   cached_dw_loc_list_table
23638     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
23639                        cached_dw_loc_list_table_eq, NULL);
23640
23641   /* Allocate the initial hunk of the decl_scope_table.  */
23642   decl_scope_table = VEC_alloc (tree, gc, 256);
23643
23644   /* Allocate the initial hunk of the abbrev_die_table.  */
23645   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
23646     (ABBREV_DIE_TABLE_INCREMENT);
23647   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
23648   /* Zero-th entry is allocated, but unused.  */
23649   abbrev_die_table_in_use = 1;
23650
23651   /* Allocate the pubtypes and pubnames vectors.  */
23652   pubname_table = VEC_alloc (pubname_entry, gc, 32);
23653   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
23654
23655   incomplete_types = VEC_alloc (tree, gc, 64);
23656
23657   used_rtx_array = VEC_alloc (rtx, gc, 32);
23658
23659   debug_info_section = get_section (DEBUG_INFO_SECTION,
23660                                     SECTION_DEBUG, NULL);
23661   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23662                                       SECTION_DEBUG, NULL);
23663   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23664                                        SECTION_DEBUG, NULL);
23665   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
23666                                        SECTION_DEBUG, NULL);
23667   debug_line_section = get_section (DEBUG_LINE_SECTION,
23668                                     SECTION_DEBUG, NULL);
23669   debug_loc_section = get_section (DEBUG_LOC_SECTION,
23670                                    SECTION_DEBUG, NULL);
23671   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23672                                         SECTION_DEBUG, NULL);
23673   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23674                                         SECTION_DEBUG, NULL);
23675   debug_str_section = get_section (DEBUG_STR_SECTION,
23676                                    DEBUG_STR_SECTION_FLAGS, NULL);
23677   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23678                                       SECTION_DEBUG, NULL);
23679   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23680                                      SECTION_DEBUG, NULL);
23681
23682   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23683   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23684                                DEBUG_ABBREV_SECTION_LABEL, 0);
23685   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23686   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23687                                COLD_TEXT_SECTION_LABEL, 0);
23688   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23689
23690   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23691                                DEBUG_INFO_SECTION_LABEL, 0);
23692   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23693                                DEBUG_LINE_SECTION_LABEL, 0);
23694   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23695                                DEBUG_RANGES_SECTION_LABEL, 0);
23696   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23697                                DEBUG_MACINFO_SECTION_LABEL, 0);
23698
23699   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23700     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
23701
23702   switch_to_section (text_section);
23703   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23704
23705   /* Make sure the line number table for .text always exists.  */
23706   text_section_line_info = new_line_info_table ();
23707   text_section_line_info->end_label = text_end_label;
23708 }
23709
23710 /* Called before cgraph_optimize starts outputtting functions, variables
23711    and toplevel asms into assembly.  */
23712
23713 static void
23714 dwarf2out_assembly_start (void)
23715 {
23716   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23717       && dwarf2out_do_cfi_asm ()
23718       && (!(flag_unwind_tables || flag_exceptions)
23719           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
23720     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23721 }
23722
23723 /* A helper function for dwarf2out_finish called through
23724    htab_traverse.  Emit one queued .debug_str string.  */
23725
23726 static int
23727 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
23728 {
23729   struct indirect_string_node *node = (struct indirect_string_node *) *h;
23730
23731   if (node->form == DW_FORM_strp)
23732     {
23733       switch_to_section (debug_str_section);
23734       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23735       assemble_string (node->str, strlen (node->str) + 1);
23736     }
23737
23738   return 1;
23739 }
23740
23741 #if ENABLE_ASSERT_CHECKING
23742 /* Verify that all marks are clear.  */
23743
23744 static void
23745 verify_marks_clear (dw_die_ref die)
23746 {
23747   dw_die_ref c;
23748
23749   gcc_assert (! die->die_mark);
23750   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23751 }
23752 #endif /* ENABLE_ASSERT_CHECKING */
23753
23754 /* Clear the marks for a die and its children.
23755    Be cool if the mark isn't set.  */
23756
23757 static void
23758 prune_unmark_dies (dw_die_ref die)
23759 {
23760   dw_die_ref c;
23761
23762   if (die->die_mark)
23763     die->die_mark = 0;
23764   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23765 }
23766
23767 /* Given DIE that we're marking as used, find any other dies
23768    it references as attributes and mark them as used.  */
23769
23770 static void
23771 prune_unused_types_walk_attribs (dw_die_ref die)
23772 {
23773   dw_attr_ref a;
23774   unsigned ix;
23775
23776   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23777     {
23778       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23779         {
23780           /* A reference to another DIE.
23781              Make sure that it will get emitted.
23782              If it was broken out into a comdat group, don't follow it.  */
23783           if (! use_debug_types
23784               || a->dw_attr == DW_AT_specification
23785               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
23786             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23787         }
23788       /* Set the string's refcount to 0 so that prune_unused_types_mark
23789          accounts properly for it.  */
23790       if (AT_class (a) == dw_val_class_str)
23791         a->dw_attr_val.v.val_str->refcount = 0;
23792     }
23793 }
23794
23795 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23796
23797 static void
23798 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23799 {
23800   dw_die_ref c;
23801
23802   if (die == NULL || die->die_child == NULL)
23803     return;
23804   c = die->die_child;
23805   do
23806     {
23807       switch (c->die_tag)
23808         {
23809         case DW_TAG_template_type_param:
23810         case DW_TAG_template_value_param:
23811         case DW_TAG_GNU_template_template_param:
23812         case DW_TAG_GNU_template_parameter_pack:
23813           prune_unused_types_mark (c, 1);
23814           break;
23815         default:
23816           break;
23817         }
23818       c = c->die_sib;
23819     } while (c && c != die->die_child);
23820 }
23821
23822 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23823    to DIE's children.  */
23824
23825 static void
23826 prune_unused_types_mark (dw_die_ref die, int dokids)
23827 {
23828   dw_die_ref c;
23829
23830   if (die->die_mark == 0)
23831     {
23832       /* We haven't done this node yet.  Mark it as used.  */
23833       die->die_mark = 1;
23834       /* If this is the DIE of a generic type instantiation,
23835          mark the children DIEs that describe its generic parms and
23836          args.  */
23837       prune_unused_types_mark_generic_parms_dies (die);
23838
23839       /* We also have to mark its parents as used.
23840          (But we don't want to mark our parents' kids due to this.)  */
23841       if (die->die_parent)
23842         prune_unused_types_mark (die->die_parent, 0);
23843
23844       /* Mark any referenced nodes.  */
23845       prune_unused_types_walk_attribs (die);
23846
23847       /* If this node is a specification,
23848          also mark the definition, if it exists.  */
23849       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23850         prune_unused_types_mark (die->die_definition, 1);
23851     }
23852
23853   if (dokids && die->die_mark != 2)
23854     {
23855       /* We need to walk the children, but haven't done so yet.
23856          Remember that we've walked the kids.  */
23857       die->die_mark = 2;
23858
23859       /* If this is an array type, we need to make sure our
23860          kids get marked, even if they're types.  If we're
23861          breaking out types into comdat sections, do this
23862          for all type definitions.  */
23863       if (die->die_tag == DW_TAG_array_type
23864           || (use_debug_types
23865               && is_type_die (die) && ! is_declaration_die (die)))
23866         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23867       else
23868         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23869     }
23870 }
23871
23872 /* For local classes, look if any static member functions were emitted
23873    and if so, mark them.  */
23874
23875 static void
23876 prune_unused_types_walk_local_classes (dw_die_ref die)
23877 {
23878   dw_die_ref c;
23879
23880   if (die->die_mark == 2)
23881     return;
23882
23883   switch (die->die_tag)
23884     {
23885     case DW_TAG_structure_type:
23886     case DW_TAG_union_type:
23887     case DW_TAG_class_type:
23888       break;
23889
23890     case DW_TAG_subprogram:
23891       if (!get_AT_flag (die, DW_AT_declaration)
23892           || die->die_definition != NULL)
23893         prune_unused_types_mark (die, 1);
23894       return;
23895
23896     default:
23897       return;
23898     }
23899
23900   /* Mark children.  */
23901   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23902 }
23903
23904 /* Walk the tree DIE and mark types that we actually use.  */
23905
23906 static void
23907 prune_unused_types_walk (dw_die_ref die)
23908 {
23909   dw_die_ref c;
23910
23911   /* Don't do anything if this node is already marked and
23912      children have been marked as well.  */
23913   if (die->die_mark == 2)
23914     return;
23915
23916   switch (die->die_tag)
23917     {
23918     case DW_TAG_structure_type:
23919     case DW_TAG_union_type:
23920     case DW_TAG_class_type:
23921       if (die->die_perennial_p)
23922         break;
23923
23924       for (c = die->die_parent; c; c = c->die_parent)
23925         if (c->die_tag == DW_TAG_subprogram)
23926           break;
23927
23928       /* Finding used static member functions inside of classes
23929          is needed just for local classes, because for other classes
23930          static member function DIEs with DW_AT_specification
23931          are emitted outside of the DW_TAG_*_type.  If we ever change
23932          it, we'd need to call this even for non-local classes.  */
23933       if (c)
23934         prune_unused_types_walk_local_classes (die);
23935
23936       /* It's a type node --- don't mark it.  */
23937       return;
23938
23939     case DW_TAG_const_type:
23940     case DW_TAG_packed_type:
23941     case DW_TAG_pointer_type:
23942     case DW_TAG_reference_type:
23943     case DW_TAG_rvalue_reference_type:
23944     case DW_TAG_volatile_type:
23945     case DW_TAG_typedef:
23946     case DW_TAG_array_type:
23947     case DW_TAG_interface_type:
23948     case DW_TAG_friend:
23949     case DW_TAG_variant_part:
23950     case DW_TAG_enumeration_type:
23951     case DW_TAG_subroutine_type:
23952     case DW_TAG_string_type:
23953     case DW_TAG_set_type:
23954     case DW_TAG_subrange_type:
23955     case DW_TAG_ptr_to_member_type:
23956     case DW_TAG_file_type:
23957       if (die->die_perennial_p)
23958         break;
23959
23960       /* It's a type node --- don't mark it.  */
23961       return;
23962
23963     default:
23964       /* Mark everything else.  */
23965       break;
23966   }
23967
23968   if (die->die_mark == 0)
23969     {
23970       die->die_mark = 1;
23971
23972       /* Now, mark any dies referenced from here.  */
23973       prune_unused_types_walk_attribs (die);
23974     }
23975
23976   die->die_mark = 2;
23977
23978   /* Mark children.  */
23979   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23980 }
23981
23982 /* Increment the string counts on strings referred to from DIE's
23983    attributes.  */
23984
23985 static void
23986 prune_unused_types_update_strings (dw_die_ref die)
23987 {
23988   dw_attr_ref a;
23989   unsigned ix;
23990
23991   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23992     if (AT_class (a) == dw_val_class_str)
23993       {
23994         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23995         s->refcount++;
23996         /* Avoid unnecessarily putting strings that are used less than
23997            twice in the hash table.  */
23998         if (s->refcount
23999             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
24000           {
24001             void ** slot;
24002             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
24003                                              htab_hash_string (s->str),
24004                                              INSERT);
24005             gcc_assert (*slot == NULL);
24006             *slot = s;
24007           }
24008       }
24009 }
24010
24011 /* Remove from the tree DIE any dies that aren't marked.  */
24012
24013 static void
24014 prune_unused_types_prune (dw_die_ref die)
24015 {
24016   dw_die_ref c;
24017
24018   gcc_assert (die->die_mark);
24019   prune_unused_types_update_strings (die);
24020
24021   if (! die->die_child)
24022     return;
24023
24024   c = die->die_child;
24025   do {
24026     dw_die_ref prev = c;
24027     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
24028       if (c == die->die_child)
24029         {
24030           /* No marked children between 'prev' and the end of the list.  */
24031           if (prev == c)
24032             /* No marked children at all.  */
24033             die->die_child = NULL;
24034           else
24035             {
24036               prev->die_sib = c->die_sib;
24037               die->die_child = prev;
24038             }
24039           return;
24040         }
24041
24042     if (c != prev->die_sib)
24043       prev->die_sib = c;
24044     prune_unused_types_prune (c);
24045   } while (c != die->die_child);
24046 }
24047
24048 /* Remove dies representing declarations that we never use.  */
24049
24050 static void
24051 prune_unused_types (void)
24052 {
24053   unsigned int i;
24054   limbo_die_node *node;
24055   comdat_type_node *ctnode;
24056   pubname_ref pub;
24057   dw_die_ref base_type;
24058
24059 #if ENABLE_ASSERT_CHECKING
24060   /* All the marks should already be clear.  */
24061   verify_marks_clear (comp_unit_die ());
24062   for (node = limbo_die_list; node; node = node->next)
24063     verify_marks_clear (node->die);
24064   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
24065     verify_marks_clear (ctnode->root_die);
24066 #endif /* ENABLE_ASSERT_CHECKING */
24067
24068   /* Mark types that are used in global variables.  */
24069   premark_types_used_by_global_vars ();
24070
24071   /* Set the mark on nodes that are actually used.  */
24072   prune_unused_types_walk (comp_unit_die ());
24073   for (node = limbo_die_list; node; node = node->next)
24074     prune_unused_types_walk (node->die);
24075   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
24076     {
24077       prune_unused_types_walk (ctnode->root_die);
24078       prune_unused_types_mark (ctnode->type_die, 1);
24079     }
24080
24081   /* Also set the mark on nodes referenced from the
24082      pubname_table.  */
24083   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
24084     prune_unused_types_mark (pub->die, 1);
24085   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
24086     prune_unused_types_mark (base_type, 1);
24087
24088   if (debug_str_hash)
24089     htab_empty (debug_str_hash);
24090   prune_unused_types_prune (comp_unit_die ());
24091   for (node = limbo_die_list; node; node = node->next)
24092     prune_unused_types_prune (node->die);
24093   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
24094     prune_unused_types_prune (ctnode->root_die);
24095
24096   /* Leave the marks clear.  */
24097   prune_unmark_dies (comp_unit_die ());
24098   for (node = limbo_die_list; node; node = node->next)
24099     prune_unmark_dies (node->die);
24100   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
24101     prune_unmark_dies (ctnode->root_die);
24102 }
24103
24104 /* Set the parameter to true if there are any relative pathnames in
24105    the file table.  */
24106 static int
24107 file_table_relative_p (void ** slot, void *param)
24108 {
24109   bool *p = (bool *) param;
24110   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
24111   if (!IS_ABSOLUTE_PATH (d->filename))
24112     {
24113       *p = true;
24114       return 0;
24115     }
24116   return 1;
24117 }
24118
24119 /* Routines to manipulate hash table of comdat type units.  */
24120
24121 static hashval_t
24122 htab_ct_hash (const void *of)
24123 {
24124   hashval_t h;
24125   const comdat_type_node *const type_node = (const comdat_type_node *) of;
24126
24127   memcpy (&h, type_node->signature, sizeof (h));
24128   return h;
24129 }
24130
24131 static int
24132 htab_ct_eq (const void *of1, const void *of2)
24133 {
24134   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
24135   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
24136
24137   return (! memcmp (type_node_1->signature, type_node_2->signature,
24138                     DWARF_TYPE_SIGNATURE_SIZE));
24139 }
24140
24141 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
24142    to the location it would have been added, should we know its
24143    DECL_ASSEMBLER_NAME when we added other attributes.  This will
24144    probably improve compactness of debug info, removing equivalent
24145    abbrevs, and hide any differences caused by deferring the
24146    computation of the assembler name, triggered by e.g. PCH.  */
24147
24148 static inline void
24149 move_linkage_attr (dw_die_ref die)
24150 {
24151   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
24152   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
24153
24154   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
24155               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
24156
24157   while (--ix > 0)
24158     {
24159       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
24160
24161       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
24162         break;
24163     }
24164
24165   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
24166     {
24167       VEC_pop (dw_attr_node, die->die_attr);
24168       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
24169     }
24170 }
24171
24172 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
24173    referenced from typed stack ops and count how often they are used.  */
24174
24175 static void
24176 mark_base_types (dw_loc_descr_ref loc)
24177 {
24178   dw_die_ref base_type = NULL;
24179
24180   for (; loc; loc = loc->dw_loc_next)
24181     {
24182       switch (loc->dw_loc_opc)
24183         {
24184         case DW_OP_GNU_regval_type:
24185         case DW_OP_GNU_deref_type:
24186           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
24187           break;
24188         case DW_OP_GNU_convert:
24189         case DW_OP_GNU_reinterpret:
24190           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
24191             continue;
24192           /* FALLTHRU */
24193         case DW_OP_GNU_const_type:
24194           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
24195           break;
24196         case DW_OP_GNU_entry_value:
24197           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
24198           continue;
24199         default:
24200           continue;
24201         }
24202       gcc_assert (base_type->die_parent == comp_unit_die ());
24203       if (base_type->die_mark)
24204         base_type->die_mark++;
24205       else
24206         {
24207           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
24208           base_type->die_mark = 1;
24209         }
24210     }
24211 }
24212
24213 /* Comparison function for sorting marked base types.  */
24214
24215 static int
24216 base_type_cmp (const void *x, const void *y)
24217 {
24218   dw_die_ref dx = *(const dw_die_ref *) x;
24219   dw_die_ref dy = *(const dw_die_ref *) y;
24220   unsigned int byte_size1, byte_size2;
24221   unsigned int encoding1, encoding2;
24222   if (dx->die_mark > dy->die_mark)
24223     return -1;
24224   if (dx->die_mark < dy->die_mark)
24225     return 1;
24226   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
24227   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
24228   if (byte_size1 < byte_size2)
24229     return 1;
24230   if (byte_size1 > byte_size2)
24231     return -1;
24232   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
24233   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
24234   if (encoding1 < encoding2)
24235     return 1;
24236   if (encoding1 > encoding2)
24237     return -1;
24238   return 0;
24239 }
24240
24241 /* Move base types marked by mark_base_types as early as possible
24242    in the CU, sorted by decreasing usage count both to make the
24243    uleb128 references as small as possible and to make sure they
24244    will have die_offset already computed by calc_die_sizes when
24245    sizes of typed stack loc ops is computed.  */
24246
24247 static void
24248 move_marked_base_types (void)
24249 {
24250   unsigned int i;
24251   dw_die_ref base_type, die, c;
24252
24253   if (VEC_empty (dw_die_ref, base_types))
24254     return;
24255
24256   /* Sort by decreasing usage count, they will be added again in that
24257      order later on.  */
24258   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
24259   die = comp_unit_die ();
24260   c = die->die_child;
24261   do
24262     {
24263       dw_die_ref prev = c;
24264       c = c->die_sib;
24265       while (c->die_mark)
24266         {
24267           remove_child_with_prev (c, prev);
24268           /* As base types got marked, there must be at least
24269              one node other than DW_TAG_base_type.  */
24270           gcc_assert (c != c->die_sib);
24271           c = c->die_sib;
24272         }
24273     }
24274   while (c != die->die_child);
24275   gcc_assert (die->die_child);
24276   c = die->die_child;
24277   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
24278     {
24279       base_type->die_mark = 0;
24280       base_type->die_sib = c->die_sib;
24281       c->die_sib = base_type;
24282       c = base_type;
24283     }
24284 }
24285
24286 /* Helper function for resolve_addr, attempt to resolve
24287    one CONST_STRING, return non-zero if not successful.  Similarly verify that
24288    SYMBOL_REFs refer to variables emitted in the current CU.  */
24289
24290 static int
24291 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
24292 {
24293   rtx rtl = *addr;
24294
24295   if (GET_CODE (rtl) == CONST_STRING)
24296     {
24297       size_t len = strlen (XSTR (rtl, 0)) + 1;
24298       tree t = build_string (len, XSTR (rtl, 0));
24299       tree tlen = size_int (len - 1);
24300       TREE_TYPE (t)
24301         = build_array_type (char_type_node, build_index_type (tlen));
24302       rtl = lookup_constant_def (t);
24303       if (!rtl || !MEM_P (rtl))
24304         return 1;
24305       rtl = XEXP (rtl, 0);
24306       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
24307       *addr = rtl;
24308       return 0;
24309     }
24310
24311   if (GET_CODE (rtl) == SYMBOL_REF
24312       && SYMBOL_REF_DECL (rtl))
24313     {
24314       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
24315         {
24316           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
24317             return 1;
24318         }
24319       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
24320         return 1;
24321     }
24322
24323   if (GET_CODE (rtl) == CONST
24324       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
24325     return 1;
24326
24327   return 0;
24328 }
24329
24330 /* Helper function for resolve_addr, handle one location
24331    expression, return false if at least one CONST_STRING or SYMBOL_REF in
24332    the location list couldn't be resolved.  */
24333
24334 static bool
24335 resolve_addr_in_expr (dw_loc_descr_ref loc)
24336 {
24337   dw_loc_descr_ref keep = NULL;
24338   for (; loc; loc = loc->dw_loc_next)
24339     switch (loc->dw_loc_opc)
24340       {
24341       case DW_OP_addr:
24342         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
24343           return false;
24344         break;
24345       case DW_OP_const4u:
24346       case DW_OP_const8u:
24347         if (loc->dtprel
24348             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
24349           return false;
24350         break;
24351       case DW_OP_implicit_value:
24352         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
24353             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
24354           return false;
24355         break;
24356       case DW_OP_GNU_implicit_pointer:
24357       case DW_OP_GNU_parameter_ref:
24358         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
24359           {
24360             dw_die_ref ref
24361               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
24362             if (ref == NULL)
24363               return false;
24364             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
24365             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
24366             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
24367           }
24368         break;
24369       case DW_OP_GNU_const_type:
24370       case DW_OP_GNU_regval_type:
24371       case DW_OP_GNU_deref_type:
24372       case DW_OP_GNU_convert:
24373       case DW_OP_GNU_reinterpret:
24374         while (loc->dw_loc_next
24375                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
24376           {
24377             dw_die_ref base1, base2;
24378             unsigned enc1, enc2, size1, size2;
24379             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24380                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24381               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
24382             else if (loc->dw_loc_oprnd1.val_class
24383                      == dw_val_class_unsigned_const)
24384               break;
24385             else
24386               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
24387             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
24388                 == dw_val_class_unsigned_const)
24389               break;
24390             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
24391             gcc_assert (base1->die_tag == DW_TAG_base_type
24392                         && base2->die_tag == DW_TAG_base_type);
24393             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
24394             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
24395             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
24396             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
24397             if (size1 == size2
24398                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
24399                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
24400                      && loc != keep)
24401                     || enc1 == enc2))
24402               {
24403                 /* Optimize away next DW_OP_GNU_convert after
24404                    adjusting LOC's base type die reference.  */
24405                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
24406                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
24407                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
24408                 else
24409                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
24410                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
24411                 continue;
24412               }
24413             /* Don't change integer DW_OP_GNU_convert after e.g. floating
24414                point typed stack entry.  */
24415             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
24416               keep = loc->dw_loc_next;
24417             break;
24418           }
24419         break;
24420       default:
24421         break;
24422       }
24423   return true;
24424 }
24425
24426 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
24427    an address in .rodata section if the string literal is emitted there,
24428    or remove the containing location list or replace DW_AT_const_value
24429    with DW_AT_location and empty location expression, if it isn't found
24430    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
24431    to something that has been emitted in the current CU.  */
24432
24433 static void
24434 resolve_addr (dw_die_ref die)
24435 {
24436   dw_die_ref c;
24437   dw_attr_ref a;
24438   dw_loc_list_ref *curr, *start, loc;
24439   unsigned ix;
24440
24441   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
24442     switch (AT_class (a))
24443       {
24444       case dw_val_class_loc_list:
24445         start = curr = AT_loc_list_ptr (a);
24446         loc = *curr;
24447         gcc_assert (loc);
24448         /* The same list can be referenced more than once.  See if we have
24449            already recorded the result from a previous pass.  */
24450         if (loc->replaced)
24451           *curr = loc->dw_loc_next;
24452         else if (!loc->resolved_addr)
24453           {
24454             /* As things stand, we do not expect or allow one die to
24455                reference a suffix of another die's location list chain.
24456                References must be identical or completely separate.
24457                There is therefore no need to cache the result of this
24458                pass on any list other than the first; doing so
24459                would lead to unnecessary writes.  */
24460             while (*curr)
24461               {
24462                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
24463                 if (!resolve_addr_in_expr ((*curr)->expr))
24464                   {
24465                     dw_loc_list_ref next = (*curr)->dw_loc_next;
24466                     if (next && (*curr)->ll_symbol)
24467                       {
24468                         gcc_assert (!next->ll_symbol);
24469                         next->ll_symbol = (*curr)->ll_symbol;
24470                       }
24471                     *curr = next;
24472                   }
24473                 else
24474                   {
24475                     mark_base_types ((*curr)->expr);
24476                     curr = &(*curr)->dw_loc_next;
24477                   }
24478               }
24479             if (loc == *start)
24480               loc->resolved_addr = 1;
24481             else
24482               {
24483                 loc->replaced = 1;
24484                 loc->dw_loc_next = *start;
24485               }
24486           }
24487         if (!*start)
24488           {
24489             remove_AT (die, a->dw_attr);
24490             ix--;
24491           }
24492         break;
24493       case dw_val_class_loc:
24494         if (!resolve_addr_in_expr (AT_loc (a)))
24495           {
24496             remove_AT (die, a->dw_attr);
24497             ix--;
24498           }
24499         else
24500           mark_base_types (AT_loc (a));
24501         break;
24502       case dw_val_class_addr:
24503         if (a->dw_attr == DW_AT_const_value
24504             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
24505           {
24506             remove_AT (die, a->dw_attr);
24507             ix--;
24508           }
24509         if (die->die_tag == DW_TAG_GNU_call_site
24510             && a->dw_attr == DW_AT_abstract_origin)
24511           {
24512             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
24513             dw_die_ref tdie = lookup_decl_die (tdecl);
24514             if (tdie == NULL
24515                 && DECL_EXTERNAL (tdecl)
24516                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
24517               {
24518                 force_decl_die (tdecl);
24519                 tdie = lookup_decl_die (tdecl);
24520               }
24521             if (tdie)
24522               {
24523                 a->dw_attr_val.val_class = dw_val_class_die_ref;
24524                 a->dw_attr_val.v.val_die_ref.die = tdie;
24525                 a->dw_attr_val.v.val_die_ref.external = 0;
24526               }
24527             else
24528               {
24529                 remove_AT (die, a->dw_attr);
24530                 ix--;
24531               }
24532           }
24533         break;
24534       default:
24535         break;
24536       }
24537
24538   FOR_EACH_CHILD (die, c, resolve_addr (c));
24539 }
24540 \f
24541 /* Helper routines for optimize_location_lists.
24542    This pass tries to share identical local lists in .debug_loc
24543    section.  */
24544
24545 /* Iteratively hash operands of LOC opcode.  */
24546
24547 static inline hashval_t
24548 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
24549 {
24550   dw_val_ref val1 = &loc->dw_loc_oprnd1;
24551   dw_val_ref val2 = &loc->dw_loc_oprnd2;
24552
24553   switch (loc->dw_loc_opc)
24554     {
24555     case DW_OP_const4u:
24556     case DW_OP_const8u:
24557       if (loc->dtprel)
24558         goto hash_addr;
24559       /* FALLTHRU */
24560     case DW_OP_const1u:
24561     case DW_OP_const1s:
24562     case DW_OP_const2u:
24563     case DW_OP_const2s:
24564     case DW_OP_const4s:
24565     case DW_OP_const8s:
24566     case DW_OP_constu:
24567     case DW_OP_consts:
24568     case DW_OP_pick:
24569     case DW_OP_plus_uconst:
24570     case DW_OP_breg0:
24571     case DW_OP_breg1:
24572     case DW_OP_breg2:
24573     case DW_OP_breg3:
24574     case DW_OP_breg4:
24575     case DW_OP_breg5:
24576     case DW_OP_breg6:
24577     case DW_OP_breg7:
24578     case DW_OP_breg8:
24579     case DW_OP_breg9:
24580     case DW_OP_breg10:
24581     case DW_OP_breg11:
24582     case DW_OP_breg12:
24583     case DW_OP_breg13:
24584     case DW_OP_breg14:
24585     case DW_OP_breg15:
24586     case DW_OP_breg16:
24587     case DW_OP_breg17:
24588     case DW_OP_breg18:
24589     case DW_OP_breg19:
24590     case DW_OP_breg20:
24591     case DW_OP_breg21:
24592     case DW_OP_breg22:
24593     case DW_OP_breg23:
24594     case DW_OP_breg24:
24595     case DW_OP_breg25:
24596     case DW_OP_breg26:
24597     case DW_OP_breg27:
24598     case DW_OP_breg28:
24599     case DW_OP_breg29:
24600     case DW_OP_breg30:
24601     case DW_OP_breg31:
24602     case DW_OP_regx:
24603     case DW_OP_fbreg:
24604     case DW_OP_piece:
24605     case DW_OP_deref_size:
24606     case DW_OP_xderef_size:
24607       hash = iterative_hash_object (val1->v.val_int, hash);
24608       break;
24609     case DW_OP_skip:
24610     case DW_OP_bra:
24611       {
24612         int offset;
24613
24614         gcc_assert (val1->val_class == dw_val_class_loc);
24615         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
24616         hash = iterative_hash_object (offset, hash);
24617       }
24618       break;
24619     case DW_OP_implicit_value:
24620       hash = iterative_hash_object (val1->v.val_unsigned, hash);
24621       switch (val2->val_class)
24622         {
24623         case dw_val_class_const:
24624           hash = iterative_hash_object (val2->v.val_int, hash);
24625           break;
24626         case dw_val_class_vec:
24627           {
24628             unsigned int elt_size = val2->v.val_vec.elt_size;
24629             unsigned int len = val2->v.val_vec.length;
24630
24631             hash = iterative_hash_object (elt_size, hash);
24632             hash = iterative_hash_object (len, hash);
24633             hash = iterative_hash (val2->v.val_vec.array,
24634                                    len * elt_size, hash);
24635           }
24636           break;
24637         case dw_val_class_const_double:
24638           hash = iterative_hash_object (val2->v.val_double.low, hash);
24639           hash = iterative_hash_object (val2->v.val_double.high, hash);
24640           break;
24641         case dw_val_class_addr:
24642           hash = iterative_hash_rtx (val2->v.val_addr, hash);
24643           break;
24644         default:
24645           gcc_unreachable ();
24646         }
24647       break;
24648     case DW_OP_bregx:
24649     case DW_OP_bit_piece:
24650       hash = iterative_hash_object (val1->v.val_int, hash);
24651       hash = iterative_hash_object (val2->v.val_int, hash);
24652       break;
24653     case DW_OP_addr:
24654     hash_addr:
24655       if (loc->dtprel)
24656         {
24657           unsigned char dtprel = 0xd1;
24658           hash = iterative_hash_object (dtprel, hash);
24659         }
24660       hash = iterative_hash_rtx (val1->v.val_addr, hash);
24661       break;
24662     case DW_OP_GNU_implicit_pointer:
24663       hash = iterative_hash_object (val2->v.val_int, hash);
24664       break;
24665     case DW_OP_GNU_entry_value:
24666       hash = hash_loc_operands (val1->v.val_loc, hash);
24667       break;
24668     case DW_OP_GNU_regval_type:
24669     case DW_OP_GNU_deref_type:
24670       {
24671         unsigned int byte_size
24672           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
24673         unsigned int encoding
24674           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
24675         hash = iterative_hash_object (val1->v.val_int, hash);
24676         hash = iterative_hash_object (byte_size, hash);
24677         hash = iterative_hash_object (encoding, hash);
24678       }
24679       break;
24680     case DW_OP_GNU_convert:
24681     case DW_OP_GNU_reinterpret:
24682       if (val1->val_class == dw_val_class_unsigned_const)
24683         {
24684           hash = iterative_hash_object (val1->v.val_unsigned, hash);
24685           break;
24686         }
24687       /* FALLTHRU */
24688     case DW_OP_GNU_const_type:
24689       {
24690         unsigned int byte_size
24691           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
24692         unsigned int encoding
24693           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
24694         hash = iterative_hash_object (byte_size, hash);
24695         hash = iterative_hash_object (encoding, hash);
24696         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
24697           break;
24698         hash = iterative_hash_object (val2->val_class, hash);
24699         switch (val2->val_class)
24700           {
24701           case dw_val_class_const:
24702             hash = iterative_hash_object (val2->v.val_int, hash);
24703             break;
24704           case dw_val_class_vec:
24705             {
24706               unsigned int elt_size = val2->v.val_vec.elt_size;
24707               unsigned int len = val2->v.val_vec.length;
24708
24709               hash = iterative_hash_object (elt_size, hash);
24710               hash = iterative_hash_object (len, hash);
24711               hash = iterative_hash (val2->v.val_vec.array,
24712                                      len * elt_size, hash);
24713             }
24714             break;
24715           case dw_val_class_const_double:
24716             hash = iterative_hash_object (val2->v.val_double.low, hash);
24717             hash = iterative_hash_object (val2->v.val_double.high, hash);
24718             break;
24719           default:
24720             gcc_unreachable ();
24721           }
24722       }
24723       break;
24724
24725     default:
24726       /* Other codes have no operands.  */
24727       break;
24728     }
24729   return hash;
24730 }
24731
24732 /* Iteratively hash the whole DWARF location expression LOC.  */
24733
24734 static inline hashval_t
24735 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
24736 {
24737   dw_loc_descr_ref l;
24738   bool sizes_computed = false;
24739   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24740   size_of_locs (loc);
24741
24742   for (l = loc; l != NULL; l = l->dw_loc_next)
24743     {
24744       enum dwarf_location_atom opc = l->dw_loc_opc;
24745       hash = iterative_hash_object (opc, hash);
24746       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24747         {
24748           size_of_locs (loc);
24749           sizes_computed = true;
24750         }
24751       hash = hash_loc_operands (l, hash);
24752     }
24753   return hash;
24754 }
24755
24756 /* Compute hash of the whole location list LIST_HEAD.  */
24757
24758 static inline void
24759 hash_loc_list (dw_loc_list_ref list_head)
24760 {
24761   dw_loc_list_ref curr = list_head;
24762   hashval_t hash = 0;
24763
24764   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24765     {
24766       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
24767       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
24768       if (curr->section)
24769         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
24770                                hash);
24771       hash = hash_locs (curr->expr, hash);
24772     }
24773   list_head->hash = hash;
24774 }
24775
24776 /* Return true if X and Y opcodes have the same operands.  */
24777
24778 static inline bool
24779 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24780 {
24781   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24782   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24783   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24784   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24785
24786   switch (x->dw_loc_opc)
24787     {
24788     case DW_OP_const4u:
24789     case DW_OP_const8u:
24790       if (x->dtprel)
24791         goto hash_addr;
24792       /* FALLTHRU */
24793     case DW_OP_const1u:
24794     case DW_OP_const1s:
24795     case DW_OP_const2u:
24796     case DW_OP_const2s:
24797     case DW_OP_const4s:
24798     case DW_OP_const8s:
24799     case DW_OP_constu:
24800     case DW_OP_consts:
24801     case DW_OP_pick:
24802     case DW_OP_plus_uconst:
24803     case DW_OP_breg0:
24804     case DW_OP_breg1:
24805     case DW_OP_breg2:
24806     case DW_OP_breg3:
24807     case DW_OP_breg4:
24808     case DW_OP_breg5:
24809     case DW_OP_breg6:
24810     case DW_OP_breg7:
24811     case DW_OP_breg8:
24812     case DW_OP_breg9:
24813     case DW_OP_breg10:
24814     case DW_OP_breg11:
24815     case DW_OP_breg12:
24816     case DW_OP_breg13:
24817     case DW_OP_breg14:
24818     case DW_OP_breg15:
24819     case DW_OP_breg16:
24820     case DW_OP_breg17:
24821     case DW_OP_breg18:
24822     case DW_OP_breg19:
24823     case DW_OP_breg20:
24824     case DW_OP_breg21:
24825     case DW_OP_breg22:
24826     case DW_OP_breg23:
24827     case DW_OP_breg24:
24828     case DW_OP_breg25:
24829     case DW_OP_breg26:
24830     case DW_OP_breg27:
24831     case DW_OP_breg28:
24832     case DW_OP_breg29:
24833     case DW_OP_breg30:
24834     case DW_OP_breg31:
24835     case DW_OP_regx:
24836     case DW_OP_fbreg:
24837     case DW_OP_piece:
24838     case DW_OP_deref_size:
24839     case DW_OP_xderef_size:
24840       return valx1->v.val_int == valy1->v.val_int;
24841     case DW_OP_skip:
24842     case DW_OP_bra:
24843       gcc_assert (valx1->val_class == dw_val_class_loc
24844                   && valy1->val_class == dw_val_class_loc
24845                   && x->dw_loc_addr == y->dw_loc_addr);
24846       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24847     case DW_OP_implicit_value:
24848       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24849           || valx2->val_class != valy2->val_class)
24850         return false;
24851       switch (valx2->val_class)
24852         {
24853         case dw_val_class_const:
24854           return valx2->v.val_int == valy2->v.val_int;
24855         case dw_val_class_vec:
24856           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24857                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24858                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24859                             valx2->v.val_vec.elt_size
24860                             * valx2->v.val_vec.length) == 0;
24861         case dw_val_class_const_double:
24862           return valx2->v.val_double.low == valy2->v.val_double.low
24863                  && valx2->v.val_double.high == valy2->v.val_double.high;
24864         case dw_val_class_addr:
24865           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24866         default:
24867           gcc_unreachable ();
24868         }
24869     case DW_OP_bregx:
24870     case DW_OP_bit_piece:
24871       return valx1->v.val_int == valy1->v.val_int
24872              && valx2->v.val_int == valy2->v.val_int;
24873     case DW_OP_addr:
24874     hash_addr:
24875       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24876     case DW_OP_GNU_implicit_pointer:
24877       return valx1->val_class == dw_val_class_die_ref
24878              && valx1->val_class == valy1->val_class
24879              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24880              && valx2->v.val_int == valy2->v.val_int;
24881     case DW_OP_GNU_entry_value:
24882       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24883     case DW_OP_GNU_const_type:
24884       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24885           || valx2->val_class != valy2->val_class)
24886         return false;
24887       switch (valx2->val_class)
24888         {
24889         case dw_val_class_const:
24890           return valx2->v.val_int == valy2->v.val_int;
24891         case dw_val_class_vec:
24892           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24893                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24894                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24895                             valx2->v.val_vec.elt_size
24896                             * valx2->v.val_vec.length) == 0;
24897         case dw_val_class_const_double:
24898           return valx2->v.val_double.low == valy2->v.val_double.low
24899                  && valx2->v.val_double.high == valy2->v.val_double.high;
24900         default:
24901           gcc_unreachable ();
24902         }
24903     case DW_OP_GNU_regval_type:
24904     case DW_OP_GNU_deref_type:
24905       return valx1->v.val_int == valy1->v.val_int
24906              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24907     case DW_OP_GNU_convert:
24908     case DW_OP_GNU_reinterpret:
24909       if (valx1->val_class != valy1->val_class)
24910         return false;
24911       if (valx1->val_class == dw_val_class_unsigned_const)
24912         return valx1->v.val_unsigned == valy1->v.val_unsigned;
24913       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24914     case DW_OP_GNU_parameter_ref:
24915       return valx1->val_class == dw_val_class_die_ref
24916              && valx1->val_class == valy1->val_class
24917              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24918     default:
24919       /* Other codes have no operands.  */
24920       return true;
24921     }
24922 }
24923
24924 /* Return true if DWARF location expressions X and Y are the same.  */
24925
24926 static inline bool
24927 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24928 {
24929   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24930     if (x->dw_loc_opc != y->dw_loc_opc
24931         || x->dtprel != y->dtprel
24932         || !compare_loc_operands (x, y))
24933       break;
24934   return x == NULL && y == NULL;
24935 }
24936
24937 /* Return precomputed hash of location list X.  */
24938
24939 static hashval_t
24940 loc_list_hash (const void *x)
24941 {
24942   return ((const struct dw_loc_list_struct *) x)->hash;
24943 }
24944
24945 /* Return 1 if location lists X and Y are the same.  */
24946
24947 static int
24948 loc_list_eq (const void *x, const void *y)
24949 {
24950   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
24951   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
24952   if (a == b)
24953     return 1;
24954   if (a->hash != b->hash)
24955     return 0;
24956   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24957     if (strcmp (a->begin, b->begin) != 0
24958         || strcmp (a->end, b->end) != 0
24959         || (a->section == NULL) != (b->section == NULL)
24960         || (a->section && strcmp (a->section, b->section) != 0)
24961         || !compare_locs (a->expr, b->expr))
24962       break;
24963   return a == NULL && b == NULL;
24964 }
24965
24966 /* Recursively optimize location lists referenced from DIE
24967    children and share them whenever possible.  */
24968
24969 static void
24970 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
24971 {
24972   dw_die_ref c;
24973   dw_attr_ref a;
24974   unsigned ix;
24975   void **slot;
24976
24977   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
24978     if (AT_class (a) == dw_val_class_loc_list)
24979       {
24980         dw_loc_list_ref list = AT_loc_list (a);
24981         /* TODO: perform some optimizations here, before hashing
24982            it and storing into the hash table.  */
24983         hash_loc_list (list);
24984         slot = htab_find_slot_with_hash (htab, list, list->hash,
24985                                          INSERT);
24986         if (*slot == NULL)
24987           *slot = (void *) list;
24988         else
24989           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
24990       }
24991
24992   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24993 }
24994
24995 /* Optimize location lists referenced from DIE
24996    children and share them whenever possible.  */
24997
24998 static void
24999 optimize_location_lists (dw_die_ref die)
25000 {
25001   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
25002   optimize_location_lists_1 (die, htab);
25003   htab_delete (htab);
25004 }
25005 \f
25006 /* Output stuff that dwarf requires at the end of every file,
25007    and generate the DWARF-2 debugging info.  */
25008
25009 static void
25010 dwarf2out_finish (const char *filename)
25011 {
25012   limbo_die_node *node, *next_node;
25013   comdat_type_node *ctnode;
25014   htab_t comdat_type_table;
25015   unsigned int i;
25016
25017   gen_scheduled_generic_parms_dies ();
25018   gen_remaining_tmpl_value_param_die_attribute ();
25019
25020   /* Add the name for the main input file now.  We delayed this from
25021      dwarf2out_init to avoid complications with PCH.  */
25022   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
25023   if (!IS_ABSOLUTE_PATH (filename))
25024     add_comp_dir_attribute (comp_unit_die ());
25025   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
25026     {
25027       bool p = false;
25028       htab_traverse (file_table, file_table_relative_p, &p);
25029       if (p)
25030         add_comp_dir_attribute (comp_unit_die ());
25031     }
25032
25033   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
25034     {
25035       add_location_or_const_value_attribute (
25036         VEC_index (deferred_locations, deferred_locations_list, i)->die,
25037         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
25038         false,
25039         DW_AT_location);
25040     }
25041
25042   /* Traverse the limbo die list, and add parent/child links.  The only
25043      dies without parents that should be here are concrete instances of
25044      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
25045      For concrete instances, we can get the parent die from the abstract
25046      instance.  */
25047   for (node = limbo_die_list; node; node = next_node)
25048     {
25049       dw_die_ref die = node->die;
25050       next_node = node->next;
25051
25052       if (die->die_parent == NULL)
25053         {
25054           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
25055
25056           if (origin && origin->die_parent)
25057             add_child_die (origin->die_parent, die);
25058           else if (is_cu_die (die))
25059             ;
25060           else if (seen_error ())
25061             /* It's OK to be confused by errors in the input.  */
25062             add_child_die (comp_unit_die (), die);
25063           else
25064             {
25065               /* In certain situations, the lexical block containing a
25066                  nested function can be optimized away, which results
25067                  in the nested function die being orphaned.  Likewise
25068                  with the return type of that nested function.  Force
25069                  this to be a child of the containing function.
25070
25071                  It may happen that even the containing function got fully
25072                  inlined and optimized out.  In that case we are lost and
25073                  assign the empty child.  This should not be big issue as
25074                  the function is likely unreachable too.  */
25075               tree context = NULL_TREE;
25076
25077               gcc_assert (node->created_for);
25078
25079               if (DECL_P (node->created_for))
25080                 context = DECL_CONTEXT (node->created_for);
25081               else if (TYPE_P (node->created_for))
25082                 context = TYPE_CONTEXT (node->created_for);
25083
25084               gcc_assert (context
25085                           && (TREE_CODE (context) == FUNCTION_DECL
25086                               || TREE_CODE (context) == NAMESPACE_DECL));
25087
25088               origin = lookup_decl_die (context);
25089               if (origin)
25090                 add_child_die (origin, die);
25091               else
25092                 add_child_die (comp_unit_die (), die);
25093             }
25094         }
25095     }
25096
25097   limbo_die_list = NULL;
25098
25099 #if ENABLE_ASSERT_CHECKING
25100   {
25101     dw_die_ref die = comp_unit_die (), c;
25102     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
25103   }
25104 #endif
25105   resolve_addr (comp_unit_die ());
25106   move_marked_base_types ();
25107
25108   for (node = deferred_asm_name; node; node = node->next)
25109     {
25110       tree decl = node->created_for;
25111       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
25112         {
25113           add_linkage_attr (node->die, decl);
25114           move_linkage_attr (node->die);
25115         }
25116     }
25117
25118   deferred_asm_name = NULL;
25119
25120   /* Walk through the list of incomplete types again, trying once more to
25121      emit full debugging info for them.  */
25122   retry_incomplete_types ();
25123
25124   if (flag_eliminate_unused_debug_types)
25125     prune_unused_types ();
25126
25127   /* Generate separate CUs for each of the include files we've seen.
25128      They will go into limbo_die_list.  */
25129   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
25130     break_out_includes (comp_unit_die ());
25131
25132   /* Generate separate COMDAT sections for type DIEs. */
25133   if (use_debug_types)
25134     {
25135       break_out_comdat_types (comp_unit_die ());
25136
25137       /* Each new type_unit DIE was added to the limbo die list when created.
25138          Since these have all been added to comdat_type_list, clear the
25139          limbo die list.  */
25140       limbo_die_list = NULL;
25141
25142       /* For each new comdat type unit, copy declarations for incomplete
25143          types to make the new unit self-contained (i.e., no direct
25144          references to the main compile unit).  */
25145       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25146         copy_decls_for_unworthy_types (ctnode->root_die);
25147       copy_decls_for_unworthy_types (comp_unit_die ());
25148
25149       /* In the process of copying declarations from one unit to another,
25150          we may have left some declarations behind that are no longer
25151          referenced.  Prune them.  */
25152       prune_unused_types ();
25153     }
25154
25155   /* Traverse the DIE's and add add sibling attributes to those DIE's
25156      that have children.  */
25157   add_sibling_attributes (comp_unit_die ());
25158   for (node = limbo_die_list; node; node = node->next)
25159     add_sibling_attributes (node->die);
25160   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25161     add_sibling_attributes (ctnode->root_die);
25162
25163   /* Output a terminator label for the .text section.  */
25164   switch_to_section (text_section);
25165   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
25166   if (cold_text_section)
25167     {
25168       switch_to_section (cold_text_section);
25169       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
25170     }
25171
25172   /* We can only use the low/high_pc attributes if all of the code was
25173      in .text.  */
25174   if (!have_multiple_function_sections 
25175       || (dwarf_version < 3 && dwarf_strict))
25176     {
25177       /* Don't add if the CU has no associated code.  */
25178       if (text_section_used)
25179         {
25180           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
25181           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
25182         }
25183     }
25184   else
25185     {
25186       unsigned fde_idx = 0;
25187       bool range_list_added = false;
25188
25189       if (text_section_used)
25190         add_ranges_by_labels (comp_unit_die (), text_section_label,
25191                               text_end_label, &range_list_added);
25192       if (cold_text_section_used)
25193         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
25194                               cold_end_label, &range_list_added);
25195
25196       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
25197         {
25198           dw_fde_ref fde = &fde_table[fde_idx];
25199
25200           if (!fde->in_std_section)
25201             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
25202                                   fde->dw_fde_end, &range_list_added);
25203           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
25204             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
25205                                   fde->dw_fde_second_end, &range_list_added);
25206         }
25207
25208       if (range_list_added)
25209         {
25210           /* We need to give .debug_loc and .debug_ranges an appropriate
25211              "base address".  Use zero so that these addresses become
25212              absolute.  Historically, we've emitted the unexpected
25213              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
25214              Emit both to give time for other tools to adapt.  */
25215           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
25216           if (! dwarf_strict && dwarf_version < 4)
25217             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
25218
25219           add_ranges (NULL);
25220         }
25221     }
25222
25223   if (debug_info_level >= DINFO_LEVEL_NORMAL)
25224     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
25225                     debug_line_section_label);
25226
25227   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25228     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
25229
25230   if (have_location_lists)
25231     optimize_location_lists (comp_unit_die ());
25232
25233   /* Output all of the compilation units.  We put the main one last so that
25234      the offsets are available to output_pubnames.  */
25235   for (node = limbo_die_list; node; node = node->next)
25236     output_comp_unit (node->die, 0);
25237
25238   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
25239   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
25240     {
25241       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
25242
25243       /* Don't output duplicate types.  */
25244       if (*slot != HTAB_EMPTY_ENTRY)
25245         continue;
25246
25247       /* Add a pointer to the line table for the main compilation unit
25248          so that the debugger can make sense of DW_AT_decl_file
25249          attributes.  */
25250       if (debug_info_level >= DINFO_LEVEL_NORMAL)
25251         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
25252                         debug_line_section_label);
25253
25254       output_comdat_type_unit (ctnode);
25255       *slot = ctnode;
25256     }
25257   htab_delete (comdat_type_table);
25258
25259   /* Output the main compilation unit if non-empty or if .debug_macinfo
25260      will be emitted.  */
25261   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
25262
25263   /* Output the abbreviation table.  */
25264   if (abbrev_die_table_in_use != 1)
25265     {
25266       switch_to_section (debug_abbrev_section);
25267       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
25268       output_abbrev_section ();
25269     }
25270
25271   /* Output location list section if necessary.  */
25272   if (have_location_lists)
25273     {
25274       /* Output the location lists info.  */
25275       switch_to_section (debug_loc_section);
25276       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
25277                                    DEBUG_LOC_SECTION_LABEL, 0);
25278       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
25279       output_location_lists (comp_unit_die ());
25280     }
25281
25282   /* Output public names table if necessary.  */
25283   if (!VEC_empty (pubname_entry, pubname_table))
25284     {
25285       gcc_assert (info_section_emitted);
25286       switch_to_section (debug_pubnames_section);
25287       output_pubnames (pubname_table);
25288     }
25289
25290   /* Output public types table if necessary.  */
25291   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
25292      It shouldn't hurt to emit it always, since pure DWARF2 consumers
25293      simply won't look for the section.  */
25294   if (!VEC_empty (pubname_entry, pubtype_table))
25295     {
25296       bool empty = false;
25297       
25298       if (flag_eliminate_unused_debug_types)
25299         {
25300           /* The pubtypes table might be emptied by pruning unused items.  */
25301           unsigned i;
25302           pubname_ref p;
25303           empty = true;
25304           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
25305             if (p->die->die_offset != 0)
25306               {
25307                 empty = false;
25308                 break;
25309               }
25310         }
25311       if (!empty)
25312         {
25313           gcc_assert (info_section_emitted);
25314           switch_to_section (debug_pubtypes_section);
25315           output_pubnames (pubtype_table);
25316         }
25317     }
25318
25319   /* Output the address range information if a CU (.debug_info section)
25320      was emitted.  We output an empty table even if we had no functions
25321      to put in it.  This because the consumer has no way to tell the
25322      difference between an empty table that we omitted and failure to
25323      generate a table that would have contained data.  */
25324   if (info_section_emitted)
25325     {
25326       unsigned long aranges_length = size_of_aranges ();
25327
25328       switch_to_section (debug_aranges_section);
25329       output_aranges (aranges_length);
25330     }
25331
25332   /* Output ranges section if necessary.  */
25333   if (ranges_table_in_use)
25334     {
25335       switch_to_section (debug_ranges_section);
25336       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
25337       output_ranges ();
25338     }
25339
25340   /* Output the source line correspondence table.  We must do this
25341      even if there is no line information.  Otherwise, on an empty
25342      translation unit, we will generate a present, but empty,
25343      .debug_info section.  IRIX 6.5 `nm' will then complain when
25344      examining the file.  This is done late so that any filenames
25345      used by the debug_info section are marked as 'used'.  */
25346   switch_to_section (debug_line_section);
25347   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
25348   if (! DWARF2_ASM_LINE_DEBUG_INFO)
25349     output_line_info ();
25350
25351   /* Have to end the macro section.  */
25352   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
25353     {
25354       switch_to_section (debug_macinfo_section);
25355       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
25356       if (!VEC_empty (macinfo_entry, macinfo_table))
25357         output_macinfo ();
25358       dw2_asm_output_data (1, 0, "End compilation unit");
25359     }
25360
25361   /* If we emitted any DW_FORM_strp form attribute, output the string
25362      table too.  */
25363   if (debug_str_hash)
25364     htab_traverse (debug_str_hash, output_indirect_string, NULL);
25365 }
25366
25367 #include "gt-dwarf2out.h"