OSDN Git Service

Revert "Fix PR debug/49047"
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95 #include "cfglayout.h"
96
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98 static rtx last_var_location_insn;
99
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102
103 /* Define this macro to be a nonzero value if the directory specifications
104     which are output in the debug info should end with a separator.  */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107    from generating indirect strings in DWARF2 debug information, for instance
108    if your target is stuck with an old version of GDB that is unable to
109    process them properly or uses VMS Debug.  */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
115
116 /* ??? Poison these here until it can be done generically.  They've been
117    totally replaced in this file; make sure it stays that way.  */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
123
124 #ifndef INCOMING_RETURN_ADDR_RTX
125 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
126 #endif
127
128 /* Map register numbers held in the call frame info that gcc has
129    collected using DWARF_FRAME_REGNUM to those that should be output in
130    .debug_frame and .eh_frame.  */
131 #ifndef DWARF2_FRAME_REG_OUT
132 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
133 #endif
134
135 /* Save the result of dwarf2out_do_frame across PCH.  */
136 static GTY(()) bool saved_do_cfi_asm = 0;
137
138 /* Decide whether we want to emit frame unwind information for the current
139    translation unit.  */
140
141 int
142 dwarf2out_do_frame (void)
143 {
144   /* We want to emit correct CFA location expressions or lists, so we
145      have to return true if we're going to output debug info, even if
146      we're not going to output frame or unwind info.  */
147   if (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
148     return true;
149
150   if (saved_do_cfi_asm)
151     return true;
152
153   if (targetm.debug_unwind_info () == UI_DWARF2)
154     return true;
155
156   if ((flag_unwind_tables || flag_exceptions)
157       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
158     return true;
159
160   return false;
161 }
162
163 /* Decide whether to emit frame unwind via assembler directives.  */
164
165 int
166 dwarf2out_do_cfi_asm (void)
167 {
168   int enc;
169
170 #ifdef MIPS_DEBUGGING_INFO
171   return false;
172 #endif
173   if (saved_do_cfi_asm)
174     return true;
175   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
176     return false;
177   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
178     return false;
179
180   /* Make sure the personality encoding is one the assembler can support.
181      In particular, aligned addresses can't be handled.  */
182   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
183   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
184     return false;
185   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
186   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
187     return false;
188
189   /* If we can't get the assembler to emit only .debug_frame, and we don't need
190      dwarf2 unwind info for exceptions, then emit .debug_frame by hand.  */
191   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE
192       && !flag_unwind_tables && !flag_exceptions
193       && targetm.except_unwind_info (&global_options) != UI_DWARF2)
194     return false;
195
196   saved_do_cfi_asm = true;
197   return true;
198 }
199
200 /* The size of the target's pointer type.  */
201 #ifndef PTR_SIZE
202 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
203 #endif
204
205 /* Array of RTXes referenced by the debugging information, which therefore
206    must be kept around forever.  */
207 static GTY(()) VEC(rtx,gc) *used_rtx_array;
208
209 /* A pointer to the base of a list of incomplete types which might be
210    completed at some later time.  incomplete_types_list needs to be a
211    VEC(tree,gc) because we want to tell the garbage collector about
212    it.  */
213 static GTY(()) VEC(tree,gc) *incomplete_types;
214
215 /* A pointer to the base of a table of references to declaration
216    scopes.  This table is a display which tracks the nesting
217    of declaration scopes at the current scope and containing
218    scopes.  This table is used to find the proper place to
219    define type declaration DIE's.  */
220 static GTY(()) VEC(tree,gc) *decl_scope_table;
221
222 /* Pointers to various DWARF2 sections.  */
223 static GTY(()) section *debug_info_section;
224 static GTY(()) section *debug_abbrev_section;
225 static GTY(()) section *debug_aranges_section;
226 static GTY(()) section *debug_macinfo_section;
227 static GTY(()) section *debug_line_section;
228 static GTY(()) section *debug_loc_section;
229 static GTY(()) section *debug_pubnames_section;
230 static GTY(()) section *debug_pubtypes_section;
231 static GTY(()) section *debug_str_section;
232 static GTY(()) section *debug_ranges_section;
233 static GTY(()) section *debug_frame_section;
234
235 /* Personality decl of current unit.  Used only when assembler does not support
236    personality CFI.  */
237 static GTY(()) rtx current_unit_personality;
238
239 /* How to start an assembler comment.  */
240 #ifndef ASM_COMMENT_START
241 #define ASM_COMMENT_START ";#"
242 #endif
243
244 typedef struct dw_cfi_struct *dw_cfi_ref;
245 typedef struct dw_fde_struct *dw_fde_ref;
246 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
247
248 /* Call frames are described using a sequence of Call Frame
249    Information instructions.  The register number, offset
250    and address fields are provided as possible operands;
251    their use is selected by the opcode field.  */
252
253 enum dw_cfi_oprnd_type {
254   dw_cfi_oprnd_unused,
255   dw_cfi_oprnd_reg_num,
256   dw_cfi_oprnd_offset,
257   dw_cfi_oprnd_addr,
258   dw_cfi_oprnd_loc
259 };
260
261 typedef union GTY(()) dw_cfi_oprnd_struct {
262   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
263   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
264   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
265   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
266 }
267 dw_cfi_oprnd;
268
269 typedef struct GTY(()) dw_cfi_struct {
270   enum dwarf_call_frame_info dw_cfi_opc;
271   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
272     dw_cfi_oprnd1;
273   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
274     dw_cfi_oprnd2;
275 }
276 dw_cfi_node;
277
278 DEF_VEC_P (dw_cfi_ref);
279 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
280 DEF_VEC_ALLOC_P (dw_cfi_ref, gc);
281
282 typedef VEC(dw_cfi_ref, gc) *cfi_vec;
283
284 /* This is how we define the location of the CFA. We use to handle it
285    as REG + OFFSET all the time,  but now it can be more complex.
286    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
287    Instead of passing around REG and OFFSET, we pass a copy
288    of this structure.  */
289 typedef struct cfa_loc {
290   HOST_WIDE_INT offset;
291   HOST_WIDE_INT base_offset;
292   unsigned int reg;
293   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
294   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
295 } dw_cfa_location;
296
297 /* All call frame descriptions (FDE's) in the GCC generated DWARF
298    refer to a single Common Information Entry (CIE), defined at
299    the beginning of the .debug_frame section.  This use of a single
300    CIE obviates the need to keep track of multiple CIE's
301    in the DWARF generation routines below.  */
302
303 typedef struct GTY(()) dw_fde_struct {
304   tree decl;
305   const char *dw_fde_begin;
306   const char *dw_fde_current_label;
307   const char *dw_fde_end;
308   const char *dw_fde_vms_end_prologue;
309   const char *dw_fde_vms_begin_epilogue;
310   const char *dw_fde_second_begin;
311   const char *dw_fde_second_end;
312   cfi_vec dw_fde_cfi;
313   int dw_fde_switch_cfi_index; /* Last CFI before switching sections.  */
314   HOST_WIDE_INT stack_realignment;
315   unsigned funcdef_number;
316   /* Dynamic realign argument pointer register.  */
317   unsigned int drap_reg;
318   /* Virtual dynamic realign argument pointer register.  */
319   unsigned int vdrap_reg;
320   /* These 3 flags are copied from rtl_data in function.h.  */
321   unsigned all_throwers_are_sibcalls : 1;
322   unsigned uses_eh_lsda : 1;
323   unsigned nothrow : 1;
324   /* Whether we did stack realign in this call frame.  */
325   unsigned stack_realign : 1;
326   /* Whether dynamic realign argument pointer register has been saved.  */
327   unsigned drap_reg_saved: 1;
328   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
329   unsigned in_std_section : 1;
330   /* True iff dw_fde_second_begin label is in text_section or
331      cold_text_section.  */
332   unsigned second_in_std_section : 1;
333 }
334 dw_fde_node;
335
336 /* Maximum size (in bytes) of an artificially generated label.  */
337 #define MAX_ARTIFICIAL_LABEL_BYTES      30
338
339 /* The size of addresses as they appear in the Dwarf 2 data.
340    Some architectures use word addresses to refer to code locations,
341    but Dwarf 2 info always uses byte addresses.  On such machines,
342    Dwarf 2 addresses need to be larger than the architecture's
343    pointers.  */
344 #ifndef DWARF2_ADDR_SIZE
345 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
346 #endif
347
348 /* The size in bytes of a DWARF field indicating an offset or length
349    relative to a debug info section, specified to be 4 bytes in the
350    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
351    as PTR_SIZE.  */
352
353 #ifndef DWARF_OFFSET_SIZE
354 #define DWARF_OFFSET_SIZE 4
355 #endif
356
357 /* The size in bytes of a DWARF 4 type signature.  */
358
359 #ifndef DWARF_TYPE_SIGNATURE_SIZE
360 #define DWARF_TYPE_SIGNATURE_SIZE 8
361 #endif
362
363 /* According to the (draft) DWARF 3 specification, the initial length
364    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
365    bytes are 0xffffffff, followed by the length stored in the next 8
366    bytes.
367
368    However, the SGI/MIPS ABI uses an initial length which is equal to
369    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
370
371 #ifndef DWARF_INITIAL_LENGTH_SIZE
372 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
373 #endif
374
375 /* Round SIZE up to the nearest BOUNDARY.  */
376 #define DWARF_ROUND(SIZE,BOUNDARY) \
377   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
378
379 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
380 #ifndef DWARF_CIE_DATA_ALIGNMENT
381 #ifdef STACK_GROWS_DOWNWARD
382 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
383 #else
384 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
385 #endif
386 #endif
387
388 /* CIE identifier.  */
389 #if HOST_BITS_PER_WIDE_INT >= 64
390 #define DWARF_CIE_ID \
391   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
392 #else
393 #define DWARF_CIE_ID DW_CIE_ID
394 #endif
395
396 /* A pointer to the base of a table that contains frame description
397    information for each routine.  */
398 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
399
400 /* Number of elements currently allocated for fde_table.  */
401 static GTY(()) unsigned fde_table_allocated;
402
403 /* Number of elements in fde_table currently in use.  */
404 static GTY(()) unsigned fde_table_in_use;
405
406 /* Size (in elements) of increments by which we may expand the
407    fde_table.  */
408 #define FDE_TABLE_INCREMENT 256
409
410 /* Get the current fde_table entry we should use.  */
411
412 static inline dw_fde_ref
413 current_fde (void)
414 {
415   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
416 }
417
418 /* A vector of call frame insns for the CIE.  */
419 static GTY(()) cfi_vec cie_cfi_vec;
420
421 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
422    attribute that accelerates the lookup of the FDE associated
423    with the subprogram.  This variable holds the table index of the FDE
424    associated with the current function (body) definition.  */
425 static unsigned current_funcdef_fde;
426
427 struct GTY(()) indirect_string_node {
428   const char *str;
429   unsigned int refcount;
430   enum dwarf_form form;
431   char *label;
432 };
433
434 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
435
436 static GTY(()) int dw2_string_counter;
437 static GTY(()) unsigned long dwarf2out_cfi_label_num;
438
439 /* True if the compilation unit places functions in more than one section.  */
440 static GTY(()) bool have_multiple_function_sections = false;
441
442 /* Whether the default text and cold text sections have been used at all.  */
443
444 static GTY(()) bool text_section_used = false;
445 static GTY(()) bool cold_text_section_used = false;
446
447 /* The default cold text section.  */
448 static GTY(()) section *cold_text_section;
449
450 /* Forward declarations for functions defined in this file.  */
451
452 static char *stripattributes (const char *);
453 static const char *dwarf_cfi_name (unsigned);
454 static dw_cfi_ref new_cfi (void);
455 static void add_cfi (cfi_vec *, dw_cfi_ref);
456 static void add_fde_cfi (const char *, dw_cfi_ref);
457 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
458 static void lookup_cfa (dw_cfa_location *);
459 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
460 static void initial_return_save (rtx);
461 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
462                                           HOST_WIDE_INT);
463 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
464 static void output_cfi_directive (dw_cfi_ref);
465 static void output_call_frame_info (int);
466 static void dwarf2out_note_section_used (void);
467 static bool clobbers_queued_reg_save (const_rtx);
468 static void dwarf2out_frame_debug_expr (rtx, const char *);
469
470 /* Support for complex CFA locations.  */
471 static void output_cfa_loc (dw_cfi_ref, int);
472 static void output_cfa_loc_raw (dw_cfi_ref);
473 static void get_cfa_from_loc_descr (dw_cfa_location *,
474                                     struct dw_loc_descr_struct *);
475 static struct dw_loc_descr_struct *build_cfa_loc
476   (dw_cfa_location *, HOST_WIDE_INT);
477 static struct dw_loc_descr_struct *build_cfa_aligned_loc
478   (HOST_WIDE_INT, HOST_WIDE_INT);
479 static void def_cfa_1 (const char *, dw_cfa_location *);
480 static struct dw_loc_descr_struct *mem_loc_descriptor
481   (rtx, enum machine_mode mode, enum machine_mode mem_mode,
482    enum var_init_status);
483
484 /* How to start an assembler comment.  */
485 #ifndef ASM_COMMENT_START
486 #define ASM_COMMENT_START ";#"
487 #endif
488
489 /* Data and reference forms for relocatable data.  */
490 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
491 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
492
493 #ifndef DEBUG_FRAME_SECTION
494 #define DEBUG_FRAME_SECTION     ".debug_frame"
495 #endif
496
497 #ifndef FUNC_BEGIN_LABEL
498 #define FUNC_BEGIN_LABEL        "LFB"
499 #endif
500
501 #ifndef FUNC_END_LABEL
502 #define FUNC_END_LABEL          "LFE"
503 #endif
504
505 #ifndef PROLOGUE_END_LABEL
506 #define PROLOGUE_END_LABEL      "LPE"
507 #endif
508
509 #ifndef EPILOGUE_BEGIN_LABEL
510 #define EPILOGUE_BEGIN_LABEL    "LEB"
511 #endif
512
513 #ifndef FRAME_BEGIN_LABEL
514 #define FRAME_BEGIN_LABEL       "Lframe"
515 #endif
516 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
517 #define CIE_END_LABEL           "LECIE"
518 #define FDE_LABEL               "LSFDE"
519 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
520 #define FDE_END_LABEL           "LEFDE"
521 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
522 #define LINE_NUMBER_END_LABEL   "LELT"
523 #define LN_PROLOG_AS_LABEL      "LASLTP"
524 #define LN_PROLOG_END_LABEL     "LELTP"
525 #define DIE_LABEL_PREFIX        "DW"
526
527 /* The DWARF 2 CFA column which tracks the return address.  Normally this
528    is the column for PC, or the first column after all of the hard
529    registers.  */
530 #ifndef DWARF_FRAME_RETURN_COLUMN
531 #ifdef PC_REGNUM
532 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
533 #else
534 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
535 #endif
536 #endif
537
538 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
539    default, we just provide columns for all registers.  */
540 #ifndef DWARF_FRAME_REGNUM
541 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
542 #endif
543 \f
544 /* Match the base name of a file to the base name of a compilation unit. */
545
546 static int
547 matches_main_base (const char *path)
548 {
549   /* Cache the last query. */
550   static const char *last_path = NULL;
551   static int last_match = 0;
552   if (path != last_path)
553     {
554       const char *base;
555       int length = base_of_path (path, &base);
556       last_path = path;
557       last_match = (length == main_input_baselength
558                     && memcmp (base, main_input_basename, length) == 0);
559     }
560   return last_match;
561 }
562
563 #ifdef DEBUG_DEBUG_STRUCT
564
565 static int
566 dump_struct_debug (tree type, enum debug_info_usage usage,
567                    enum debug_struct_file criterion, int generic,
568                    int matches, int result)
569 {
570   /* Find the type name. */
571   tree type_decl = TYPE_STUB_DECL (type);
572   tree t = type_decl;
573   const char *name = 0;
574   if (TREE_CODE (t) == TYPE_DECL)
575     t = DECL_NAME (t);
576   if (t)
577     name = IDENTIFIER_POINTER (t);
578
579   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
580            criterion,
581            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
582            matches ? "bas" : "hdr",
583            generic ? "gen" : "ord",
584            usage == DINFO_USAGE_DFN ? ";" :
585              usage == DINFO_USAGE_DIR_USE ? "." : "*",
586            result,
587            (void*) type_decl, name);
588   return result;
589 }
590 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
591   dump_struct_debug (type, usage, criterion, generic, matches, result)
592
593 #else
594
595 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
596   (result)
597
598 #endif
599
600 static bool
601 should_emit_struct_debug (tree type, enum debug_info_usage usage)
602 {
603   enum debug_struct_file criterion;
604   tree type_decl;
605   bool generic = lang_hooks.types.generic_p (type);
606
607   if (generic)
608     criterion = debug_struct_generic[usage];
609   else
610     criterion = debug_struct_ordinary[usage];
611
612   if (criterion == DINFO_STRUCT_FILE_NONE)
613     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
614   if (criterion == DINFO_STRUCT_FILE_ANY)
615     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
616
617   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
618
619   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
620     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
621
622   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
623     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
624   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
625 }
626 \f
627 /* Hook used by __throw.  */
628
629 rtx
630 expand_builtin_dwarf_sp_column (void)
631 {
632   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
633   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
634 }
635
636 /* Return a pointer to a copy of the section string name S with all
637    attributes stripped off, and an asterisk prepended (for assemble_name).  */
638
639 static inline char *
640 stripattributes (const char *s)
641 {
642   char *stripped = XNEWVEC (char, strlen (s) + 2);
643   char *p = stripped;
644
645   *p++ = '*';
646
647   while (*s && *s != ',')
648     *p++ = *s++;
649
650   *p = '\0';
651   return stripped;
652 }
653
654 /* MEM is a memory reference for the register size table, each element of
655    which has mode MODE.  Initialize column C as a return address column.  */
656
657 static void
658 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
659 {
660   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
661   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
662   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
663 }
664
665 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
666
667 static inline HOST_WIDE_INT
668 div_data_align (HOST_WIDE_INT off)
669 {
670   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
671   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
672   return r;
673 }
674
675 /* Return true if we need a signed version of a given opcode
676    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
677
678 static inline bool
679 need_data_align_sf_opcode (HOST_WIDE_INT off)
680 {
681   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
682 }
683
684 /* Generate code to initialize the register size table.  */
685
686 void
687 expand_builtin_init_dwarf_reg_sizes (tree address)
688 {
689   unsigned int i;
690   enum machine_mode mode = TYPE_MODE (char_type_node);
691   rtx addr = expand_normal (address);
692   rtx mem = gen_rtx_MEM (BLKmode, addr);
693   bool wrote_return_column = false;
694
695   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
696     {
697       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
698
699       if (rnum < DWARF_FRAME_REGISTERS)
700         {
701           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
702           enum machine_mode save_mode = reg_raw_mode[i];
703           HOST_WIDE_INT size;
704
705           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
706             save_mode = choose_hard_reg_mode (i, 1, true);
707           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
708             {
709               if (save_mode == VOIDmode)
710                 continue;
711               wrote_return_column = true;
712             }
713           size = GET_MODE_SIZE (save_mode);
714           if (offset < 0)
715             continue;
716
717           emit_move_insn (adjust_address (mem, mode, offset),
718                           gen_int_mode (size, mode));
719         }
720     }
721
722   if (!wrote_return_column)
723     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
724
725 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
726   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
727 #endif
728
729   targetm.init_dwarf_reg_sizes_extra (address);
730 }
731
732 /* Convert a DWARF call frame info. operation to its string name */
733
734 static const char *
735 dwarf_cfi_name (unsigned int cfi_opc)
736 {
737   switch (cfi_opc)
738     {
739     case DW_CFA_advance_loc:
740       return "DW_CFA_advance_loc";
741     case DW_CFA_offset:
742       return "DW_CFA_offset";
743     case DW_CFA_restore:
744       return "DW_CFA_restore";
745     case DW_CFA_nop:
746       return "DW_CFA_nop";
747     case DW_CFA_set_loc:
748       return "DW_CFA_set_loc";
749     case DW_CFA_advance_loc1:
750       return "DW_CFA_advance_loc1";
751     case DW_CFA_advance_loc2:
752       return "DW_CFA_advance_loc2";
753     case DW_CFA_advance_loc4:
754       return "DW_CFA_advance_loc4";
755     case DW_CFA_offset_extended:
756       return "DW_CFA_offset_extended";
757     case DW_CFA_restore_extended:
758       return "DW_CFA_restore_extended";
759     case DW_CFA_undefined:
760       return "DW_CFA_undefined";
761     case DW_CFA_same_value:
762       return "DW_CFA_same_value";
763     case DW_CFA_register:
764       return "DW_CFA_register";
765     case DW_CFA_remember_state:
766       return "DW_CFA_remember_state";
767     case DW_CFA_restore_state:
768       return "DW_CFA_restore_state";
769     case DW_CFA_def_cfa:
770       return "DW_CFA_def_cfa";
771     case DW_CFA_def_cfa_register:
772       return "DW_CFA_def_cfa_register";
773     case DW_CFA_def_cfa_offset:
774       return "DW_CFA_def_cfa_offset";
775
776     /* DWARF 3 */
777     case DW_CFA_def_cfa_expression:
778       return "DW_CFA_def_cfa_expression";
779     case DW_CFA_expression:
780       return "DW_CFA_expression";
781     case DW_CFA_offset_extended_sf:
782       return "DW_CFA_offset_extended_sf";
783     case DW_CFA_def_cfa_sf:
784       return "DW_CFA_def_cfa_sf";
785     case DW_CFA_def_cfa_offset_sf:
786       return "DW_CFA_def_cfa_offset_sf";
787
788     /* SGI/MIPS specific */
789     case DW_CFA_MIPS_advance_loc8:
790       return "DW_CFA_MIPS_advance_loc8";
791
792     /* GNU extensions */
793     case DW_CFA_GNU_window_save:
794       return "DW_CFA_GNU_window_save";
795     case DW_CFA_GNU_args_size:
796       return "DW_CFA_GNU_args_size";
797     case DW_CFA_GNU_negative_offset_extended:
798       return "DW_CFA_GNU_negative_offset_extended";
799
800     default:
801       return "DW_CFA_<unknown>";
802     }
803 }
804
805 /* Return a pointer to a newly allocated Call Frame Instruction.  */
806
807 static inline dw_cfi_ref
808 new_cfi (void)
809 {
810   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
811
812   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
813   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
814
815   return cfi;
816 }
817
818 /* Add a Call Frame Instruction to list of instructions.  */
819
820 static inline void
821 add_cfi (cfi_vec *vec, dw_cfi_ref cfi)
822 {
823   dw_fde_ref fde = current_fde ();
824
825   /* When DRAP is used, CFA is defined with an expression.  Redefine
826      CFA may lead to a different CFA value.   */
827   /* ??? Of course, this heuristic fails when we're annotating epilogues,
828      because of course we'll always want to redefine the CFA back to the
829      stack pointer on the way out.  Where should we move this check?  */
830   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
831     switch (cfi->dw_cfi_opc)
832       {
833         case DW_CFA_def_cfa_register:
834         case DW_CFA_def_cfa_offset:
835         case DW_CFA_def_cfa_offset_sf:
836         case DW_CFA_def_cfa:
837         case DW_CFA_def_cfa_sf:
838           gcc_unreachable ();
839
840         default:
841           break;
842       }
843
844   VEC_safe_push (dw_cfi_ref, gc, *vec, cfi);
845 }
846
847 /* Generate a new label for the CFI info to refer to.  FORCE is true
848    if a label needs to be output even when using .cfi_* directives.  */
849
850 char *
851 dwarf2out_cfi_label (bool force)
852 {
853   static char label[20];
854
855   if (!force && dwarf2out_do_cfi_asm ())
856     {
857       /* In this case, we will be emitting the asm directive instead of
858          the label, so just return a placeholder to keep the rest of the
859          interfaces happy.  */
860       strcpy (label, "<do not output>");
861     }
862   else
863     {
864       int num = dwarf2out_cfi_label_num++;
865       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", num);
866       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LCFI", num);
867     }
868
869   return label;
870 }
871
872 /* True if remember_state should be emitted before following CFI directive.  */
873 static bool emit_cfa_remember;
874
875 /* True if any CFI directives were emitted at the current insn.  */
876 static bool any_cfis_emitted;
877
878 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
879    or to the CIE if LABEL is NULL.  */
880
881 static void
882 add_fde_cfi (const char *label, dw_cfi_ref cfi)
883 {
884   cfi_vec *vec;
885
886   if (cie_cfi_vec == NULL)
887     cie_cfi_vec = VEC_alloc (dw_cfi_ref, gc, 20);
888
889   vec = &cie_cfi_vec;
890
891   if (emit_cfa_remember)
892     {
893       dw_cfi_ref cfi_remember;
894
895       /* Emit the state save.  */
896       emit_cfa_remember = false;
897       cfi_remember = new_cfi ();
898       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
899       add_fde_cfi (label, cfi_remember);
900     }
901
902   if (dwarf2out_do_cfi_asm ())
903     {
904       if (label)
905         {
906           dw_fde_ref fde = current_fde ();
907
908           gcc_assert (fde != NULL);
909
910           /* We still have to add the cfi to the list so that lookup_cfa
911              works later on.  When -g2 and above we even need to force
912              emitting of CFI labels and add to list a DW_CFA_set_loc for
913              convert_cfa_to_fb_loc_list purposes.  If we're generating
914              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
915              convert_cfa_to_fb_loc_list.  */
916           if (dwarf_version == 2
917               && debug_info_level > DINFO_LEVEL_TERSE
918               && (write_symbols == DWARF2_DEBUG
919                   || write_symbols == VMS_AND_DWARF2_DEBUG))
920             {
921               switch (cfi->dw_cfi_opc)
922                 {
923                 case DW_CFA_def_cfa_offset:
924                 case DW_CFA_def_cfa_offset_sf:
925                 case DW_CFA_def_cfa_register:
926                 case DW_CFA_def_cfa:
927                 case DW_CFA_def_cfa_sf:
928                 case DW_CFA_def_cfa_expression:
929                 case DW_CFA_restore_state:
930                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
931                     label = dwarf2out_cfi_label (true);
932
933                   if (fde->dw_fde_current_label == NULL
934                       || strcmp (label, fde->dw_fde_current_label) != 0)
935                     {
936                       dw_cfi_ref xcfi;
937
938                       label = xstrdup (label);
939
940                       /* Set the location counter to the new label.  */
941                       xcfi = new_cfi ();
942                       /* It doesn't metter whether DW_CFA_set_loc
943                          or DW_CFA_advance_loc4 is added here, those aren't
944                          emitted into assembly, only looked up by
945                          convert_cfa_to_fb_loc_list.  */
946                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
947                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
948                       add_cfi (&fde->dw_fde_cfi, xcfi);
949                       fde->dw_fde_current_label = label;
950                     }
951                   break;
952                 default:
953                   break;
954                 }
955             }
956
957           output_cfi_directive (cfi);
958
959           vec = &fde->dw_fde_cfi;
960           any_cfis_emitted = true;
961         }
962       /* ??? If this is a CFI for the CIE, we don't emit.  This
963          assumes that the standard CIE contents that the assembler
964          uses matches the standard CIE contents that the compiler
965          uses.  This is probably a bad assumption.  I'm not quite
966          sure how to address this for now.  */
967     }
968   else if (label)
969     {
970       dw_fde_ref fde = current_fde ();
971
972       gcc_assert (fde != NULL);
973
974       if (*label == 0)
975         label = dwarf2out_cfi_label (false);
976
977       if (fde->dw_fde_current_label == NULL
978           || strcmp (label, fde->dw_fde_current_label) != 0)
979         {
980           dw_cfi_ref xcfi;
981
982           label = xstrdup (label);
983
984           /* Set the location counter to the new label.  */
985           xcfi = new_cfi ();
986           /* If we have a current label, advance from there, otherwise
987              set the location directly using set_loc.  */
988           xcfi->dw_cfi_opc = fde->dw_fde_current_label
989                              ? DW_CFA_advance_loc4
990                              : DW_CFA_set_loc;
991           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
992           add_cfi (&fde->dw_fde_cfi, xcfi);
993
994           fde->dw_fde_current_label = label;
995         }
996
997       vec = &fde->dw_fde_cfi;
998       any_cfis_emitted = true;
999     }
1000
1001   add_cfi (vec, cfi);
1002 }
1003
1004 /* Subroutine of lookup_cfa.  */
1005
1006 static void
1007 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
1008 {
1009   switch (cfi->dw_cfi_opc)
1010     {
1011     case DW_CFA_def_cfa_offset:
1012     case DW_CFA_def_cfa_offset_sf:
1013       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
1014       break;
1015     case DW_CFA_def_cfa_register:
1016       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1017       break;
1018     case DW_CFA_def_cfa:
1019     case DW_CFA_def_cfa_sf:
1020       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
1021       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
1022       break;
1023     case DW_CFA_def_cfa_expression:
1024       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
1025       break;
1026
1027     case DW_CFA_remember_state:
1028       gcc_assert (!remember->in_use);
1029       *remember = *loc;
1030       remember->in_use = 1;
1031       break;
1032     case DW_CFA_restore_state:
1033       gcc_assert (remember->in_use);
1034       *loc = *remember;
1035       remember->in_use = 0;
1036       break;
1037
1038     default:
1039       break;
1040     }
1041 }
1042
1043 /* Find the previous value for the CFA.  */
1044
1045 static void
1046 lookup_cfa (dw_cfa_location *loc)
1047 {
1048   int ix;
1049   dw_cfi_ref cfi;
1050   dw_fde_ref fde;
1051   dw_cfa_location remember;
1052
1053   memset (loc, 0, sizeof (*loc));
1054   loc->reg = INVALID_REGNUM;
1055   remember = *loc;
1056
1057   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
1058     lookup_cfa_1 (cfi, loc, &remember);
1059
1060   fde = current_fde ();
1061   if (fde)
1062     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1063       lookup_cfa_1 (cfi, loc, &remember);
1064 }
1065
1066 /* The current rule for calculating the DWARF2 canonical frame address.  */
1067 static dw_cfa_location cfa;
1068
1069 /* The register used for saving registers to the stack, and its offset
1070    from the CFA.  */
1071 static dw_cfa_location cfa_store;
1072
1073 /* The current save location around an epilogue.  */
1074 static dw_cfa_location cfa_remember;
1075
1076 /* The running total of the size of arguments pushed onto the stack.  */
1077 static HOST_WIDE_INT args_size;
1078
1079 /* The last args_size we actually output.  */
1080 static HOST_WIDE_INT old_args_size;
1081
1082 /* Entry point to update the canonical frame address (CFA).
1083    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1084    calculated from REG+OFFSET.  */
1085
1086 void
1087 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1088 {
1089   dw_cfa_location loc;
1090   loc.indirect = 0;
1091   loc.base_offset = 0;
1092   loc.reg = reg;
1093   loc.offset = offset;
1094   def_cfa_1 (label, &loc);
1095 }
1096
1097 /* Determine if two dw_cfa_location structures define the same data.  */
1098
1099 static bool
1100 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1101 {
1102   return (loc1->reg == loc2->reg
1103           && loc1->offset == loc2->offset
1104           && loc1->indirect == loc2->indirect
1105           && (loc1->indirect == 0
1106               || loc1->base_offset == loc2->base_offset));
1107 }
1108
1109 /* This routine does the actual work.  The CFA is now calculated from
1110    the dw_cfa_location structure.  */
1111
1112 static void
1113 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1114 {
1115   dw_cfi_ref cfi;
1116   dw_cfa_location old_cfa, loc;
1117
1118   cfa = *loc_p;
1119   loc = *loc_p;
1120
1121   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1122     cfa_store.offset = loc.offset;
1123
1124   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1125   lookup_cfa (&old_cfa);
1126
1127   /* If nothing changed, no need to issue any call frame instructions.  */
1128   if (cfa_equal_p (&loc, &old_cfa))
1129     return;
1130
1131   cfi = new_cfi ();
1132
1133   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1134     {
1135       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1136          the CFA register did not change but the offset did.  The data
1137          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1138          in the assembler via the .cfi_def_cfa_offset directive.  */
1139       if (loc.offset < 0)
1140         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1141       else
1142         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1143       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1144     }
1145
1146 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1147   else if (loc.offset == old_cfa.offset
1148            && old_cfa.reg != INVALID_REGNUM
1149            && !loc.indirect
1150            && !old_cfa.indirect)
1151     {
1152       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1153          indicating the CFA register has changed to <register> but the
1154          offset has not changed.  */
1155       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1156       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1157     }
1158 #endif
1159
1160   else if (loc.indirect == 0)
1161     {
1162       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1163          indicating the CFA register has changed to <register> with
1164          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1165          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1166          directive.  */
1167       if (loc.offset < 0)
1168         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1169       else
1170         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1171       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1172       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1173     }
1174   else
1175     {
1176       /* Construct a DW_CFA_def_cfa_expression instruction to
1177          calculate the CFA using a full location expression since no
1178          register-offset pair is available.  */
1179       struct dw_loc_descr_struct *loc_list;
1180
1181       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1182       loc_list = build_cfa_loc (&loc, 0);
1183       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1184     }
1185
1186   add_fde_cfi (label, cfi);
1187 }
1188
1189 /* Add the CFI for saving a register.  REG is the CFA column number.
1190    LABEL is passed to add_fde_cfi.
1191    If SREG is -1, the register is saved at OFFSET from the CFA;
1192    otherwise it is saved in SREG.  */
1193
1194 static void
1195 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1196 {
1197   dw_cfi_ref cfi = new_cfi ();
1198   dw_fde_ref fde = current_fde ();
1199
1200   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1201
1202   /* When stack is aligned, store REG using DW_CFA_expression with
1203      FP.  */
1204   if (fde
1205       && fde->stack_realign
1206       && sreg == INVALID_REGNUM)
1207     {
1208       cfi->dw_cfi_opc = DW_CFA_expression;
1209       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1210       cfi->dw_cfi_oprnd2.dw_cfi_loc
1211         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1212     }
1213   else if (sreg == INVALID_REGNUM)
1214     {
1215       if (need_data_align_sf_opcode (offset))
1216         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1217       else if (reg & ~0x3f)
1218         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1219       else
1220         cfi->dw_cfi_opc = DW_CFA_offset;
1221       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1222     }
1223   else if (sreg == reg)
1224     cfi->dw_cfi_opc = DW_CFA_same_value;
1225   else
1226     {
1227       cfi->dw_cfi_opc = DW_CFA_register;
1228       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1229     }
1230
1231   add_fde_cfi (label, cfi);
1232 }
1233
1234 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1235    This CFI tells the unwinder that it needs to restore the window registers
1236    from the previous frame's window save area.
1237
1238    ??? Perhaps we should note in the CIE where windows are saved (instead of
1239    assuming 0(cfa)) and what registers are in the window.  */
1240
1241 void
1242 dwarf2out_window_save (const char *label)
1243 {
1244   dw_cfi_ref cfi = new_cfi ();
1245
1246   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1247   add_fde_cfi (label, cfi);
1248 }
1249
1250 /* Entry point for saving a register to the stack.  REG is the GCC register
1251    number.  LABEL and OFFSET are passed to reg_save.  */
1252
1253 void
1254 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1255 {
1256   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1257 }
1258
1259 /* Entry point for saving the return address in the stack.
1260    LABEL and OFFSET are passed to reg_save.  */
1261
1262 void
1263 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1264 {
1265   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1266 }
1267
1268 /* Entry point for saving the return address in a register.
1269    LABEL and SREG are passed to reg_save.  */
1270
1271 void
1272 dwarf2out_return_reg (const char *label, unsigned int sreg)
1273 {
1274   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1275 }
1276
1277 /* Record the initial position of the return address.  RTL is
1278    INCOMING_RETURN_ADDR_RTX.  */
1279
1280 static void
1281 initial_return_save (rtx rtl)
1282 {
1283   unsigned int reg = INVALID_REGNUM;
1284   HOST_WIDE_INT offset = 0;
1285
1286   switch (GET_CODE (rtl))
1287     {
1288     case REG:
1289       /* RA is in a register.  */
1290       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1291       break;
1292
1293     case MEM:
1294       /* RA is on the stack.  */
1295       rtl = XEXP (rtl, 0);
1296       switch (GET_CODE (rtl))
1297         {
1298         case REG:
1299           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1300           offset = 0;
1301           break;
1302
1303         case PLUS:
1304           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1305           offset = INTVAL (XEXP (rtl, 1));
1306           break;
1307
1308         case MINUS:
1309           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1310           offset = -INTVAL (XEXP (rtl, 1));
1311           break;
1312
1313         default:
1314           gcc_unreachable ();
1315         }
1316
1317       break;
1318
1319     case PLUS:
1320       /* The return address is at some offset from any value we can
1321          actually load.  For instance, on the SPARC it is in %i7+8. Just
1322          ignore the offset for now; it doesn't matter for unwinding frames.  */
1323       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1324       initial_return_save (XEXP (rtl, 0));
1325       return;
1326
1327     default:
1328       gcc_unreachable ();
1329     }
1330
1331   if (reg != DWARF_FRAME_RETURN_COLUMN)
1332     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1333 }
1334
1335 /* Given a SET, calculate the amount of stack adjustment it
1336    contains.  */
1337
1338 static HOST_WIDE_INT
1339 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1340                      HOST_WIDE_INT cur_offset)
1341 {
1342   const_rtx src = SET_SRC (pattern);
1343   const_rtx dest = SET_DEST (pattern);
1344   HOST_WIDE_INT offset = 0;
1345   enum rtx_code code;
1346
1347   if (dest == stack_pointer_rtx)
1348     {
1349       code = GET_CODE (src);
1350
1351       /* Assume (set (reg sp) (reg whatever)) sets args_size
1352          level to 0.  */
1353       if (code == REG && src != stack_pointer_rtx)
1354         {
1355           offset = -cur_args_size;
1356 #ifndef STACK_GROWS_DOWNWARD
1357           offset = -offset;
1358 #endif
1359           return offset - cur_offset;
1360         }
1361
1362       if (! (code == PLUS || code == MINUS)
1363           || XEXP (src, 0) != stack_pointer_rtx
1364           || !CONST_INT_P (XEXP (src, 1)))
1365         return 0;
1366
1367       /* (set (reg sp) (plus (reg sp) (const_int))) */
1368       offset = INTVAL (XEXP (src, 1));
1369       if (code == PLUS)
1370         offset = -offset;
1371       return offset;
1372     }
1373
1374   if (MEM_P (src) && !MEM_P (dest))
1375     dest = src;
1376   if (MEM_P (dest))
1377     {
1378       /* (set (mem (pre_dec (reg sp))) (foo)) */
1379       src = XEXP (dest, 0);
1380       code = GET_CODE (src);
1381
1382       switch (code)
1383         {
1384         case PRE_MODIFY:
1385         case POST_MODIFY:
1386           if (XEXP (src, 0) == stack_pointer_rtx)
1387             {
1388               rtx val = XEXP (XEXP (src, 1), 1);
1389               /* We handle only adjustments by constant amount.  */
1390               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1391                           && CONST_INT_P (val));
1392               offset = -INTVAL (val);
1393               break;
1394             }
1395           return 0;
1396
1397         case PRE_DEC:
1398         case POST_DEC:
1399           if (XEXP (src, 0) == stack_pointer_rtx)
1400             {
1401               offset = GET_MODE_SIZE (GET_MODE (dest));
1402               break;
1403             }
1404           return 0;
1405
1406         case PRE_INC:
1407         case POST_INC:
1408           if (XEXP (src, 0) == stack_pointer_rtx)
1409             {
1410               offset = -GET_MODE_SIZE (GET_MODE (dest));
1411               break;
1412             }
1413           return 0;
1414
1415         default:
1416           return 0;
1417         }
1418     }
1419   else
1420     return 0;
1421
1422   return offset;
1423 }
1424
1425 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1426    indexed by INSN_UID.  */
1427
1428 static HOST_WIDE_INT *barrier_args_size;
1429
1430 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1431
1432 static HOST_WIDE_INT
1433 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1434                              VEC (rtx, heap) **next)
1435 {
1436   HOST_WIDE_INT offset = 0;
1437   int i;
1438
1439   if (! RTX_FRAME_RELATED_P (insn))
1440     {
1441       if (prologue_epilogue_contains (insn))
1442         /* Nothing */;
1443       else if (GET_CODE (PATTERN (insn)) == SET)
1444         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1445       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1446                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1447         {
1448           /* There may be stack adjustments inside compound insns.  Search
1449              for them.  */
1450           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1451             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1452               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1453                                              cur_args_size, offset);
1454         }
1455     }
1456   else
1457     {
1458       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1459
1460       if (expr)
1461         {
1462           expr = XEXP (expr, 0);
1463           if (GET_CODE (expr) == PARALLEL
1464               || GET_CODE (expr) == SEQUENCE)
1465             for (i = 1; i < XVECLEN (expr, 0); i++)
1466               {
1467                 rtx elem = XVECEXP (expr, 0, i);
1468
1469                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1470                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1471               }
1472         }
1473     }
1474
1475 #ifndef STACK_GROWS_DOWNWARD
1476   offset = -offset;
1477 #endif
1478
1479   cur_args_size += offset;
1480   if (cur_args_size < 0)
1481     cur_args_size = 0;
1482
1483   if (JUMP_P (insn))
1484     {
1485       rtx dest = JUMP_LABEL (insn);
1486
1487       if (dest)
1488         {
1489           if (barrier_args_size [INSN_UID (dest)] < 0)
1490             {
1491               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1492               VEC_safe_push (rtx, heap, *next, dest);
1493             }
1494         }
1495     }
1496
1497   return cur_args_size;
1498 }
1499
1500 /* Walk the whole function and compute args_size on BARRIERs.  */
1501
1502 static void
1503 compute_barrier_args_size (void)
1504 {
1505   int max_uid = get_max_uid (), i;
1506   rtx insn;
1507   VEC (rtx, heap) *worklist, *next, *tmp;
1508
1509   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1510   for (i = 0; i < max_uid; i++)
1511     barrier_args_size[i] = -1;
1512
1513   worklist = VEC_alloc (rtx, heap, 20);
1514   next = VEC_alloc (rtx, heap, 20);
1515   insn = get_insns ();
1516   barrier_args_size[INSN_UID (insn)] = 0;
1517   VEC_quick_push (rtx, worklist, insn);
1518   for (;;)
1519     {
1520       while (!VEC_empty (rtx, worklist))
1521         {
1522           rtx prev, body, first_insn;
1523           HOST_WIDE_INT cur_args_size;
1524
1525           first_insn = insn = VEC_pop (rtx, worklist);
1526           cur_args_size = barrier_args_size[INSN_UID (insn)];
1527           prev = prev_nonnote_insn (insn);
1528           if (prev && BARRIER_P (prev))
1529             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1530
1531           for (; insn; insn = NEXT_INSN (insn))
1532             {
1533               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1534                 continue;
1535               if (BARRIER_P (insn))
1536                 break;
1537
1538               if (LABEL_P (insn))
1539                 {
1540                   if (insn == first_insn)
1541                     continue;
1542                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1543                     {
1544                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1545                       continue;
1546                     }
1547                   else
1548                     {
1549                       /* The insns starting with this label have been
1550                          already scanned or are in the worklist.  */
1551                       break;
1552                     }
1553                 }
1554
1555               body = PATTERN (insn);
1556               if (GET_CODE (body) == SEQUENCE)
1557                 {
1558                   HOST_WIDE_INT dest_args_size = cur_args_size;
1559                   for (i = 1; i < XVECLEN (body, 0); i++)
1560                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1561                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1562                       dest_args_size
1563                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1564                                                        dest_args_size, &next);
1565                     else
1566                       cur_args_size
1567                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1568                                                        cur_args_size, &next);
1569
1570                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1571                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1572                                                  dest_args_size, &next);
1573                   else
1574                     cur_args_size
1575                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1576                                                      cur_args_size, &next);
1577                 }
1578               else
1579                 cur_args_size
1580                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1581             }
1582         }
1583
1584       if (VEC_empty (rtx, next))
1585         break;
1586
1587       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1588       tmp = next;
1589       next = worklist;
1590       worklist = tmp;
1591       VEC_truncate (rtx, next, 0);
1592     }
1593
1594   VEC_free (rtx, heap, worklist);
1595   VEC_free (rtx, heap, next);
1596 }
1597
1598 /* Add a CFI to update the running total of the size of arguments
1599    pushed onto the stack.  */
1600
1601 static void
1602 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1603 {
1604   dw_cfi_ref cfi;
1605
1606   if (size == old_args_size)
1607     return;
1608
1609   old_args_size = size;
1610
1611   cfi = new_cfi ();
1612   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1613   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1614   add_fde_cfi (label, cfi);
1615 }
1616
1617 /* Record a stack adjustment of OFFSET bytes.  */
1618
1619 static void
1620 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1621 {
1622   if (cfa.reg == STACK_POINTER_REGNUM)
1623     cfa.offset += offset;
1624
1625   if (cfa_store.reg == STACK_POINTER_REGNUM)
1626     cfa_store.offset += offset;
1627
1628   if (ACCUMULATE_OUTGOING_ARGS)
1629     return;
1630
1631 #ifndef STACK_GROWS_DOWNWARD
1632   offset = -offset;
1633 #endif
1634
1635   args_size += offset;
1636   if (args_size < 0)
1637     args_size = 0;
1638
1639   def_cfa_1 (label, &cfa);
1640   if (flag_asynchronous_unwind_tables)
1641     dwarf2out_args_size (label, args_size);
1642 }
1643
1644 /* Check INSN to see if it looks like a push or a stack adjustment, and
1645    make a note of it if it does.  EH uses this information to find out
1646    how much extra space it needs to pop off the stack.  */
1647
1648 static void
1649 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1650 {
1651   HOST_WIDE_INT offset;
1652   const char *label;
1653   int i;
1654
1655   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1656      with this function.  Proper support would require all frame-related
1657      insns to be marked, and to be able to handle saving state around
1658      epilogues textually in the middle of the function.  */
1659   if (prologue_epilogue_contains (insn))
1660     return;
1661
1662   /* If INSN is an instruction from target of an annulled branch, the
1663      effects are for the target only and so current argument size
1664      shouldn't change at all.  */
1665   if (final_sequence
1666       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1667       && INSN_FROM_TARGET_P (insn))
1668     return;
1669
1670   /* If only calls can throw, and we have a frame pointer,
1671      save up adjustments until we see the CALL_INSN.  */
1672   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1673     {
1674       if (CALL_P (insn) && !after_p)
1675         {
1676           /* Extract the size of the args from the CALL rtx itself.  */
1677           insn = PATTERN (insn);
1678           if (GET_CODE (insn) == PARALLEL)
1679             insn = XVECEXP (insn, 0, 0);
1680           if (GET_CODE (insn) == SET)
1681             insn = SET_SRC (insn);
1682           gcc_assert (GET_CODE (insn) == CALL);
1683           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1684         }
1685       return;
1686     }
1687
1688   if (CALL_P (insn) && !after_p)
1689     {
1690       if (!flag_asynchronous_unwind_tables)
1691         dwarf2out_args_size ("", args_size);
1692       return;
1693     }
1694   else if (BARRIER_P (insn))
1695     {
1696       /* Don't call compute_barrier_args_size () if the only
1697          BARRIER is at the end of function.  */
1698       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1699         compute_barrier_args_size ();
1700       if (barrier_args_size == NULL)
1701         offset = 0;
1702       else
1703         {
1704           offset = barrier_args_size[INSN_UID (insn)];
1705           if (offset < 0)
1706             offset = 0;
1707         }
1708
1709       offset -= args_size;
1710 #ifndef STACK_GROWS_DOWNWARD
1711       offset = -offset;
1712 #endif
1713     }
1714   else if (GET_CODE (PATTERN (insn)) == SET)
1715     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1716   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1717            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1718     {
1719       /* There may be stack adjustments inside compound insns.  Search
1720          for them.  */
1721       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1722         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1723           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1724                                          args_size, offset);
1725     }
1726   else
1727     return;
1728
1729   if (offset == 0)
1730     return;
1731
1732   label = dwarf2out_cfi_label (false);
1733   dwarf2out_stack_adjust (offset, label);
1734 }
1735
1736 /* We delay emitting a register save until either (a) we reach the end
1737    of the prologue or (b) the register is clobbered.  This clusters
1738    register saves so that there are fewer pc advances.  */
1739
1740 struct GTY(()) queued_reg_save {
1741   struct queued_reg_save *next;
1742   rtx reg;
1743   HOST_WIDE_INT cfa_offset;
1744   rtx saved_reg;
1745 };
1746
1747 static GTY(()) struct queued_reg_save *queued_reg_saves;
1748
1749 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1750 struct GTY(()) reg_saved_in_data {
1751   rtx orig_reg;
1752   rtx saved_in_reg;
1753 };
1754
1755 /* A list of registers saved in other registers.
1756    The list intentionally has a small maximum capacity of 4; if your
1757    port needs more than that, you might consider implementing a
1758    more efficient data structure.  */
1759 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1760 static GTY(()) size_t num_regs_saved_in_regs;
1761
1762 static const char *last_reg_save_label;
1763
1764 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1765    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1766
1767 static void
1768 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1769 {
1770   struct queued_reg_save *q;
1771
1772   /* Duplicates waste space, but it's also necessary to remove them
1773      for correctness, since the queue gets output in reverse
1774      order.  */
1775   for (q = queued_reg_saves; q != NULL; q = q->next)
1776     if (REGNO (q->reg) == REGNO (reg))
1777       break;
1778
1779   if (q == NULL)
1780     {
1781       q = ggc_alloc_queued_reg_save ();
1782       q->next = queued_reg_saves;
1783       queued_reg_saves = q;
1784     }
1785
1786   q->reg = reg;
1787   q->cfa_offset = offset;
1788   q->saved_reg = sreg;
1789
1790   last_reg_save_label = label;
1791 }
1792
1793 /* Output all the entries in QUEUED_REG_SAVES.  */
1794
1795 void
1796 dwarf2out_flush_queued_reg_saves (void)
1797 {
1798   struct queued_reg_save *q;
1799
1800   for (q = queued_reg_saves; q; q = q->next)
1801     {
1802       size_t i;
1803       unsigned int reg, sreg;
1804
1805       for (i = 0; i < num_regs_saved_in_regs; i++)
1806         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1807           break;
1808       if (q->saved_reg && i == num_regs_saved_in_regs)
1809         {
1810           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1811           num_regs_saved_in_regs++;
1812         }
1813       if (i != num_regs_saved_in_regs)
1814         {
1815           regs_saved_in_regs[i].orig_reg = q->reg;
1816           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1817         }
1818
1819       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1820       if (q->saved_reg)
1821         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1822       else
1823         sreg = INVALID_REGNUM;
1824       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1825     }
1826
1827   queued_reg_saves = NULL;
1828   last_reg_save_label = NULL;
1829 }
1830
1831 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1832    location for?  Or, does it clobber a register which we've previously
1833    said that some other register is saved in, and for which we now
1834    have a new location for?  */
1835
1836 static bool
1837 clobbers_queued_reg_save (const_rtx insn)
1838 {
1839   struct queued_reg_save *q;
1840
1841   for (q = queued_reg_saves; q; q = q->next)
1842     {
1843       size_t i;
1844       if (modified_in_p (q->reg, insn))
1845         return true;
1846       for (i = 0; i < num_regs_saved_in_regs; i++)
1847         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1848             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1849           return true;
1850     }
1851
1852   return false;
1853 }
1854
1855 /* Entry point for saving the first register into the second.  */
1856
1857 void
1858 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1859 {
1860   size_t i;
1861   unsigned int regno, sregno;
1862
1863   for (i = 0; i < num_regs_saved_in_regs; i++)
1864     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1865       break;
1866   if (i == num_regs_saved_in_regs)
1867     {
1868       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1869       num_regs_saved_in_regs++;
1870     }
1871   regs_saved_in_regs[i].orig_reg = reg;
1872   regs_saved_in_regs[i].saved_in_reg = sreg;
1873
1874   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1875   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1876   reg_save (label, regno, sregno, 0);
1877 }
1878
1879 /* What register, if any, is currently saved in REG?  */
1880
1881 static rtx
1882 reg_saved_in (rtx reg)
1883 {
1884   unsigned int regn = REGNO (reg);
1885   size_t i;
1886   struct queued_reg_save *q;
1887
1888   for (q = queued_reg_saves; q; q = q->next)
1889     if (q->saved_reg && regn == REGNO (q->saved_reg))
1890       return q->reg;
1891
1892   for (i = 0; i < num_regs_saved_in_regs; i++)
1893     if (regs_saved_in_regs[i].saved_in_reg
1894         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1895       return regs_saved_in_regs[i].orig_reg;
1896
1897   return NULL_RTX;
1898 }
1899
1900
1901 /* A temporary register holding an integral value used in adjusting SP
1902    or setting up the store_reg.  The "offset" field holds the integer
1903    value, not an offset.  */
1904 static dw_cfa_location cfa_temp;
1905
1906 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1907
1908 static void
1909 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1910 {
1911   memset (&cfa, 0, sizeof (cfa));
1912
1913   switch (GET_CODE (pat))
1914     {
1915     case PLUS:
1916       cfa.reg = REGNO (XEXP (pat, 0));
1917       cfa.offset = INTVAL (XEXP (pat, 1));
1918       break;
1919
1920     case REG:
1921       cfa.reg = REGNO (pat);
1922       break;
1923
1924     case MEM:
1925       cfa.indirect = 1;
1926       pat = XEXP (pat, 0);
1927       if (GET_CODE (pat) == PLUS)
1928         {
1929           cfa.base_offset = INTVAL (XEXP (pat, 1));
1930           pat = XEXP (pat, 0);
1931         }
1932       cfa.reg = REGNO (pat);
1933       break;
1934
1935     default:
1936       /* Recurse and define an expression.  */
1937       gcc_unreachable ();
1938     }
1939
1940   def_cfa_1 (label, &cfa);
1941 }
1942
1943 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1944
1945 static void
1946 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1947 {
1948   rtx src, dest;
1949
1950   gcc_assert (GET_CODE (pat) == SET);
1951   dest = XEXP (pat, 0);
1952   src = XEXP (pat, 1);
1953
1954   switch (GET_CODE (src))
1955     {
1956     case PLUS:
1957       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1958       cfa.offset -= INTVAL (XEXP (src, 1));
1959       break;
1960
1961     case REG:
1962         break;
1963
1964     default:
1965         gcc_unreachable ();
1966     }
1967
1968   cfa.reg = REGNO (dest);
1969   gcc_assert (cfa.indirect == 0);
1970
1971   def_cfa_1 (label, &cfa);
1972 }
1973
1974 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1975
1976 static void
1977 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1978 {
1979   HOST_WIDE_INT offset;
1980   rtx src, addr, span;
1981
1982   src = XEXP (set, 1);
1983   addr = XEXP (set, 0);
1984   gcc_assert (MEM_P (addr));
1985   addr = XEXP (addr, 0);
1986
1987   /* As documented, only consider extremely simple addresses.  */
1988   switch (GET_CODE (addr))
1989     {
1990     case REG:
1991       gcc_assert (REGNO (addr) == cfa.reg);
1992       offset = -cfa.offset;
1993       break;
1994     case PLUS:
1995       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1996       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1997       break;
1998     default:
1999       gcc_unreachable ();
2000     }
2001
2002   span = targetm.dwarf_register_span (src);
2003
2004   /* ??? We'd like to use queue_reg_save, but we need to come up with
2005      a different flushing heuristic for epilogues.  */
2006   if (!span)
2007     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
2008   else
2009     {
2010       /* We have a PARALLEL describing where the contents of SRC live.
2011          Queue register saves for each piece of the PARALLEL.  */
2012       int par_index;
2013       int limit;
2014       HOST_WIDE_INT span_offset = offset;
2015
2016       gcc_assert (GET_CODE (span) == PARALLEL);
2017
2018       limit = XVECLEN (span, 0);
2019       for (par_index = 0; par_index < limit; par_index++)
2020         {
2021           rtx elem = XVECEXP (span, 0, par_index);
2022
2023           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
2024                     INVALID_REGNUM, span_offset);
2025           span_offset += GET_MODE_SIZE (GET_MODE (elem));
2026         }
2027     }
2028 }
2029
2030 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
2031
2032 static void
2033 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
2034 {
2035   rtx src, dest;
2036   unsigned sregno, dregno;
2037
2038   src = XEXP (set, 1);
2039   dest = XEXP (set, 0);
2040
2041   if (src == pc_rtx)
2042     sregno = DWARF_FRAME_RETURN_COLUMN;
2043   else
2044     sregno = DWARF_FRAME_REGNUM (REGNO (src));
2045
2046   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
2047
2048   /* ??? We'd like to use queue_reg_save, but we need to come up with
2049      a different flushing heuristic for epilogues.  */
2050   reg_save (label, sregno, dregno, 0);
2051 }
2052
2053 /* Helper function to get mode of MEM's address.  */
2054
2055 static inline enum machine_mode
2056 get_address_mode (rtx mem)
2057 {
2058   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
2059   if (mode != VOIDmode)
2060     return mode;
2061   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
2062 }
2063
2064 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_EXPRESSION note. */
2065
2066 static void
2067 dwarf2out_frame_debug_cfa_expression (rtx set, const char *label)
2068 {
2069   rtx src, dest, span;
2070   dw_cfi_ref cfi = new_cfi ();
2071
2072   dest = SET_DEST (set);
2073   src = SET_SRC (set);
2074
2075   gcc_assert (REG_P (src));
2076   gcc_assert (MEM_P (dest));
2077
2078   span = targetm.dwarf_register_span (src);
2079   gcc_assert (!span);
2080
2081   cfi->dw_cfi_opc = DW_CFA_expression;
2082   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = DWARF_FRAME_REGNUM (REGNO (src));
2083   cfi->dw_cfi_oprnd2.dw_cfi_loc
2084     = mem_loc_descriptor (XEXP (dest, 0), get_address_mode (dest),
2085                           GET_MODE (dest), VAR_INIT_STATUS_INITIALIZED);
2086
2087   /* ??? We'd like to use queue_reg_save, were the interface different,
2088      and, as above, we could manage flushing for epilogues.  */
2089   add_fde_cfi (label, cfi);
2090 }
2091
2092 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
2093
2094 static void
2095 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
2096 {
2097   dw_cfi_ref cfi = new_cfi ();
2098   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
2099
2100   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
2101   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
2102
2103   add_fde_cfi (label, cfi);
2104 }
2105
2106 /* Record call frame debugging information for an expression EXPR,
2107    which either sets SP or FP (adjusting how we calculate the frame
2108    address) or saves a register to the stack or another register.
2109    LABEL indicates the address of EXPR.
2110
2111    This function encodes a state machine mapping rtxes to actions on
2112    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
2113    users need not read the source code.
2114
2115   The High-Level Picture
2116
2117   Changes in the register we use to calculate the CFA: Currently we
2118   assume that if you copy the CFA register into another register, we
2119   should take the other one as the new CFA register; this seems to
2120   work pretty well.  If it's wrong for some target, it's simple
2121   enough not to set RTX_FRAME_RELATED_P on the insn in question.
2122
2123   Changes in the register we use for saving registers to the stack:
2124   This is usually SP, but not always.  Again, we deduce that if you
2125   copy SP into another register (and SP is not the CFA register),
2126   then the new register is the one we will be using for register
2127   saves.  This also seems to work.
2128
2129   Register saves: There's not much guesswork about this one; if
2130   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2131   register save, and the register used to calculate the destination
2132   had better be the one we think we're using for this purpose.
2133   It's also assumed that a copy from a call-saved register to another
2134   register is saving that register if RTX_FRAME_RELATED_P is set on
2135   that instruction.  If the copy is from a call-saved register to
2136   the *same* register, that means that the register is now the same
2137   value as in the caller.
2138
2139   Except: If the register being saved is the CFA register, and the
2140   offset is nonzero, we are saving the CFA, so we assume we have to
2141   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2142   the intent is to save the value of SP from the previous frame.
2143
2144   In addition, if a register has previously been saved to a different
2145   register,
2146
2147   Invariants / Summaries of Rules
2148
2149   cfa          current rule for calculating the CFA.  It usually
2150                consists of a register and an offset.
2151   cfa_store    register used by prologue code to save things to the stack
2152                cfa_store.offset is the offset from the value of
2153                cfa_store.reg to the actual CFA
2154   cfa_temp     register holding an integral value.  cfa_temp.offset
2155                stores the value, which will be used to adjust the
2156                stack pointer.  cfa_temp is also used like cfa_store,
2157                to track stores to the stack via fp or a temp reg.
2158
2159   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2160                with cfa.reg as the first operand changes the cfa.reg and its
2161                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2162                cfa_temp.offset.
2163
2164   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2165                expression yielding a constant.  This sets cfa_temp.reg
2166                and cfa_temp.offset.
2167
2168   Rule 5:      Create a new register cfa_store used to save items to the
2169                stack.
2170
2171   Rules 10-14: Save a register to the stack.  Define offset as the
2172                difference of the original location and cfa_store's
2173                location (or cfa_temp's location if cfa_temp is used).
2174
2175   Rules 16-20: If AND operation happens on sp in prologue, we assume
2176                stack is realigned.  We will use a group of DW_OP_XXX
2177                expressions to represent the location of the stored
2178                register instead of CFA+offset.
2179
2180   The Rules
2181
2182   "{a,b}" indicates a choice of a xor b.
2183   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2184
2185   Rule 1:
2186   (set <reg1> <reg2>:cfa.reg)
2187   effects: cfa.reg = <reg1>
2188            cfa.offset unchanged
2189            cfa_temp.reg = <reg1>
2190            cfa_temp.offset = cfa.offset
2191
2192   Rule 2:
2193   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2194                               {<const_int>,<reg>:cfa_temp.reg}))
2195   effects: cfa.reg = sp if fp used
2196            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2197            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2198              if cfa_store.reg==sp
2199
2200   Rule 3:
2201   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2202   effects: cfa.reg = fp
2203            cfa_offset += +/- <const_int>
2204
2205   Rule 4:
2206   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2207   constraints: <reg1> != fp
2208                <reg1> != sp
2209   effects: cfa.reg = <reg1>
2210            cfa_temp.reg = <reg1>
2211            cfa_temp.offset = cfa.offset
2212
2213   Rule 5:
2214   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2215   constraints: <reg1> != fp
2216                <reg1> != sp
2217   effects: cfa_store.reg = <reg1>
2218            cfa_store.offset = cfa.offset - cfa_temp.offset
2219
2220   Rule 6:
2221   (set <reg> <const_int>)
2222   effects: cfa_temp.reg = <reg>
2223            cfa_temp.offset = <const_int>
2224
2225   Rule 7:
2226   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2227   effects: cfa_temp.reg = <reg1>
2228            cfa_temp.offset |= <const_int>
2229
2230   Rule 8:
2231   (set <reg> (high <exp>))
2232   effects: none
2233
2234   Rule 9:
2235   (set <reg> (lo_sum <exp> <const_int>))
2236   effects: cfa_temp.reg = <reg>
2237            cfa_temp.offset = <const_int>
2238
2239   Rule 10:
2240   (set (mem ({pre,post}_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2241   effects: cfa_store.offset -= <const_int>
2242            cfa.offset = cfa_store.offset if cfa.reg == sp
2243            cfa.reg = sp
2244            cfa.base_offset = -cfa_store.offset
2245
2246   Rule 11:
2247   (set (mem ({pre_inc,pre_dec,post_dec} sp:cfa_store.reg)) <reg>)
2248   effects: cfa_store.offset += -/+ mode_size(mem)
2249            cfa.offset = cfa_store.offset if cfa.reg == sp
2250            cfa.reg = sp
2251            cfa.base_offset = -cfa_store.offset
2252
2253   Rule 12:
2254   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2255
2256        <reg2>)
2257   effects: cfa.reg = <reg1>
2258            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2259
2260   Rule 13:
2261   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2262   effects: cfa.reg = <reg1>
2263            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2264
2265   Rule 14:
2266   (set (mem (post_inc <reg1>:cfa_temp <const_int>)) <reg2>)
2267   effects: cfa.reg = <reg1>
2268            cfa.base_offset = -cfa_temp.offset
2269            cfa_temp.offset -= mode_size(mem)
2270
2271   Rule 15:
2272   (set <reg> {unspec, unspec_volatile})
2273   effects: target-dependent
2274
2275   Rule 16:
2276   (set sp (and: sp <const_int>))
2277   constraints: cfa_store.reg == sp
2278   effects: current_fde.stack_realign = 1
2279            cfa_store.offset = 0
2280            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2281
2282   Rule 17:
2283   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2284   effects: cfa_store.offset += -/+ mode_size(mem)
2285
2286   Rule 18:
2287   (set (mem ({pre_inc, pre_dec} sp)) fp)
2288   constraints: fde->stack_realign == 1
2289   effects: cfa_store.offset = 0
2290            cfa.reg != HARD_FRAME_POINTER_REGNUM
2291
2292   Rule 19:
2293   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2294   constraints: fde->stack_realign == 1
2295                && cfa.offset == 0
2296                && cfa.indirect == 0
2297                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2298   effects: Use DW_CFA_def_cfa_expression to define cfa
2299            cfa.reg == fde->drap_reg  */
2300
2301 static void
2302 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2303 {
2304   rtx src, dest, span;
2305   HOST_WIDE_INT offset;
2306   dw_fde_ref fde;
2307
2308   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2309      the PARALLEL independently. The first element is always processed if
2310      it is a SET. This is for backward compatibility.   Other elements
2311      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2312      flag is set in them.  */
2313   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2314     {
2315       int par_index;
2316       int limit = XVECLEN (expr, 0);
2317       rtx elem;
2318
2319       /* PARALLELs have strict read-modify-write semantics, so we
2320          ought to evaluate every rvalue before changing any lvalue.
2321          It's cumbersome to do that in general, but there's an
2322          easy approximation that is enough for all current users:
2323          handle register saves before register assignments.  */
2324       if (GET_CODE (expr) == PARALLEL)
2325         for (par_index = 0; par_index < limit; par_index++)
2326           {
2327             elem = XVECEXP (expr, 0, par_index);
2328             if (GET_CODE (elem) == SET
2329                 && MEM_P (SET_DEST (elem))
2330                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2331               dwarf2out_frame_debug_expr (elem, label);
2332           }
2333
2334       for (par_index = 0; par_index < limit; par_index++)
2335         {
2336           elem = XVECEXP (expr, 0, par_index);
2337           if (GET_CODE (elem) == SET
2338               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2339               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2340             dwarf2out_frame_debug_expr (elem, label);
2341           else if (GET_CODE (elem) == SET
2342                    && par_index != 0
2343                    && !RTX_FRAME_RELATED_P (elem))
2344             {
2345               /* Stack adjustment combining might combine some post-prologue
2346                  stack adjustment into a prologue stack adjustment.  */
2347               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2348
2349               if (offset != 0)
2350                 dwarf2out_stack_adjust (offset, label);
2351             }
2352         }
2353       return;
2354     }
2355
2356   gcc_assert (GET_CODE (expr) == SET);
2357
2358   src = SET_SRC (expr);
2359   dest = SET_DEST (expr);
2360
2361   if (REG_P (src))
2362     {
2363       rtx rsi = reg_saved_in (src);
2364       if (rsi)
2365         src = rsi;
2366     }
2367
2368   fde = current_fde ();
2369
2370   switch (GET_CODE (dest))
2371     {
2372     case REG:
2373       switch (GET_CODE (src))
2374         {
2375           /* Setting FP from SP.  */
2376         case REG:
2377           if (cfa.reg == (unsigned) REGNO (src))
2378             {
2379               /* Rule 1 */
2380               /* Update the CFA rule wrt SP or FP.  Make sure src is
2381                  relative to the current CFA register.
2382
2383                  We used to require that dest be either SP or FP, but the
2384                  ARM copies SP to a temporary register, and from there to
2385                  FP.  So we just rely on the backends to only set
2386                  RTX_FRAME_RELATED_P on appropriate insns.  */
2387               cfa.reg = REGNO (dest);
2388               cfa_temp.reg = cfa.reg;
2389               cfa_temp.offset = cfa.offset;
2390             }
2391           else
2392             {
2393               /* Saving a register in a register.  */
2394               gcc_assert (!fixed_regs [REGNO (dest)]
2395                           /* For the SPARC and its register window.  */
2396                           || (DWARF_FRAME_REGNUM (REGNO (src))
2397                               == DWARF_FRAME_RETURN_COLUMN));
2398
2399               /* After stack is aligned, we can only save SP in FP
2400                  if drap register is used.  In this case, we have
2401                  to restore stack pointer with the CFA value and we
2402                  don't generate this DWARF information.  */
2403               if (fde
2404                   && fde->stack_realign
2405                   && REGNO (src) == STACK_POINTER_REGNUM)
2406                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2407                             && fde->drap_reg != INVALID_REGNUM
2408                             && cfa.reg != REGNO (src));
2409               else
2410                 queue_reg_save (label, src, dest, 0);
2411             }
2412           break;
2413
2414         case PLUS:
2415         case MINUS:
2416         case LO_SUM:
2417           if (dest == stack_pointer_rtx)
2418             {
2419               /* Rule 2 */
2420               /* Adjusting SP.  */
2421               switch (GET_CODE (XEXP (src, 1)))
2422                 {
2423                 case CONST_INT:
2424                   offset = INTVAL (XEXP (src, 1));
2425                   break;
2426                 case REG:
2427                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2428                               == cfa_temp.reg);
2429                   offset = cfa_temp.offset;
2430                   break;
2431                 default:
2432                   gcc_unreachable ();
2433                 }
2434
2435               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2436                 {
2437                   /* Restoring SP from FP in the epilogue.  */
2438                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2439                   cfa.reg = STACK_POINTER_REGNUM;
2440                 }
2441               else if (GET_CODE (src) == LO_SUM)
2442                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2443                 ;
2444               else
2445                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2446
2447               if (GET_CODE (src) != MINUS)
2448                 offset = -offset;
2449               if (cfa.reg == STACK_POINTER_REGNUM)
2450                 cfa.offset += offset;
2451               if (cfa_store.reg == STACK_POINTER_REGNUM)
2452                 cfa_store.offset += offset;
2453             }
2454           else if (dest == hard_frame_pointer_rtx)
2455             {
2456               /* Rule 3 */
2457               /* Either setting the FP from an offset of the SP,
2458                  or adjusting the FP */
2459               gcc_assert (frame_pointer_needed);
2460
2461               gcc_assert (REG_P (XEXP (src, 0))
2462                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2463                           && CONST_INT_P (XEXP (src, 1)));
2464               offset = INTVAL (XEXP (src, 1));
2465               if (GET_CODE (src) != MINUS)
2466                 offset = -offset;
2467               cfa.offset += offset;
2468               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2469             }
2470           else
2471             {
2472               gcc_assert (GET_CODE (src) != MINUS);
2473
2474               /* Rule 4 */
2475               if (REG_P (XEXP (src, 0))
2476                   && REGNO (XEXP (src, 0)) == cfa.reg
2477                   && CONST_INT_P (XEXP (src, 1)))
2478                 {
2479                   /* Setting a temporary CFA register that will be copied
2480                      into the FP later on.  */
2481                   offset = - INTVAL (XEXP (src, 1));
2482                   cfa.offset += offset;
2483                   cfa.reg = REGNO (dest);
2484                   /* Or used to save regs to the stack.  */
2485                   cfa_temp.reg = cfa.reg;
2486                   cfa_temp.offset = cfa.offset;
2487                 }
2488
2489               /* Rule 5 */
2490               else if (REG_P (XEXP (src, 0))
2491                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2492                        && XEXP (src, 1) == stack_pointer_rtx)
2493                 {
2494                   /* Setting a scratch register that we will use instead
2495                      of SP for saving registers to the stack.  */
2496                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2497                   cfa_store.reg = REGNO (dest);
2498                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2499                 }
2500
2501               /* Rule 9 */
2502               else if (GET_CODE (src) == LO_SUM
2503                        && CONST_INT_P (XEXP (src, 1)))
2504                 {
2505                   cfa_temp.reg = REGNO (dest);
2506                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2507                 }
2508               else
2509                 gcc_unreachable ();
2510             }
2511           break;
2512
2513           /* Rule 6 */
2514         case CONST_INT:
2515           cfa_temp.reg = REGNO (dest);
2516           cfa_temp.offset = INTVAL (src);
2517           break;
2518
2519           /* Rule 7 */
2520         case IOR:
2521           gcc_assert (REG_P (XEXP (src, 0))
2522                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2523                       && CONST_INT_P (XEXP (src, 1)));
2524
2525           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2526             cfa_temp.reg = REGNO (dest);
2527           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2528           break;
2529
2530           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2531              which will fill in all of the bits.  */
2532           /* Rule 8 */
2533         case HIGH:
2534           break;
2535
2536           /* Rule 15 */
2537         case UNSPEC:
2538         case UNSPEC_VOLATILE:
2539           gcc_assert (targetm.dwarf_handle_frame_unspec);
2540           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2541           return;
2542
2543           /* Rule 16 */
2544         case AND:
2545           /* If this AND operation happens on stack pointer in prologue,
2546              we assume the stack is realigned and we extract the
2547              alignment.  */
2548           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2549             {
2550               /* We interpret reg_save differently with stack_realign set.
2551                  Thus we must flush whatever we have queued first.  */
2552               dwarf2out_flush_queued_reg_saves ();
2553
2554               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2555               fde->stack_realign = 1;
2556               fde->stack_realignment = INTVAL (XEXP (src, 1));
2557               cfa_store.offset = 0;
2558
2559               if (cfa.reg != STACK_POINTER_REGNUM
2560                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2561                 fde->drap_reg = cfa.reg;
2562             }
2563           return;
2564
2565         default:
2566           gcc_unreachable ();
2567         }
2568
2569       def_cfa_1 (label, &cfa);
2570       break;
2571
2572     case MEM:
2573
2574       /* Saving a register to the stack.  Make sure dest is relative to the
2575          CFA register.  */
2576       switch (GET_CODE (XEXP (dest, 0)))
2577         {
2578           /* Rule 10 */
2579           /* With a push.  */
2580         case PRE_MODIFY:
2581         case POST_MODIFY:
2582           /* We can't handle variable size modifications.  */
2583           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2584                       == CONST_INT);
2585           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2586
2587           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2588                       && cfa_store.reg == STACK_POINTER_REGNUM);
2589
2590           cfa_store.offset += offset;
2591           if (cfa.reg == STACK_POINTER_REGNUM)
2592             cfa.offset = cfa_store.offset;
2593
2594           if (GET_CODE (XEXP (dest, 0)) == POST_MODIFY)
2595             offset -= cfa_store.offset;
2596           else
2597             offset = -cfa_store.offset;
2598           break;
2599
2600           /* Rule 11 */
2601         case PRE_INC:
2602         case PRE_DEC:
2603         case POST_DEC:
2604           offset = GET_MODE_SIZE (GET_MODE (dest));
2605           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2606             offset = -offset;
2607
2608           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2609                        == STACK_POINTER_REGNUM)
2610                       && cfa_store.reg == STACK_POINTER_REGNUM);
2611
2612           cfa_store.offset += offset;
2613
2614           /* Rule 18: If stack is aligned, we will use FP as a
2615              reference to represent the address of the stored
2616              regiser.  */
2617           if (fde
2618               && fde->stack_realign
2619               && src == hard_frame_pointer_rtx)
2620             {
2621               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2622               cfa_store.offset = 0;
2623             }
2624
2625           if (cfa.reg == STACK_POINTER_REGNUM)
2626             cfa.offset = cfa_store.offset;
2627
2628           if (GET_CODE (XEXP (dest, 0)) == POST_DEC)
2629             offset += -cfa_store.offset;
2630           else
2631             offset = -cfa_store.offset;
2632           break;
2633
2634           /* Rule 12 */
2635           /* With an offset.  */
2636         case PLUS:
2637         case MINUS:
2638         case LO_SUM:
2639           {
2640             int regno;
2641
2642             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2643                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2644             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2645             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2646               offset = -offset;
2647
2648             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2649
2650             if (cfa.reg == (unsigned) regno)
2651               offset -= cfa.offset;
2652             else if (cfa_store.reg == (unsigned) regno)
2653               offset -= cfa_store.offset;
2654             else
2655               {
2656                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2657                 offset -= cfa_temp.offset;
2658               }
2659           }
2660           break;
2661
2662           /* Rule 13 */
2663           /* Without an offset.  */
2664         case REG:
2665           {
2666             int regno = REGNO (XEXP (dest, 0));
2667
2668             if (cfa.reg == (unsigned) regno)
2669               offset = -cfa.offset;
2670             else if (cfa_store.reg == (unsigned) regno)
2671               offset = -cfa_store.offset;
2672             else
2673               {
2674                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2675                 offset = -cfa_temp.offset;
2676               }
2677           }
2678           break;
2679
2680           /* Rule 14 */
2681         case POST_INC:
2682           gcc_assert (cfa_temp.reg
2683                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2684           offset = -cfa_temp.offset;
2685           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2686           break;
2687
2688         default:
2689           gcc_unreachable ();
2690         }
2691
2692         /* Rule 17 */
2693         /* If the source operand of this MEM operation is not a
2694            register, basically the source is return address.  Here
2695            we only care how much stack grew and we don't save it.  */
2696       if (!REG_P (src))
2697         break;
2698
2699       if (REGNO (src) != STACK_POINTER_REGNUM
2700           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2701           && (unsigned) REGNO (src) == cfa.reg)
2702         {
2703           /* We're storing the current CFA reg into the stack.  */
2704
2705           if (cfa.offset == 0)
2706             {
2707               /* Rule 19 */
2708               /* If stack is aligned, putting CFA reg into stack means
2709                  we can no longer use reg + offset to represent CFA.
2710                  Here we use DW_CFA_def_cfa_expression instead.  The
2711                  result of this expression equals to the original CFA
2712                  value.  */
2713               if (fde
2714                   && fde->stack_realign
2715                   && cfa.indirect == 0
2716                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2717                 {
2718                   dw_cfa_location cfa_exp;
2719
2720                   gcc_assert (fde->drap_reg == cfa.reg);
2721
2722                   cfa_exp.indirect = 1;
2723                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2724                   cfa_exp.base_offset = offset;
2725                   cfa_exp.offset = 0;
2726
2727                   fde->drap_reg_saved = 1;
2728
2729                   def_cfa_1 (label, &cfa_exp);
2730                   break;
2731                 }
2732
2733               /* If the source register is exactly the CFA, assume
2734                  we're saving SP like any other register; this happens
2735                  on the ARM.  */
2736               def_cfa_1 (label, &cfa);
2737               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2738               break;
2739             }
2740           else
2741             {
2742               /* Otherwise, we'll need to look in the stack to
2743                  calculate the CFA.  */
2744               rtx x = XEXP (dest, 0);
2745
2746               if (!REG_P (x))
2747                 x = XEXP (x, 0);
2748               gcc_assert (REG_P (x));
2749
2750               cfa.reg = REGNO (x);
2751               cfa.base_offset = offset;
2752               cfa.indirect = 1;
2753               def_cfa_1 (label, &cfa);
2754               break;
2755             }
2756         }
2757
2758       def_cfa_1 (label, &cfa);
2759       {
2760         span = targetm.dwarf_register_span (src);
2761
2762         if (!span)
2763           queue_reg_save (label, src, NULL_RTX, offset);
2764         else
2765           {
2766             /* We have a PARALLEL describing where the contents of SRC
2767                live.  Queue register saves for each piece of the
2768                PARALLEL.  */
2769             int par_index;
2770             int limit;
2771             HOST_WIDE_INT span_offset = offset;
2772
2773             gcc_assert (GET_CODE (span) == PARALLEL);
2774
2775             limit = XVECLEN (span, 0);
2776             for (par_index = 0; par_index < limit; par_index++)
2777               {
2778                 rtx elem = XVECEXP (span, 0, par_index);
2779
2780                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2781                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2782               }
2783           }
2784       }
2785       break;
2786
2787     default:
2788       gcc_unreachable ();
2789     }
2790 }
2791
2792 /* Record call frame debugging information for INSN, which either
2793    sets SP or FP (adjusting how we calculate the frame address) or saves a
2794    register to the stack.  If INSN is NULL_RTX, initialize our state.
2795
2796    If AFTER_P is false, we're being called before the insn is emitted,
2797    otherwise after.  Call instructions get invoked twice.  */
2798
2799 void
2800 dwarf2out_frame_debug (rtx insn, bool after_p)
2801 {
2802   const char *label;
2803   rtx note, n;
2804   bool handled_one = false;
2805
2806   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2807     dwarf2out_flush_queued_reg_saves ();
2808
2809   if (!RTX_FRAME_RELATED_P (insn))
2810     {
2811       /* ??? This should be done unconditionally since stack adjustments
2812          matter if the stack pointer is not the CFA register anymore but
2813          is still used to save registers.  */
2814       if (!ACCUMULATE_OUTGOING_ARGS)
2815         dwarf2out_notice_stack_adjust (insn, after_p);
2816       return;
2817     }
2818
2819   label = dwarf2out_cfi_label (false);
2820   any_cfis_emitted = false;
2821
2822   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2823     switch (REG_NOTE_KIND (note))
2824       {
2825       case REG_FRAME_RELATED_EXPR:
2826         insn = XEXP (note, 0);
2827         goto found;
2828
2829       case REG_CFA_DEF_CFA:
2830         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2831         handled_one = true;
2832         break;
2833
2834       case REG_CFA_ADJUST_CFA:
2835         n = XEXP (note, 0);
2836         if (n == NULL)
2837           {
2838             n = PATTERN (insn);
2839             if (GET_CODE (n) == PARALLEL)
2840               n = XVECEXP (n, 0, 0);
2841           }
2842         dwarf2out_frame_debug_adjust_cfa (n, label);
2843         handled_one = true;
2844         break;
2845
2846       case REG_CFA_OFFSET:
2847         n = XEXP (note, 0);
2848         if (n == NULL)
2849           n = single_set (insn);
2850         dwarf2out_frame_debug_cfa_offset (n, label);
2851         handled_one = true;
2852         break;
2853
2854       case REG_CFA_REGISTER:
2855         n = XEXP (note, 0);
2856         if (n == NULL)
2857           {
2858             n = PATTERN (insn);
2859             if (GET_CODE (n) == PARALLEL)
2860               n = XVECEXP (n, 0, 0);
2861           }
2862         dwarf2out_frame_debug_cfa_register (n, label);
2863         handled_one = true;
2864         break;
2865
2866       case REG_CFA_EXPRESSION:
2867         n = XEXP (note, 0);
2868         if (n == NULL)
2869           n = single_set (insn);
2870         dwarf2out_frame_debug_cfa_expression (n, label);
2871         handled_one = true;
2872         break;
2873
2874       case REG_CFA_RESTORE:
2875         n = XEXP (note, 0);
2876         if (n == NULL)
2877           {
2878             n = PATTERN (insn);
2879             if (GET_CODE (n) == PARALLEL)
2880               n = XVECEXP (n, 0, 0);
2881             n = XEXP (n, 0);
2882           }
2883         dwarf2out_frame_debug_cfa_restore (n, label);
2884         handled_one = true;
2885         break;
2886
2887       case REG_CFA_SET_VDRAP:
2888         n = XEXP (note, 0);
2889         if (REG_P (n))
2890           {
2891             dw_fde_ref fde = current_fde ();
2892             if (fde)
2893               {
2894                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2895                 if (REG_P (n))
2896                   fde->vdrap_reg = REGNO (n);
2897               }
2898           }
2899         handled_one = true;
2900         break;
2901
2902       default:
2903         break;
2904       }
2905   if (handled_one)
2906     {
2907       if (any_cfis_emitted)
2908         dwarf2out_flush_queued_reg_saves ();
2909       return;
2910     }
2911
2912   insn = PATTERN (insn);
2913  found:
2914   dwarf2out_frame_debug_expr (insn, label);
2915
2916   /* Check again.  A parallel can save and update the same register.
2917      We could probably check just once, here, but this is safer than
2918      removing the check above.  */
2919   if (any_cfis_emitted || clobbers_queued_reg_save (insn))
2920     dwarf2out_flush_queued_reg_saves ();
2921 }
2922
2923 /* Called once at the start of final to initialize some data for the
2924    current function.  */
2925 void
2926 dwarf2out_frame_debug_init (void)
2927 {
2928   size_t i;
2929
2930   /* Flush any queued register saves.  */
2931   dwarf2out_flush_queued_reg_saves ();
2932
2933   /* Set up state for generating call frame debug info.  */
2934   lookup_cfa (&cfa);
2935   gcc_assert (cfa.reg
2936               == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2937
2938   cfa.reg = STACK_POINTER_REGNUM;
2939   cfa_store = cfa;
2940   cfa_temp.reg = -1;
2941   cfa_temp.offset = 0;
2942
2943   for (i = 0; i < num_regs_saved_in_regs; i++)
2944     {
2945       regs_saved_in_regs[i].orig_reg = NULL_RTX;
2946       regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2947     }
2948   num_regs_saved_in_regs = 0;
2949
2950   if (barrier_args_size)
2951     {
2952       XDELETEVEC (barrier_args_size);
2953       barrier_args_size = NULL;
2954     }
2955 }
2956
2957 /* Determine if we need to save and restore CFI information around this
2958    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2959    we do need to save/restore, then emit the save now, and insert a
2960    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2961
2962 void
2963 dwarf2out_cfi_begin_epilogue (rtx insn)
2964 {
2965   bool saw_frp = false;
2966   rtx i;
2967
2968   /* Scan forward to the return insn, noticing if there are possible
2969      frame related insns.  */
2970   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2971     {
2972       if (!INSN_P (i))
2973         continue;
2974
2975       /* Look for both regular and sibcalls to end the block.  */
2976       if (returnjump_p (i))
2977         break;
2978       if (CALL_P (i) && SIBLING_CALL_P (i))
2979         break;
2980
2981       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2982         {
2983           int idx;
2984           rtx seq = PATTERN (i);
2985
2986           if (returnjump_p (XVECEXP (seq, 0, 0)))
2987             break;
2988           if (CALL_P (XVECEXP (seq, 0, 0))
2989               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2990             break;
2991
2992           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2993             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2994               saw_frp = true;
2995         }
2996
2997       if (RTX_FRAME_RELATED_P (i))
2998         saw_frp = true;
2999     }
3000
3001   /* If the port doesn't emit epilogue unwind info, we don't need a
3002      save/restore pair.  */
3003   if (!saw_frp)
3004     return;
3005
3006   /* Otherwise, search forward to see if the return insn was the last
3007      basic block of the function.  If so, we don't need save/restore.  */
3008   gcc_assert (i != NULL);
3009   i = next_real_insn (i);
3010   if (i == NULL)
3011     return;
3012
3013   /* Insert the restore before that next real insn in the stream, and before
3014      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
3015      properly nested.  This should be after any label or alignment.  This
3016      will be pushed into the CFI stream by the function below.  */
3017   while (1)
3018     {
3019       rtx p = PREV_INSN (i);
3020       if (!NOTE_P (p))
3021         break;
3022       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
3023         break;
3024       i = p;
3025     }
3026   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
3027
3028   emit_cfa_remember = true;
3029
3030   /* And emulate the state save.  */
3031   gcc_assert (!cfa_remember.in_use);
3032   cfa_remember = cfa;
3033   cfa_remember.in_use = 1;
3034 }
3035
3036 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
3037    required.  */
3038
3039 void
3040 dwarf2out_frame_debug_restore_state (void)
3041 {
3042   dw_cfi_ref cfi = new_cfi ();
3043   const char *label = dwarf2out_cfi_label (false);
3044
3045   cfi->dw_cfi_opc = DW_CFA_restore_state;
3046   add_fde_cfi (label, cfi);
3047
3048   gcc_assert (cfa_remember.in_use);
3049   cfa = cfa_remember;
3050   cfa_remember.in_use = 0;
3051 }
3052
3053 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
3054 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
3055  (enum dwarf_call_frame_info cfi);
3056
3057 static enum dw_cfi_oprnd_type
3058 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
3059 {
3060   switch (cfi)
3061     {
3062     case DW_CFA_nop:
3063     case DW_CFA_GNU_window_save:
3064     case DW_CFA_remember_state:
3065     case DW_CFA_restore_state:
3066       return dw_cfi_oprnd_unused;
3067
3068     case DW_CFA_set_loc:
3069     case DW_CFA_advance_loc1:
3070     case DW_CFA_advance_loc2:
3071     case DW_CFA_advance_loc4:
3072     case DW_CFA_MIPS_advance_loc8:
3073       return dw_cfi_oprnd_addr;
3074
3075     case DW_CFA_offset:
3076     case DW_CFA_offset_extended:
3077     case DW_CFA_def_cfa:
3078     case DW_CFA_offset_extended_sf:
3079     case DW_CFA_def_cfa_sf:
3080     case DW_CFA_restore:
3081     case DW_CFA_restore_extended:
3082     case DW_CFA_undefined:
3083     case DW_CFA_same_value:
3084     case DW_CFA_def_cfa_register:
3085     case DW_CFA_register:
3086     case DW_CFA_expression:
3087       return dw_cfi_oprnd_reg_num;
3088
3089     case DW_CFA_def_cfa_offset:
3090     case DW_CFA_GNU_args_size:
3091     case DW_CFA_def_cfa_offset_sf:
3092       return dw_cfi_oprnd_offset;
3093
3094     case DW_CFA_def_cfa_expression:
3095       return dw_cfi_oprnd_loc;
3096
3097     default:
3098       gcc_unreachable ();
3099     }
3100 }
3101
3102 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
3103 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
3104  (enum dwarf_call_frame_info cfi);
3105
3106 static enum dw_cfi_oprnd_type
3107 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
3108 {
3109   switch (cfi)
3110     {
3111     case DW_CFA_def_cfa:
3112     case DW_CFA_def_cfa_sf:
3113     case DW_CFA_offset:
3114     case DW_CFA_offset_extended_sf:
3115     case DW_CFA_offset_extended:
3116       return dw_cfi_oprnd_offset;
3117
3118     case DW_CFA_register:
3119       return dw_cfi_oprnd_reg_num;
3120
3121     case DW_CFA_expression:
3122       return dw_cfi_oprnd_loc;
3123
3124     default:
3125       return dw_cfi_oprnd_unused;
3126     }
3127 }
3128
3129 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
3130    switch to the data section instead, and write out a synthetic start label
3131    for collect2 the first time around.  */
3132
3133 static void
3134 switch_to_eh_frame_section (bool back)
3135 {
3136   tree label;
3137
3138 #ifdef EH_FRAME_SECTION_NAME
3139   if (eh_frame_section == 0)
3140     {
3141       int flags;
3142
3143       if (EH_TABLES_CAN_BE_READ_ONLY)
3144         {
3145           int fde_encoding;
3146           int per_encoding;
3147           int lsda_encoding;
3148
3149           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
3150                                                        /*global=*/0);
3151           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3152                                                        /*global=*/1);
3153           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3154                                                         /*global=*/0);
3155           flags = ((! flag_pic
3156                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3157                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3158                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3159                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3160                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3161                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3162                    ? 0 : SECTION_WRITE);
3163         }
3164       else
3165         flags = SECTION_WRITE;
3166       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3167     }
3168 #endif /* EH_FRAME_SECTION_NAME */
3169
3170   if (eh_frame_section)
3171     switch_to_section (eh_frame_section);
3172   else
3173     {
3174       /* We have no special eh_frame section.  Put the information in
3175          the data section and emit special labels to guide collect2.  */
3176       switch_to_section (data_section);
3177
3178       if (!back)
3179         {
3180           label = get_file_function_name ("F");
3181           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3182           targetm.asm_out.globalize_label (asm_out_file,
3183                                            IDENTIFIER_POINTER (label));
3184           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3185         }
3186     }
3187 }
3188
3189 /* Switch [BACK] to the eh or debug frame table section, depending on
3190    FOR_EH.  */
3191
3192 static void
3193 switch_to_frame_table_section (int for_eh, bool back)
3194 {
3195   if (for_eh)
3196     switch_to_eh_frame_section (back);
3197   else
3198     {
3199       if (!debug_frame_section)
3200         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3201                                            SECTION_DEBUG, NULL);
3202       switch_to_section (debug_frame_section);
3203     }
3204 }
3205
3206 /* Output a Call Frame Information opcode and its operand(s).  */
3207
3208 static void
3209 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3210 {
3211   unsigned long r;
3212   HOST_WIDE_INT off;
3213
3214   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3215     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3216                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3217                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3218                          ((unsigned HOST_WIDE_INT)
3219                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3220   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3221     {
3222       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3223       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3224                            "DW_CFA_offset, column %#lx", r);
3225       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3226       dw2_asm_output_data_uleb128 (off, NULL);
3227     }
3228   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3229     {
3230       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3231       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3232                            "DW_CFA_restore, column %#lx", r);
3233     }
3234   else
3235     {
3236       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3237                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3238
3239       switch (cfi->dw_cfi_opc)
3240         {
3241         case DW_CFA_set_loc:
3242           if (for_eh)
3243             dw2_asm_output_encoded_addr_rtx (
3244                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3245                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3246                 false, NULL);
3247           else
3248             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3249                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3250           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3251           break;
3252
3253         case DW_CFA_advance_loc1:
3254           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3255                                 fde->dw_fde_current_label, NULL);
3256           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3257           break;
3258
3259         case DW_CFA_advance_loc2:
3260           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3261                                 fde->dw_fde_current_label, NULL);
3262           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3263           break;
3264
3265         case DW_CFA_advance_loc4:
3266           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3267                                 fde->dw_fde_current_label, NULL);
3268           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3269           break;
3270
3271         case DW_CFA_MIPS_advance_loc8:
3272           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3273                                 fde->dw_fde_current_label, NULL);
3274           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3275           break;
3276
3277         case DW_CFA_offset_extended:
3278           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3279           dw2_asm_output_data_uleb128 (r, NULL);
3280           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3281           dw2_asm_output_data_uleb128 (off, NULL);
3282           break;
3283
3284         case DW_CFA_def_cfa:
3285           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3286           dw2_asm_output_data_uleb128 (r, NULL);
3287           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3288           break;
3289
3290         case DW_CFA_offset_extended_sf:
3291           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3292           dw2_asm_output_data_uleb128 (r, NULL);
3293           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3294           dw2_asm_output_data_sleb128 (off, NULL);
3295           break;
3296
3297         case DW_CFA_def_cfa_sf:
3298           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3299           dw2_asm_output_data_uleb128 (r, NULL);
3300           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3301           dw2_asm_output_data_sleb128 (off, NULL);
3302           break;
3303
3304         case DW_CFA_restore_extended:
3305         case DW_CFA_undefined:
3306         case DW_CFA_same_value:
3307         case DW_CFA_def_cfa_register:
3308           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3309           dw2_asm_output_data_uleb128 (r, NULL);
3310           break;
3311
3312         case DW_CFA_register:
3313           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3314           dw2_asm_output_data_uleb128 (r, NULL);
3315           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3316           dw2_asm_output_data_uleb128 (r, NULL);
3317           break;
3318
3319         case DW_CFA_def_cfa_offset:
3320         case DW_CFA_GNU_args_size:
3321           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3322           break;
3323
3324         case DW_CFA_def_cfa_offset_sf:
3325           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3326           dw2_asm_output_data_sleb128 (off, NULL);
3327           break;
3328
3329         case DW_CFA_GNU_window_save:
3330           break;
3331
3332         case DW_CFA_def_cfa_expression:
3333         case DW_CFA_expression:
3334           output_cfa_loc (cfi, for_eh);
3335           break;
3336
3337         case DW_CFA_GNU_negative_offset_extended:
3338           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3339           gcc_unreachable ();
3340
3341         default:
3342           break;
3343         }
3344     }
3345 }
3346
3347 /* Similar, but do it via assembler directives instead.  */
3348
3349 static void
3350 output_cfi_directive (dw_cfi_ref cfi)
3351 {
3352   unsigned long r, r2;
3353
3354   switch (cfi->dw_cfi_opc)
3355     {
3356     case DW_CFA_advance_loc:
3357     case DW_CFA_advance_loc1:
3358     case DW_CFA_advance_loc2:
3359     case DW_CFA_advance_loc4:
3360     case DW_CFA_MIPS_advance_loc8:
3361     case DW_CFA_set_loc:
3362       /* Should only be created by add_fde_cfi in a code path not
3363          followed when emitting via directives.  The assembler is
3364          going to take care of this for us.  */
3365       gcc_unreachable ();
3366
3367     case DW_CFA_offset:
3368     case DW_CFA_offset_extended:
3369     case DW_CFA_offset_extended_sf:
3370       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3371       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3372                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3373       break;
3374
3375     case DW_CFA_restore:
3376     case DW_CFA_restore_extended:
3377       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3378       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3379       break;
3380
3381     case DW_CFA_undefined:
3382       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3383       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3384       break;
3385
3386     case DW_CFA_same_value:
3387       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3388       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3389       break;
3390
3391     case DW_CFA_def_cfa:
3392     case DW_CFA_def_cfa_sf:
3393       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3394       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3395                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3396       break;
3397
3398     case DW_CFA_def_cfa_register:
3399       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3400       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3401       break;
3402
3403     case DW_CFA_register:
3404       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3405       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3406       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3407       break;
3408
3409     case DW_CFA_def_cfa_offset:
3410     case DW_CFA_def_cfa_offset_sf:
3411       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3412                HOST_WIDE_INT_PRINT_DEC"\n",
3413                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3414       break;
3415
3416     case DW_CFA_remember_state:
3417       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3418       break;
3419     case DW_CFA_restore_state:
3420       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3421       break;
3422
3423     case DW_CFA_GNU_args_size:
3424       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3425       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3426       if (flag_debug_asm)
3427         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3428                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3429       fputc ('\n', asm_out_file);
3430       break;
3431
3432     case DW_CFA_GNU_window_save:
3433       fprintf (asm_out_file, "\t.cfi_window_save\n");
3434       break;
3435
3436     case DW_CFA_def_cfa_expression:
3437     case DW_CFA_expression:
3438       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3439       output_cfa_loc_raw (cfi);
3440       fputc ('\n', asm_out_file);
3441       break;
3442
3443     default:
3444       gcc_unreachable ();
3445     }
3446 }
3447
3448 /* Output CFIs from VEC, up to index UPTO, to bring current FDE to the
3449    same state as after executing CFIs in CFI chain.  DO_CFI_ASM is
3450    true if .cfi_* directives shall be emitted, false otherwise.  If it
3451    is false, FDE and FOR_EH are the other arguments to pass to
3452    output_cfi.  */
3453
3454 static void
3455 output_cfis (cfi_vec vec, int upto, bool do_cfi_asm,
3456              dw_fde_ref fde, bool for_eh)
3457 {
3458   int ix;
3459   struct dw_cfi_struct cfi_buf;
3460   dw_cfi_ref cfi2;
3461   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3462   VEC(dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3463   unsigned int len, idx;
3464
3465   for (ix = 0; ix < upto + 1; ix++)
3466     {
3467       dw_cfi_ref cfi = ix < upto ? VEC_index (dw_cfi_ref, vec, ix) : NULL;
3468       switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3469         {
3470         case DW_CFA_advance_loc:
3471         case DW_CFA_advance_loc1:
3472         case DW_CFA_advance_loc2:
3473         case DW_CFA_advance_loc4:
3474         case DW_CFA_MIPS_advance_loc8:
3475         case DW_CFA_set_loc:
3476           /* All advances should be ignored.  */
3477           break;
3478         case DW_CFA_remember_state:
3479           {
3480             dw_cfi_ref args_size = cfi_args_size;
3481
3482             /* Skip everything between .cfi_remember_state and
3483                .cfi_restore_state.  */
3484             ix++;
3485             if (ix == upto)
3486               goto flush_all;
3487
3488             for (; ix < upto; ix++)
3489               {
3490                 cfi2 = VEC_index (dw_cfi_ref, vec, ix);
3491                 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3492                   break;
3493                 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3494                   args_size = cfi2;
3495                 else
3496                   gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3497               }
3498
3499             cfi_args_size = args_size;
3500             break;
3501           }
3502         case DW_CFA_GNU_args_size:
3503           cfi_args_size = cfi;
3504           break;
3505         case DW_CFA_GNU_window_save:
3506           goto flush_all;
3507         case DW_CFA_offset:
3508         case DW_CFA_offset_extended:
3509         case DW_CFA_offset_extended_sf:
3510         case DW_CFA_restore:
3511         case DW_CFA_restore_extended:
3512         case DW_CFA_undefined:
3513         case DW_CFA_same_value:
3514         case DW_CFA_register:
3515         case DW_CFA_val_offset:
3516         case DW_CFA_val_offset_sf:
3517         case DW_CFA_expression:
3518         case DW_CFA_val_expression:
3519         case DW_CFA_GNU_negative_offset_extended:
3520           if (VEC_length (dw_cfi_ref, regs)
3521               <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3522             VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3523                                    cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3524           VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
3525                        cfi);
3526           break;
3527         case DW_CFA_def_cfa:
3528         case DW_CFA_def_cfa_sf:
3529         case DW_CFA_def_cfa_expression:
3530           cfi_cfa = cfi;
3531           cfi_cfa_offset = cfi;
3532           break;
3533         case DW_CFA_def_cfa_register:
3534           cfi_cfa = cfi;
3535           break;
3536         case DW_CFA_def_cfa_offset:
3537         case DW_CFA_def_cfa_offset_sf:
3538           cfi_cfa_offset = cfi;
3539           break;
3540         case DW_CFA_nop:
3541           gcc_assert (cfi == NULL);
3542         flush_all:
3543           len = VEC_length (dw_cfi_ref, regs);
3544           for (idx = 0; idx < len; idx++)
3545             {
3546               cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3547               if (cfi2 != NULL
3548                   && cfi2->dw_cfi_opc != DW_CFA_restore
3549                   && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3550                 {
3551                   if (do_cfi_asm)
3552                     output_cfi_directive (cfi2);
3553                   else
3554                     output_cfi (cfi2, fde, for_eh);
3555                 }
3556             }
3557           if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3558             {
3559               gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3560               cfi_buf = *cfi_cfa;
3561               switch (cfi_cfa_offset->dw_cfi_opc)
3562                 {
3563                 case DW_CFA_def_cfa_offset:
3564                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3565                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3566                   break;
3567                 case DW_CFA_def_cfa_offset_sf:
3568                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3569                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3570                   break;
3571                 case DW_CFA_def_cfa:
3572                 case DW_CFA_def_cfa_sf:
3573                   cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3574                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3575                   break;
3576                 default:
3577                   gcc_unreachable ();
3578                 }
3579               cfi_cfa = &cfi_buf;
3580             }
3581           else if (cfi_cfa_offset)
3582             cfi_cfa = cfi_cfa_offset;
3583           if (cfi_cfa)
3584             {
3585               if (do_cfi_asm)
3586                 output_cfi_directive (cfi_cfa);
3587               else
3588                 output_cfi (cfi_cfa, fde, for_eh);
3589             }
3590           cfi_cfa = NULL;
3591           cfi_cfa_offset = NULL;
3592           if (cfi_args_size
3593               && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3594             {
3595               if (do_cfi_asm)
3596                 output_cfi_directive (cfi_args_size);
3597               else
3598                 output_cfi (cfi_args_size, fde, for_eh);
3599             }
3600           cfi_args_size = NULL;
3601           if (cfi == NULL)
3602             {
3603               VEC_free (dw_cfi_ref, heap, regs);
3604               return;
3605             }
3606           else if (do_cfi_asm)
3607             output_cfi_directive (cfi);
3608           else
3609             output_cfi (cfi, fde, for_eh);
3610           break;
3611         default:
3612           gcc_unreachable ();
3613         }
3614     }
3615 }
3616
3617 /* Like output_cfis, but emit all CFIs in the vector.  */
3618 static void
3619 output_all_cfis (cfi_vec vec, bool do_cfi_asm,
3620                  dw_fde_ref fde, bool for_eh)
3621 {
3622   output_cfis (vec, VEC_length (dw_cfi_ref, vec), do_cfi_asm, fde, for_eh);
3623 }
3624
3625 /* Output one FDE.  */
3626
3627 static void
3628 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3629             char *section_start_label, int fde_encoding, char *augmentation,
3630             bool any_lsda_needed, int lsda_encoding)
3631 {
3632   int ix;
3633   const char *begin, *end;
3634   static unsigned int j;
3635   char l1[20], l2[20];
3636   dw_cfi_ref cfi;
3637
3638   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3639                                      /* empty */ 0);
3640   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3641                                   for_eh + j);
3642   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3643   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3644   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3645     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3646                          " indicating 64-bit DWARF extension");
3647   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3648                         "FDE Length");
3649   ASM_OUTPUT_LABEL (asm_out_file, l1);
3650
3651   if (for_eh)
3652     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3653   else
3654     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3655                            debug_frame_section, "FDE CIE offset");
3656
3657   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
3658   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
3659
3660   if (for_eh)
3661     {
3662       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3663       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3664       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3665                                        "FDE initial location");
3666       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3667                             end, begin, "FDE address range");
3668     }
3669   else
3670     {
3671       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3672       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3673     }
3674
3675   if (augmentation[0])
3676     {
3677       if (any_lsda_needed)
3678         {
3679           int size = size_of_encoded_value (lsda_encoding);
3680
3681           if (lsda_encoding == DW_EH_PE_aligned)
3682             {
3683               int offset = (  4         /* Length */
3684                             + 4         /* CIE offset */
3685                             + 2 * size_of_encoded_value (fde_encoding)
3686                             + 1         /* Augmentation size */ );
3687               int pad = -offset & (PTR_SIZE - 1);
3688
3689               size += pad;
3690               gcc_assert (size_of_uleb128 (size) == 1);
3691             }
3692
3693           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3694
3695           if (fde->uses_eh_lsda)
3696             {
3697               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3698                                            fde->funcdef_number);
3699               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3700                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3701                                                false,
3702                                                "Language Specific Data Area");
3703             }
3704           else
3705             {
3706               if (lsda_encoding == DW_EH_PE_aligned)
3707                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3708               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3709                                    "Language Specific Data Area (none)");
3710             }
3711         }
3712       else
3713         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3714     }
3715
3716   /* Loop through the Call Frame Instructions associated with
3717      this FDE.  */
3718   fde->dw_fde_current_label = begin;
3719   if (fde->dw_fde_second_begin == NULL)
3720     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3721       output_cfi (cfi, fde, for_eh);
3722   else if (!second)
3723     {
3724       if (fde->dw_fde_switch_cfi_index > 0)
3725         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
3726           {
3727             if (ix == fde->dw_fde_switch_cfi_index)
3728               break;
3729             output_cfi (cfi, fde, for_eh);
3730           }
3731     }
3732   else
3733     {
3734       int i, from = 0;
3735       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
3736
3737       if (fde->dw_fde_switch_cfi_index > 0)
3738         {
3739           from = fde->dw_fde_switch_cfi_index;
3740           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
3741         }
3742       for (i = from; i < until; i++)
3743         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
3744                     fde, for_eh);
3745     }
3746
3747   /* If we are to emit a ref/link from function bodies to their frame tables,
3748      do it now.  This is typically performed to make sure that tables
3749      associated with functions are dragged with them and not discarded in
3750      garbage collecting links. We need to do this on a per function basis to
3751      cope with -ffunction-sections.  */
3752
3753 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3754   /* Switch to the function section, emit the ref to the tables, and
3755      switch *back* into the table section.  */
3756   switch_to_section (function_section (fde->decl));
3757   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3758   switch_to_frame_table_section (for_eh, true);
3759 #endif
3760
3761   /* Pad the FDE out to an address sized boundary.  */
3762   ASM_OUTPUT_ALIGN (asm_out_file,
3763                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3764   ASM_OUTPUT_LABEL (asm_out_file, l2);
3765
3766   j += 2;
3767 }
3768
3769 /* Return true if frame description entry FDE is needed for EH.  */
3770
3771 static bool
3772 fde_needed_for_eh_p (dw_fde_ref fde)
3773 {
3774   if (flag_asynchronous_unwind_tables)
3775     return true;
3776
3777   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3778     return true;
3779
3780   if (fde->uses_eh_lsda)
3781     return true;
3782
3783   /* If exceptions are enabled, we have collected nothrow info.  */
3784   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3785     return false;
3786
3787   return true;
3788 }
3789
3790 /* Output the call frame information used to record information
3791    that relates to calculating the frame pointer, and records the
3792    location of saved registers.  */
3793
3794 static void
3795 output_call_frame_info (int for_eh)
3796 {
3797   unsigned int i;
3798   dw_fde_ref fde;
3799   dw_cfi_ref cfi;
3800   char l1[20], l2[20], section_start_label[20];
3801   bool any_lsda_needed = false;
3802   char augmentation[6];
3803   int augmentation_size;
3804   int fde_encoding = DW_EH_PE_absptr;
3805   int per_encoding = DW_EH_PE_absptr;
3806   int lsda_encoding = DW_EH_PE_absptr;
3807   int return_reg;
3808   rtx personality = NULL;
3809   int dw_cie_version;
3810
3811   /* Don't emit a CIE if there won't be any FDEs.  */
3812   if (fde_table_in_use == 0)
3813     return;
3814
3815   /* Nothing to do if the assembler's doing it all.  */
3816   if (dwarf2out_do_cfi_asm ())
3817     return;
3818
3819   /* If we don't have any functions we'll want to unwind out of, don't emit
3820      any EH unwind information.  If we make FDEs linkonce, we may have to
3821      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3822      want to avoid having an FDE kept around when the function it refers to
3823      is discarded.  Example where this matters: a primary function template
3824      in C++ requires EH information, an explicit specialization doesn't.  */
3825   if (for_eh)
3826     {
3827       bool any_eh_needed = false;
3828
3829       for (i = 0; i < fde_table_in_use; i++)
3830         if (fde_table[i].uses_eh_lsda)
3831           any_eh_needed = any_lsda_needed = true;
3832         else if (fde_needed_for_eh_p (&fde_table[i]))
3833           any_eh_needed = true;
3834         else if (TARGET_USES_WEAK_UNWIND_INFO)
3835           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3836                                              1, 1);
3837
3838       if (!any_eh_needed)
3839         return;
3840     }
3841
3842   /* We're going to be generating comments, so turn on app.  */
3843   if (flag_debug_asm)
3844     app_enable ();
3845
3846   /* Switch to the proper frame section, first time.  */
3847   switch_to_frame_table_section (for_eh, false);
3848
3849   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3850   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3851
3852   /* Output the CIE.  */
3853   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3854   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3855   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3856     dw2_asm_output_data (4, 0xffffffff,
3857       "Initial length escape value indicating 64-bit DWARF extension");
3858   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3859                         "Length of Common Information Entry");
3860   ASM_OUTPUT_LABEL (asm_out_file, l1);
3861
3862   /* Now that the CIE pointer is PC-relative for EH,
3863      use 0 to identify the CIE.  */
3864   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3865                        (for_eh ? 0 : DWARF_CIE_ID),
3866                        "CIE Identifier Tag");
3867
3868   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3869      use CIE version 1, unless that would produce incorrect results
3870      due to overflowing the return register column.  */
3871   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3872   dw_cie_version = 1;
3873   if (return_reg >= 256 || dwarf_version > 2)
3874     dw_cie_version = 3;
3875   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3876
3877   augmentation[0] = 0;
3878   augmentation_size = 0;
3879
3880   personality = current_unit_personality;
3881   if (for_eh)
3882     {
3883       char *p;
3884
3885       /* Augmentation:
3886          z      Indicates that a uleb128 is present to size the
3887                 augmentation section.
3888          L      Indicates the encoding (and thus presence) of
3889                 an LSDA pointer in the FDE augmentation.
3890          R      Indicates a non-default pointer encoding for
3891                 FDE code pointers.
3892          P      Indicates the presence of an encoding + language
3893                 personality routine in the CIE augmentation.  */
3894
3895       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3896       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3897       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3898
3899       p = augmentation + 1;
3900       if (personality)
3901         {
3902           *p++ = 'P';
3903           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3904           assemble_external_libcall (personality);
3905         }
3906       if (any_lsda_needed)
3907         {
3908           *p++ = 'L';
3909           augmentation_size += 1;
3910         }
3911       if (fde_encoding != DW_EH_PE_absptr)
3912         {
3913           *p++ = 'R';
3914           augmentation_size += 1;
3915         }
3916       if (p > augmentation + 1)
3917         {
3918           augmentation[0] = 'z';
3919           *p = '\0';
3920         }
3921
3922       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3923       if (personality && per_encoding == DW_EH_PE_aligned)
3924         {
3925           int offset = (  4             /* Length */
3926                         + 4             /* CIE Id */
3927                         + 1             /* CIE version */
3928                         + strlen (augmentation) + 1     /* Augmentation */
3929                         + size_of_uleb128 (1)           /* Code alignment */
3930                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3931                         + 1             /* RA column */
3932                         + 1             /* Augmentation size */
3933                         + 1             /* Personality encoding */ );
3934           int pad = -offset & (PTR_SIZE - 1);
3935
3936           augmentation_size += pad;
3937
3938           /* Augmentations should be small, so there's scarce need to
3939              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3940           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3941         }
3942     }
3943
3944   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3945   if (dw_cie_version >= 4)
3946     {
3947       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3948       dw2_asm_output_data (1, 0, "CIE Segment Size");
3949     }
3950   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3951   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3952                                "CIE Data Alignment Factor");
3953
3954   if (dw_cie_version == 1)
3955     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3956   else
3957     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3958
3959   if (augmentation[0])
3960     {
3961       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3962       if (personality)
3963         {
3964           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3965                                eh_data_format_name (per_encoding));
3966           dw2_asm_output_encoded_addr_rtx (per_encoding,
3967                                            personality,
3968                                            true, NULL);
3969         }
3970
3971       if (any_lsda_needed)
3972         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3973                              eh_data_format_name (lsda_encoding));
3974
3975       if (fde_encoding != DW_EH_PE_absptr)
3976         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3977                              eh_data_format_name (fde_encoding));
3978     }
3979
3980   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
3981     output_cfi (cfi, NULL, for_eh);
3982
3983   /* Pad the CIE out to an address sized boundary.  */
3984   ASM_OUTPUT_ALIGN (asm_out_file,
3985                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3986   ASM_OUTPUT_LABEL (asm_out_file, l2);
3987
3988   /* Loop through all of the FDE's.  */
3989   for (i = 0; i < fde_table_in_use; i++)
3990     {
3991       unsigned int k;
3992       fde = &fde_table[i];
3993
3994       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3995       if (for_eh && !fde_needed_for_eh_p (fde))
3996         continue;
3997
3998       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
3999         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
4000                     augmentation, any_lsda_needed, lsda_encoding);
4001     }
4002
4003   if (for_eh && targetm.terminate_dw2_eh_frame_info)
4004     dw2_asm_output_data (4, 0, "End of Table");
4005 #ifdef MIPS_DEBUGGING_INFO
4006   /* Work around Irix 6 assembler bug whereby labels at the end of a section
4007      get a value of 0.  Putting .align 0 after the label fixes it.  */
4008   ASM_OUTPUT_ALIGN (asm_out_file, 0);
4009 #endif
4010
4011   /* Turn off app to make assembly quicker.  */
4012   if (flag_debug_asm)
4013     app_disable ();
4014 }
4015
4016 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
4017
4018 static void
4019 dwarf2out_do_cfi_startproc (bool second)
4020 {
4021   int enc;
4022   rtx ref;
4023   rtx personality = get_personality_function (current_function_decl);
4024
4025   fprintf (asm_out_file, "\t.cfi_startproc\n");
4026
4027   if (personality)
4028     {
4029       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
4030       ref = personality;
4031
4032       /* ??? The GAS support isn't entirely consistent.  We have to
4033          handle indirect support ourselves, but PC-relative is done
4034          in the assembler.  Further, the assembler can't handle any
4035          of the weirder relocation types.  */
4036       if (enc & DW_EH_PE_indirect)
4037         ref = dw2_force_const_mem (ref, true);
4038
4039       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
4040       output_addr_const (asm_out_file, ref);
4041       fputc ('\n', asm_out_file);
4042     }
4043
4044   if (crtl->uses_eh_lsda)
4045     {
4046       char lab[20];
4047
4048       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
4049       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
4050                                    current_function_funcdef_no);
4051       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
4052       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
4053
4054       if (enc & DW_EH_PE_indirect)
4055         ref = dw2_force_const_mem (ref, true);
4056
4057       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
4058       output_addr_const (asm_out_file, ref);
4059       fputc ('\n', asm_out_file);
4060     }
4061 }
4062
4063 /* Output a marker (i.e. a label) for the beginning of a function, before
4064    the prologue.  */
4065
4066 void
4067 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
4068                           const char *file ATTRIBUTE_UNUSED)
4069 {
4070   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4071   char * dup_label;
4072   dw_fde_ref fde;
4073   section *fnsec;
4074   bool do_frame;
4075
4076   current_function_func_begin_label = NULL;
4077
4078   do_frame = dwarf2out_do_frame ();
4079
4080   /* ??? current_function_func_begin_label is also used by except.c for
4081      call-site information.  We must emit this label if it might be used.  */
4082   if (!do_frame
4083       && (!flag_exceptions
4084           || targetm.except_unwind_info (&global_options) != UI_TARGET))
4085     return;
4086
4087   fnsec = function_section (current_function_decl);
4088   switch_to_section (fnsec);
4089   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
4090                                current_function_funcdef_no);
4091   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
4092                           current_function_funcdef_no);
4093   dup_label = xstrdup (label);
4094   current_function_func_begin_label = dup_label;
4095
4096   /* We can elide the fde allocation if we're not emitting debug info.  */
4097   if (!do_frame)
4098     return;
4099
4100   /* Expand the fde table if necessary.  */
4101   if (fde_table_in_use == fde_table_allocated)
4102     {
4103       fde_table_allocated += FDE_TABLE_INCREMENT;
4104       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
4105       memset (fde_table + fde_table_in_use, 0,
4106               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
4107     }
4108
4109   /* Record the FDE associated with this function.  */
4110   current_funcdef_fde = fde_table_in_use;
4111
4112   /* Add the new FDE at the end of the fde_table.  */
4113   fde = &fde_table[fde_table_in_use++];
4114   fde->decl = current_function_decl;
4115   fde->dw_fde_begin = dup_label;
4116   fde->dw_fde_end = NULL;
4117   fde->dw_fde_current_label = dup_label;
4118   fde->dw_fde_second_begin = NULL;
4119   fde->dw_fde_second_end = NULL;
4120   fde->dw_fde_vms_end_prologue = NULL;
4121   fde->dw_fde_vms_begin_epilogue = NULL;
4122   fde->dw_fde_cfi = VEC_alloc (dw_cfi_ref, gc, 20);
4123   fde->dw_fde_switch_cfi_index = 0;
4124   fde->funcdef_number = current_function_funcdef_no;
4125   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
4126   fde->uses_eh_lsda = crtl->uses_eh_lsda;
4127   fde->nothrow = crtl->nothrow;
4128   fde->drap_reg = INVALID_REGNUM;
4129   fde->vdrap_reg = INVALID_REGNUM;
4130   fde->in_std_section = (fnsec == text_section
4131                          || (cold_text_section && fnsec == cold_text_section));
4132   fde->second_in_std_section = 0;
4133
4134   args_size = old_args_size = 0;
4135
4136   /* We only want to output line number information for the genuine dwarf2
4137      prologue case, not the eh frame case.  */
4138 #ifdef DWARF2_DEBUGGING_INFO
4139   if (file)
4140     dwarf2out_source_line (line, file, 0, true);
4141 #endif
4142
4143   if (dwarf2out_do_cfi_asm ())
4144     dwarf2out_do_cfi_startproc (false);
4145   else
4146     {
4147       rtx personality = get_personality_function (current_function_decl);
4148       if (!current_unit_personality)
4149         current_unit_personality = personality;
4150
4151       /* We cannot keep a current personality per function as without CFI
4152          asm, at the point where we emit the CFI data, there is no current
4153          function anymore.  */
4154       if (personality && current_unit_personality != personality)
4155         sorry ("multiple EH personalities are supported only with assemblers "
4156                "supporting .cfi_personality directive");
4157     }
4158 }
4159
4160 /* Output a marker (i.e. a label) for the end of the generated code
4161    for a function prologue.  This gets called *after* the prologue code has
4162    been generated.  */
4163
4164 void
4165 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4166                         const char *file ATTRIBUTE_UNUSED)
4167 {
4168   dw_fde_ref fde;
4169   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4170
4171   /* Output a label to mark the endpoint of the code generated for this
4172      function.  */
4173   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4174                                current_function_funcdef_no);
4175   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4176                           current_function_funcdef_no);
4177   fde = &fde_table[fde_table_in_use - 1];
4178   fde->dw_fde_vms_end_prologue = xstrdup (label);
4179 }
4180
4181 /* Output a marker (i.e. a label) for the beginning of the generated code
4182    for a function epilogue.  This gets called *before* the prologue code has
4183    been generated.  */
4184
4185 void
4186 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4187                           const char *file ATTRIBUTE_UNUSED)
4188 {
4189   dw_fde_ref fde;
4190   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4191
4192   fde = &fde_table[fde_table_in_use - 1];
4193   if (fde->dw_fde_vms_begin_epilogue)
4194     return;
4195
4196   /* Output a label to mark the endpoint of the code generated for this
4197      function.  */
4198   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4199                                current_function_funcdef_no);
4200   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4201                           current_function_funcdef_no);
4202   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4203 }
4204
4205 /* Output a marker (i.e. a label) for the absolute end of the generated code
4206    for a function definition.  This gets called *after* the epilogue code has
4207    been generated.  */
4208
4209 void
4210 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4211                         const char *file ATTRIBUTE_UNUSED)
4212 {
4213   dw_fde_ref fde;
4214   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4215
4216   last_var_location_insn = NULL_RTX;
4217
4218   if (dwarf2out_do_cfi_asm ())
4219     fprintf (asm_out_file, "\t.cfi_endproc\n");
4220
4221   /* Output a label to mark the endpoint of the code generated for this
4222      function.  */
4223   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4224                                current_function_funcdef_no);
4225   ASM_OUTPUT_LABEL (asm_out_file, label);
4226   fde = current_fde ();
4227   gcc_assert (fde != NULL);
4228   if (fde->dw_fde_second_begin == NULL)
4229     fde->dw_fde_end = xstrdup (label);
4230 }
4231
4232 void
4233 dwarf2out_frame_init (void)
4234 {
4235   /* Allocate the initial hunk of the fde_table.  */
4236   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4237   fde_table_allocated = FDE_TABLE_INCREMENT;
4238   fde_table_in_use = 0;
4239
4240   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4241      sake of lookup_cfa.  */
4242
4243   /* On entry, the Canonical Frame Address is at SP.  */
4244   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4245
4246   if (targetm.debug_unwind_info () == UI_DWARF2
4247       || targetm.except_unwind_info (&global_options) == UI_DWARF2)
4248     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4249 }
4250
4251 void
4252 dwarf2out_frame_finish (void)
4253 {
4254   /* Output call frame information.  */
4255   if (targetm.debug_unwind_info () == UI_DWARF2)
4256     output_call_frame_info (0);
4257
4258   /* Output another copy for the unwinder.  */
4259   if ((flag_unwind_tables || flag_exceptions)
4260       && targetm.except_unwind_info (&global_options) == UI_DWARF2)
4261     output_call_frame_info (1);
4262 }
4263
4264 /* Note that the current function section is being used for code.  */
4265
4266 static void
4267 dwarf2out_note_section_used (void)
4268 {
4269   section *sec = current_function_section ();
4270   if (sec == text_section)
4271     text_section_used = true;
4272   else if (sec == cold_text_section)
4273     cold_text_section_used = true;
4274 }
4275
4276 static void var_location_switch_text_section (void);
4277 static void set_cur_line_info_table (section *);
4278
4279 void
4280 dwarf2out_switch_text_section (void)
4281 {
4282   section *sect;
4283   dw_fde_ref fde = current_fde ();
4284
4285   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
4286
4287   if (!in_cold_section_p)
4288     {
4289       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
4290       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
4291       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
4292     }
4293   else
4294     {
4295       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
4296       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
4297       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
4298     }
4299   have_multiple_function_sections = true;
4300
4301   /* Reset the current label on switching text sections, so that we
4302      don't attempt to advance_loc4 between labels in different sections.  */
4303   fde->dw_fde_current_label = NULL;
4304
4305   /* There is no need to mark used sections when not debugging.  */
4306   if (cold_text_section != NULL)
4307     dwarf2out_note_section_used ();
4308
4309   if (dwarf2out_do_cfi_asm ())
4310     fprintf (asm_out_file, "\t.cfi_endproc\n");
4311
4312   /* Now do the real section switch.  */
4313   sect = current_function_section ();
4314   switch_to_section (sect);
4315
4316   fde->second_in_std_section
4317     = (sect == text_section
4318        || (cold_text_section && sect == cold_text_section));
4319
4320   if (dwarf2out_do_cfi_asm ())
4321     {
4322       dwarf2out_do_cfi_startproc (true);
4323       /* As this is a different FDE, insert all current CFI instructions
4324          again.  */
4325       output_all_cfis (fde->dw_fde_cfi, true, fde, true);
4326     }
4327   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
4328   var_location_switch_text_section ();
4329
4330   set_cur_line_info_table (sect);
4331 }
4332 \f
4333 /* And now, the subset of the debugging information support code necessary
4334    for emitting location expressions.  */
4335
4336 /* Data about a single source file.  */
4337 struct GTY(()) dwarf_file_data {
4338   const char * filename;
4339   int emitted_number;
4340 };
4341
4342 typedef struct dw_val_struct *dw_val_ref;
4343 typedef struct die_struct *dw_die_ref;
4344 typedef const struct die_struct *const_dw_die_ref;
4345 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4346 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4347
4348 typedef struct GTY(()) deferred_locations_struct
4349 {
4350   tree variable;
4351   dw_die_ref die;
4352 } deferred_locations;
4353
4354 DEF_VEC_O(deferred_locations);
4355 DEF_VEC_ALLOC_O(deferred_locations,gc);
4356
4357 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4358
4359 DEF_VEC_P(dw_die_ref);
4360 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4361
4362 /* Each DIE may have a series of attribute/value pairs.  Values
4363    can take on several forms.  The forms that are used in this
4364    implementation are listed below.  */
4365
4366 enum dw_val_class
4367 {
4368   dw_val_class_addr,
4369   dw_val_class_offset,
4370   dw_val_class_loc,
4371   dw_val_class_loc_list,
4372   dw_val_class_range_list,
4373   dw_val_class_const,
4374   dw_val_class_unsigned_const,
4375   dw_val_class_const_double,
4376   dw_val_class_vec,
4377   dw_val_class_flag,
4378   dw_val_class_die_ref,
4379   dw_val_class_fde_ref,
4380   dw_val_class_lbl_id,
4381   dw_val_class_lineptr,
4382   dw_val_class_str,
4383   dw_val_class_macptr,
4384   dw_val_class_file,
4385   dw_val_class_data8,
4386   dw_val_class_decl_ref,
4387   dw_val_class_vms_delta
4388 };
4389
4390 /* Describe a floating point constant value, or a vector constant value.  */
4391
4392 typedef struct GTY(()) dw_vec_struct {
4393   unsigned char * GTY((length ("%h.length"))) array;
4394   unsigned length;
4395   unsigned elt_size;
4396 }
4397 dw_vec_const;
4398
4399 /* The dw_val_node describes an attribute's value, as it is
4400    represented internally.  */
4401
4402 typedef struct GTY(()) dw_val_struct {
4403   enum dw_val_class val_class;
4404   union dw_val_struct_union
4405     {
4406       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4407       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4408       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4409       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4410       HOST_WIDE_INT GTY ((default)) val_int;
4411       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4412       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4413       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4414       struct dw_val_die_union
4415         {
4416           dw_die_ref die;
4417           int external;
4418         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4419       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4420       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4421       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4422       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4423       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4424       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4425       tree GTY ((tag ("dw_val_class_decl_ref"))) val_decl_ref;
4426       struct dw_val_vms_delta_union
4427         {
4428           char * lbl1;
4429           char * lbl2;
4430         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4431     }
4432   GTY ((desc ("%1.val_class"))) v;
4433 }
4434 dw_val_node;
4435
4436 /* Locations in memory are described using a sequence of stack machine
4437    operations.  */
4438
4439 typedef struct GTY(()) dw_loc_descr_struct {
4440   dw_loc_descr_ref dw_loc_next;
4441   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4442   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4443      from DW_OP_addr with a dtp-relative symbol relocation.  */
4444   unsigned int dtprel : 1;
4445   int dw_loc_addr;
4446   dw_val_node dw_loc_oprnd1;
4447   dw_val_node dw_loc_oprnd2;
4448 }
4449 dw_loc_descr_node;
4450
4451 /* Location lists are ranges + location descriptions for that range,
4452    so you can track variables that are in different places over
4453    their entire life.  */
4454 typedef struct GTY(()) dw_loc_list_struct {
4455   dw_loc_list_ref dw_loc_next;
4456   const char *begin; /* Label for begin address of range */
4457   const char *end;  /* Label for end address of range */
4458   char *ll_symbol; /* Label for beginning of location list.
4459                       Only on head of list */
4460   const char *section; /* Section this loclist is relative to */
4461   dw_loc_descr_ref expr;
4462   hashval_t hash;
4463   /* True if all addresses in this and subsequent lists are known to be
4464      resolved.  */
4465   bool resolved_addr;
4466   /* True if this list has been replaced by dw_loc_next.  */
4467   bool replaced;
4468   bool emitted;
4469 } dw_loc_list_node;
4470
4471 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4472
4473 /* Convert a DWARF stack opcode into its string name.  */
4474
4475 static const char *
4476 dwarf_stack_op_name (unsigned int op)
4477 {
4478   switch (op)
4479     {
4480     case DW_OP_addr:
4481       return "DW_OP_addr";
4482     case DW_OP_deref:
4483       return "DW_OP_deref";
4484     case DW_OP_const1u:
4485       return "DW_OP_const1u";
4486     case DW_OP_const1s:
4487       return "DW_OP_const1s";
4488     case DW_OP_const2u:
4489       return "DW_OP_const2u";
4490     case DW_OP_const2s:
4491       return "DW_OP_const2s";
4492     case DW_OP_const4u:
4493       return "DW_OP_const4u";
4494     case DW_OP_const4s:
4495       return "DW_OP_const4s";
4496     case DW_OP_const8u:
4497       return "DW_OP_const8u";
4498     case DW_OP_const8s:
4499       return "DW_OP_const8s";
4500     case DW_OP_constu:
4501       return "DW_OP_constu";
4502     case DW_OP_consts:
4503       return "DW_OP_consts";
4504     case DW_OP_dup:
4505       return "DW_OP_dup";
4506     case DW_OP_drop:
4507       return "DW_OP_drop";
4508     case DW_OP_over:
4509       return "DW_OP_over";
4510     case DW_OP_pick:
4511       return "DW_OP_pick";
4512     case DW_OP_swap:
4513       return "DW_OP_swap";
4514     case DW_OP_rot:
4515       return "DW_OP_rot";
4516     case DW_OP_xderef:
4517       return "DW_OP_xderef";
4518     case DW_OP_abs:
4519       return "DW_OP_abs";
4520     case DW_OP_and:
4521       return "DW_OP_and";
4522     case DW_OP_div:
4523       return "DW_OP_div";
4524     case DW_OP_minus:
4525       return "DW_OP_minus";
4526     case DW_OP_mod:
4527       return "DW_OP_mod";
4528     case DW_OP_mul:
4529       return "DW_OP_mul";
4530     case DW_OP_neg:
4531       return "DW_OP_neg";
4532     case DW_OP_not:
4533       return "DW_OP_not";
4534     case DW_OP_or:
4535       return "DW_OP_or";
4536     case DW_OP_plus:
4537       return "DW_OP_plus";
4538     case DW_OP_plus_uconst:
4539       return "DW_OP_plus_uconst";
4540     case DW_OP_shl:
4541       return "DW_OP_shl";
4542     case DW_OP_shr:
4543       return "DW_OP_shr";
4544     case DW_OP_shra:
4545       return "DW_OP_shra";
4546     case DW_OP_xor:
4547       return "DW_OP_xor";
4548     case DW_OP_bra:
4549       return "DW_OP_bra";
4550     case DW_OP_eq:
4551       return "DW_OP_eq";
4552     case DW_OP_ge:
4553       return "DW_OP_ge";
4554     case DW_OP_gt:
4555       return "DW_OP_gt";
4556     case DW_OP_le:
4557       return "DW_OP_le";
4558     case DW_OP_lt:
4559       return "DW_OP_lt";
4560     case DW_OP_ne:
4561       return "DW_OP_ne";
4562     case DW_OP_skip:
4563       return "DW_OP_skip";
4564     case DW_OP_lit0:
4565       return "DW_OP_lit0";
4566     case DW_OP_lit1:
4567       return "DW_OP_lit1";
4568     case DW_OP_lit2:
4569       return "DW_OP_lit2";
4570     case DW_OP_lit3:
4571       return "DW_OP_lit3";
4572     case DW_OP_lit4:
4573       return "DW_OP_lit4";
4574     case DW_OP_lit5:
4575       return "DW_OP_lit5";
4576     case DW_OP_lit6:
4577       return "DW_OP_lit6";
4578     case DW_OP_lit7:
4579       return "DW_OP_lit7";
4580     case DW_OP_lit8:
4581       return "DW_OP_lit8";
4582     case DW_OP_lit9:
4583       return "DW_OP_lit9";
4584     case DW_OP_lit10:
4585       return "DW_OP_lit10";
4586     case DW_OP_lit11:
4587       return "DW_OP_lit11";
4588     case DW_OP_lit12:
4589       return "DW_OP_lit12";
4590     case DW_OP_lit13:
4591       return "DW_OP_lit13";
4592     case DW_OP_lit14:
4593       return "DW_OP_lit14";
4594     case DW_OP_lit15:
4595       return "DW_OP_lit15";
4596     case DW_OP_lit16:
4597       return "DW_OP_lit16";
4598     case DW_OP_lit17:
4599       return "DW_OP_lit17";
4600     case DW_OP_lit18:
4601       return "DW_OP_lit18";
4602     case DW_OP_lit19:
4603       return "DW_OP_lit19";
4604     case DW_OP_lit20:
4605       return "DW_OP_lit20";
4606     case DW_OP_lit21:
4607       return "DW_OP_lit21";
4608     case DW_OP_lit22:
4609       return "DW_OP_lit22";
4610     case DW_OP_lit23:
4611       return "DW_OP_lit23";
4612     case DW_OP_lit24:
4613       return "DW_OP_lit24";
4614     case DW_OP_lit25:
4615       return "DW_OP_lit25";
4616     case DW_OP_lit26:
4617       return "DW_OP_lit26";
4618     case DW_OP_lit27:
4619       return "DW_OP_lit27";
4620     case DW_OP_lit28:
4621       return "DW_OP_lit28";
4622     case DW_OP_lit29:
4623       return "DW_OP_lit29";
4624     case DW_OP_lit30:
4625       return "DW_OP_lit30";
4626     case DW_OP_lit31:
4627       return "DW_OP_lit31";
4628     case DW_OP_reg0:
4629       return "DW_OP_reg0";
4630     case DW_OP_reg1:
4631       return "DW_OP_reg1";
4632     case DW_OP_reg2:
4633       return "DW_OP_reg2";
4634     case DW_OP_reg3:
4635       return "DW_OP_reg3";
4636     case DW_OP_reg4:
4637       return "DW_OP_reg4";
4638     case DW_OP_reg5:
4639       return "DW_OP_reg5";
4640     case DW_OP_reg6:
4641       return "DW_OP_reg6";
4642     case DW_OP_reg7:
4643       return "DW_OP_reg7";
4644     case DW_OP_reg8:
4645       return "DW_OP_reg8";
4646     case DW_OP_reg9:
4647       return "DW_OP_reg9";
4648     case DW_OP_reg10:
4649       return "DW_OP_reg10";
4650     case DW_OP_reg11:
4651       return "DW_OP_reg11";
4652     case DW_OP_reg12:
4653       return "DW_OP_reg12";
4654     case DW_OP_reg13:
4655       return "DW_OP_reg13";
4656     case DW_OP_reg14:
4657       return "DW_OP_reg14";
4658     case DW_OP_reg15:
4659       return "DW_OP_reg15";
4660     case DW_OP_reg16:
4661       return "DW_OP_reg16";
4662     case DW_OP_reg17:
4663       return "DW_OP_reg17";
4664     case DW_OP_reg18:
4665       return "DW_OP_reg18";
4666     case DW_OP_reg19:
4667       return "DW_OP_reg19";
4668     case DW_OP_reg20:
4669       return "DW_OP_reg20";
4670     case DW_OP_reg21:
4671       return "DW_OP_reg21";
4672     case DW_OP_reg22:
4673       return "DW_OP_reg22";
4674     case DW_OP_reg23:
4675       return "DW_OP_reg23";
4676     case DW_OP_reg24:
4677       return "DW_OP_reg24";
4678     case DW_OP_reg25:
4679       return "DW_OP_reg25";
4680     case DW_OP_reg26:
4681       return "DW_OP_reg26";
4682     case DW_OP_reg27:
4683       return "DW_OP_reg27";
4684     case DW_OP_reg28:
4685       return "DW_OP_reg28";
4686     case DW_OP_reg29:
4687       return "DW_OP_reg29";
4688     case DW_OP_reg30:
4689       return "DW_OP_reg30";
4690     case DW_OP_reg31:
4691       return "DW_OP_reg31";
4692     case DW_OP_breg0:
4693       return "DW_OP_breg0";
4694     case DW_OP_breg1:
4695       return "DW_OP_breg1";
4696     case DW_OP_breg2:
4697       return "DW_OP_breg2";
4698     case DW_OP_breg3:
4699       return "DW_OP_breg3";
4700     case DW_OP_breg4:
4701       return "DW_OP_breg4";
4702     case DW_OP_breg5:
4703       return "DW_OP_breg5";
4704     case DW_OP_breg6:
4705       return "DW_OP_breg6";
4706     case DW_OP_breg7:
4707       return "DW_OP_breg7";
4708     case DW_OP_breg8:
4709       return "DW_OP_breg8";
4710     case DW_OP_breg9:
4711       return "DW_OP_breg9";
4712     case DW_OP_breg10:
4713       return "DW_OP_breg10";
4714     case DW_OP_breg11:
4715       return "DW_OP_breg11";
4716     case DW_OP_breg12:
4717       return "DW_OP_breg12";
4718     case DW_OP_breg13:
4719       return "DW_OP_breg13";
4720     case DW_OP_breg14:
4721       return "DW_OP_breg14";
4722     case DW_OP_breg15:
4723       return "DW_OP_breg15";
4724     case DW_OP_breg16:
4725       return "DW_OP_breg16";
4726     case DW_OP_breg17:
4727       return "DW_OP_breg17";
4728     case DW_OP_breg18:
4729       return "DW_OP_breg18";
4730     case DW_OP_breg19:
4731       return "DW_OP_breg19";
4732     case DW_OP_breg20:
4733       return "DW_OP_breg20";
4734     case DW_OP_breg21:
4735       return "DW_OP_breg21";
4736     case DW_OP_breg22:
4737       return "DW_OP_breg22";
4738     case DW_OP_breg23:
4739       return "DW_OP_breg23";
4740     case DW_OP_breg24:
4741       return "DW_OP_breg24";
4742     case DW_OP_breg25:
4743       return "DW_OP_breg25";
4744     case DW_OP_breg26:
4745       return "DW_OP_breg26";
4746     case DW_OP_breg27:
4747       return "DW_OP_breg27";
4748     case DW_OP_breg28:
4749       return "DW_OP_breg28";
4750     case DW_OP_breg29:
4751       return "DW_OP_breg29";
4752     case DW_OP_breg30:
4753       return "DW_OP_breg30";
4754     case DW_OP_breg31:
4755       return "DW_OP_breg31";
4756     case DW_OP_regx:
4757       return "DW_OP_regx";
4758     case DW_OP_fbreg:
4759       return "DW_OP_fbreg";
4760     case DW_OP_bregx:
4761       return "DW_OP_bregx";
4762     case DW_OP_piece:
4763       return "DW_OP_piece";
4764     case DW_OP_deref_size:
4765       return "DW_OP_deref_size";
4766     case DW_OP_xderef_size:
4767       return "DW_OP_xderef_size";
4768     case DW_OP_nop:
4769       return "DW_OP_nop";
4770
4771     case DW_OP_push_object_address:
4772       return "DW_OP_push_object_address";
4773     case DW_OP_call2:
4774       return "DW_OP_call2";
4775     case DW_OP_call4:
4776       return "DW_OP_call4";
4777     case DW_OP_call_ref:
4778       return "DW_OP_call_ref";
4779     case DW_OP_implicit_value:
4780       return "DW_OP_implicit_value";
4781     case DW_OP_stack_value:
4782       return "DW_OP_stack_value";
4783     case DW_OP_form_tls_address:
4784       return "DW_OP_form_tls_address";
4785     case DW_OP_call_frame_cfa:
4786       return "DW_OP_call_frame_cfa";
4787     case DW_OP_bit_piece:
4788       return "DW_OP_bit_piece";
4789
4790     case DW_OP_GNU_push_tls_address:
4791       return "DW_OP_GNU_push_tls_address";
4792     case DW_OP_GNU_uninit:
4793       return "DW_OP_GNU_uninit";
4794     case DW_OP_GNU_encoded_addr:
4795       return "DW_OP_GNU_encoded_addr";
4796     case DW_OP_GNU_implicit_pointer:
4797       return "DW_OP_GNU_implicit_pointer";
4798     case DW_OP_GNU_entry_value:
4799       return "DW_OP_GNU_entry_value";
4800     case DW_OP_GNU_const_type:
4801       return "DW_OP_GNU_const_type";
4802     case DW_OP_GNU_regval_type:
4803       return "DW_OP_GNU_regval_type";
4804     case DW_OP_GNU_deref_type:
4805       return "DW_OP_GNU_deref_type";
4806     case DW_OP_GNU_convert:
4807       return "DW_OP_GNU_convert";
4808     case DW_OP_GNU_reinterpret:
4809       return "DW_OP_GNU_reinterpret";
4810
4811     default:
4812       return "OP_<unknown>";
4813     }
4814 }
4815
4816 /* Return a pointer to a newly allocated location description.  Location
4817    descriptions are simple expression terms that can be strung
4818    together to form more complicated location (address) descriptions.  */
4819
4820 static inline dw_loc_descr_ref
4821 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4822                unsigned HOST_WIDE_INT oprnd2)
4823 {
4824   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4825
4826   descr->dw_loc_opc = op;
4827   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4828   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4829   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4830   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4831
4832   return descr;
4833 }
4834
4835 /* Return a pointer to a newly allocated location description for
4836    REG and OFFSET.  */
4837
4838 static inline dw_loc_descr_ref
4839 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4840 {
4841   if (reg <= 31)
4842     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4843                           offset, 0);
4844   else
4845     return new_loc_descr (DW_OP_bregx, reg, offset);
4846 }
4847
4848 /* Add a location description term to a location description expression.  */
4849
4850 static inline void
4851 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4852 {
4853   dw_loc_descr_ref *d;
4854
4855   /* Find the end of the chain.  */
4856   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4857     ;
4858
4859   *d = descr;
4860 }
4861
4862 /* Add a constant OFFSET to a location expression.  */
4863
4864 static void
4865 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4866 {
4867   dw_loc_descr_ref loc;
4868   HOST_WIDE_INT *p;
4869
4870   gcc_assert (*list_head != NULL);
4871
4872   if (!offset)
4873     return;
4874
4875   /* Find the end of the chain.  */
4876   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4877     ;
4878
4879   p = NULL;
4880   if (loc->dw_loc_opc == DW_OP_fbreg
4881       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4882     p = &loc->dw_loc_oprnd1.v.val_int;
4883   else if (loc->dw_loc_opc == DW_OP_bregx)
4884     p = &loc->dw_loc_oprnd2.v.val_int;
4885
4886   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4887      offset.  Don't optimize if an signed integer overflow would happen.  */
4888   if (p != NULL
4889       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4890           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4891     *p += offset;
4892
4893   else if (offset > 0)
4894     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4895
4896   else
4897     {
4898       loc->dw_loc_next = int_loc_descriptor (-offset);
4899       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4900     }
4901 }
4902
4903 /* Add a constant OFFSET to a location list.  */
4904
4905 static void
4906 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4907 {
4908   dw_loc_list_ref d;
4909   for (d = list_head; d != NULL; d = d->dw_loc_next)
4910     loc_descr_plus_const (&d->expr, offset);
4911 }
4912
4913 #define DWARF_REF_SIZE  \
4914   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
4915
4916 static unsigned long size_of_locs (dw_loc_descr_ref);
4917 static unsigned long int get_base_type_offset (dw_die_ref);
4918
4919 /* Return the size of a location descriptor.  */
4920
4921 static unsigned long
4922 size_of_loc_descr (dw_loc_descr_ref loc)
4923 {
4924   unsigned long size = 1;
4925
4926   switch (loc->dw_loc_opc)
4927     {
4928     case DW_OP_addr:
4929       size += DWARF2_ADDR_SIZE;
4930       break;
4931     case DW_OP_const1u:
4932     case DW_OP_const1s:
4933       size += 1;
4934       break;
4935     case DW_OP_const2u:
4936     case DW_OP_const2s:
4937       size += 2;
4938       break;
4939     case DW_OP_const4u:
4940     case DW_OP_const4s:
4941       size += 4;
4942       break;
4943     case DW_OP_const8u:
4944     case DW_OP_const8s:
4945       size += 8;
4946       break;
4947     case DW_OP_constu:
4948       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4949       break;
4950     case DW_OP_consts:
4951       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4952       break;
4953     case DW_OP_pick:
4954       size += 1;
4955       break;
4956     case DW_OP_plus_uconst:
4957       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4958       break;
4959     case DW_OP_skip:
4960     case DW_OP_bra:
4961       size += 2;
4962       break;
4963     case DW_OP_breg0:
4964     case DW_OP_breg1:
4965     case DW_OP_breg2:
4966     case DW_OP_breg3:
4967     case DW_OP_breg4:
4968     case DW_OP_breg5:
4969     case DW_OP_breg6:
4970     case DW_OP_breg7:
4971     case DW_OP_breg8:
4972     case DW_OP_breg9:
4973     case DW_OP_breg10:
4974     case DW_OP_breg11:
4975     case DW_OP_breg12:
4976     case DW_OP_breg13:
4977     case DW_OP_breg14:
4978     case DW_OP_breg15:
4979     case DW_OP_breg16:
4980     case DW_OP_breg17:
4981     case DW_OP_breg18:
4982     case DW_OP_breg19:
4983     case DW_OP_breg20:
4984     case DW_OP_breg21:
4985     case DW_OP_breg22:
4986     case DW_OP_breg23:
4987     case DW_OP_breg24:
4988     case DW_OP_breg25:
4989     case DW_OP_breg26:
4990     case DW_OP_breg27:
4991     case DW_OP_breg28:
4992     case DW_OP_breg29:
4993     case DW_OP_breg30:
4994     case DW_OP_breg31:
4995       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4996       break;
4997     case DW_OP_regx:
4998       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4999       break;
5000     case DW_OP_fbreg:
5001       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
5002       break;
5003     case DW_OP_bregx:
5004       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5005       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5006       break;
5007     case DW_OP_piece:
5008       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5009       break;
5010     case DW_OP_bit_piece:
5011       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
5012       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
5013       break;
5014     case DW_OP_deref_size:
5015     case DW_OP_xderef_size:
5016       size += 1;
5017       break;
5018     case DW_OP_call2:
5019       size += 2;
5020       break;
5021     case DW_OP_call4:
5022       size += 4;
5023       break;
5024     case DW_OP_call_ref:
5025       size += DWARF_REF_SIZE;
5026       break;
5027     case DW_OP_implicit_value:
5028       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5029               + loc->dw_loc_oprnd1.v.val_unsigned;
5030       break;
5031     case DW_OP_GNU_implicit_pointer:
5032       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
5033       break;
5034     case DW_OP_GNU_entry_value:
5035       {
5036         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
5037         size += size_of_uleb128 (op_size) + op_size;
5038         break;
5039       }
5040     case DW_OP_GNU_const_type:
5041       {
5042         unsigned long o
5043           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5044         size += size_of_uleb128 (o) + 1;
5045         switch (loc->dw_loc_oprnd2.val_class)
5046           {
5047           case dw_val_class_vec:
5048             size += loc->dw_loc_oprnd2.v.val_vec.length
5049                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
5050             break;
5051           case dw_val_class_const:
5052             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5053             break;
5054           case dw_val_class_const_double:
5055             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
5056             break;
5057           default:
5058             gcc_unreachable ();
5059           }
5060         break;
5061       }
5062     case DW_OP_GNU_regval_type:
5063       {
5064         unsigned long o
5065           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5066         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
5067                 + size_of_uleb128 (o);
5068       }
5069       break;
5070     case DW_OP_GNU_deref_type:
5071       {
5072         unsigned long o
5073           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
5074         size += 1 + size_of_uleb128 (o);
5075       }
5076       break;
5077     case DW_OP_GNU_convert:
5078     case DW_OP_GNU_reinterpret:
5079       {
5080         unsigned long o
5081           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
5082         size += size_of_uleb128 (o);
5083       }
5084     default:
5085       break;
5086     }
5087
5088   return size;
5089 }
5090
5091 /* Return the size of a series of location descriptors.  */
5092
5093 static unsigned long
5094 size_of_locs (dw_loc_descr_ref loc)
5095 {
5096   dw_loc_descr_ref l;
5097   unsigned long size;
5098
5099   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
5100      field, to avoid writing to a PCH file.  */
5101   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5102     {
5103       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
5104         break;
5105       size += size_of_loc_descr (l);
5106     }
5107   if (! l)
5108     return size;
5109
5110   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
5111     {
5112       l->dw_loc_addr = size;
5113       size += size_of_loc_descr (l);
5114     }
5115
5116   return size;
5117 }
5118
5119 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
5120 static void get_ref_die_offset_label (char *, dw_die_ref);
5121 static void output_loc_sequence (dw_loc_descr_ref, int);
5122
5123 /* Output location description stack opcode's operands (if any).
5124    The for_eh_or_skip parameter controls whether register numbers are
5125    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5126    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5127    info).  This should be suppressed for the cases that have not been converted
5128    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5129
5130 static void
5131 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
5132 {
5133   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5134   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5135
5136   switch (loc->dw_loc_opc)
5137     {
5138 #ifdef DWARF2_DEBUGGING_INFO
5139     case DW_OP_const2u:
5140     case DW_OP_const2s:
5141       dw2_asm_output_data (2, val1->v.val_int, NULL);
5142       break;
5143     case DW_OP_const4u:
5144       if (loc->dtprel)
5145         {
5146           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5147           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
5148                                                val1->v.val_addr);
5149           fputc ('\n', asm_out_file);
5150           break;
5151         }
5152       /* FALLTHRU */
5153     case DW_OP_const4s:
5154       dw2_asm_output_data (4, val1->v.val_int, NULL);
5155       break;
5156     case DW_OP_const8u:
5157       if (loc->dtprel)
5158         {
5159           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
5160           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
5161                                                val1->v.val_addr);
5162           fputc ('\n', asm_out_file);
5163           break;
5164         }
5165       /* FALLTHRU */
5166     case DW_OP_const8s:
5167       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5168       dw2_asm_output_data (8, val1->v.val_int, NULL);
5169       break;
5170     case DW_OP_skip:
5171     case DW_OP_bra:
5172       {
5173         int offset;
5174
5175         gcc_assert (val1->val_class == dw_val_class_loc);
5176         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5177
5178         dw2_asm_output_data (2, offset, NULL);
5179       }
5180       break;
5181     case DW_OP_implicit_value:
5182       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5183       switch (val2->val_class)
5184         {
5185         case dw_val_class_const:
5186           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
5187           break;
5188         case dw_val_class_vec:
5189           {
5190             unsigned int elt_size = val2->v.val_vec.elt_size;
5191             unsigned int len = val2->v.val_vec.length;
5192             unsigned int i;
5193             unsigned char *p;
5194
5195             if (elt_size > sizeof (HOST_WIDE_INT))
5196               {
5197                 elt_size /= 2;
5198                 len *= 2;
5199               }
5200             for (i = 0, p = val2->v.val_vec.array;
5201                  i < len;
5202                  i++, p += elt_size)
5203               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5204                                    "fp or vector constant word %u", i);
5205           }
5206           break;
5207         case dw_val_class_const_double:
5208           {
5209             unsigned HOST_WIDE_INT first, second;
5210
5211             if (WORDS_BIG_ENDIAN)
5212               {
5213                 first = val2->v.val_double.high;
5214                 second = val2->v.val_double.low;
5215               }
5216             else
5217               {
5218                 first = val2->v.val_double.low;
5219                 second = val2->v.val_double.high;
5220               }
5221             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5222                                  first, NULL);
5223             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5224                                  second, NULL);
5225           }
5226           break;
5227         case dw_val_class_addr:
5228           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5229           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5230           break;
5231         default:
5232           gcc_unreachable ();
5233         }
5234       break;
5235 #else
5236     case DW_OP_const2u:
5237     case DW_OP_const2s:
5238     case DW_OP_const4u:
5239     case DW_OP_const4s:
5240     case DW_OP_const8u:
5241     case DW_OP_const8s:
5242     case DW_OP_skip:
5243     case DW_OP_bra:
5244     case DW_OP_implicit_value:
5245       /* We currently don't make any attempt to make sure these are
5246          aligned properly like we do for the main unwind info, so
5247          don't support emitting things larger than a byte if we're
5248          only doing unwinding.  */
5249       gcc_unreachable ();
5250 #endif
5251     case DW_OP_const1u:
5252     case DW_OP_const1s:
5253       dw2_asm_output_data (1, val1->v.val_int, NULL);
5254       break;
5255     case DW_OP_constu:
5256       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5257       break;
5258     case DW_OP_consts:
5259       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5260       break;
5261     case DW_OP_pick:
5262       dw2_asm_output_data (1, val1->v.val_int, NULL);
5263       break;
5264     case DW_OP_plus_uconst:
5265       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5266       break;
5267     case DW_OP_breg0:
5268     case DW_OP_breg1:
5269     case DW_OP_breg2:
5270     case DW_OP_breg3:
5271     case DW_OP_breg4:
5272     case DW_OP_breg5:
5273     case DW_OP_breg6:
5274     case DW_OP_breg7:
5275     case DW_OP_breg8:
5276     case DW_OP_breg9:
5277     case DW_OP_breg10:
5278     case DW_OP_breg11:
5279     case DW_OP_breg12:
5280     case DW_OP_breg13:
5281     case DW_OP_breg14:
5282     case DW_OP_breg15:
5283     case DW_OP_breg16:
5284     case DW_OP_breg17:
5285     case DW_OP_breg18:
5286     case DW_OP_breg19:
5287     case DW_OP_breg20:
5288     case DW_OP_breg21:
5289     case DW_OP_breg22:
5290     case DW_OP_breg23:
5291     case DW_OP_breg24:
5292     case DW_OP_breg25:
5293     case DW_OP_breg26:
5294     case DW_OP_breg27:
5295     case DW_OP_breg28:
5296     case DW_OP_breg29:
5297     case DW_OP_breg30:
5298     case DW_OP_breg31:
5299       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5300       break;
5301     case DW_OP_regx:
5302       {
5303         unsigned r = val1->v.val_unsigned;
5304         if (for_eh_or_skip >= 0)
5305           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5306         gcc_assert (size_of_uleb128 (r) 
5307                     == size_of_uleb128 (val1->v.val_unsigned));
5308         dw2_asm_output_data_uleb128 (r, NULL);  
5309       }
5310       break;
5311     case DW_OP_fbreg:
5312       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5313       break;
5314     case DW_OP_bregx:
5315       {
5316         unsigned r = val1->v.val_unsigned;
5317         if (for_eh_or_skip >= 0)
5318           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5319         gcc_assert (size_of_uleb128 (r) 
5320                     == size_of_uleb128 (val1->v.val_unsigned));
5321         dw2_asm_output_data_uleb128 (r, NULL);  
5322         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5323       }
5324       break;
5325     case DW_OP_piece:
5326       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5327       break;
5328     case DW_OP_bit_piece:
5329       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5330       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5331       break;
5332     case DW_OP_deref_size:
5333     case DW_OP_xderef_size:
5334       dw2_asm_output_data (1, val1->v.val_int, NULL);
5335       break;
5336
5337     case DW_OP_addr:
5338       if (loc->dtprel)
5339         {
5340           if (targetm.asm_out.output_dwarf_dtprel)
5341             {
5342               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5343                                                    DWARF2_ADDR_SIZE,
5344                                                    val1->v.val_addr);
5345               fputc ('\n', asm_out_file);
5346             }
5347           else
5348             gcc_unreachable ();
5349         }
5350       else
5351         {
5352 #ifdef DWARF2_DEBUGGING_INFO
5353           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5354 #else
5355           gcc_unreachable ();
5356 #endif
5357         }
5358       break;
5359
5360     case DW_OP_GNU_implicit_pointer:
5361       {
5362         char label[MAX_ARTIFICIAL_LABEL_BYTES
5363                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
5364         gcc_assert (val1->val_class == dw_val_class_die_ref);
5365         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
5366         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
5367         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5368       }
5369       break;
5370
5371     case DW_OP_GNU_entry_value:
5372       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
5373       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
5374       break;
5375
5376     case DW_OP_GNU_const_type:
5377       {
5378         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
5379         gcc_assert (o);
5380         dw2_asm_output_data_uleb128 (o, NULL);
5381         switch (val2->val_class)
5382           {
5383           case dw_val_class_const:
5384             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5385             dw2_asm_output_data (1, l, NULL);
5386             dw2_asm_output_data (l, val2->v.val_int, NULL);
5387             break;
5388           case dw_val_class_vec:
5389             {
5390               unsigned int elt_size = val2->v.val_vec.elt_size;
5391               unsigned int len = val2->v.val_vec.length;
5392               unsigned int i;
5393               unsigned char *p;
5394
5395               l = len * elt_size;
5396               dw2_asm_output_data (1, l, NULL);
5397               if (elt_size > sizeof (HOST_WIDE_INT))
5398                 {
5399                   elt_size /= 2;
5400                   len *= 2;
5401                 }
5402               for (i = 0, p = val2->v.val_vec.array;
5403                    i < len;
5404                    i++, p += elt_size)
5405                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
5406                                      "fp or vector constant word %u", i);
5407             }
5408             break;
5409           case dw_val_class_const_double:
5410             {
5411               unsigned HOST_WIDE_INT first, second;
5412               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
5413
5414               dw2_asm_output_data (1, 2 * l, NULL);
5415               if (WORDS_BIG_ENDIAN)
5416                 {
5417                   first = val2->v.val_double.high;
5418                   second = val2->v.val_double.low;
5419                 }
5420               else
5421                 {
5422                   first = val2->v.val_double.low;
5423                   second = val2->v.val_double.high;
5424                 }
5425               dw2_asm_output_data (l, first, NULL);
5426               dw2_asm_output_data (l, second, NULL);
5427             }
5428             break;
5429           default:
5430             gcc_unreachable ();
5431           }
5432       }
5433       break;
5434     case DW_OP_GNU_regval_type:
5435       {
5436         unsigned r = val1->v.val_unsigned;
5437         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5438         gcc_assert (o);
5439         if (for_eh_or_skip >= 0)
5440           {
5441             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5442             gcc_assert (size_of_uleb128 (r)
5443                         == size_of_uleb128 (val1->v.val_unsigned));
5444           }
5445         dw2_asm_output_data_uleb128 (r, NULL);
5446         dw2_asm_output_data_uleb128 (o, NULL);
5447       }
5448       break;
5449     case DW_OP_GNU_deref_type:
5450       {
5451         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
5452         gcc_assert (o);
5453         dw2_asm_output_data (1, val1->v.val_int, NULL);
5454         dw2_asm_output_data_uleb128 (o, NULL);
5455       }
5456       break;
5457     case DW_OP_GNU_convert:
5458     case DW_OP_GNU_reinterpret:
5459       {
5460         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
5461         gcc_assert (o);
5462         dw2_asm_output_data_uleb128 (o, NULL);
5463       }
5464       break;
5465
5466     default:
5467       /* Other codes have no operands.  */
5468       break;
5469     }
5470 }
5471
5472 /* Output a sequence of location operations.  
5473    The for_eh_or_skip parameter controls whether register numbers are
5474    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
5475    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
5476    info).  This should be suppressed for the cases that have not been converted
5477    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
5478
5479 static void
5480 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
5481 {
5482   for (; loc != NULL; loc = loc->dw_loc_next)
5483     {
5484       enum dwarf_location_atom opc = loc->dw_loc_opc;
5485       /* Output the opcode.  */
5486       if (for_eh_or_skip >= 0 
5487           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5488         {
5489           unsigned r = (opc - DW_OP_breg0);
5490           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5491           gcc_assert (r <= 31);
5492           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5493         }
5494       else if (for_eh_or_skip >= 0 
5495                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5496         {
5497           unsigned r = (opc - DW_OP_reg0);
5498           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
5499           gcc_assert (r <= 31);
5500           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5501         }
5502
5503       dw2_asm_output_data (1, opc,
5504                              "%s", dwarf_stack_op_name (opc));
5505
5506       /* Output the operand(s) (if any).  */
5507       output_loc_operands (loc, for_eh_or_skip);
5508     }
5509 }
5510
5511 /* Output location description stack opcode's operands (if any).
5512    The output is single bytes on a line, suitable for .cfi_escape.  */
5513
5514 static void
5515 output_loc_operands_raw (dw_loc_descr_ref loc)
5516 {
5517   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5518   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5519
5520   switch (loc->dw_loc_opc)
5521     {
5522     case DW_OP_addr:
5523     case DW_OP_implicit_value:
5524       /* We cannot output addresses in .cfi_escape, only bytes.  */
5525       gcc_unreachable ();
5526
5527     case DW_OP_const1u:
5528     case DW_OP_const1s:
5529     case DW_OP_pick:
5530     case DW_OP_deref_size:
5531     case DW_OP_xderef_size:
5532       fputc (',', asm_out_file);
5533       dw2_asm_output_data_raw (1, val1->v.val_int);
5534       break;
5535
5536     case DW_OP_const2u:
5537     case DW_OP_const2s:
5538       fputc (',', asm_out_file);
5539       dw2_asm_output_data_raw (2, val1->v.val_int);
5540       break;
5541
5542     case DW_OP_const4u:
5543     case DW_OP_const4s:
5544       fputc (',', asm_out_file);
5545       dw2_asm_output_data_raw (4, val1->v.val_int);
5546       break;
5547
5548     case DW_OP_const8u:
5549     case DW_OP_const8s:
5550       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5551       fputc (',', asm_out_file);
5552       dw2_asm_output_data_raw (8, val1->v.val_int);
5553       break;
5554
5555     case DW_OP_skip:
5556     case DW_OP_bra:
5557       {
5558         int offset;
5559
5560         gcc_assert (val1->val_class == dw_val_class_loc);
5561         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5562
5563         fputc (',', asm_out_file);
5564         dw2_asm_output_data_raw (2, offset);
5565       }
5566       break;
5567
5568     case DW_OP_regx:
5569       {
5570         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5571         gcc_assert (size_of_uleb128 (r) 
5572                     == size_of_uleb128 (val1->v.val_unsigned));
5573         fputc (',', asm_out_file);
5574         dw2_asm_output_data_uleb128_raw (r);
5575       }
5576       break;
5577       
5578     case DW_OP_constu:
5579     case DW_OP_plus_uconst:
5580     case DW_OP_piece:
5581       fputc (',', asm_out_file);
5582       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5583       break;
5584
5585     case DW_OP_bit_piece:
5586       fputc (',', asm_out_file);
5587       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5588       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5589       break;
5590
5591     case DW_OP_consts:
5592     case DW_OP_breg0:
5593     case DW_OP_breg1:
5594     case DW_OP_breg2:
5595     case DW_OP_breg3:
5596     case DW_OP_breg4:
5597     case DW_OP_breg5:
5598     case DW_OP_breg6:
5599     case DW_OP_breg7:
5600     case DW_OP_breg8:
5601     case DW_OP_breg9:
5602     case DW_OP_breg10:
5603     case DW_OP_breg11:
5604     case DW_OP_breg12:
5605     case DW_OP_breg13:
5606     case DW_OP_breg14:
5607     case DW_OP_breg15:
5608     case DW_OP_breg16:
5609     case DW_OP_breg17:
5610     case DW_OP_breg18:
5611     case DW_OP_breg19:
5612     case DW_OP_breg20:
5613     case DW_OP_breg21:
5614     case DW_OP_breg22:
5615     case DW_OP_breg23:
5616     case DW_OP_breg24:
5617     case DW_OP_breg25:
5618     case DW_OP_breg26:
5619     case DW_OP_breg27:
5620     case DW_OP_breg28:
5621     case DW_OP_breg29:
5622     case DW_OP_breg30:
5623     case DW_OP_breg31:
5624     case DW_OP_fbreg:
5625       fputc (',', asm_out_file);
5626       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5627       break;
5628
5629     case DW_OP_bregx:
5630       {
5631         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
5632         gcc_assert (size_of_uleb128 (r) 
5633                     == size_of_uleb128 (val1->v.val_unsigned));
5634         fputc (',', asm_out_file);
5635         dw2_asm_output_data_uleb128_raw (r);
5636         fputc (',', asm_out_file);
5637         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5638       }
5639       break;
5640
5641     case DW_OP_GNU_implicit_pointer:
5642     case DW_OP_GNU_entry_value:
5643     case DW_OP_GNU_const_type:
5644     case DW_OP_GNU_regval_type:
5645     case DW_OP_GNU_deref_type:
5646     case DW_OP_GNU_convert:
5647     case DW_OP_GNU_reinterpret:
5648       gcc_unreachable ();
5649       break;
5650
5651     default:
5652       /* Other codes have no operands.  */
5653       break;
5654     }
5655 }
5656
5657 static void
5658 output_loc_sequence_raw (dw_loc_descr_ref loc)
5659 {
5660   while (1)
5661     {
5662       enum dwarf_location_atom opc = loc->dw_loc_opc;
5663       /* Output the opcode.  */
5664       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
5665         {
5666           unsigned r = (opc - DW_OP_breg0);
5667           r = DWARF2_FRAME_REG_OUT (r, 1);
5668           gcc_assert (r <= 31);
5669           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
5670         }
5671       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
5672         {
5673           unsigned r = (opc - DW_OP_reg0);
5674           r = DWARF2_FRAME_REG_OUT (r, 1);
5675           gcc_assert (r <= 31);
5676           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
5677         }
5678       /* Output the opcode.  */
5679       fprintf (asm_out_file, "%#x", opc);
5680       output_loc_operands_raw (loc);
5681
5682       if (!loc->dw_loc_next)
5683         break;
5684       loc = loc->dw_loc_next;
5685
5686       fputc (',', asm_out_file);
5687     }
5688 }
5689
5690 /* This routine will generate the correct assembly data for a location
5691    description based on a cfi entry with a complex address.  */
5692
5693 static void
5694 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
5695 {
5696   dw_loc_descr_ref loc;
5697   unsigned long size;
5698
5699   if (cfi->dw_cfi_opc == DW_CFA_expression)
5700     {
5701       unsigned r = 
5702         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
5703       dw2_asm_output_data (1, r, NULL);
5704       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5705     }
5706   else
5707     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5708
5709   /* Output the size of the block.  */
5710   size = size_of_locs (loc);
5711   dw2_asm_output_data_uleb128 (size, NULL);
5712
5713   /* Now output the operations themselves.  */
5714   output_loc_sequence (loc, for_eh);
5715 }
5716
5717 /* Similar, but used for .cfi_escape.  */
5718
5719 static void
5720 output_cfa_loc_raw (dw_cfi_ref cfi)
5721 {
5722   dw_loc_descr_ref loc;
5723   unsigned long size;
5724
5725   if (cfi->dw_cfi_opc == DW_CFA_expression)
5726     {
5727       unsigned r = 
5728         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
5729       fprintf (asm_out_file, "%#x,", r);
5730       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5731     }
5732   else
5733     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5734
5735   /* Output the size of the block.  */
5736   size = size_of_locs (loc);
5737   dw2_asm_output_data_uleb128_raw (size);
5738   fputc (',', asm_out_file);
5739
5740   /* Now output the operations themselves.  */
5741   output_loc_sequence_raw (loc);
5742 }
5743
5744 /* This function builds a dwarf location descriptor sequence from a
5745    dw_cfa_location, adding the given OFFSET to the result of the
5746    expression.  */
5747
5748 static struct dw_loc_descr_struct *
5749 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5750 {
5751   struct dw_loc_descr_struct *head, *tmp;
5752
5753   offset += cfa->offset;
5754
5755   if (cfa->indirect)
5756     {
5757       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5758       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5759       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5760       add_loc_descr (&head, tmp);
5761       if (offset != 0)
5762         {
5763           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5764           add_loc_descr (&head, tmp);
5765         }
5766     }
5767   else
5768     head = new_reg_loc_descr (cfa->reg, offset);
5769
5770   return head;
5771 }
5772
5773 /* This function builds a dwarf location descriptor sequence for
5774    the address at OFFSET from the CFA when stack is aligned to
5775    ALIGNMENT byte.  */
5776
5777 static struct dw_loc_descr_struct *
5778 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5779 {
5780   struct dw_loc_descr_struct *head;
5781   unsigned int dwarf_fp
5782     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5783
5784  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5785   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5786     {
5787       head = new_reg_loc_descr (dwarf_fp, 0);
5788       add_loc_descr (&head, int_loc_descriptor (alignment));
5789       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5790       loc_descr_plus_const (&head, offset);
5791     }
5792   else
5793     head = new_reg_loc_descr (dwarf_fp, offset);
5794   return head;
5795 }
5796
5797 /* This function fills in aa dw_cfa_location structure from a dwarf location
5798    descriptor sequence.  */
5799
5800 static void
5801 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5802 {
5803   struct dw_loc_descr_struct *ptr;
5804   cfa->offset = 0;
5805   cfa->base_offset = 0;
5806   cfa->indirect = 0;
5807   cfa->reg = -1;
5808
5809   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5810     {
5811       enum dwarf_location_atom op = ptr->dw_loc_opc;
5812
5813       switch (op)
5814         {
5815         case DW_OP_reg0:
5816         case DW_OP_reg1:
5817         case DW_OP_reg2:
5818         case DW_OP_reg3:
5819         case DW_OP_reg4:
5820         case DW_OP_reg5:
5821         case DW_OP_reg6:
5822         case DW_OP_reg7:
5823         case DW_OP_reg8:
5824         case DW_OP_reg9:
5825         case DW_OP_reg10:
5826         case DW_OP_reg11:
5827         case DW_OP_reg12:
5828         case DW_OP_reg13:
5829         case DW_OP_reg14:
5830         case DW_OP_reg15:
5831         case DW_OP_reg16:
5832         case DW_OP_reg17:
5833         case DW_OP_reg18:
5834         case DW_OP_reg19:
5835         case DW_OP_reg20:
5836         case DW_OP_reg21:
5837         case DW_OP_reg22:
5838         case DW_OP_reg23:
5839         case DW_OP_reg24:
5840         case DW_OP_reg25:
5841         case DW_OP_reg26:
5842         case DW_OP_reg27:
5843         case DW_OP_reg28:
5844         case DW_OP_reg29:
5845         case DW_OP_reg30:
5846         case DW_OP_reg31:
5847           cfa->reg = op - DW_OP_reg0;
5848           break;
5849         case DW_OP_regx:
5850           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5851           break;
5852         case DW_OP_breg0:
5853         case DW_OP_breg1:
5854         case DW_OP_breg2:
5855         case DW_OP_breg3:
5856         case DW_OP_breg4:
5857         case DW_OP_breg5:
5858         case DW_OP_breg6:
5859         case DW_OP_breg7:
5860         case DW_OP_breg8:
5861         case DW_OP_breg9:
5862         case DW_OP_breg10:
5863         case DW_OP_breg11:
5864         case DW_OP_breg12:
5865         case DW_OP_breg13:
5866         case DW_OP_breg14:
5867         case DW_OP_breg15:
5868         case DW_OP_breg16:
5869         case DW_OP_breg17:
5870         case DW_OP_breg18:
5871         case DW_OP_breg19:
5872         case DW_OP_breg20:
5873         case DW_OP_breg21:
5874         case DW_OP_breg22:
5875         case DW_OP_breg23:
5876         case DW_OP_breg24:
5877         case DW_OP_breg25:
5878         case DW_OP_breg26:
5879         case DW_OP_breg27:
5880         case DW_OP_breg28:
5881         case DW_OP_breg29:
5882         case DW_OP_breg30:
5883         case DW_OP_breg31:
5884           cfa->reg = op - DW_OP_breg0;
5885           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5886           break;
5887         case DW_OP_bregx:
5888           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5889           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5890           break;
5891         case DW_OP_deref:
5892           cfa->indirect = 1;
5893           break;
5894         case DW_OP_plus_uconst:
5895           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5896           break;
5897         default:
5898           internal_error ("DW_LOC_OP %s not implemented",
5899                           dwarf_stack_op_name (ptr->dw_loc_opc));
5900         }
5901     }
5902 }
5903 \f
5904 /* And now, the support for symbolic debugging information.  */
5905
5906 /* .debug_str support.  */
5907 static int output_indirect_string (void **, void *);
5908
5909 static void dwarf2out_init (const char *);
5910 static void dwarf2out_finish (const char *);
5911 static void dwarf2out_assembly_start (void);
5912 static void dwarf2out_define (unsigned int, const char *);
5913 static void dwarf2out_undef (unsigned int, const char *);
5914 static void dwarf2out_start_source_file (unsigned, const char *);
5915 static void dwarf2out_end_source_file (unsigned);
5916 static void dwarf2out_function_decl (tree);
5917 static void dwarf2out_begin_block (unsigned, unsigned);
5918 static void dwarf2out_end_block (unsigned, unsigned);
5919 static bool dwarf2out_ignore_block (const_tree);
5920 static void dwarf2out_global_decl (tree);
5921 static void dwarf2out_type_decl (tree, int);
5922 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5923 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5924                                                  dw_die_ref);
5925 static void dwarf2out_abstract_function (tree);
5926 static void dwarf2out_var_location (rtx);
5927 static void dwarf2out_begin_function (tree);
5928 static void dwarf2out_set_name (tree, tree);
5929
5930 /* The debug hooks structure.  */
5931
5932 const struct gcc_debug_hooks dwarf2_debug_hooks =
5933 {
5934   dwarf2out_init,
5935   dwarf2out_finish,
5936   dwarf2out_assembly_start,
5937   dwarf2out_define,
5938   dwarf2out_undef,
5939   dwarf2out_start_source_file,
5940   dwarf2out_end_source_file,
5941   dwarf2out_begin_block,
5942   dwarf2out_end_block,
5943   dwarf2out_ignore_block,
5944   dwarf2out_source_line,
5945   dwarf2out_begin_prologue,
5946 #if VMS_DEBUGGING_INFO
5947   dwarf2out_vms_end_prologue,
5948   dwarf2out_vms_begin_epilogue,
5949 #else
5950   debug_nothing_int_charstar,
5951   debug_nothing_int_charstar,
5952 #endif
5953   dwarf2out_end_epilogue,
5954   dwarf2out_begin_function,
5955   debug_nothing_int,            /* end_function */
5956   dwarf2out_function_decl,      /* function_decl */
5957   dwarf2out_global_decl,
5958   dwarf2out_type_decl,          /* type_decl */
5959   dwarf2out_imported_module_or_decl,
5960   debug_nothing_tree,           /* deferred_inline_function */
5961   /* The DWARF 2 backend tries to reduce debugging bloat by not
5962      emitting the abstract description of inline functions until
5963      something tries to reference them.  */
5964   dwarf2out_abstract_function,  /* outlining_inline_function */
5965   debug_nothing_rtx,            /* label */
5966   debug_nothing_int,            /* handle_pch */
5967   dwarf2out_var_location,
5968   dwarf2out_switch_text_section,
5969   dwarf2out_set_name,
5970   1,                            /* start_end_main_source_file */
5971   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
5972 };
5973 \f
5974 /* NOTE: In the comments in this file, many references are made to
5975    "Debugging Information Entries".  This term is abbreviated as `DIE'
5976    throughout the remainder of this file.  */
5977
5978 /* An internal representation of the DWARF output is built, and then
5979    walked to generate the DWARF debugging info.  The walk of the internal
5980    representation is done after the entire program has been compiled.
5981    The types below are used to describe the internal representation.  */
5982
5983 /* Whether to put type DIEs into their own section .debug_types instead
5984    of making them part of the .debug_info section.  Only supported for
5985    Dwarf V4 or higher and the user didn't disable them through
5986    -fno-debug-types-section.  It is more efficient to put them in a
5987    separate comdat sections since the linker will then be able to
5988    remove duplicates.  But not all tools support .debug_types sections
5989    yet.  */
5990
5991 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
5992
5993 /* Various DIE's use offsets relative to the beginning of the
5994    .debug_info section to refer to each other.  */
5995
5996 typedef long int dw_offset;
5997
5998 /* Define typedefs here to avoid circular dependencies.  */
5999
6000 typedef struct dw_attr_struct *dw_attr_ref;
6001 typedef struct dw_line_info_struct *dw_line_info_ref;
6002 typedef struct pubname_struct *pubname_ref;
6003 typedef struct dw_ranges_struct *dw_ranges_ref;
6004 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
6005 typedef struct comdat_type_struct *comdat_type_node_ref;
6006
6007 /* The entries in the line_info table more-or-less mirror the opcodes
6008    that are used in the real dwarf line table.  Arrays of these entries
6009    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
6010    supported.  */
6011
6012 enum dw_line_info_opcode {
6013   /* Emit DW_LNE_set_address; the operand is the label index.  */
6014   LI_set_address,
6015
6016   /* Emit a row to the matrix with the given line.  This may be done
6017      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
6018      special opcodes.  */
6019   LI_set_line,
6020
6021   /* Emit a DW_LNS_set_file.  */
6022   LI_set_file,
6023
6024   /* Emit a DW_LNS_set_column.  */
6025   LI_set_column,
6026
6027   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
6028   LI_negate_stmt,
6029
6030   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
6031   LI_set_prologue_end,
6032   LI_set_epilogue_begin,
6033
6034   /* Emit a DW_LNE_set_discriminator.  */
6035   LI_set_discriminator
6036 };
6037
6038 typedef struct GTY(()) dw_line_info_struct {
6039   enum dw_line_info_opcode opcode;
6040   unsigned int val;
6041 } dw_line_info_entry;
6042
6043 DEF_VEC_O(dw_line_info_entry);
6044 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
6045
6046 typedef struct GTY(()) dw_line_info_table_struct {
6047   /* The label that marks the end of this section.  */
6048   const char *end_label;
6049
6050   /* The values for the last row of the matrix, as collected in the table.
6051      These are used to minimize the changes to the next row.  */
6052   unsigned int file_num;
6053   unsigned int line_num;
6054   unsigned int column_num;
6055   int discrim_num;
6056   bool is_stmt;
6057   bool in_use;
6058
6059   VEC(dw_line_info_entry, gc) *entries;
6060 } dw_line_info_table;
6061
6062 typedef dw_line_info_table *dw_line_info_table_p;
6063
6064 DEF_VEC_P(dw_line_info_table_p);
6065 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
6066
6067 /* Each DIE attribute has a field specifying the attribute kind,
6068    a link to the next attribute in the chain, and an attribute value.
6069    Attributes are typically linked below the DIE they modify.  */
6070
6071 typedef struct GTY(()) dw_attr_struct {
6072   enum dwarf_attribute dw_attr;
6073   dw_val_node dw_attr_val;
6074 }
6075 dw_attr_node;
6076
6077 DEF_VEC_O(dw_attr_node);
6078 DEF_VEC_ALLOC_O(dw_attr_node,gc);
6079
6080 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
6081    The children of each node form a circular list linked by
6082    die_sib.  die_child points to the node *before* the "first" child node.  */
6083
6084 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
6085   union die_symbol_or_type_node
6086     {
6087       char * GTY ((tag ("0"))) die_symbol;
6088       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
6089     }
6090   GTY ((desc ("use_debug_types"))) die_id;
6091   VEC(dw_attr_node,gc) * die_attr;
6092   dw_die_ref die_parent;
6093   dw_die_ref die_child;
6094   dw_die_ref die_sib;
6095   dw_die_ref die_definition; /* ref from a specification to its definition */
6096   dw_offset die_offset;
6097   unsigned long die_abbrev;
6098   int die_mark;
6099   /* Die is used and must not be pruned as unused.  */
6100   int die_perennial_p;
6101   unsigned int decl_id;
6102   enum dwarf_tag die_tag;
6103 }
6104 die_node;
6105
6106 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
6107 #define FOR_EACH_CHILD(die, c, expr) do {       \
6108   c = die->die_child;                           \
6109   if (c) do {                                   \
6110     c = c->die_sib;                             \
6111     expr;                                       \
6112   } while (c != die->die_child);                \
6113 } while (0)
6114
6115 /* The pubname structure */
6116
6117 typedef struct GTY(()) pubname_struct {
6118   dw_die_ref die;
6119   const char *name;
6120 }
6121 pubname_entry;
6122
6123 DEF_VEC_O(pubname_entry);
6124 DEF_VEC_ALLOC_O(pubname_entry, gc);
6125
6126 struct GTY(()) dw_ranges_struct {
6127   /* If this is positive, it's a block number, otherwise it's a
6128      bitwise-negated index into dw_ranges_by_label.  */
6129   int num;
6130 };
6131
6132 /* A structure to hold a macinfo entry.  */
6133
6134 typedef struct GTY(()) macinfo_struct {
6135   unsigned HOST_WIDE_INT code;
6136   unsigned HOST_WIDE_INT lineno;
6137   const char *info;
6138 }
6139 macinfo_entry;
6140
6141 DEF_VEC_O(macinfo_entry);
6142 DEF_VEC_ALLOC_O(macinfo_entry, gc);
6143
6144 struct GTY(()) dw_ranges_by_label_struct {
6145   const char *begin;
6146   const char *end;
6147 };
6148
6149 /* The comdat type node structure.  */
6150 typedef struct GTY(()) comdat_type_struct
6151 {
6152   dw_die_ref root_die;
6153   dw_die_ref type_die;
6154   char signature[DWARF_TYPE_SIGNATURE_SIZE];
6155   struct comdat_type_struct *next;
6156 }
6157 comdat_type_node;
6158
6159 /* The limbo die list structure.  */
6160 typedef struct GTY(()) limbo_die_struct {
6161   dw_die_ref die;
6162   tree created_for;
6163   struct limbo_die_struct *next;
6164 }
6165 limbo_die_node;
6166
6167 typedef struct skeleton_chain_struct
6168 {
6169   dw_die_ref old_die;
6170   dw_die_ref new_die;
6171   struct skeleton_chain_struct *parent;
6172 }
6173 skeleton_chain_node;
6174
6175 /* How to start an assembler comment.  */
6176 #ifndef ASM_COMMENT_START
6177 #define ASM_COMMENT_START ";#"
6178 #endif
6179
6180 /* Define a macro which returns nonzero for a TYPE_DECL which was
6181    implicitly generated for a tagged type.
6182
6183    Note that unlike the gcc front end (which generates a NULL named
6184    TYPE_DECL node for each complete tagged type, each array type, and
6185    each function type node created) the g++ front end generates a
6186    _named_ TYPE_DECL node for each tagged type node created.
6187    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
6188    generate a DW_TAG_typedef DIE for them.  */
6189
6190 #define TYPE_DECL_IS_STUB(decl)                         \
6191   (DECL_NAME (decl) == NULL_TREE                        \
6192    || (DECL_ARTIFICIAL (decl)                           \
6193        && is_tagged_type (TREE_TYPE (decl))             \
6194        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
6195            /* This is necessary for stub decls that     \
6196               appear in nested inline functions.  */    \
6197            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
6198                && (decl_ultimate_origin (decl)          \
6199                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
6200
6201 /* Information concerning the compilation unit's programming
6202    language, and compiler version.  */
6203
6204 /* Fixed size portion of the DWARF compilation unit header.  */
6205 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
6206   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
6207
6208 /* Fixed size portion of the DWARF comdat type unit header.  */
6209 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
6210   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
6211    + DWARF_OFFSET_SIZE)
6212
6213 /* Fixed size portion of public names info.  */
6214 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
6215
6216 /* Fixed size portion of the address range info.  */
6217 #define DWARF_ARANGES_HEADER_SIZE                                       \
6218   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
6219                 DWARF2_ADDR_SIZE * 2)                                   \
6220    - DWARF_INITIAL_LENGTH_SIZE)
6221
6222 /* Size of padding portion in the address range info.  It must be
6223    aligned to twice the pointer size.  */
6224 #define DWARF_ARANGES_PAD_SIZE \
6225   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
6226                 DWARF2_ADDR_SIZE * 2)                              \
6227    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
6228
6229 /* Use assembler line directives if available.  */
6230 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
6231 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
6232 #define DWARF2_ASM_LINE_DEBUG_INFO 1
6233 #else
6234 #define DWARF2_ASM_LINE_DEBUG_INFO 0
6235 #endif
6236 #endif
6237
6238 /* Minimum line offset in a special line info. opcode.
6239    This value was chosen to give a reasonable range of values.  */
6240 #define DWARF_LINE_BASE  -10
6241
6242 /* First special line opcode - leave room for the standard opcodes.  */
6243 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
6244
6245 /* Range of line offsets in a special line info. opcode.  */
6246 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
6247
6248 /* Flag that indicates the initial value of the is_stmt_start flag.
6249    In the present implementation, we do not mark any lines as
6250    the beginning of a source statement, because that information
6251    is not made available by the GCC front-end.  */
6252 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
6253
6254 /* Maximum number of operations per instruction bundle.  */
6255 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
6256 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
6257 #endif
6258
6259 /* This location is used by calc_die_sizes() to keep track
6260    the offset of each DIE within the .debug_info section.  */
6261 static unsigned long next_die_offset;
6262
6263 /* Record the root of the DIE's built for the current compilation unit.  */
6264 static GTY(()) dw_die_ref single_comp_unit_die;
6265
6266 /* A list of type DIEs that have been separated into comdat sections.  */
6267 static GTY(()) comdat_type_node *comdat_type_list;
6268
6269 /* A list of DIEs with a NULL parent waiting to be relocated.  */
6270 static GTY(()) limbo_die_node *limbo_die_list;
6271
6272 /* A list of DIEs for which we may have to generate
6273    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
6274 static GTY(()) limbo_die_node *deferred_asm_name;
6275
6276 /* Filenames referenced by this compilation unit.  */
6277 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
6278
6279 /* A hash table of references to DIE's that describe declarations.
6280    The key is a DECL_UID() which is a unique number identifying each decl.  */
6281 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
6282
6283 /* A hash table of references to DIE's that describe COMMON blocks.
6284    The key is DECL_UID() ^ die_parent.  */
6285 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
6286
6287 typedef struct GTY(()) die_arg_entry_struct {
6288     dw_die_ref die;
6289     tree arg;
6290 } die_arg_entry;
6291
6292 DEF_VEC_O(die_arg_entry);
6293 DEF_VEC_ALLOC_O(die_arg_entry,gc);
6294
6295 /* Node of the variable location list.  */
6296 struct GTY ((chain_next ("%h.next"))) var_loc_node {
6297   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
6298      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
6299      in mode of the EXPR_LIST node and first EXPR_LIST operand
6300      is either NOTE_INSN_VAR_LOCATION for a piece with a known
6301      location or NULL for padding.  For larger bitsizes,
6302      mode is 0 and first operand is a CONCAT with bitsize
6303      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
6304      NULL as second operand.  */
6305   rtx GTY (()) loc;
6306   const char * GTY (()) label;
6307   struct var_loc_node * GTY (()) next;
6308 };
6309
6310 /* Variable location list.  */
6311 struct GTY (()) var_loc_list_def {
6312   struct var_loc_node * GTY (()) first;
6313
6314   /* Pointer to the last but one or last element of the
6315      chained list.  If the list is empty, both first and
6316      last are NULL, if the list contains just one node
6317      or the last node certainly is not redundant, it points
6318      to the last node, otherwise points to the last but one.
6319      Do not mark it for GC because it is marked through the chain.  */
6320   struct var_loc_node * GTY ((skip ("%h"))) last;
6321
6322   /* Pointer to the last element before section switch,
6323      if NULL, either sections weren't switched or first
6324      is after section switch.  */
6325   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
6326
6327   /* DECL_UID of the variable decl.  */
6328   unsigned int decl_id;
6329 };
6330 typedef struct var_loc_list_def var_loc_list;
6331
6332 /* Call argument location list.  */
6333 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
6334   rtx GTY (()) call_arg_loc_note;
6335   const char * GTY (()) label;
6336   tree GTY (()) block;
6337   bool tail_call_p;
6338   rtx GTY (()) symbol_ref;
6339   struct call_arg_loc_node * GTY (()) next;
6340 };
6341
6342
6343 /* Table of decl location linked lists.  */
6344 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
6345
6346 /* Head and tail of call_arg_loc chain.  */
6347 static GTY (()) struct call_arg_loc_node *call_arg_locations;
6348 static struct call_arg_loc_node *call_arg_loc_last;
6349
6350 /* Number of call sites in the current function.  */
6351 static int call_site_count = -1;
6352 /* Number of tail call sites in the current function.  */
6353 static int tail_call_site_count = -1;
6354
6355 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
6356    DIEs.  */
6357 static VEC (dw_die_ref, heap) *block_map;
6358
6359 /* A cached location list.  */
6360 struct GTY (()) cached_dw_loc_list_def {
6361   /* The DECL_UID of the decl that this entry describes.  */
6362   unsigned int decl_id;
6363
6364   /* The cached location list.  */
6365   dw_loc_list_ref loc_list;
6366 };
6367 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
6368
6369 /* Table of cached location lists.  */
6370 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
6371
6372 /* A pointer to the base of a list of references to DIE's that
6373    are uniquely identified by their tag, presence/absence of
6374    children DIE's, and list of attribute/value pairs.  */
6375 static GTY((length ("abbrev_die_table_allocated")))
6376   dw_die_ref *abbrev_die_table;
6377
6378 /* Number of elements currently allocated for abbrev_die_table.  */
6379 static GTY(()) unsigned abbrev_die_table_allocated;
6380
6381 /* Number of elements in type_die_table currently in use.  */
6382 static GTY(()) unsigned abbrev_die_table_in_use;
6383
6384 /* Size (in elements) of increments by which we may expand the
6385    abbrev_die_table.  */
6386 #define ABBREV_DIE_TABLE_INCREMENT 256
6387
6388 /* A global counter for generating labels for line number data.  */
6389 static unsigned int line_info_label_num;
6390
6391 /* The current table to which we should emit line number information
6392    for the current function.  This will be set up at the beginning of
6393    assembly for the function.  */
6394 static dw_line_info_table *cur_line_info_table;
6395
6396 /* The two default tables of line number info.  */
6397 static GTY(()) dw_line_info_table *text_section_line_info;
6398 static GTY(()) dw_line_info_table *cold_text_section_line_info;
6399
6400 /* The set of all non-default tables of line number info.  */
6401 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
6402
6403 /* A flag to tell pubnames/types export if there is an info section to
6404    refer to.  */
6405 static bool info_section_emitted;
6406
6407 /* A pointer to the base of a table that contains a list of publicly
6408    accessible names.  */
6409 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
6410
6411 /* A pointer to the base of a table that contains a list of publicly
6412    accessible types.  */
6413 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
6414
6415 /* A pointer to the base of a table that contains a list of macro
6416    defines/undefines (and file start/end markers).  */
6417 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
6418
6419 /* Array of dies for which we should generate .debug_ranges info.  */
6420 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
6421
6422 /* Number of elements currently allocated for ranges_table.  */
6423 static GTY(()) unsigned ranges_table_allocated;
6424
6425 /* Number of elements in ranges_table currently in use.  */
6426 static GTY(()) unsigned ranges_table_in_use;
6427
6428 /* Array of pairs of labels referenced in ranges_table.  */
6429 static GTY ((length ("ranges_by_label_allocated")))
6430      dw_ranges_by_label_ref ranges_by_label;
6431
6432 /* Number of elements currently allocated for ranges_by_label.  */
6433 static GTY(()) unsigned ranges_by_label_allocated;
6434
6435 /* Number of elements in ranges_by_label currently in use.  */
6436 static GTY(()) unsigned ranges_by_label_in_use;
6437
6438 /* Size (in elements) of increments by which we may expand the
6439    ranges_table.  */
6440 #define RANGES_TABLE_INCREMENT 64
6441
6442 /* Whether we have location lists that need outputting */
6443 static GTY(()) bool have_location_lists;
6444
6445 /* Unique label counter.  */
6446 static GTY(()) unsigned int loclabel_num;
6447
6448 /* Unique label counter for point-of-call tables.  */
6449 static GTY(()) unsigned int poc_label_num;
6450
6451 /* Record whether the function being analyzed contains inlined functions.  */
6452 static int current_function_has_inlines;
6453
6454 /* The last file entry emitted by maybe_emit_file().  */
6455 static GTY(()) struct dwarf_file_data * last_emitted_file;
6456
6457 /* Number of internal labels generated by gen_internal_sym().  */
6458 static GTY(()) int label_num;
6459
6460 /* Cached result of previous call to lookup_filename.  */
6461 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6462
6463 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6464
6465 /* Instances of generic types for which we need to generate debug
6466    info that describe their generic parameters and arguments. That
6467    generation needs to happen once all types are properly laid out so
6468    we do it at the end of compilation.  */
6469 static GTY(()) VEC(tree,gc) *generic_type_instances;
6470
6471 /* Offset from the "steady-state frame pointer" to the frame base,
6472    within the current function.  */
6473 static HOST_WIDE_INT frame_pointer_fb_offset;
6474
6475 static VEC (dw_die_ref, heap) *base_types;
6476
6477 /* Forward declarations for functions defined in this file.  */
6478
6479 static int is_pseudo_reg (const_rtx);
6480 static tree type_main_variant (tree);
6481 static int is_tagged_type (const_tree);
6482 static const char *dwarf_tag_name (unsigned);
6483 static const char *dwarf_attr_name (unsigned);
6484 static const char *dwarf_form_name (unsigned);
6485 static tree decl_ultimate_origin (const_tree);
6486 static tree decl_class_context (tree);
6487 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6488 static inline enum dw_val_class AT_class (dw_attr_ref);
6489 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6490 static inline unsigned AT_flag (dw_attr_ref);
6491 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6492 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6493 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6494 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6495 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6496                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6497 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6498                                unsigned int, unsigned char *);
6499 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6500 static hashval_t debug_str_do_hash (const void *);
6501 static int debug_str_eq (const void *, const void *);
6502 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6503 static inline const char *AT_string (dw_attr_ref);
6504 static enum dwarf_form AT_string_form (dw_attr_ref);
6505 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6506 static void add_AT_specification (dw_die_ref, dw_die_ref);
6507 static inline dw_die_ref AT_ref (dw_attr_ref);
6508 static inline int AT_ref_external (dw_attr_ref);
6509 static inline void set_AT_ref_external (dw_attr_ref, int);
6510 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6511 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6512 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6513 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6514                              dw_loc_list_ref);
6515 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6516 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6517 static inline rtx AT_addr (dw_attr_ref);
6518 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6519 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6520 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6521 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6522                            unsigned HOST_WIDE_INT);
6523 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6524                                unsigned long);
6525 static inline const char *AT_lbl (dw_attr_ref);
6526 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6527 static const char *get_AT_low_pc (dw_die_ref);
6528 static const char *get_AT_hi_pc (dw_die_ref);
6529 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6530 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6531 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6532 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6533 static bool is_cxx (void);
6534 static bool is_fortran (void);
6535 static bool is_ada (void);
6536 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6537 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6538 static void add_child_die (dw_die_ref, dw_die_ref);
6539 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6540 static dw_die_ref lookup_type_die (tree);
6541 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
6542 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
6543 static void equate_type_number_to_die (tree, dw_die_ref);
6544 static hashval_t decl_die_table_hash (const void *);
6545 static int decl_die_table_eq (const void *, const void *);
6546 static dw_die_ref lookup_decl_die (tree);
6547 static hashval_t common_block_die_table_hash (const void *);
6548 static int common_block_die_table_eq (const void *, const void *);
6549 static hashval_t decl_loc_table_hash (const void *);
6550 static int decl_loc_table_eq (const void *, const void *);
6551 static var_loc_list *lookup_decl_loc (const_tree);
6552 static void equate_decl_number_to_die (tree, dw_die_ref);
6553 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6554 static void print_spaces (FILE *);
6555 static void print_die (dw_die_ref, FILE *);
6556 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6557 static dw_die_ref pop_compile_unit (dw_die_ref);
6558 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6559 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6560 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6561 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6562 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6563 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6564 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6565                                    struct md5_ctx *, int *);
6566 struct checksum_attributes;
6567 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6568 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6569 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6570 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6571 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6572 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6573 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6574 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6575 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6576 static void compute_section_prefix (dw_die_ref);
6577 static int is_type_die (dw_die_ref);
6578 static int is_comdat_die (dw_die_ref);
6579 static int is_symbol_die (dw_die_ref);
6580 static void assign_symbol_names (dw_die_ref);
6581 static void break_out_includes (dw_die_ref);
6582 static int is_declaration_die (dw_die_ref);
6583 static int should_move_die_to_comdat (dw_die_ref);
6584 static dw_die_ref clone_as_declaration (dw_die_ref);
6585 static dw_die_ref clone_die (dw_die_ref);
6586 static dw_die_ref clone_tree (dw_die_ref);
6587 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6588 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6589 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6590 static dw_die_ref generate_skeleton (dw_die_ref);
6591 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6592                                                          dw_die_ref);
6593 static void break_out_comdat_types (dw_die_ref);
6594 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6595 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6596 static void copy_decls_for_unworthy_types (dw_die_ref);
6597
6598 static hashval_t htab_cu_hash (const void *);
6599 static int htab_cu_eq (const void *, const void *);
6600 static void htab_cu_del (void *);
6601 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6602 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6603 static void add_sibling_attributes (dw_die_ref);
6604 static void build_abbrev_table (dw_die_ref);
6605 static void output_location_lists (dw_die_ref);
6606 static int constant_size (unsigned HOST_WIDE_INT);
6607 static unsigned long size_of_die (dw_die_ref);
6608 static void calc_die_sizes (dw_die_ref);
6609 static void calc_base_type_die_sizes (void);
6610 static void mark_dies (dw_die_ref);
6611 static void unmark_dies (dw_die_ref);
6612 static void unmark_all_dies (dw_die_ref);
6613 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6614 static unsigned long size_of_aranges (void);
6615 static enum dwarf_form value_format (dw_attr_ref);
6616 static void output_value_format (dw_attr_ref);
6617 static void output_abbrev_section (void);
6618 static void output_die_symbol (dw_die_ref);
6619 static void output_die (dw_die_ref);
6620 static void output_compilation_unit_header (void);
6621 static void output_comp_unit (dw_die_ref, int);
6622 static void output_comdat_type_unit (comdat_type_node *);
6623 static const char *dwarf2_name (tree, int);
6624 static void add_pubname (tree, dw_die_ref);
6625 static void add_pubname_string (const char *, dw_die_ref);
6626 static void add_pubtype (tree, dw_die_ref);
6627 static void output_pubnames (VEC (pubname_entry,gc) *);
6628 static void output_aranges (unsigned long);
6629 static unsigned int add_ranges_num (int);
6630 static unsigned int add_ranges (const_tree);
6631 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6632                                   bool *);
6633 static void output_ranges (void);
6634 static dw_line_info_table *new_line_info_table (void);
6635 static void output_line_info (void);
6636 static void output_file_names (void);
6637 static dw_die_ref base_type_die (tree);
6638 static int is_base_type (tree);
6639 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6640 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6641 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6642 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6643 static int type_is_enum (const_tree);
6644 static unsigned int dbx_reg_number (const_rtx);
6645 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6646 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6647 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6648                                                 enum var_init_status);
6649 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6650                                                      enum var_init_status);
6651 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6652                                          enum var_init_status);
6653 static int is_based_loc (const_rtx);
6654 static int resolve_one_addr (rtx *, void *);
6655 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6656                                                enum var_init_status);
6657 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6658                                         enum var_init_status);
6659 static dw_loc_list_ref loc_list_from_tree (tree, int);
6660 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6661 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6662 static tree field_type (const_tree);
6663 static unsigned int simple_type_align_in_bits (const_tree);
6664 static unsigned int simple_decl_align_in_bits (const_tree);
6665 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6666 static HOST_WIDE_INT field_byte_offset (const_tree);
6667 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6668                                          dw_loc_list_ref);
6669 static void add_data_member_location_attribute (dw_die_ref, tree);
6670 static bool add_const_value_attribute (dw_die_ref, rtx);
6671 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6672 static void insert_double (double_int, unsigned char *);
6673 static void insert_float (const_rtx, unsigned char *);
6674 static rtx rtl_for_decl_location (tree);
6675 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
6676                                                    enum dwarf_attribute);
6677 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6678 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6679 static void add_name_attribute (dw_die_ref, const char *);
6680 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
6681 static void add_comp_dir_attribute (dw_die_ref);
6682 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6683 static void add_subscript_info (dw_die_ref, tree, bool);
6684 static void add_byte_size_attribute (dw_die_ref, tree);
6685 static void add_bit_offset_attribute (dw_die_ref, tree);
6686 static void add_bit_size_attribute (dw_die_ref, tree);
6687 static void add_prototyped_attribute (dw_die_ref, tree);
6688 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6689 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6690 static void add_src_coords_attributes (dw_die_ref, tree);
6691 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6692 static void push_decl_scope (tree);
6693 static void pop_decl_scope (void);
6694 static dw_die_ref scope_die_for (tree, dw_die_ref);
6695 static inline int local_scope_p (dw_die_ref);
6696 static inline int class_scope_p (dw_die_ref);
6697 static inline int class_or_namespace_scope_p (dw_die_ref);
6698 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6699 static void add_calling_convention_attribute (dw_die_ref, tree);
6700 static const char *type_tag (const_tree);
6701 static tree member_declared_type (const_tree);
6702 #if 0
6703 static const char *decl_start_label (tree);
6704 #endif
6705 static void gen_array_type_die (tree, dw_die_ref);
6706 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6707 #if 0
6708 static void gen_entry_point_die (tree, dw_die_ref);
6709 #endif
6710 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6711 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6712 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6713 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6714 static void gen_formal_types_die (tree, dw_die_ref);
6715 static void gen_subprogram_die (tree, dw_die_ref);
6716 static void gen_variable_die (tree, tree, dw_die_ref);
6717 static void gen_const_die (tree, dw_die_ref);
6718 static void gen_label_die (tree, dw_die_ref);
6719 static void gen_lexical_block_die (tree, dw_die_ref, int);
6720 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6721 static void gen_field_die (tree, dw_die_ref);
6722 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6723 static dw_die_ref gen_compile_unit_die (const char *);
6724 static void gen_inheritance_die (tree, tree, dw_die_ref);
6725 static void gen_member_die (tree, dw_die_ref);
6726 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6727                                                 enum debug_info_usage);
6728 static void gen_subroutine_type_die (tree, dw_die_ref);
6729 static void gen_typedef_die (tree, dw_die_ref);
6730 static void gen_type_die (tree, dw_die_ref);
6731 static void gen_block_die (tree, dw_die_ref, int);
6732 static void decls_for_scope (tree, dw_die_ref, int);
6733 static inline int is_redundant_typedef (const_tree);
6734 static bool is_naming_typedef_decl (const_tree);
6735 static inline dw_die_ref get_context_die (tree);
6736 static void gen_namespace_die (tree, dw_die_ref);
6737 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6738 static dw_die_ref force_decl_die (tree);
6739 static dw_die_ref force_type_die (tree);
6740 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6741 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6742 static struct dwarf_file_data * lookup_filename (const char *);
6743 static void retry_incomplete_types (void);
6744 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6745 static void gen_generic_params_dies (tree);
6746 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6747 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6748 static void splice_child_die (dw_die_ref, dw_die_ref);
6749 static int file_info_cmp (const void *, const void *);
6750 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6751                                      const char *, const char *);
6752 static void output_loc_list (dw_loc_list_ref);
6753 static char *gen_internal_sym (const char *);
6754
6755 static void prune_unmark_dies (dw_die_ref);
6756 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
6757 static void prune_unused_types_mark (dw_die_ref, int);
6758 static void prune_unused_types_walk (dw_die_ref);
6759 static void prune_unused_types_walk_attribs (dw_die_ref);
6760 static void prune_unused_types_prune (dw_die_ref);
6761 static void prune_unused_types (void);
6762 static int maybe_emit_file (struct dwarf_file_data *fd);
6763 static inline const char *AT_vms_delta1 (dw_attr_ref);
6764 static inline const char *AT_vms_delta2 (dw_attr_ref);
6765 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6766                                      const char *, const char *);
6767 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6768 static void gen_remaining_tmpl_value_param_die_attribute (void);
6769 static bool generic_type_p (tree);
6770 static void schedule_generic_params_dies_gen (tree t);
6771 static void gen_scheduled_generic_parms_dies (void);
6772
6773 /* Section names used to hold DWARF debugging information.  */
6774 #ifndef DEBUG_INFO_SECTION
6775 #define DEBUG_INFO_SECTION      ".debug_info"
6776 #endif
6777 #ifndef DEBUG_ABBREV_SECTION
6778 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6779 #endif
6780 #ifndef DEBUG_ARANGES_SECTION
6781 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6782 #endif
6783 #ifndef DEBUG_MACINFO_SECTION
6784 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6785 #endif
6786 #ifndef DEBUG_LINE_SECTION
6787 #define DEBUG_LINE_SECTION      ".debug_line"
6788 #endif
6789 #ifndef DEBUG_LOC_SECTION
6790 #define DEBUG_LOC_SECTION       ".debug_loc"
6791 #endif
6792 #ifndef DEBUG_PUBNAMES_SECTION
6793 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6794 #endif
6795 #ifndef DEBUG_PUBTYPES_SECTION
6796 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6797 #endif
6798 #ifndef DEBUG_STR_SECTION
6799 #define DEBUG_STR_SECTION       ".debug_str"
6800 #endif
6801 #ifndef DEBUG_RANGES_SECTION
6802 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6803 #endif
6804
6805 /* Standard ELF section names for compiled code and data.  */
6806 #ifndef TEXT_SECTION_NAME
6807 #define TEXT_SECTION_NAME       ".text"
6808 #endif
6809
6810 /* Section flags for .debug_str section.  */
6811 #define DEBUG_STR_SECTION_FLAGS \
6812   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6813    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6814    : SECTION_DEBUG)
6815
6816 /* Labels we insert at beginning sections we can reference instead of
6817    the section names themselves.  */
6818
6819 #ifndef TEXT_SECTION_LABEL
6820 #define TEXT_SECTION_LABEL              "Ltext"
6821 #endif
6822 #ifndef COLD_TEXT_SECTION_LABEL
6823 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6824 #endif
6825 #ifndef DEBUG_LINE_SECTION_LABEL
6826 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6827 #endif
6828 #ifndef DEBUG_INFO_SECTION_LABEL
6829 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6830 #endif
6831 #ifndef DEBUG_ABBREV_SECTION_LABEL
6832 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6833 #endif
6834 #ifndef DEBUG_LOC_SECTION_LABEL
6835 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6836 #endif
6837 #ifndef DEBUG_RANGES_SECTION_LABEL
6838 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6839 #endif
6840 #ifndef DEBUG_MACINFO_SECTION_LABEL
6841 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6842 #endif
6843
6844
6845 /* Definitions of defaults for formats and names of various special
6846    (artificial) labels which may be generated within this file (when the -g
6847    options is used and DWARF2_DEBUGGING_INFO is in effect.
6848    If necessary, these may be overridden from within the tm.h file, but
6849    typically, overriding these defaults is unnecessary.  */
6850
6851 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6852 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6853 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6854 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6855 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6856 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6857 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6858 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6859 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6860 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6861
6862 #ifndef TEXT_END_LABEL
6863 #define TEXT_END_LABEL          "Letext"
6864 #endif
6865 #ifndef COLD_END_LABEL
6866 #define COLD_END_LABEL          "Letext_cold"
6867 #endif
6868 #ifndef BLOCK_BEGIN_LABEL
6869 #define BLOCK_BEGIN_LABEL       "LBB"
6870 #endif
6871 #ifndef BLOCK_END_LABEL
6872 #define BLOCK_END_LABEL         "LBE"
6873 #endif
6874 #ifndef LINE_CODE_LABEL
6875 #define LINE_CODE_LABEL         "LM"
6876 #endif
6877
6878 \f
6879 /* Return the root of the DIE's built for the current compilation unit.  */
6880 static dw_die_ref
6881 comp_unit_die (void)
6882 {
6883   if (!single_comp_unit_die)
6884     single_comp_unit_die = gen_compile_unit_die (NULL);
6885   return single_comp_unit_die;
6886 }
6887
6888 /* We allow a language front-end to designate a function that is to be
6889    called to "demangle" any name before it is put into a DIE.  */
6890
6891 static const char *(*demangle_name_func) (const char *);
6892
6893 void
6894 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6895 {
6896   demangle_name_func = func;
6897 }
6898
6899 /* Test if rtl node points to a pseudo register.  */
6900
6901 static inline int
6902 is_pseudo_reg (const_rtx rtl)
6903 {
6904   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6905           || (GET_CODE (rtl) == SUBREG
6906               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6907 }
6908
6909 /* Return a reference to a type, with its const and volatile qualifiers
6910    removed.  */
6911
6912 static inline tree
6913 type_main_variant (tree type)
6914 {
6915   type = TYPE_MAIN_VARIANT (type);
6916
6917   /* ??? There really should be only one main variant among any group of
6918      variants of a given type (and all of the MAIN_VARIANT values for all
6919      members of the group should point to that one type) but sometimes the C
6920      front-end messes this up for array types, so we work around that bug
6921      here.  */
6922   if (TREE_CODE (type) == ARRAY_TYPE)
6923     while (type != TYPE_MAIN_VARIANT (type))
6924       type = TYPE_MAIN_VARIANT (type);
6925
6926   return type;
6927 }
6928
6929 /* Return nonzero if the given type node represents a tagged type.  */
6930
6931 static inline int
6932 is_tagged_type (const_tree type)
6933 {
6934   enum tree_code code = TREE_CODE (type);
6935
6936   return (code == RECORD_TYPE || code == UNION_TYPE
6937           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6938 }
6939
6940 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
6941
6942 static void
6943 get_ref_die_offset_label (char *label, dw_die_ref ref)
6944 {
6945   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
6946 }
6947
6948 /* Return die_offset of a DIE reference to a base type.  */
6949
6950 static unsigned long int
6951 get_base_type_offset (dw_die_ref ref)
6952 {
6953   if (ref->die_offset)
6954     return ref->die_offset;
6955   if (comp_unit_die ()->die_abbrev)
6956     {
6957       calc_base_type_die_sizes ();
6958       gcc_assert (ref->die_offset);
6959     }
6960   return ref->die_offset;
6961 }
6962
6963 /* Convert a DIE tag into its string name.  */
6964
6965 static const char *
6966 dwarf_tag_name (unsigned int tag)
6967 {
6968   switch (tag)
6969     {
6970     case DW_TAG_padding:
6971       return "DW_TAG_padding";
6972     case DW_TAG_array_type:
6973       return "DW_TAG_array_type";
6974     case DW_TAG_class_type:
6975       return "DW_TAG_class_type";
6976     case DW_TAG_entry_point:
6977       return "DW_TAG_entry_point";
6978     case DW_TAG_enumeration_type:
6979       return "DW_TAG_enumeration_type";
6980     case DW_TAG_formal_parameter:
6981       return "DW_TAG_formal_parameter";
6982     case DW_TAG_imported_declaration:
6983       return "DW_TAG_imported_declaration";
6984     case DW_TAG_label:
6985       return "DW_TAG_label";
6986     case DW_TAG_lexical_block:
6987       return "DW_TAG_lexical_block";
6988     case DW_TAG_member:
6989       return "DW_TAG_member";
6990     case DW_TAG_pointer_type:
6991       return "DW_TAG_pointer_type";
6992     case DW_TAG_reference_type:
6993       return "DW_TAG_reference_type";
6994     case DW_TAG_compile_unit:
6995       return "DW_TAG_compile_unit";
6996     case DW_TAG_string_type:
6997       return "DW_TAG_string_type";
6998     case DW_TAG_structure_type:
6999       return "DW_TAG_structure_type";
7000     case DW_TAG_subroutine_type:
7001       return "DW_TAG_subroutine_type";
7002     case DW_TAG_typedef:
7003       return "DW_TAG_typedef";
7004     case DW_TAG_union_type:
7005       return "DW_TAG_union_type";
7006     case DW_TAG_unspecified_parameters:
7007       return "DW_TAG_unspecified_parameters";
7008     case DW_TAG_variant:
7009       return "DW_TAG_variant";
7010     case DW_TAG_common_block:
7011       return "DW_TAG_common_block";
7012     case DW_TAG_common_inclusion:
7013       return "DW_TAG_common_inclusion";
7014     case DW_TAG_inheritance:
7015       return "DW_TAG_inheritance";
7016     case DW_TAG_inlined_subroutine:
7017       return "DW_TAG_inlined_subroutine";
7018     case DW_TAG_module:
7019       return "DW_TAG_module";
7020     case DW_TAG_ptr_to_member_type:
7021       return "DW_TAG_ptr_to_member_type";
7022     case DW_TAG_set_type:
7023       return "DW_TAG_set_type";
7024     case DW_TAG_subrange_type:
7025       return "DW_TAG_subrange_type";
7026     case DW_TAG_with_stmt:
7027       return "DW_TAG_with_stmt";
7028     case DW_TAG_access_declaration:
7029       return "DW_TAG_access_declaration";
7030     case DW_TAG_base_type:
7031       return "DW_TAG_base_type";
7032     case DW_TAG_catch_block:
7033       return "DW_TAG_catch_block";
7034     case DW_TAG_const_type:
7035       return "DW_TAG_const_type";
7036     case DW_TAG_constant:
7037       return "DW_TAG_constant";
7038     case DW_TAG_enumerator:
7039       return "DW_TAG_enumerator";
7040     case DW_TAG_file_type:
7041       return "DW_TAG_file_type";
7042     case DW_TAG_friend:
7043       return "DW_TAG_friend";
7044     case DW_TAG_namelist:
7045       return "DW_TAG_namelist";
7046     case DW_TAG_namelist_item:
7047       return "DW_TAG_namelist_item";
7048     case DW_TAG_packed_type:
7049       return "DW_TAG_packed_type";
7050     case DW_TAG_subprogram:
7051       return "DW_TAG_subprogram";
7052     case DW_TAG_template_type_param:
7053       return "DW_TAG_template_type_param";
7054     case DW_TAG_template_value_param:
7055       return "DW_TAG_template_value_param";
7056     case DW_TAG_thrown_type:
7057       return "DW_TAG_thrown_type";
7058     case DW_TAG_try_block:
7059       return "DW_TAG_try_block";
7060     case DW_TAG_variant_part:
7061       return "DW_TAG_variant_part";
7062     case DW_TAG_variable:
7063       return "DW_TAG_variable";
7064     case DW_TAG_volatile_type:
7065       return "DW_TAG_volatile_type";
7066     case DW_TAG_dwarf_procedure:
7067       return "DW_TAG_dwarf_procedure";
7068     case DW_TAG_restrict_type:
7069       return "DW_TAG_restrict_type";
7070     case DW_TAG_interface_type:
7071       return "DW_TAG_interface_type";
7072     case DW_TAG_namespace:
7073       return "DW_TAG_namespace";
7074     case DW_TAG_imported_module:
7075       return "DW_TAG_imported_module";
7076     case DW_TAG_unspecified_type:
7077       return "DW_TAG_unspecified_type";
7078     case DW_TAG_partial_unit:
7079       return "DW_TAG_partial_unit";
7080     case DW_TAG_imported_unit:
7081       return "DW_TAG_imported_unit";
7082     case DW_TAG_condition:
7083       return "DW_TAG_condition";
7084     case DW_TAG_shared_type:
7085       return "DW_TAG_shared_type";
7086     case DW_TAG_type_unit:
7087       return "DW_TAG_type_unit";
7088     case DW_TAG_rvalue_reference_type:
7089       return "DW_TAG_rvalue_reference_type";
7090     case DW_TAG_template_alias:
7091       return "DW_TAG_template_alias";
7092     case DW_TAG_GNU_template_parameter_pack:
7093       return "DW_TAG_GNU_template_parameter_pack";
7094     case DW_TAG_GNU_formal_parameter_pack:
7095       return "DW_TAG_GNU_formal_parameter_pack";
7096     case DW_TAG_MIPS_loop:
7097       return "DW_TAG_MIPS_loop";
7098     case DW_TAG_format_label:
7099       return "DW_TAG_format_label";
7100     case DW_TAG_function_template:
7101       return "DW_TAG_function_template";
7102     case DW_TAG_class_template:
7103       return "DW_TAG_class_template";
7104     case DW_TAG_GNU_BINCL:
7105       return "DW_TAG_GNU_BINCL";
7106     case DW_TAG_GNU_EINCL:
7107       return "DW_TAG_GNU_EINCL";
7108     case DW_TAG_GNU_template_template_param:
7109       return "DW_TAG_GNU_template_template_param";
7110     case DW_TAG_GNU_call_site:
7111       return "DW_TAG_GNU_call_site";
7112     case DW_TAG_GNU_call_site_parameter:
7113       return "DW_TAG_GNU_call_site_parameter";
7114     default:
7115       return "DW_TAG_<unknown>";
7116     }
7117 }
7118
7119 /* Convert a DWARF attribute code into its string name.  */
7120
7121 static const char *
7122 dwarf_attr_name (unsigned int attr)
7123 {
7124   switch (attr)
7125     {
7126     case DW_AT_sibling:
7127       return "DW_AT_sibling";
7128     case DW_AT_location:
7129       return "DW_AT_location";
7130     case DW_AT_name:
7131       return "DW_AT_name";
7132     case DW_AT_ordering:
7133       return "DW_AT_ordering";
7134     case DW_AT_subscr_data:
7135       return "DW_AT_subscr_data";
7136     case DW_AT_byte_size:
7137       return "DW_AT_byte_size";
7138     case DW_AT_bit_offset:
7139       return "DW_AT_bit_offset";
7140     case DW_AT_bit_size:
7141       return "DW_AT_bit_size";
7142     case DW_AT_element_list:
7143       return "DW_AT_element_list";
7144     case DW_AT_stmt_list:
7145       return "DW_AT_stmt_list";
7146     case DW_AT_low_pc:
7147       return "DW_AT_low_pc";
7148     case DW_AT_high_pc:
7149       return "DW_AT_high_pc";
7150     case DW_AT_language:
7151       return "DW_AT_language";
7152     case DW_AT_member:
7153       return "DW_AT_member";
7154     case DW_AT_discr:
7155       return "DW_AT_discr";
7156     case DW_AT_discr_value:
7157       return "DW_AT_discr_value";
7158     case DW_AT_visibility:
7159       return "DW_AT_visibility";
7160     case DW_AT_import:
7161       return "DW_AT_import";
7162     case DW_AT_string_length:
7163       return "DW_AT_string_length";
7164     case DW_AT_common_reference:
7165       return "DW_AT_common_reference";
7166     case DW_AT_comp_dir:
7167       return "DW_AT_comp_dir";
7168     case DW_AT_const_value:
7169       return "DW_AT_const_value";
7170     case DW_AT_containing_type:
7171       return "DW_AT_containing_type";
7172     case DW_AT_default_value:
7173       return "DW_AT_default_value";
7174     case DW_AT_inline:
7175       return "DW_AT_inline";
7176     case DW_AT_is_optional:
7177       return "DW_AT_is_optional";
7178     case DW_AT_lower_bound:
7179       return "DW_AT_lower_bound";
7180     case DW_AT_producer:
7181       return "DW_AT_producer";
7182     case DW_AT_prototyped:
7183       return "DW_AT_prototyped";
7184     case DW_AT_return_addr:
7185       return "DW_AT_return_addr";
7186     case DW_AT_start_scope:
7187       return "DW_AT_start_scope";
7188     case DW_AT_bit_stride:
7189       return "DW_AT_bit_stride";
7190     case DW_AT_upper_bound:
7191       return "DW_AT_upper_bound";
7192     case DW_AT_abstract_origin:
7193       return "DW_AT_abstract_origin";
7194     case DW_AT_accessibility:
7195       return "DW_AT_accessibility";
7196     case DW_AT_address_class:
7197       return "DW_AT_address_class";
7198     case DW_AT_artificial:
7199       return "DW_AT_artificial";
7200     case DW_AT_base_types:
7201       return "DW_AT_base_types";
7202     case DW_AT_calling_convention:
7203       return "DW_AT_calling_convention";
7204     case DW_AT_count:
7205       return "DW_AT_count";
7206     case DW_AT_data_member_location:
7207       return "DW_AT_data_member_location";
7208     case DW_AT_decl_column:
7209       return "DW_AT_decl_column";
7210     case DW_AT_decl_file:
7211       return "DW_AT_decl_file";
7212     case DW_AT_decl_line:
7213       return "DW_AT_decl_line";
7214     case DW_AT_declaration:
7215       return "DW_AT_declaration";
7216     case DW_AT_discr_list:
7217       return "DW_AT_discr_list";
7218     case DW_AT_encoding:
7219       return "DW_AT_encoding";
7220     case DW_AT_external:
7221       return "DW_AT_external";
7222     case DW_AT_explicit:
7223       return "DW_AT_explicit";
7224     case DW_AT_frame_base:
7225       return "DW_AT_frame_base";
7226     case DW_AT_friend:
7227       return "DW_AT_friend";
7228     case DW_AT_identifier_case:
7229       return "DW_AT_identifier_case";
7230     case DW_AT_macro_info:
7231       return "DW_AT_macro_info";
7232     case DW_AT_namelist_items:
7233       return "DW_AT_namelist_items";
7234     case DW_AT_priority:
7235       return "DW_AT_priority";
7236     case DW_AT_segment:
7237       return "DW_AT_segment";
7238     case DW_AT_specification:
7239       return "DW_AT_specification";
7240     case DW_AT_static_link:
7241       return "DW_AT_static_link";
7242     case DW_AT_type:
7243       return "DW_AT_type";
7244     case DW_AT_use_location:
7245       return "DW_AT_use_location";
7246     case DW_AT_variable_parameter:
7247       return "DW_AT_variable_parameter";
7248     case DW_AT_virtuality:
7249       return "DW_AT_virtuality";
7250     case DW_AT_vtable_elem_location:
7251       return "DW_AT_vtable_elem_location";
7252
7253     case DW_AT_allocated:
7254       return "DW_AT_allocated";
7255     case DW_AT_associated:
7256       return "DW_AT_associated";
7257     case DW_AT_data_location:
7258       return "DW_AT_data_location";
7259     case DW_AT_byte_stride:
7260       return "DW_AT_byte_stride";
7261     case DW_AT_entry_pc:
7262       return "DW_AT_entry_pc";
7263     case DW_AT_use_UTF8:
7264       return "DW_AT_use_UTF8";
7265     case DW_AT_extension:
7266       return "DW_AT_extension";
7267     case DW_AT_ranges:
7268       return "DW_AT_ranges";
7269     case DW_AT_trampoline:
7270       return "DW_AT_trampoline";
7271     case DW_AT_call_column:
7272       return "DW_AT_call_column";
7273     case DW_AT_call_file:
7274       return "DW_AT_call_file";
7275     case DW_AT_call_line:
7276       return "DW_AT_call_line";
7277     case DW_AT_object_pointer:
7278       return "DW_AT_object_pointer";
7279
7280     case DW_AT_signature:
7281       return "DW_AT_signature";
7282     case DW_AT_main_subprogram:
7283       return "DW_AT_main_subprogram";
7284     case DW_AT_data_bit_offset:
7285       return "DW_AT_data_bit_offset";
7286     case DW_AT_const_expr:
7287       return "DW_AT_const_expr";
7288     case DW_AT_enum_class:
7289       return "DW_AT_enum_class";
7290     case DW_AT_linkage_name:
7291       return "DW_AT_linkage_name";
7292
7293     case DW_AT_MIPS_fde:
7294       return "DW_AT_MIPS_fde";
7295     case DW_AT_MIPS_loop_begin:
7296       return "DW_AT_MIPS_loop_begin";
7297     case DW_AT_MIPS_tail_loop_begin:
7298       return "DW_AT_MIPS_tail_loop_begin";
7299     case DW_AT_MIPS_epilog_begin:
7300       return "DW_AT_MIPS_epilog_begin";
7301 #if VMS_DEBUGGING_INFO
7302     case DW_AT_HP_prologue:
7303       return "DW_AT_HP_prologue";
7304 #else
7305     case DW_AT_MIPS_loop_unroll_factor:
7306       return "DW_AT_MIPS_loop_unroll_factor";
7307 #endif
7308     case DW_AT_MIPS_software_pipeline_depth:
7309       return "DW_AT_MIPS_software_pipeline_depth";
7310     case DW_AT_MIPS_linkage_name:
7311       return "DW_AT_MIPS_linkage_name";
7312 #if VMS_DEBUGGING_INFO
7313     case DW_AT_HP_epilogue:
7314       return "DW_AT_HP_epilogue";
7315 #else
7316     case DW_AT_MIPS_stride:
7317       return "DW_AT_MIPS_stride";
7318 #endif
7319     case DW_AT_MIPS_abstract_name:
7320       return "DW_AT_MIPS_abstract_name";
7321     case DW_AT_MIPS_clone_origin:
7322       return "DW_AT_MIPS_clone_origin";
7323     case DW_AT_MIPS_has_inlines:
7324       return "DW_AT_MIPS_has_inlines";
7325
7326     case DW_AT_sf_names:
7327       return "DW_AT_sf_names";
7328     case DW_AT_src_info:
7329       return "DW_AT_src_info";
7330     case DW_AT_mac_info:
7331       return "DW_AT_mac_info";
7332     case DW_AT_src_coords:
7333       return "DW_AT_src_coords";
7334     case DW_AT_body_begin:
7335       return "DW_AT_body_begin";
7336     case DW_AT_body_end:
7337       return "DW_AT_body_end";
7338
7339     case DW_AT_GNU_vector:
7340       return "DW_AT_GNU_vector";
7341     case DW_AT_GNU_guarded_by:
7342       return "DW_AT_GNU_guarded_by";
7343     case DW_AT_GNU_pt_guarded_by:
7344       return "DW_AT_GNU_pt_guarded_by";
7345     case DW_AT_GNU_guarded:
7346       return "DW_AT_GNU_guarded";
7347     case DW_AT_GNU_pt_guarded:
7348       return "DW_AT_GNU_pt_guarded";
7349     case DW_AT_GNU_locks_excluded:
7350       return "DW_AT_GNU_locks_excluded";
7351     case DW_AT_GNU_exclusive_locks_required:
7352       return "DW_AT_GNU_exclusive_locks_required";
7353     case DW_AT_GNU_shared_locks_required:
7354       return "DW_AT_GNU_shared_locks_required";
7355     case DW_AT_GNU_odr_signature:
7356       return "DW_AT_GNU_odr_signature";
7357     case DW_AT_GNU_template_name:
7358       return "DW_AT_GNU_template_name";
7359     case DW_AT_GNU_call_site_value:
7360       return "DW_AT_GNU_call_site_value";
7361     case DW_AT_GNU_call_site_data_value:
7362       return "DW_AT_GNU_call_site_data_value";
7363     case DW_AT_GNU_call_site_target:
7364       return "DW_AT_GNU_call_site_target";
7365     case DW_AT_GNU_call_site_target_clobbered:
7366       return "DW_AT_GNU_call_site_target_clobbered";
7367     case DW_AT_GNU_tail_call:
7368       return "DW_AT_GNU_tail_call";
7369     case DW_AT_GNU_all_tail_call_sites:
7370       return "DW_AT_GNU_all_tail_call_sites";
7371     case DW_AT_GNU_all_call_sites:
7372       return "DW_AT_GNU_all_call_sites";
7373     case DW_AT_GNU_all_source_call_sites:
7374       return "DW_AT_GNU_all_source_call_sites";
7375
7376     case DW_AT_GNAT_descriptive_type:
7377       return "DW_AT_GNAT_descriptive_type";
7378
7379     case DW_AT_VMS_rtnbeg_pd_address:
7380       return "DW_AT_VMS_rtnbeg_pd_address";
7381
7382     default:
7383       return "DW_AT_<unknown>";
7384     }
7385 }
7386
7387 /* Convert a DWARF value form code into its string name.  */
7388
7389 static const char *
7390 dwarf_form_name (unsigned int form)
7391 {
7392   switch (form)
7393     {
7394     case DW_FORM_addr:
7395       return "DW_FORM_addr";
7396     case DW_FORM_block2:
7397       return "DW_FORM_block2";
7398     case DW_FORM_block4:
7399       return "DW_FORM_block4";
7400     case DW_FORM_data2:
7401       return "DW_FORM_data2";
7402     case DW_FORM_data4:
7403       return "DW_FORM_data4";
7404     case DW_FORM_data8:
7405       return "DW_FORM_data8";
7406     case DW_FORM_string:
7407       return "DW_FORM_string";
7408     case DW_FORM_block:
7409       return "DW_FORM_block";
7410     case DW_FORM_block1:
7411       return "DW_FORM_block1";
7412     case DW_FORM_data1:
7413       return "DW_FORM_data1";
7414     case DW_FORM_flag:
7415       return "DW_FORM_flag";
7416     case DW_FORM_sdata:
7417       return "DW_FORM_sdata";
7418     case DW_FORM_strp:
7419       return "DW_FORM_strp";
7420     case DW_FORM_udata:
7421       return "DW_FORM_udata";
7422     case DW_FORM_ref_addr:
7423       return "DW_FORM_ref_addr";
7424     case DW_FORM_ref1:
7425       return "DW_FORM_ref1";
7426     case DW_FORM_ref2:
7427       return "DW_FORM_ref2";
7428     case DW_FORM_ref4:
7429       return "DW_FORM_ref4";
7430     case DW_FORM_ref8:
7431       return "DW_FORM_ref8";
7432     case DW_FORM_ref_udata:
7433       return "DW_FORM_ref_udata";
7434     case DW_FORM_indirect:
7435       return "DW_FORM_indirect";
7436     case DW_FORM_sec_offset:
7437       return "DW_FORM_sec_offset";
7438     case DW_FORM_exprloc:
7439       return "DW_FORM_exprloc";
7440     case DW_FORM_flag_present:
7441       return "DW_FORM_flag_present";
7442     case DW_FORM_ref_sig8:
7443       return "DW_FORM_ref_sig8";
7444     default:
7445       return "DW_FORM_<unknown>";
7446     }
7447 }
7448 \f
7449 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
7450    instance of an inlined instance of a decl which is local to an inline
7451    function, so we have to trace all of the way back through the origin chain
7452    to find out what sort of node actually served as the original seed for the
7453    given block.  */
7454
7455 static tree
7456 decl_ultimate_origin (const_tree decl)
7457 {
7458   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
7459     return NULL_TREE;
7460
7461   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
7462      nodes in the function to point to themselves; ignore that if
7463      we're trying to output the abstract instance of this function.  */
7464   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
7465     return NULL_TREE;
7466
7467   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
7468      most distant ancestor, this should never happen.  */
7469   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
7470
7471   return DECL_ABSTRACT_ORIGIN (decl);
7472 }
7473
7474 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
7475    of a virtual function may refer to a base class, so we check the 'this'
7476    parameter.  */
7477
7478 static tree
7479 decl_class_context (tree decl)
7480 {
7481   tree context = NULL_TREE;
7482
7483   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
7484     context = DECL_CONTEXT (decl);
7485   else
7486     context = TYPE_MAIN_VARIANT
7487       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7488
7489   if (context && !TYPE_P (context))
7490     context = NULL_TREE;
7491
7492   return context;
7493 }
7494 \f
7495 /* Add an attribute/value pair to a DIE.  */
7496
7497 static inline void
7498 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7499 {
7500   /* Maybe this should be an assert?  */
7501   if (die == NULL)
7502     return;
7503
7504   if (die->die_attr == NULL)
7505     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7506   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7507 }
7508
7509 static inline enum dw_val_class
7510 AT_class (dw_attr_ref a)
7511 {
7512   return a->dw_attr_val.val_class;
7513 }
7514
7515 /* Add a flag value attribute to a DIE.  */
7516
7517 static inline void
7518 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7519 {
7520   dw_attr_node attr;
7521
7522   attr.dw_attr = attr_kind;
7523   attr.dw_attr_val.val_class = dw_val_class_flag;
7524   attr.dw_attr_val.v.val_flag = flag;
7525   add_dwarf_attr (die, &attr);
7526 }
7527
7528 static inline unsigned
7529 AT_flag (dw_attr_ref a)
7530 {
7531   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7532   return a->dw_attr_val.v.val_flag;
7533 }
7534
7535 /* Add a signed integer attribute value to a DIE.  */
7536
7537 static inline void
7538 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7539 {
7540   dw_attr_node attr;
7541
7542   attr.dw_attr = attr_kind;
7543   attr.dw_attr_val.val_class = dw_val_class_const;
7544   attr.dw_attr_val.v.val_int = int_val;
7545   add_dwarf_attr (die, &attr);
7546 }
7547
7548 static inline HOST_WIDE_INT
7549 AT_int (dw_attr_ref a)
7550 {
7551   gcc_assert (a && AT_class (a) == dw_val_class_const);
7552   return a->dw_attr_val.v.val_int;
7553 }
7554
7555 /* Add an unsigned integer attribute value to a DIE.  */
7556
7557 static inline void
7558 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7559                  unsigned HOST_WIDE_INT unsigned_val)
7560 {
7561   dw_attr_node attr;
7562
7563   attr.dw_attr = attr_kind;
7564   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7565   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7566   add_dwarf_attr (die, &attr);
7567 }
7568
7569 static inline unsigned HOST_WIDE_INT
7570 AT_unsigned (dw_attr_ref a)
7571 {
7572   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7573   return a->dw_attr_val.v.val_unsigned;
7574 }
7575
7576 /* Add an unsigned double integer attribute value to a DIE.  */
7577
7578 static inline void
7579 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7580                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7581 {
7582   dw_attr_node attr;
7583
7584   attr.dw_attr = attr_kind;
7585   attr.dw_attr_val.val_class = dw_val_class_const_double;
7586   attr.dw_attr_val.v.val_double.high = high;
7587   attr.dw_attr_val.v.val_double.low = low;
7588   add_dwarf_attr (die, &attr);
7589 }
7590
7591 /* Add a floating point attribute value to a DIE and return it.  */
7592
7593 static inline void
7594 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7595             unsigned int length, unsigned int elt_size, unsigned char *array)
7596 {
7597   dw_attr_node attr;
7598
7599   attr.dw_attr = attr_kind;
7600   attr.dw_attr_val.val_class = dw_val_class_vec;
7601   attr.dw_attr_val.v.val_vec.length = length;
7602   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7603   attr.dw_attr_val.v.val_vec.array = array;
7604   add_dwarf_attr (die, &attr);
7605 }
7606
7607 /* Add an 8-byte data attribute value to a DIE.  */
7608
7609 static inline void
7610 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7611               unsigned char data8[8])
7612 {
7613   dw_attr_node attr;
7614
7615   attr.dw_attr = attr_kind;
7616   attr.dw_attr_val.val_class = dw_val_class_data8;
7617   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7618   add_dwarf_attr (die, &attr);
7619 }
7620
7621 /* Hash and equality functions for debug_str_hash.  */
7622
7623 static hashval_t
7624 debug_str_do_hash (const void *x)
7625 {
7626   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7627 }
7628
7629 static int
7630 debug_str_eq (const void *x1, const void *x2)
7631 {
7632   return strcmp ((((const struct indirect_string_node *)x1)->str),
7633                  (const char *)x2) == 0;
7634 }
7635
7636 /* Add STR to the indirect string hash table.  */
7637
7638 static struct indirect_string_node *
7639 find_AT_string (const char *str)
7640 {
7641   struct indirect_string_node *node;
7642   void **slot;
7643
7644   if (! debug_str_hash)
7645     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7646                                       debug_str_eq, NULL);
7647
7648   slot = htab_find_slot_with_hash (debug_str_hash, str,
7649                                    htab_hash_string (str), INSERT);
7650   if (*slot == NULL)
7651     {
7652       node = ggc_alloc_cleared_indirect_string_node ();
7653       node->str = ggc_strdup (str);
7654       *slot = node;
7655     }
7656   else
7657     node = (struct indirect_string_node *) *slot;
7658
7659   node->refcount++;
7660   return node;
7661 }
7662
7663 /* Add a string attribute value to a DIE.  */
7664
7665 static inline void
7666 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7667 {
7668   dw_attr_node attr;
7669   struct indirect_string_node *node;
7670
7671   node = find_AT_string (str);
7672
7673   attr.dw_attr = attr_kind;
7674   attr.dw_attr_val.val_class = dw_val_class_str;
7675   attr.dw_attr_val.v.val_str = node;
7676   add_dwarf_attr (die, &attr);
7677 }
7678
7679 static inline const char *
7680 AT_string (dw_attr_ref a)
7681 {
7682   gcc_assert (a && AT_class (a) == dw_val_class_str);
7683   return a->dw_attr_val.v.val_str->str;
7684 }
7685
7686 /* Find out whether a string should be output inline in DIE
7687    or out-of-line in .debug_str section.  */
7688
7689 static enum dwarf_form
7690 AT_string_form (dw_attr_ref a)
7691 {
7692   struct indirect_string_node *node;
7693   unsigned int len;
7694   char label[32];
7695
7696   gcc_assert (a && AT_class (a) == dw_val_class_str);
7697
7698   node = a->dw_attr_val.v.val_str;
7699   if (node->form)
7700     return node->form;
7701
7702   len = strlen (node->str) + 1;
7703
7704   /* If the string is shorter or equal to the size of the reference, it is
7705      always better to put it inline.  */
7706   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7707     return node->form = DW_FORM_string;
7708
7709   /* If we cannot expect the linker to merge strings in .debug_str
7710      section, only put it into .debug_str if it is worth even in this
7711      single module.  */
7712   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7713       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7714       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7715     return node->form = DW_FORM_string;
7716
7717   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7718   ++dw2_string_counter;
7719   node->label = xstrdup (label);
7720
7721   return node->form = DW_FORM_strp;
7722 }
7723
7724 /* Add a DIE reference attribute value to a DIE.  */
7725
7726 static inline void
7727 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7728 {
7729   dw_attr_node attr;
7730
7731 #ifdef ENABLE_CHECKING
7732   gcc_assert (targ_die != NULL);
7733 #else
7734   /* With LTO we can end up trying to reference something we didn't create
7735      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
7736   if (targ_die == NULL)
7737     return;
7738 #endif
7739
7740   attr.dw_attr = attr_kind;
7741   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7742   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7743   attr.dw_attr_val.v.val_die_ref.external = 0;
7744   add_dwarf_attr (die, &attr);
7745 }
7746
7747 /* Add an AT_specification attribute to a DIE, and also make the back
7748    pointer from the specification to the definition.  */
7749
7750 static inline void
7751 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7752 {
7753   add_AT_die_ref (die, DW_AT_specification, targ_die);
7754   gcc_assert (!targ_die->die_definition);
7755   targ_die->die_definition = die;
7756 }
7757
7758 static inline dw_die_ref
7759 AT_ref (dw_attr_ref a)
7760 {
7761   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7762   return a->dw_attr_val.v.val_die_ref.die;
7763 }
7764
7765 static inline int
7766 AT_ref_external (dw_attr_ref a)
7767 {
7768   if (a && AT_class (a) == dw_val_class_die_ref)
7769     return a->dw_attr_val.v.val_die_ref.external;
7770
7771   return 0;
7772 }
7773
7774 static inline void
7775 set_AT_ref_external (dw_attr_ref a, int i)
7776 {
7777   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7778   a->dw_attr_val.v.val_die_ref.external = i;
7779 }
7780
7781 /* Add an FDE reference attribute value to a DIE.  */
7782
7783 static inline void
7784 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7785 {
7786   dw_attr_node attr;
7787
7788   attr.dw_attr = attr_kind;
7789   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7790   attr.dw_attr_val.v.val_fde_index = targ_fde;
7791   add_dwarf_attr (die, &attr);
7792 }
7793
7794 /* Add a location description attribute value to a DIE.  */
7795
7796 static inline void
7797 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7798 {
7799   dw_attr_node attr;
7800
7801   attr.dw_attr = attr_kind;
7802   attr.dw_attr_val.val_class = dw_val_class_loc;
7803   attr.dw_attr_val.v.val_loc = loc;
7804   add_dwarf_attr (die, &attr);
7805 }
7806
7807 static inline dw_loc_descr_ref
7808 AT_loc (dw_attr_ref a)
7809 {
7810   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7811   return a->dw_attr_val.v.val_loc;
7812 }
7813
7814 static inline void
7815 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7816 {
7817   dw_attr_node attr;
7818
7819   attr.dw_attr = attr_kind;
7820   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7821   attr.dw_attr_val.v.val_loc_list = loc_list;
7822   add_dwarf_attr (die, &attr);
7823   have_location_lists = true;
7824 }
7825
7826 static inline dw_loc_list_ref
7827 AT_loc_list (dw_attr_ref a)
7828 {
7829   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7830   return a->dw_attr_val.v.val_loc_list;
7831 }
7832
7833 static inline dw_loc_list_ref *
7834 AT_loc_list_ptr (dw_attr_ref a)
7835 {
7836   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7837   return &a->dw_attr_val.v.val_loc_list;
7838 }
7839
7840 /* Add an address constant attribute value to a DIE.  */
7841
7842 static inline void
7843 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7844 {
7845   dw_attr_node attr;
7846
7847   attr.dw_attr = attr_kind;
7848   attr.dw_attr_val.val_class = dw_val_class_addr;
7849   attr.dw_attr_val.v.val_addr = addr;
7850   add_dwarf_attr (die, &attr);
7851 }
7852
7853 /* Get the RTX from to an address DIE attribute.  */
7854
7855 static inline rtx
7856 AT_addr (dw_attr_ref a)
7857 {
7858   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7859   return a->dw_attr_val.v.val_addr;
7860 }
7861
7862 /* Add a file attribute value to a DIE.  */
7863
7864 static inline void
7865 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7866              struct dwarf_file_data *fd)
7867 {
7868   dw_attr_node attr;
7869
7870   attr.dw_attr = attr_kind;
7871   attr.dw_attr_val.val_class = dw_val_class_file;
7872   attr.dw_attr_val.v.val_file = fd;
7873   add_dwarf_attr (die, &attr);
7874 }
7875
7876 /* Get the dwarf_file_data from a file DIE attribute.  */
7877
7878 static inline struct dwarf_file_data *
7879 AT_file (dw_attr_ref a)
7880 {
7881   gcc_assert (a && AT_class (a) == dw_val_class_file);
7882   return a->dw_attr_val.v.val_file;
7883 }
7884
7885 /* Add a vms delta attribute value to a DIE.  */
7886
7887 static inline void
7888 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7889                   const char *lbl1, const char *lbl2)
7890 {
7891   dw_attr_node attr;
7892
7893   attr.dw_attr = attr_kind;
7894   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7895   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7896   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7897   add_dwarf_attr (die, &attr);
7898 }
7899
7900 /* Add a label identifier attribute value to a DIE.  */
7901
7902 static inline void
7903 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7904 {
7905   dw_attr_node attr;
7906
7907   attr.dw_attr = attr_kind;
7908   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7909   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7910   add_dwarf_attr (die, &attr);
7911 }
7912
7913 /* Add a section offset attribute value to a DIE, an offset into the
7914    debug_line section.  */
7915
7916 static inline void
7917 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7918                 const char *label)
7919 {
7920   dw_attr_node attr;
7921
7922   attr.dw_attr = attr_kind;
7923   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7924   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7925   add_dwarf_attr (die, &attr);
7926 }
7927
7928 /* Add a section offset attribute value to a DIE, an offset into the
7929    debug_macinfo section.  */
7930
7931 static inline void
7932 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7933                const char *label)
7934 {
7935   dw_attr_node attr;
7936
7937   attr.dw_attr = attr_kind;
7938   attr.dw_attr_val.val_class = dw_val_class_macptr;
7939   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7940   add_dwarf_attr (die, &attr);
7941 }
7942
7943 /* Add an offset attribute value to a DIE.  */
7944
7945 static inline void
7946 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7947                unsigned HOST_WIDE_INT offset)
7948 {
7949   dw_attr_node attr;
7950
7951   attr.dw_attr = attr_kind;
7952   attr.dw_attr_val.val_class = dw_val_class_offset;
7953   attr.dw_attr_val.v.val_offset = offset;
7954   add_dwarf_attr (die, &attr);
7955 }
7956
7957 /* Add an range_list attribute value to a DIE.  */
7958
7959 static void
7960 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7961                    long unsigned int offset)
7962 {
7963   dw_attr_node attr;
7964
7965   attr.dw_attr = attr_kind;
7966   attr.dw_attr_val.val_class = dw_val_class_range_list;
7967   attr.dw_attr_val.v.val_offset = offset;
7968   add_dwarf_attr (die, &attr);
7969 }
7970
7971 /* Return the start label of a delta attribute.  */
7972
7973 static inline const char *
7974 AT_vms_delta1 (dw_attr_ref a)
7975 {
7976   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7977   return a->dw_attr_val.v.val_vms_delta.lbl1;
7978 }
7979
7980 /* Return the end label of a delta attribute.  */
7981
7982 static inline const char *
7983 AT_vms_delta2 (dw_attr_ref a)
7984 {
7985   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7986   return a->dw_attr_val.v.val_vms_delta.lbl2;
7987 }
7988
7989 static inline const char *
7990 AT_lbl (dw_attr_ref a)
7991 {
7992   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7993                     || AT_class (a) == dw_val_class_lineptr
7994                     || AT_class (a) == dw_val_class_macptr));
7995   return a->dw_attr_val.v.val_lbl_id;
7996 }
7997
7998 /* Get the attribute of type attr_kind.  */
7999
8000 static dw_attr_ref
8001 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8002 {
8003   dw_attr_ref a;
8004   unsigned ix;
8005   dw_die_ref spec = NULL;
8006
8007   if (! die)
8008     return NULL;
8009
8010   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8011     if (a->dw_attr == attr_kind)
8012       return a;
8013     else if (a->dw_attr == DW_AT_specification
8014              || a->dw_attr == DW_AT_abstract_origin)
8015       spec = AT_ref (a);
8016
8017   if (spec)
8018     return get_AT (spec, attr_kind);
8019
8020   return NULL;
8021 }
8022
8023 /* Return the "low pc" attribute value, typically associated with a subprogram
8024    DIE.  Return null if the "low pc" attribute is either not present, or if it
8025    cannot be represented as an assembler label identifier.  */
8026
8027 static inline const char *
8028 get_AT_low_pc (dw_die_ref die)
8029 {
8030   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
8031
8032   return a ? AT_lbl (a) : NULL;
8033 }
8034
8035 /* Return the "high pc" attribute value, typically associated with a subprogram
8036    DIE.  Return null if the "high pc" attribute is either not present, or if it
8037    cannot be represented as an assembler label identifier.  */
8038
8039 static inline const char *
8040 get_AT_hi_pc (dw_die_ref die)
8041 {
8042   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
8043
8044   return a ? AT_lbl (a) : NULL;
8045 }
8046
8047 /* Return the value of the string attribute designated by ATTR_KIND, or
8048    NULL if it is not present.  */
8049
8050 static inline const char *
8051 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
8052 {
8053   dw_attr_ref a = get_AT (die, attr_kind);
8054
8055   return a ? AT_string (a) : NULL;
8056 }
8057
8058 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
8059    if it is not present.  */
8060
8061 static inline int
8062 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
8063 {
8064   dw_attr_ref a = get_AT (die, attr_kind);
8065
8066   return a ? AT_flag (a) : 0;
8067 }
8068
8069 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
8070    if it is not present.  */
8071
8072 static inline unsigned
8073 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
8074 {
8075   dw_attr_ref a = get_AT (die, attr_kind);
8076
8077   return a ? AT_unsigned (a) : 0;
8078 }
8079
8080 static inline dw_die_ref
8081 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
8082 {
8083   dw_attr_ref a = get_AT (die, attr_kind);
8084
8085   return a ? AT_ref (a) : NULL;
8086 }
8087
8088 static inline struct dwarf_file_data *
8089 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
8090 {
8091   dw_attr_ref a = get_AT (die, attr_kind);
8092
8093   return a ? AT_file (a) : NULL;
8094 }
8095
8096 /* Return TRUE if the language is C++.  */
8097
8098 static inline bool
8099 is_cxx (void)
8100 {
8101   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8102
8103   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
8104 }
8105
8106 /* Return TRUE if the language is Fortran.  */
8107
8108 static inline bool
8109 is_fortran (void)
8110 {
8111   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8112
8113   return (lang == DW_LANG_Fortran77
8114           || lang == DW_LANG_Fortran90
8115           || lang == DW_LANG_Fortran95);
8116 }
8117
8118 /* Return TRUE if the language is Ada.  */
8119
8120 static inline bool
8121 is_ada (void)
8122 {
8123   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
8124
8125   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
8126 }
8127
8128 /* Remove the specified attribute if present.  */
8129
8130 static void
8131 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
8132 {
8133   dw_attr_ref a;
8134   unsigned ix;
8135
8136   if (! die)
8137     return;
8138
8139   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8140     if (a->dw_attr == attr_kind)
8141       {
8142         if (AT_class (a) == dw_val_class_str)
8143           if (a->dw_attr_val.v.val_str->refcount)
8144             a->dw_attr_val.v.val_str->refcount--;
8145
8146         /* VEC_ordered_remove should help reduce the number of abbrevs
8147            that are needed.  */
8148         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
8149         return;
8150       }
8151 }
8152
8153 /* Remove CHILD from its parent.  PREV must have the property that
8154    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
8155
8156 static void
8157 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
8158 {
8159   gcc_assert (child->die_parent == prev->die_parent);
8160   gcc_assert (prev->die_sib == child);
8161   if (prev == child)
8162     {
8163       gcc_assert (child->die_parent->die_child == child);
8164       prev = NULL;
8165     }
8166   else
8167     prev->die_sib = child->die_sib;
8168   if (child->die_parent->die_child == child)
8169     child->die_parent->die_child = prev;
8170 }
8171
8172 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
8173    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
8174
8175 static void
8176 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
8177 {
8178   dw_die_ref parent = old_child->die_parent;
8179
8180   gcc_assert (parent == prev->die_parent);
8181   gcc_assert (prev->die_sib == old_child);
8182
8183   new_child->die_parent = parent;
8184   if (prev == old_child)
8185     {
8186       gcc_assert (parent->die_child == old_child);
8187       new_child->die_sib = new_child;
8188     }
8189   else
8190     {
8191       prev->die_sib = new_child;
8192       new_child->die_sib = old_child->die_sib;
8193     }
8194   if (old_child->die_parent->die_child == old_child)
8195     old_child->die_parent->die_child = new_child;
8196 }
8197
8198 /* Move all children from OLD_PARENT to NEW_PARENT.  */
8199
8200 static void
8201 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
8202 {
8203   dw_die_ref c;
8204   new_parent->die_child = old_parent->die_child;
8205   old_parent->die_child = NULL;
8206   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
8207 }
8208
8209 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
8210    matches TAG.  */
8211
8212 static void
8213 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
8214 {
8215   dw_die_ref c;
8216
8217   c = die->die_child;
8218   if (c) do {
8219     dw_die_ref prev = c;
8220     c = c->die_sib;
8221     while (c->die_tag == tag)
8222       {
8223         remove_child_with_prev (c, prev);
8224         /* Might have removed every child.  */
8225         if (c == c->die_sib)
8226           return;
8227         c = c->die_sib;
8228       }
8229   } while (c != die->die_child);
8230 }
8231
8232 /* Add a CHILD_DIE as the last child of DIE.  */
8233
8234 static void
8235 add_child_die (dw_die_ref die, dw_die_ref child_die)
8236 {
8237   /* FIXME this should probably be an assert.  */
8238   if (! die || ! child_die)
8239     return;
8240   gcc_assert (die != child_die);
8241
8242   child_die->die_parent = die;
8243   if (die->die_child)
8244     {
8245       child_die->die_sib = die->die_child->die_sib;
8246       die->die_child->die_sib = child_die;
8247     }
8248   else
8249     child_die->die_sib = child_die;
8250   die->die_child = child_die;
8251 }
8252
8253 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
8254    is the specification, to the end of PARENT's list of children.
8255    This is done by removing and re-adding it.  */
8256
8257 static void
8258 splice_child_die (dw_die_ref parent, dw_die_ref child)
8259 {
8260   dw_die_ref p;
8261
8262   /* We want the declaration DIE from inside the class, not the
8263      specification DIE at toplevel.  */
8264   if (child->die_parent != parent)
8265     {
8266       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
8267
8268       if (tmp)
8269         child = tmp;
8270     }
8271
8272   gcc_assert (child->die_parent == parent
8273               || (child->die_parent
8274                   == get_AT_ref (parent, DW_AT_specification)));
8275
8276   for (p = child->die_parent->die_child; ; p = p->die_sib)
8277     if (p->die_sib == child)
8278       {
8279         remove_child_with_prev (child, p);
8280         break;
8281       }
8282
8283   add_child_die (parent, child);
8284 }
8285
8286 /* Return a pointer to a newly created DIE node.  */
8287
8288 static inline dw_die_ref
8289 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
8290 {
8291   dw_die_ref die = ggc_alloc_cleared_die_node ();
8292
8293   die->die_tag = tag_value;
8294
8295   if (parent_die != NULL)
8296     add_child_die (parent_die, die);
8297   else
8298     {
8299       limbo_die_node *limbo_node;
8300
8301       limbo_node = ggc_alloc_cleared_limbo_die_node ();
8302       limbo_node->die = die;
8303       limbo_node->created_for = t;
8304       limbo_node->next = limbo_die_list;
8305       limbo_die_list = limbo_node;
8306     }
8307
8308   return die;
8309 }
8310
8311 /* Return the DIE associated with the given type specifier.  */
8312
8313 static inline dw_die_ref
8314 lookup_type_die (tree type)
8315 {
8316   return TYPE_SYMTAB_DIE (type);
8317 }
8318
8319 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
8320    anonymous type named by the typedef TYPE_DIE, return the DIE of the
8321    anonymous type instead the one of the naming typedef.  */
8322
8323 static inline dw_die_ref
8324 strip_naming_typedef (tree type, dw_die_ref type_die)
8325 {
8326   if (type
8327       && TREE_CODE (type) == RECORD_TYPE
8328       && type_die
8329       && type_die->die_tag == DW_TAG_typedef
8330       && is_naming_typedef_decl (TYPE_NAME (type)))
8331     type_die = get_AT_ref (type_die, DW_AT_type);
8332   return type_die;
8333 }
8334
8335 /* Like lookup_type_die, but if type is an anonymous type named by a
8336    typedef[1], return the DIE of the anonymous type instead the one of
8337    the naming typedef.  This is because in gen_typedef_die, we did
8338    equate the anonymous struct named by the typedef with the DIE of
8339    the naming typedef. So by default, lookup_type_die on an anonymous
8340    struct yields the DIE of the naming typedef.
8341
8342    [1]: Read the comment of is_naming_typedef_decl to learn about what
8343    a naming typedef is.  */
8344
8345 static inline dw_die_ref
8346 lookup_type_die_strip_naming_typedef (tree type)
8347 {
8348   dw_die_ref die = lookup_type_die (type);
8349   return strip_naming_typedef (type, die);
8350 }
8351
8352 /* Equate a DIE to a given type specifier.  */
8353
8354 static inline void
8355 equate_type_number_to_die (tree type, dw_die_ref type_die)
8356 {
8357   TYPE_SYMTAB_DIE (type) = type_die;
8358 }
8359
8360 /* Returns a hash value for X (which really is a die_struct).  */
8361
8362 static hashval_t
8363 decl_die_table_hash (const void *x)
8364 {
8365   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
8366 }
8367
8368 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
8369
8370 static int
8371 decl_die_table_eq (const void *x, const void *y)
8372 {
8373   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
8374 }
8375
8376 /* Return the DIE associated with a given declaration.  */
8377
8378 static inline dw_die_ref
8379 lookup_decl_die (tree decl)
8380 {
8381   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
8382 }
8383
8384 /* Returns a hash value for X (which really is a var_loc_list).  */
8385
8386 static hashval_t
8387 decl_loc_table_hash (const void *x)
8388 {
8389   return (hashval_t) ((const var_loc_list *) x)->decl_id;
8390 }
8391
8392 /* Return nonzero if decl_id of var_loc_list X is the same as
8393    UID of decl *Y.  */
8394
8395 static int
8396 decl_loc_table_eq (const void *x, const void *y)
8397 {
8398   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
8399 }
8400
8401 /* Return the var_loc list associated with a given declaration.  */
8402
8403 static inline var_loc_list *
8404 lookup_decl_loc (const_tree decl)
8405 {
8406   if (!decl_loc_table)
8407     return NULL;
8408   return (var_loc_list *)
8409     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
8410 }
8411
8412 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
8413
8414 static hashval_t
8415 cached_dw_loc_list_table_hash (const void *x)
8416 {
8417   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
8418 }
8419
8420 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
8421    UID of decl *Y.  */
8422
8423 static int
8424 cached_dw_loc_list_table_eq (const void *x, const void *y)
8425 {
8426   return (((const cached_dw_loc_list *) x)->decl_id
8427           == DECL_UID ((const_tree) y));
8428 }
8429
8430 /* Equate a DIE to a particular declaration.  */
8431
8432 static void
8433 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
8434 {
8435   unsigned int decl_id = DECL_UID (decl);
8436   void **slot;
8437
8438   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
8439   *slot = decl_die;
8440   decl_die->decl_id = decl_id;
8441 }
8442
8443 /* Return how many bits covers PIECE EXPR_LIST.  */
8444
8445 static int
8446 decl_piece_bitsize (rtx piece)
8447 {
8448   int ret = (int) GET_MODE (piece);
8449   if (ret)
8450     return ret;
8451   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
8452               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
8453   return INTVAL (XEXP (XEXP (piece, 0), 0));
8454 }
8455
8456 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
8457
8458 static rtx *
8459 decl_piece_varloc_ptr (rtx piece)
8460 {
8461   if ((int) GET_MODE (piece))
8462     return &XEXP (piece, 0);
8463   else
8464     return &XEXP (XEXP (piece, 0), 1);
8465 }
8466
8467 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
8468    Next is the chain of following piece nodes.  */
8469
8470 static rtx
8471 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
8472 {
8473   if (bitsize <= (int) MAX_MACHINE_MODE)
8474     return alloc_EXPR_LIST (bitsize, loc_note, next);
8475   else
8476     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
8477                                                GEN_INT (bitsize),
8478                                                loc_note), next);
8479 }
8480
8481 /* Return rtx that should be stored into loc field for
8482    LOC_NOTE and BITPOS/BITSIZE.  */
8483
8484 static rtx
8485 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
8486                       HOST_WIDE_INT bitsize)
8487 {
8488   if (bitsize != -1)
8489     {
8490       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
8491       if (bitpos != 0)
8492         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
8493     }
8494   return loc_note;
8495 }
8496
8497 /* This function either modifies location piece list *DEST in
8498    place (if SRC and INNER is NULL), or copies location piece list
8499    *SRC to *DEST while modifying it.  Location BITPOS is modified
8500    to contain LOC_NOTE, any pieces overlapping it are removed resp.
8501    not copied and if needed some padding around it is added.
8502    When modifying in place, DEST should point to EXPR_LIST where
8503    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
8504    to the start of the whole list and INNER points to the EXPR_LIST
8505    where earlier pieces cover PIECE_BITPOS bits.  */
8506
8507 static void
8508 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
8509                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
8510                    HOST_WIDE_INT bitsize, rtx loc_note)
8511 {
8512   int diff;
8513   bool copy = inner != NULL;
8514
8515   if (copy)
8516     {
8517       /* First copy all nodes preceeding the current bitpos.  */
8518       while (src != inner)
8519         {
8520           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8521                                    decl_piece_bitsize (*src), NULL_RTX);
8522           dest = &XEXP (*dest, 1);
8523           src = &XEXP (*src, 1);
8524         }
8525     }
8526   /* Add padding if needed.  */
8527   if (bitpos != piece_bitpos)
8528     {
8529       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8530                                copy ? NULL_RTX : *dest);
8531       dest = &XEXP (*dest, 1);
8532     }
8533   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8534     {
8535       gcc_assert (!copy);
8536       /* A piece with correct bitpos and bitsize already exist,
8537          just update the location for it and return.  */
8538       *decl_piece_varloc_ptr (*dest) = loc_note;
8539       return;
8540     }
8541   /* Add the piece that changed.  */
8542   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8543   dest = &XEXP (*dest, 1);
8544   /* Skip over pieces that overlap it.  */
8545   diff = bitpos - piece_bitpos + bitsize;
8546   if (!copy)
8547     src = dest;
8548   while (diff > 0 && *src)
8549     {
8550       rtx piece = *src;
8551       diff -= decl_piece_bitsize (piece);
8552       if (copy)
8553         src = &XEXP (piece, 1);
8554       else
8555         {
8556           *src = XEXP (piece, 1);
8557           free_EXPR_LIST_node (piece);
8558         }
8559     }
8560   /* Add padding if needed.  */
8561   if (diff < 0 && *src)
8562     {
8563       if (!copy)
8564         dest = src;
8565       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8566       dest = &XEXP (*dest, 1);
8567     }
8568   if (!copy)
8569     return;
8570   /* Finally copy all nodes following it.  */
8571   while (*src)
8572     {
8573       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8574                                decl_piece_bitsize (*src), NULL_RTX);
8575       dest = &XEXP (*dest, 1);
8576       src = &XEXP (*src, 1);
8577     }
8578 }
8579
8580 /* Add a variable location node to the linked list for DECL.  */
8581
8582 static struct var_loc_node *
8583 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8584 {
8585   unsigned int decl_id;
8586   var_loc_list *temp;
8587   void **slot;
8588   struct var_loc_node *loc = NULL;
8589   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8590
8591   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8592     {
8593       tree realdecl = DECL_DEBUG_EXPR (decl);
8594       if (realdecl && handled_component_p (realdecl))
8595         {
8596           HOST_WIDE_INT maxsize;
8597           tree innerdecl;
8598           innerdecl
8599             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8600           if (!DECL_P (innerdecl)
8601               || DECL_IGNORED_P (innerdecl)
8602               || TREE_STATIC (innerdecl)
8603               || bitsize <= 0
8604               || bitpos + bitsize > 256
8605               || bitsize != maxsize)
8606             return NULL;
8607           decl = innerdecl;
8608         }
8609     }
8610
8611   decl_id = DECL_UID (decl);
8612   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8613   if (*slot == NULL)
8614     {
8615       temp = ggc_alloc_cleared_var_loc_list ();
8616       temp->decl_id = decl_id;
8617       *slot = temp;
8618     }
8619   else
8620     temp = (var_loc_list *) *slot;
8621
8622   if (temp->last)
8623     {
8624       struct var_loc_node *last = temp->last, *unused = NULL;
8625       rtx *piece_loc = NULL, last_loc_note;
8626       int piece_bitpos = 0;
8627       if (last->next)
8628         {
8629           last = last->next;
8630           gcc_assert (last->next == NULL);
8631         }
8632       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8633         {
8634           piece_loc = &last->loc;
8635           do
8636             {
8637               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8638               if (piece_bitpos + cur_bitsize > bitpos)
8639                 break;
8640               piece_bitpos += cur_bitsize;
8641               piece_loc = &XEXP (*piece_loc, 1);
8642             }
8643           while (*piece_loc);
8644         }
8645       /* TEMP->LAST here is either pointer to the last but one or
8646          last element in the chained list, LAST is pointer to the
8647          last element.  */
8648       if (label && strcmp (last->label, label) == 0)
8649         {
8650           /* For SRA optimized variables if there weren't any real
8651              insns since last note, just modify the last node.  */
8652           if (piece_loc != NULL)
8653             {
8654               adjust_piece_list (piece_loc, NULL, NULL,
8655                                  bitpos, piece_bitpos, bitsize, loc_note);
8656               return NULL;
8657             }
8658           /* If the last note doesn't cover any instructions, remove it.  */
8659           if (temp->last != last)
8660             {
8661               temp->last->next = NULL;
8662               unused = last;
8663               last = temp->last;
8664               gcc_assert (strcmp (last->label, label) != 0);
8665             }
8666           else
8667             {
8668               gcc_assert (temp->first == temp->last);
8669               memset (temp->last, '\0', sizeof (*temp->last));
8670               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8671               return temp->last;
8672             }
8673         }
8674       if (bitsize == -1 && NOTE_P (last->loc))
8675         last_loc_note = last->loc;
8676       else if (piece_loc != NULL
8677                && *piece_loc != NULL_RTX
8678                && piece_bitpos == bitpos
8679                && decl_piece_bitsize (*piece_loc) == bitsize)
8680         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8681       else
8682         last_loc_note = NULL_RTX;
8683       /* If the current location is the same as the end of the list,
8684          and either both or neither of the locations is uninitialized,
8685          we have nothing to do.  */
8686       if (last_loc_note == NULL_RTX
8687           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8688                             NOTE_VAR_LOCATION_LOC (loc_note)))
8689           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8690                != NOTE_VAR_LOCATION_STATUS (loc_note))
8691               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8692                    == VAR_INIT_STATUS_UNINITIALIZED)
8693                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8694                       == VAR_INIT_STATUS_UNINITIALIZED))))
8695         {
8696           /* Add LOC to the end of list and update LAST.  If the last
8697              element of the list has been removed above, reuse its
8698              memory for the new node, otherwise allocate a new one.  */
8699           if (unused)
8700             {
8701               loc = unused;
8702               memset (loc, '\0', sizeof (*loc));
8703             }
8704           else
8705             loc = ggc_alloc_cleared_var_loc_node ();
8706           if (bitsize == -1 || piece_loc == NULL)
8707             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8708           else
8709             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8710                                bitpos, piece_bitpos, bitsize, loc_note);
8711           last->next = loc;
8712           /* Ensure TEMP->LAST will point either to the new last but one
8713              element of the chain, or to the last element in it.  */
8714           if (last != temp->last)
8715             temp->last = last;
8716         }
8717       else if (unused)
8718         ggc_free (unused);
8719     }
8720   else
8721     {
8722       loc = ggc_alloc_cleared_var_loc_node ();
8723       temp->first = loc;
8724       temp->last = loc;
8725       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8726     }
8727   return loc;
8728 }
8729 \f
8730 /* Keep track of the number of spaces used to indent the
8731    output of the debugging routines that print the structure of
8732    the DIE internal representation.  */
8733 static int print_indent;
8734
8735 /* Indent the line the number of spaces given by print_indent.  */
8736
8737 static inline void
8738 print_spaces (FILE *outfile)
8739 {
8740   fprintf (outfile, "%*s", print_indent, "");
8741 }
8742
8743 /* Print a type signature in hex.  */
8744
8745 static inline void
8746 print_signature (FILE *outfile, char *sig)
8747 {
8748   int i;
8749
8750   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8751     fprintf (outfile, "%02x", sig[i] & 0xff);
8752 }
8753
8754 /* Print the information associated with a given DIE, and its children.
8755    This routine is a debugging aid only.  */
8756
8757 static void
8758 print_die (dw_die_ref die, FILE *outfile)
8759 {
8760   dw_attr_ref a;
8761   dw_die_ref c;
8762   unsigned ix;
8763
8764   print_spaces (outfile);
8765   fprintf (outfile, "DIE %4ld: %s (%p)\n",
8766            die->die_offset, dwarf_tag_name (die->die_tag),
8767            (void*) die);
8768   print_spaces (outfile);
8769   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8770   fprintf (outfile, " offset: %ld", die->die_offset);
8771   fprintf (outfile, " mark: %d\n", die->die_mark);
8772
8773   if (use_debug_types && die->die_id.die_type_node)
8774     {
8775       print_spaces (outfile);
8776       fprintf (outfile, "  signature: ");
8777       print_signature (outfile, die->die_id.die_type_node->signature);
8778       fprintf (outfile, "\n");
8779     }
8780
8781   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8782     {
8783       print_spaces (outfile);
8784       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8785
8786       switch (AT_class (a))
8787         {
8788         case dw_val_class_addr:
8789           fprintf (outfile, "address");
8790           break;
8791         case dw_val_class_offset:
8792           fprintf (outfile, "offset");
8793           break;
8794         case dw_val_class_loc:
8795           fprintf (outfile, "location descriptor");
8796           break;
8797         case dw_val_class_loc_list:
8798           fprintf (outfile, "location list -> label:%s",
8799                    AT_loc_list (a)->ll_symbol);
8800           break;
8801         case dw_val_class_range_list:
8802           fprintf (outfile, "range list");
8803           break;
8804         case dw_val_class_const:
8805           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8806           break;
8807         case dw_val_class_unsigned_const:
8808           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8809           break;
8810         case dw_val_class_const_double:
8811           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8812                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8813                    a->dw_attr_val.v.val_double.high,
8814                    a->dw_attr_val.v.val_double.low);
8815           break;
8816         case dw_val_class_vec:
8817           fprintf (outfile, "floating-point or vector constant");
8818           break;
8819         case dw_val_class_flag:
8820           fprintf (outfile, "%u", AT_flag (a));
8821           break;
8822         case dw_val_class_die_ref:
8823           if (AT_ref (a) != NULL)
8824             {
8825               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
8826                 {
8827                   fprintf (outfile, "die -> signature: ");
8828                   print_signature (outfile,
8829                                    AT_ref (a)->die_id.die_type_node->signature);
8830                 }
8831               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
8832                 fprintf (outfile, "die -> label: %s",
8833                          AT_ref (a)->die_id.die_symbol);
8834               else
8835                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8836               fprintf (outfile, " (%p)", (void *) AT_ref (a));
8837             }
8838           else
8839             fprintf (outfile, "die -> <null>");
8840           break;
8841         case dw_val_class_vms_delta:
8842           fprintf (outfile, "delta: @slotcount(%s-%s)",
8843                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8844           break;
8845         case dw_val_class_lbl_id:
8846         case dw_val_class_lineptr:
8847         case dw_val_class_macptr:
8848           fprintf (outfile, "label: %s", AT_lbl (a));
8849           break;
8850         case dw_val_class_str:
8851           if (AT_string (a) != NULL)
8852             fprintf (outfile, "\"%s\"", AT_string (a));
8853           else
8854             fprintf (outfile, "<null>");
8855           break;
8856         case dw_val_class_file:
8857           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8858                    AT_file (a)->emitted_number);
8859           break;
8860         case dw_val_class_data8:
8861           {
8862             int i;
8863
8864             for (i = 0; i < 8; i++)
8865               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8866             break;
8867           }
8868         default:
8869           break;
8870         }
8871
8872       fprintf (outfile, "\n");
8873     }
8874
8875   if (die->die_child != NULL)
8876     {
8877       print_indent += 4;
8878       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8879       print_indent -= 4;
8880     }
8881   if (print_indent == 0)
8882     fprintf (outfile, "\n");
8883 }
8884
8885 /* Print the information collected for a given DIE.  */
8886
8887 DEBUG_FUNCTION void
8888 debug_dwarf_die (dw_die_ref die)
8889 {
8890   print_die (die, stderr);
8891 }
8892
8893 /* Print all DWARF information collected for the compilation unit.
8894    This routine is a debugging aid only.  */
8895
8896 DEBUG_FUNCTION void
8897 debug_dwarf (void)
8898 {
8899   print_indent = 0;
8900   print_die (comp_unit_die (), stderr);
8901 }
8902 \f
8903 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8904    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8905    DIE that marks the start of the DIEs for this include file.  */
8906
8907 static dw_die_ref
8908 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8909 {
8910   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8911   dw_die_ref new_unit = gen_compile_unit_die (filename);
8912
8913   new_unit->die_sib = old_unit;
8914   return new_unit;
8915 }
8916
8917 /* Close an include-file CU and reopen the enclosing one.  */
8918
8919 static dw_die_ref
8920 pop_compile_unit (dw_die_ref old_unit)
8921 {
8922   dw_die_ref new_unit = old_unit->die_sib;
8923
8924   old_unit->die_sib = NULL;
8925   return new_unit;
8926 }
8927
8928 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8929 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8930
8931 /* Calculate the checksum of a location expression.  */
8932
8933 static inline void
8934 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8935 {
8936   int tem;
8937
8938   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8939   CHECKSUM (tem);
8940   CHECKSUM (loc->dw_loc_oprnd1);
8941   CHECKSUM (loc->dw_loc_oprnd2);
8942 }
8943
8944 /* Calculate the checksum of an attribute.  */
8945
8946 static void
8947 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8948 {
8949   dw_loc_descr_ref loc;
8950   rtx r;
8951
8952   CHECKSUM (at->dw_attr);
8953
8954   /* We don't care that this was compiled with a different compiler
8955      snapshot; if the output is the same, that's what matters.  */
8956   if (at->dw_attr == DW_AT_producer)
8957     return;
8958
8959   switch (AT_class (at))
8960     {
8961     case dw_val_class_const:
8962       CHECKSUM (at->dw_attr_val.v.val_int);
8963       break;
8964     case dw_val_class_unsigned_const:
8965       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8966       break;
8967     case dw_val_class_const_double:
8968       CHECKSUM (at->dw_attr_val.v.val_double);
8969       break;
8970     case dw_val_class_vec:
8971       CHECKSUM (at->dw_attr_val.v.val_vec);
8972       break;
8973     case dw_val_class_flag:
8974       CHECKSUM (at->dw_attr_val.v.val_flag);
8975       break;
8976     case dw_val_class_str:
8977       CHECKSUM_STRING (AT_string (at));
8978       break;
8979
8980     case dw_val_class_addr:
8981       r = AT_addr (at);
8982       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8983       CHECKSUM_STRING (XSTR (r, 0));
8984       break;
8985
8986     case dw_val_class_offset:
8987       CHECKSUM (at->dw_attr_val.v.val_offset);
8988       break;
8989
8990     case dw_val_class_loc:
8991       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8992         loc_checksum (loc, ctx);
8993       break;
8994
8995     case dw_val_class_die_ref:
8996       die_checksum (AT_ref (at), ctx, mark);
8997       break;
8998
8999     case dw_val_class_fde_ref:
9000     case dw_val_class_vms_delta:
9001     case dw_val_class_lbl_id:
9002     case dw_val_class_lineptr:
9003     case dw_val_class_macptr:
9004       break;
9005
9006     case dw_val_class_file:
9007       CHECKSUM_STRING (AT_file (at)->filename);
9008       break;
9009
9010     case dw_val_class_data8:
9011       CHECKSUM (at->dw_attr_val.v.val_data8);
9012       break;
9013
9014     default:
9015       break;
9016     }
9017 }
9018
9019 /* Calculate the checksum of a DIE.  */
9020
9021 static void
9022 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9023 {
9024   dw_die_ref c;
9025   dw_attr_ref a;
9026   unsigned ix;
9027
9028   /* To avoid infinite recursion.  */
9029   if (die->die_mark)
9030     {
9031       CHECKSUM (die->die_mark);
9032       return;
9033     }
9034   die->die_mark = ++(*mark);
9035
9036   CHECKSUM (die->die_tag);
9037
9038   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9039     attr_checksum (a, ctx, mark);
9040
9041   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
9042 }
9043
9044 #undef CHECKSUM
9045 #undef CHECKSUM_STRING
9046
9047 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
9048 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
9049 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
9050 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
9051 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
9052 #define CHECKSUM_ATTR(FOO) \
9053   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
9054
9055 /* Calculate the checksum of a number in signed LEB128 format.  */
9056
9057 static void
9058 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
9059 {
9060   unsigned char byte;
9061   bool more;
9062
9063   while (1)
9064     {
9065       byte = (value & 0x7f);
9066       value >>= 7;
9067       more = !((value == 0 && (byte & 0x40) == 0)
9068                 || (value == -1 && (byte & 0x40) != 0));
9069       if (more)
9070         byte |= 0x80;
9071       CHECKSUM (byte);
9072       if (!more)
9073         break;
9074     }
9075 }
9076
9077 /* Calculate the checksum of a number in unsigned LEB128 format.  */
9078
9079 static void
9080 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
9081 {
9082   while (1)
9083     {
9084       unsigned char byte = (value & 0x7f);
9085       value >>= 7;
9086       if (value != 0)
9087         /* More bytes to follow.  */
9088         byte |= 0x80;
9089       CHECKSUM (byte);
9090       if (value == 0)
9091         break;
9092     }
9093 }
9094
9095 /* Checksum the context of the DIE.  This adds the names of any
9096    surrounding namespaces or structures to the checksum.  */
9097
9098 static void
9099 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
9100 {
9101   const char *name;
9102   dw_die_ref spec;
9103   int tag = die->die_tag;
9104
9105   if (tag != DW_TAG_namespace
9106       && tag != DW_TAG_structure_type
9107       && tag != DW_TAG_class_type)
9108     return;
9109
9110   name = get_AT_string (die, DW_AT_name);
9111
9112   spec = get_AT_ref (die, DW_AT_specification);
9113   if (spec != NULL)
9114     die = spec;
9115
9116   if (die->die_parent != NULL)
9117     checksum_die_context (die->die_parent, ctx);
9118
9119   CHECKSUM_ULEB128 ('C');
9120   CHECKSUM_ULEB128 (tag);
9121   if (name != NULL)
9122     CHECKSUM_STRING (name);
9123 }
9124
9125 /* Calculate the checksum of a location expression.  */
9126
9127 static inline void
9128 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
9129 {
9130   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
9131      were emitted as a DW_FORM_sdata instead of a location expression.  */
9132   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
9133     {
9134       CHECKSUM_ULEB128 (DW_FORM_sdata);
9135       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
9136       return;
9137     }
9138
9139   /* Otherwise, just checksum the raw location expression.  */
9140   while (loc != NULL)
9141     {
9142       CHECKSUM_ULEB128 (loc->dw_loc_opc);
9143       CHECKSUM (loc->dw_loc_oprnd1);
9144       CHECKSUM (loc->dw_loc_oprnd2);
9145       loc = loc->dw_loc_next;
9146     }
9147 }
9148
9149 /* Calculate the checksum of an attribute.  */
9150
9151 static void
9152 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
9153                        struct md5_ctx *ctx, int *mark)
9154 {
9155   dw_loc_descr_ref loc;
9156   rtx r;
9157
9158   if (AT_class (at) == dw_val_class_die_ref)
9159     {
9160       dw_die_ref target_die = AT_ref (at);
9161
9162       /* For pointer and reference types, we checksum only the (qualified)
9163          name of the target type (if there is a name).  For friend entries,
9164          we checksum only the (qualified) name of the target type or function.
9165          This allows the checksum to remain the same whether the target type
9166          is complete or not.  */
9167       if ((at->dw_attr == DW_AT_type
9168            && (tag == DW_TAG_pointer_type
9169                || tag == DW_TAG_reference_type
9170                || tag == DW_TAG_rvalue_reference_type
9171                || tag == DW_TAG_ptr_to_member_type))
9172           || (at->dw_attr == DW_AT_friend
9173               && tag == DW_TAG_friend))
9174         {
9175           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
9176
9177           if (name_attr != NULL)
9178             {
9179               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9180
9181               if (decl == NULL)
9182                 decl = target_die;
9183               CHECKSUM_ULEB128 ('N');
9184               CHECKSUM_ULEB128 (at->dw_attr);
9185               if (decl->die_parent != NULL)
9186                 checksum_die_context (decl->die_parent, ctx);
9187               CHECKSUM_ULEB128 ('E');
9188               CHECKSUM_STRING (AT_string (name_attr));
9189               return;
9190             }
9191         }
9192
9193       /* For all other references to another DIE, we check to see if the
9194          target DIE has already been visited.  If it has, we emit a
9195          backward reference; if not, we descend recursively.  */
9196       if (target_die->die_mark > 0)
9197         {
9198           CHECKSUM_ULEB128 ('R');
9199           CHECKSUM_ULEB128 (at->dw_attr);
9200           CHECKSUM_ULEB128 (target_die->die_mark);
9201         }
9202       else
9203         {
9204           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
9205
9206           if (decl == NULL)
9207             decl = target_die;
9208           target_die->die_mark = ++(*mark);
9209           CHECKSUM_ULEB128 ('T');
9210           CHECKSUM_ULEB128 (at->dw_attr);
9211           if (decl->die_parent != NULL)
9212             checksum_die_context (decl->die_parent, ctx);
9213           die_checksum_ordered (target_die, ctx, mark);
9214         }
9215       return;
9216     }
9217
9218   CHECKSUM_ULEB128 ('A');
9219   CHECKSUM_ULEB128 (at->dw_attr);
9220
9221   switch (AT_class (at))
9222     {
9223     case dw_val_class_const:
9224       CHECKSUM_ULEB128 (DW_FORM_sdata);
9225       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
9226       break;
9227
9228     case dw_val_class_unsigned_const:
9229       CHECKSUM_ULEB128 (DW_FORM_sdata);
9230       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
9231       break;
9232
9233     case dw_val_class_const_double:
9234       CHECKSUM_ULEB128 (DW_FORM_block);
9235       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
9236       CHECKSUM (at->dw_attr_val.v.val_double);
9237       break;
9238
9239     case dw_val_class_vec:
9240       CHECKSUM_ULEB128 (DW_FORM_block);
9241       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
9242       CHECKSUM (at->dw_attr_val.v.val_vec);
9243       break;
9244
9245     case dw_val_class_flag:
9246       CHECKSUM_ULEB128 (DW_FORM_flag);
9247       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
9248       break;
9249
9250     case dw_val_class_str:
9251       CHECKSUM_ULEB128 (DW_FORM_string);
9252       CHECKSUM_STRING (AT_string (at));
9253       break;
9254
9255     case dw_val_class_addr:
9256       r = AT_addr (at);
9257       gcc_assert (GET_CODE (r) == SYMBOL_REF);
9258       CHECKSUM_ULEB128 (DW_FORM_string);
9259       CHECKSUM_STRING (XSTR (r, 0));
9260       break;
9261
9262     case dw_val_class_offset:
9263       CHECKSUM_ULEB128 (DW_FORM_sdata);
9264       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
9265       break;
9266
9267     case dw_val_class_loc:
9268       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
9269         loc_checksum_ordered (loc, ctx);
9270       break;
9271
9272     case dw_val_class_fde_ref:
9273     case dw_val_class_lbl_id:
9274     case dw_val_class_lineptr:
9275     case dw_val_class_macptr:
9276       break;
9277
9278     case dw_val_class_file:
9279       CHECKSUM_ULEB128 (DW_FORM_string);
9280       CHECKSUM_STRING (AT_file (at)->filename);
9281       break;
9282
9283     case dw_val_class_data8:
9284       CHECKSUM (at->dw_attr_val.v.val_data8);
9285       break;
9286
9287     default:
9288       break;
9289     }
9290 }
9291
9292 struct checksum_attributes
9293 {
9294   dw_attr_ref at_name;
9295   dw_attr_ref at_type;
9296   dw_attr_ref at_friend;
9297   dw_attr_ref at_accessibility;
9298   dw_attr_ref at_address_class;
9299   dw_attr_ref at_allocated;
9300   dw_attr_ref at_artificial;
9301   dw_attr_ref at_associated;
9302   dw_attr_ref at_binary_scale;
9303   dw_attr_ref at_bit_offset;
9304   dw_attr_ref at_bit_size;
9305   dw_attr_ref at_bit_stride;
9306   dw_attr_ref at_byte_size;
9307   dw_attr_ref at_byte_stride;
9308   dw_attr_ref at_const_value;
9309   dw_attr_ref at_containing_type;
9310   dw_attr_ref at_count;
9311   dw_attr_ref at_data_location;
9312   dw_attr_ref at_data_member_location;
9313   dw_attr_ref at_decimal_scale;
9314   dw_attr_ref at_decimal_sign;
9315   dw_attr_ref at_default_value;
9316   dw_attr_ref at_digit_count;
9317   dw_attr_ref at_discr;
9318   dw_attr_ref at_discr_list;
9319   dw_attr_ref at_discr_value;
9320   dw_attr_ref at_encoding;
9321   dw_attr_ref at_endianity;
9322   dw_attr_ref at_explicit;
9323   dw_attr_ref at_is_optional;
9324   dw_attr_ref at_location;
9325   dw_attr_ref at_lower_bound;
9326   dw_attr_ref at_mutable;
9327   dw_attr_ref at_ordering;
9328   dw_attr_ref at_picture_string;
9329   dw_attr_ref at_prototyped;
9330   dw_attr_ref at_small;
9331   dw_attr_ref at_segment;
9332   dw_attr_ref at_string_length;
9333   dw_attr_ref at_threads_scaled;
9334   dw_attr_ref at_upper_bound;
9335   dw_attr_ref at_use_location;
9336   dw_attr_ref at_use_UTF8;
9337   dw_attr_ref at_variable_parameter;
9338   dw_attr_ref at_virtuality;
9339   dw_attr_ref at_visibility;
9340   dw_attr_ref at_vtable_elem_location;
9341 };
9342
9343 /* Collect the attributes that we will want to use for the checksum.  */
9344
9345 static void
9346 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
9347 {
9348   dw_attr_ref a;
9349   unsigned ix;
9350
9351   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
9352     {
9353       switch (a->dw_attr)
9354         {
9355         case DW_AT_name:
9356           attrs->at_name = a;
9357           break;
9358         case DW_AT_type:
9359           attrs->at_type = a;
9360           break;
9361         case DW_AT_friend:
9362           attrs->at_friend = a;
9363           break;
9364         case DW_AT_accessibility:
9365           attrs->at_accessibility = a;
9366           break;
9367         case DW_AT_address_class:
9368           attrs->at_address_class = a;
9369           break;
9370         case DW_AT_allocated:
9371           attrs->at_allocated = a;
9372           break;
9373         case DW_AT_artificial:
9374           attrs->at_artificial = a;
9375           break;
9376         case DW_AT_associated:
9377           attrs->at_associated = a;
9378           break;
9379         case DW_AT_binary_scale:
9380           attrs->at_binary_scale = a;
9381           break;
9382         case DW_AT_bit_offset:
9383           attrs->at_bit_offset = a;
9384           break;
9385         case DW_AT_bit_size:
9386           attrs->at_bit_size = a;
9387           break;
9388         case DW_AT_bit_stride:
9389           attrs->at_bit_stride = a;
9390           break;
9391         case DW_AT_byte_size:
9392           attrs->at_byte_size = a;
9393           break;
9394         case DW_AT_byte_stride:
9395           attrs->at_byte_stride = a;
9396           break;
9397         case DW_AT_const_value:
9398           attrs->at_const_value = a;
9399           break;
9400         case DW_AT_containing_type:
9401           attrs->at_containing_type = a;
9402           break;
9403         case DW_AT_count:
9404           attrs->at_count = a;
9405           break;
9406         case DW_AT_data_location:
9407           attrs->at_data_location = a;
9408           break;
9409         case DW_AT_data_member_location:
9410           attrs->at_data_member_location = a;
9411           break;
9412         case DW_AT_decimal_scale:
9413           attrs->at_decimal_scale = a;
9414           break;
9415         case DW_AT_decimal_sign:
9416           attrs->at_decimal_sign = a;
9417           break;
9418         case DW_AT_default_value:
9419           attrs->at_default_value = a;
9420           break;
9421         case DW_AT_digit_count:
9422           attrs->at_digit_count = a;
9423           break;
9424         case DW_AT_discr:
9425           attrs->at_discr = a;
9426           break;
9427         case DW_AT_discr_list:
9428           attrs->at_discr_list = a;
9429           break;
9430         case DW_AT_discr_value:
9431           attrs->at_discr_value = a;
9432           break;
9433         case DW_AT_encoding:
9434           attrs->at_encoding = a;
9435           break;
9436         case DW_AT_endianity:
9437           attrs->at_endianity = a;
9438           break;
9439         case DW_AT_explicit:
9440           attrs->at_explicit = a;
9441           break;
9442         case DW_AT_is_optional:
9443           attrs->at_is_optional = a;
9444           break;
9445         case DW_AT_location:
9446           attrs->at_location = a;
9447           break;
9448         case DW_AT_lower_bound:
9449           attrs->at_lower_bound = a;
9450           break;
9451         case DW_AT_mutable:
9452           attrs->at_mutable = a;
9453           break;
9454         case DW_AT_ordering:
9455           attrs->at_ordering = a;
9456           break;
9457         case DW_AT_picture_string:
9458           attrs->at_picture_string = a;
9459           break;
9460         case DW_AT_prototyped:
9461           attrs->at_prototyped = a;
9462           break;
9463         case DW_AT_small:
9464           attrs->at_small = a;
9465           break;
9466         case DW_AT_segment:
9467           attrs->at_segment = a;
9468           break;
9469         case DW_AT_string_length:
9470           attrs->at_string_length = a;
9471           break;
9472         case DW_AT_threads_scaled:
9473           attrs->at_threads_scaled = a;
9474           break;
9475         case DW_AT_upper_bound:
9476           attrs->at_upper_bound = a;
9477           break;
9478         case DW_AT_use_location:
9479           attrs->at_use_location = a;
9480           break;
9481         case DW_AT_use_UTF8:
9482           attrs->at_use_UTF8 = a;
9483           break;
9484         case DW_AT_variable_parameter:
9485           attrs->at_variable_parameter = a;
9486           break;
9487         case DW_AT_virtuality:
9488           attrs->at_virtuality = a;
9489           break;
9490         case DW_AT_visibility:
9491           attrs->at_visibility = a;
9492           break;
9493         case DW_AT_vtable_elem_location:
9494           attrs->at_vtable_elem_location = a;
9495           break;
9496         default:
9497           break;
9498         }
9499     }
9500 }
9501
9502 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9503
9504 static void
9505 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9506 {
9507   dw_die_ref c;
9508   dw_die_ref decl;
9509   struct checksum_attributes attrs;
9510
9511   CHECKSUM_ULEB128 ('D');
9512   CHECKSUM_ULEB128 (die->die_tag);
9513
9514   memset (&attrs, 0, sizeof (attrs));
9515
9516   decl = get_AT_ref (die, DW_AT_specification);
9517   if (decl != NULL)
9518     collect_checksum_attributes (&attrs, decl);
9519   collect_checksum_attributes (&attrs, die);
9520
9521   CHECKSUM_ATTR (attrs.at_name);
9522   CHECKSUM_ATTR (attrs.at_accessibility);
9523   CHECKSUM_ATTR (attrs.at_address_class);
9524   CHECKSUM_ATTR (attrs.at_allocated);
9525   CHECKSUM_ATTR (attrs.at_artificial);
9526   CHECKSUM_ATTR (attrs.at_associated);
9527   CHECKSUM_ATTR (attrs.at_binary_scale);
9528   CHECKSUM_ATTR (attrs.at_bit_offset);
9529   CHECKSUM_ATTR (attrs.at_bit_size);
9530   CHECKSUM_ATTR (attrs.at_bit_stride);
9531   CHECKSUM_ATTR (attrs.at_byte_size);
9532   CHECKSUM_ATTR (attrs.at_byte_stride);
9533   CHECKSUM_ATTR (attrs.at_const_value);
9534   CHECKSUM_ATTR (attrs.at_containing_type);
9535   CHECKSUM_ATTR (attrs.at_count);
9536   CHECKSUM_ATTR (attrs.at_data_location);
9537   CHECKSUM_ATTR (attrs.at_data_member_location);
9538   CHECKSUM_ATTR (attrs.at_decimal_scale);
9539   CHECKSUM_ATTR (attrs.at_decimal_sign);
9540   CHECKSUM_ATTR (attrs.at_default_value);
9541   CHECKSUM_ATTR (attrs.at_digit_count);
9542   CHECKSUM_ATTR (attrs.at_discr);
9543   CHECKSUM_ATTR (attrs.at_discr_list);
9544   CHECKSUM_ATTR (attrs.at_discr_value);
9545   CHECKSUM_ATTR (attrs.at_encoding);
9546   CHECKSUM_ATTR (attrs.at_endianity);
9547   CHECKSUM_ATTR (attrs.at_explicit);
9548   CHECKSUM_ATTR (attrs.at_is_optional);
9549   CHECKSUM_ATTR (attrs.at_location);
9550   CHECKSUM_ATTR (attrs.at_lower_bound);
9551   CHECKSUM_ATTR (attrs.at_mutable);
9552   CHECKSUM_ATTR (attrs.at_ordering);
9553   CHECKSUM_ATTR (attrs.at_picture_string);
9554   CHECKSUM_ATTR (attrs.at_prototyped);
9555   CHECKSUM_ATTR (attrs.at_small);
9556   CHECKSUM_ATTR (attrs.at_segment);
9557   CHECKSUM_ATTR (attrs.at_string_length);
9558   CHECKSUM_ATTR (attrs.at_threads_scaled);
9559   CHECKSUM_ATTR (attrs.at_upper_bound);
9560   CHECKSUM_ATTR (attrs.at_use_location);
9561   CHECKSUM_ATTR (attrs.at_use_UTF8);
9562   CHECKSUM_ATTR (attrs.at_variable_parameter);
9563   CHECKSUM_ATTR (attrs.at_virtuality);
9564   CHECKSUM_ATTR (attrs.at_visibility);
9565   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9566   CHECKSUM_ATTR (attrs.at_type);
9567   CHECKSUM_ATTR (attrs.at_friend);
9568
9569   /* Checksum the child DIEs, except for nested types and member functions.  */
9570   c = die->die_child;
9571   if (c) do {
9572     dw_attr_ref name_attr;
9573
9574     c = c->die_sib;
9575     name_attr = get_AT (c, DW_AT_name);
9576     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9577         && name_attr != NULL)
9578       {
9579         CHECKSUM_ULEB128 ('S');
9580         CHECKSUM_ULEB128 (c->die_tag);
9581         CHECKSUM_STRING (AT_string (name_attr));
9582       }
9583     else
9584       {
9585         /* Mark this DIE so it gets processed when unmarking.  */
9586         if (c->die_mark == 0)
9587           c->die_mark = -1;
9588         die_checksum_ordered (c, ctx, mark);
9589       }
9590   } while (c != die->die_child);
9591
9592   CHECKSUM_ULEB128 (0);
9593 }
9594
9595 #undef CHECKSUM
9596 #undef CHECKSUM_STRING
9597 #undef CHECKSUM_ATTR
9598 #undef CHECKSUM_LEB128
9599 #undef CHECKSUM_ULEB128
9600
9601 /* Generate the type signature for DIE.  This is computed by generating an
9602    MD5 checksum over the DIE's tag, its relevant attributes, and its
9603    children.  Attributes that are references to other DIEs are processed
9604    by recursion, using the MARK field to prevent infinite recursion.
9605    If the DIE is nested inside a namespace or another type, we also
9606    need to include that context in the signature.  The lower 64 bits
9607    of the resulting MD5 checksum comprise the signature.  */
9608
9609 static void
9610 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9611 {
9612   int mark;
9613   const char *name;
9614   unsigned char checksum[16];
9615   struct md5_ctx ctx;
9616   dw_die_ref decl;
9617
9618   name = get_AT_string (die, DW_AT_name);
9619   decl = get_AT_ref (die, DW_AT_specification);
9620
9621   /* First, compute a signature for just the type name (and its surrounding
9622      context, if any.  This is stored in the type unit DIE for link-time
9623      ODR (one-definition rule) checking.  */
9624
9625   if (is_cxx() && name != NULL)
9626     {
9627       md5_init_ctx (&ctx);
9628
9629       /* Checksum the names of surrounding namespaces and structures.  */
9630       if (decl != NULL && decl->die_parent != NULL)
9631         checksum_die_context (decl->die_parent, &ctx);
9632
9633       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9634       md5_process_bytes (name, strlen (name) + 1, &ctx);
9635       md5_finish_ctx (&ctx, checksum);
9636
9637       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9638     }
9639
9640   /* Next, compute the complete type signature.  */
9641
9642   md5_init_ctx (&ctx);
9643   mark = 1;
9644   die->die_mark = mark;
9645
9646   /* Checksum the names of surrounding namespaces and structures.  */
9647   if (decl != NULL && decl->die_parent != NULL)
9648     checksum_die_context (decl->die_parent, &ctx);
9649
9650   /* Checksum the DIE and its children.  */
9651   die_checksum_ordered (die, &ctx, &mark);
9652   unmark_all_dies (die);
9653   md5_finish_ctx (&ctx, checksum);
9654
9655   /* Store the signature in the type node and link the type DIE and the
9656      type node together.  */
9657   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9658           DWARF_TYPE_SIGNATURE_SIZE);
9659   die->die_id.die_type_node = type_node;
9660   type_node->type_die = die;
9661
9662   /* If the DIE is a specification, link its declaration to the type node
9663      as well.  */
9664   if (decl != NULL)
9665     decl->die_id.die_type_node = type_node;
9666 }
9667
9668 /* Do the location expressions look same?  */
9669 static inline int
9670 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9671 {
9672   return loc1->dw_loc_opc == loc2->dw_loc_opc
9673          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9674          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9675 }
9676
9677 /* Do the values look the same?  */
9678 static int
9679 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9680 {
9681   dw_loc_descr_ref loc1, loc2;
9682   rtx r1, r2;
9683
9684   if (v1->val_class != v2->val_class)
9685     return 0;
9686
9687   switch (v1->val_class)
9688     {
9689     case dw_val_class_const:
9690       return v1->v.val_int == v2->v.val_int;
9691     case dw_val_class_unsigned_const:
9692       return v1->v.val_unsigned == v2->v.val_unsigned;
9693     case dw_val_class_const_double:
9694       return v1->v.val_double.high == v2->v.val_double.high
9695              && v1->v.val_double.low == v2->v.val_double.low;
9696     case dw_val_class_vec:
9697       if (v1->v.val_vec.length != v2->v.val_vec.length
9698           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9699         return 0;
9700       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9701                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9702         return 0;
9703       return 1;
9704     case dw_val_class_flag:
9705       return v1->v.val_flag == v2->v.val_flag;
9706     case dw_val_class_str:
9707       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9708
9709     case dw_val_class_addr:
9710       r1 = v1->v.val_addr;
9711       r2 = v2->v.val_addr;
9712       if (GET_CODE (r1) != GET_CODE (r2))
9713         return 0;
9714       return !rtx_equal_p (r1, r2);
9715
9716     case dw_val_class_offset:
9717       return v1->v.val_offset == v2->v.val_offset;
9718
9719     case dw_val_class_loc:
9720       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9721            loc1 && loc2;
9722            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9723         if (!same_loc_p (loc1, loc2, mark))
9724           return 0;
9725       return !loc1 && !loc2;
9726
9727     case dw_val_class_die_ref:
9728       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9729
9730     case dw_val_class_fde_ref:
9731     case dw_val_class_vms_delta:
9732     case dw_val_class_lbl_id:
9733     case dw_val_class_lineptr:
9734     case dw_val_class_macptr:
9735       return 1;
9736
9737     case dw_val_class_file:
9738       return v1->v.val_file == v2->v.val_file;
9739
9740     case dw_val_class_data8:
9741       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9742
9743     default:
9744       return 1;
9745     }
9746 }
9747
9748 /* Do the attributes look the same?  */
9749
9750 static int
9751 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9752 {
9753   if (at1->dw_attr != at2->dw_attr)
9754     return 0;
9755
9756   /* We don't care that this was compiled with a different compiler
9757      snapshot; if the output is the same, that's what matters. */
9758   if (at1->dw_attr == DW_AT_producer)
9759     return 1;
9760
9761   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9762 }
9763
9764 /* Do the dies look the same?  */
9765
9766 static int
9767 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9768 {
9769   dw_die_ref c1, c2;
9770   dw_attr_ref a1;
9771   unsigned ix;
9772
9773   /* To avoid infinite recursion.  */
9774   if (die1->die_mark)
9775     return die1->die_mark == die2->die_mark;
9776   die1->die_mark = die2->die_mark = ++(*mark);
9777
9778   if (die1->die_tag != die2->die_tag)
9779     return 0;
9780
9781   if (VEC_length (dw_attr_node, die1->die_attr)
9782       != VEC_length (dw_attr_node, die2->die_attr))
9783     return 0;
9784
9785   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
9786     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9787       return 0;
9788
9789   c1 = die1->die_child;
9790   c2 = die2->die_child;
9791   if (! c1)
9792     {
9793       if (c2)
9794         return 0;
9795     }
9796   else
9797     for (;;)
9798       {
9799         if (!same_die_p (c1, c2, mark))
9800           return 0;
9801         c1 = c1->die_sib;
9802         c2 = c2->die_sib;
9803         if (c1 == die1->die_child)
9804           {
9805             if (c2 == die2->die_child)
9806               break;
9807             else
9808               return 0;
9809           }
9810     }
9811
9812   return 1;
9813 }
9814
9815 /* Do the dies look the same?  Wrapper around same_die_p.  */
9816
9817 static int
9818 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9819 {
9820   int mark = 0;
9821   int ret = same_die_p (die1, die2, &mark);
9822
9823   unmark_all_dies (die1);
9824   unmark_all_dies (die2);
9825
9826   return ret;
9827 }
9828
9829 /* The prefix to attach to symbols on DIEs in the current comdat debug
9830    info section.  */
9831 static char *comdat_symbol_id;
9832
9833 /* The index of the current symbol within the current comdat CU.  */
9834 static unsigned int comdat_symbol_number;
9835
9836 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9837    children, and set comdat_symbol_id accordingly.  */
9838
9839 static void
9840 compute_section_prefix (dw_die_ref unit_die)
9841 {
9842   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9843   const char *base = die_name ? lbasename (die_name) : "anonymous";
9844   char *name = XALLOCAVEC (char, strlen (base) + 64);
9845   char *p;
9846   int i, mark;
9847   unsigned char checksum[16];
9848   struct md5_ctx ctx;
9849
9850   /* Compute the checksum of the DIE, then append part of it as hex digits to
9851      the name filename of the unit.  */
9852
9853   md5_init_ctx (&ctx);
9854   mark = 0;
9855   die_checksum (unit_die, &ctx, &mark);
9856   unmark_all_dies (unit_die);
9857   md5_finish_ctx (&ctx, checksum);
9858
9859   sprintf (name, "%s.", base);
9860   clean_symbol_name (name);
9861
9862   p = name + strlen (name);
9863   for (i = 0; i < 4; i++)
9864     {
9865       sprintf (p, "%.2x", checksum[i]);
9866       p += 2;
9867     }
9868
9869   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9870   comdat_symbol_number = 0;
9871 }
9872
9873 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9874
9875 static int
9876 is_type_die (dw_die_ref die)
9877 {
9878   switch (die->die_tag)
9879     {
9880     case DW_TAG_array_type:
9881     case DW_TAG_class_type:
9882     case DW_TAG_interface_type:
9883     case DW_TAG_enumeration_type:
9884     case DW_TAG_pointer_type:
9885     case DW_TAG_reference_type:
9886     case DW_TAG_rvalue_reference_type:
9887     case DW_TAG_string_type:
9888     case DW_TAG_structure_type:
9889     case DW_TAG_subroutine_type:
9890     case DW_TAG_union_type:
9891     case DW_TAG_ptr_to_member_type:
9892     case DW_TAG_set_type:
9893     case DW_TAG_subrange_type:
9894     case DW_TAG_base_type:
9895     case DW_TAG_const_type:
9896     case DW_TAG_file_type:
9897     case DW_TAG_packed_type:
9898     case DW_TAG_volatile_type:
9899     case DW_TAG_typedef:
9900       return 1;
9901     default:
9902       return 0;
9903     }
9904 }
9905
9906 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9907    Basically, we want to choose the bits that are likely to be shared between
9908    compilations (types) and leave out the bits that are specific to individual
9909    compilations (functions).  */
9910
9911 static int
9912 is_comdat_die (dw_die_ref c)
9913 {
9914   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9915      we do for stabs.  The advantage is a greater likelihood of sharing between
9916      objects that don't include headers in the same order (and therefore would
9917      put the base types in a different comdat).  jason 8/28/00 */
9918
9919   if (c->die_tag == DW_TAG_base_type)
9920     return 0;
9921
9922   if (c->die_tag == DW_TAG_pointer_type
9923       || c->die_tag == DW_TAG_reference_type
9924       || c->die_tag == DW_TAG_rvalue_reference_type
9925       || c->die_tag == DW_TAG_const_type
9926       || c->die_tag == DW_TAG_volatile_type)
9927     {
9928       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9929
9930       return t ? is_comdat_die (t) : 0;
9931     }
9932
9933   return is_type_die (c);
9934 }
9935
9936 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9937    compilation unit.  */
9938
9939 static int
9940 is_symbol_die (dw_die_ref c)
9941 {
9942   return (is_type_die (c)
9943           || is_declaration_die (c)
9944           || c->die_tag == DW_TAG_namespace
9945           || c->die_tag == DW_TAG_module);
9946 }
9947
9948 /* Returns true iff C is a compile-unit DIE.  */
9949
9950 static inline bool
9951 is_cu_die (dw_die_ref c)
9952 {
9953   return c && c->die_tag == DW_TAG_compile_unit;
9954 }
9955
9956 static char *
9957 gen_internal_sym (const char *prefix)
9958 {
9959   char buf[256];
9960
9961   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9962   return xstrdup (buf);
9963 }
9964
9965 /* Assign symbols to all worthy DIEs under DIE.  */
9966
9967 static void
9968 assign_symbol_names (dw_die_ref die)
9969 {
9970   dw_die_ref c;
9971
9972   if (is_symbol_die (die))
9973     {
9974       if (comdat_symbol_id)
9975         {
9976           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9977
9978           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9979                    comdat_symbol_id, comdat_symbol_number++);
9980           die->die_id.die_symbol = xstrdup (p);
9981         }
9982       else
9983         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9984     }
9985
9986   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9987 }
9988
9989 struct cu_hash_table_entry
9990 {
9991   dw_die_ref cu;
9992   unsigned min_comdat_num, max_comdat_num;
9993   struct cu_hash_table_entry *next;
9994 };
9995
9996 /* Routines to manipulate hash table of CUs.  */
9997 static hashval_t
9998 htab_cu_hash (const void *of)
9999 {
10000   const struct cu_hash_table_entry *const entry =
10001     (const struct cu_hash_table_entry *) of;
10002
10003   return htab_hash_string (entry->cu->die_id.die_symbol);
10004 }
10005
10006 static int
10007 htab_cu_eq (const void *of1, const void *of2)
10008 {
10009   const struct cu_hash_table_entry *const entry1 =
10010     (const struct cu_hash_table_entry *) of1;
10011   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10012
10013   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
10014 }
10015
10016 static void
10017 htab_cu_del (void *what)
10018 {
10019   struct cu_hash_table_entry *next,
10020     *entry = (struct cu_hash_table_entry *) what;
10021
10022   while (entry)
10023     {
10024       next = entry->next;
10025       free (entry);
10026       entry = next;
10027     }
10028 }
10029
10030 /* Check whether we have already seen this CU and set up SYM_NUM
10031    accordingly.  */
10032 static int
10033 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
10034 {
10035   struct cu_hash_table_entry dummy;
10036   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
10037
10038   dummy.max_comdat_num = 0;
10039
10040   slot = (struct cu_hash_table_entry **)
10041     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10042         INSERT);
10043   entry = *slot;
10044
10045   for (; entry; last = entry, entry = entry->next)
10046     {
10047       if (same_die_p_wrap (cu, entry->cu))
10048         break;
10049     }
10050
10051   if (entry)
10052     {
10053       *sym_num = entry->min_comdat_num;
10054       return 1;
10055     }
10056
10057   entry = XCNEW (struct cu_hash_table_entry);
10058   entry->cu = cu;
10059   entry->min_comdat_num = *sym_num = last->max_comdat_num;
10060   entry->next = *slot;
10061   *slot = entry;
10062
10063   return 0;
10064 }
10065
10066 /* Record SYM_NUM to record of CU in HTABLE.  */
10067 static void
10068 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
10069 {
10070   struct cu_hash_table_entry **slot, *entry;
10071
10072   slot = (struct cu_hash_table_entry **)
10073     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
10074         NO_INSERT);
10075   entry = *slot;
10076
10077   entry->max_comdat_num = sym_num;
10078 }
10079
10080 /* Traverse the DIE (which is always comp_unit_die), and set up
10081    additional compilation units for each of the include files we see
10082    bracketed by BINCL/EINCL.  */
10083
10084 static void
10085 break_out_includes (dw_die_ref die)
10086 {
10087   dw_die_ref c;
10088   dw_die_ref unit = NULL;
10089   limbo_die_node *node, **pnode;
10090   htab_t cu_hash_table;
10091
10092   c = die->die_child;
10093   if (c) do {
10094     dw_die_ref prev = c;
10095     c = c->die_sib;
10096     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
10097            || (unit && is_comdat_die (c)))
10098       {
10099         dw_die_ref next = c->die_sib;
10100
10101         /* This DIE is for a secondary CU; remove it from the main one.  */
10102         remove_child_with_prev (c, prev);
10103
10104         if (c->die_tag == DW_TAG_GNU_BINCL)
10105           unit = push_new_compile_unit (unit, c);
10106         else if (c->die_tag == DW_TAG_GNU_EINCL)
10107           unit = pop_compile_unit (unit);
10108         else
10109           add_child_die (unit, c);
10110         c = next;
10111         if (c == die->die_child)
10112           break;
10113       }
10114   } while (c != die->die_child);
10115
10116 #if 0
10117   /* We can only use this in debugging, since the frontend doesn't check
10118      to make sure that we leave every include file we enter.  */
10119   gcc_assert (!unit);
10120 #endif
10121
10122   assign_symbol_names (die);
10123   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
10124   for (node = limbo_die_list, pnode = &limbo_die_list;
10125        node;
10126        node = node->next)
10127     {
10128       int is_dupl;
10129
10130       compute_section_prefix (node->die);
10131       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
10132                         &comdat_symbol_number);
10133       assign_symbol_names (node->die);
10134       if (is_dupl)
10135         *pnode = node->next;
10136       else
10137         {
10138           pnode = &node->next;
10139           record_comdat_symbol_number (node->die, cu_hash_table,
10140                 comdat_symbol_number);
10141         }
10142     }
10143   htab_delete (cu_hash_table);
10144 }
10145
10146 /* Return non-zero if this DIE is a declaration.  */
10147
10148 static int
10149 is_declaration_die (dw_die_ref die)
10150 {
10151   dw_attr_ref a;
10152   unsigned ix;
10153
10154   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10155     if (a->dw_attr == DW_AT_declaration)
10156       return 1;
10157
10158   return 0;
10159 }
10160
10161 /* Return non-zero if this DIE is nested inside a subprogram.  */
10162
10163 static int
10164 is_nested_in_subprogram (dw_die_ref die)
10165 {
10166   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
10167
10168   if (decl == NULL)
10169     decl = die;
10170   return local_scope_p (decl);
10171 }
10172
10173 /* Return non-zero if this DIE contains a defining declaration of a
10174    subprogram.  */
10175
10176 static int
10177 contains_subprogram_definition (dw_die_ref die)
10178 {
10179   dw_die_ref c;
10180
10181   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
10182     return 1;
10183   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
10184   return 0;
10185 }
10186
10187 /* Return non-zero if this is a type DIE that should be moved to a
10188    COMDAT .debug_types section.  */
10189
10190 static int
10191 should_move_die_to_comdat (dw_die_ref die)
10192 {
10193   switch (die->die_tag)
10194     {
10195     case DW_TAG_class_type:
10196     case DW_TAG_structure_type:
10197     case DW_TAG_enumeration_type:
10198     case DW_TAG_union_type:
10199       /* Don't move declarations, inlined instances, or types nested in a
10200          subprogram.  */
10201       if (is_declaration_die (die)
10202           || get_AT (die, DW_AT_abstract_origin)
10203           || is_nested_in_subprogram (die))
10204         return 0;
10205       /* A type definition should never contain a subprogram definition.  */
10206       gcc_assert (!contains_subprogram_definition (die));
10207       return 1;
10208     case DW_TAG_array_type:
10209     case DW_TAG_interface_type:
10210     case DW_TAG_pointer_type:
10211     case DW_TAG_reference_type:
10212     case DW_TAG_rvalue_reference_type:
10213     case DW_TAG_string_type:
10214     case DW_TAG_subroutine_type:
10215     case DW_TAG_ptr_to_member_type:
10216     case DW_TAG_set_type:
10217     case DW_TAG_subrange_type:
10218     case DW_TAG_base_type:
10219     case DW_TAG_const_type:
10220     case DW_TAG_file_type:
10221     case DW_TAG_packed_type:
10222     case DW_TAG_volatile_type:
10223     case DW_TAG_typedef:
10224     default:
10225       return 0;
10226     }
10227 }
10228
10229 /* Make a clone of DIE.  */
10230
10231 static dw_die_ref
10232 clone_die (dw_die_ref die)
10233 {
10234   dw_die_ref clone;
10235   dw_attr_ref a;
10236   unsigned ix;
10237
10238   clone = ggc_alloc_cleared_die_node ();
10239   clone->die_tag = die->die_tag;
10240
10241   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10242     add_dwarf_attr (clone, a);
10243
10244   return clone;
10245 }
10246
10247 /* Make a clone of the tree rooted at DIE.  */
10248
10249 static dw_die_ref
10250 clone_tree (dw_die_ref die)
10251 {
10252   dw_die_ref c;
10253   dw_die_ref clone = clone_die (die);
10254
10255   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
10256
10257   return clone;
10258 }
10259
10260 /* Make a clone of DIE as a declaration.  */
10261
10262 static dw_die_ref
10263 clone_as_declaration (dw_die_ref die)
10264 {
10265   dw_die_ref clone;
10266   dw_die_ref decl;
10267   dw_attr_ref a;
10268   unsigned ix;
10269
10270   /* If the DIE is already a declaration, just clone it.  */
10271   if (is_declaration_die (die))
10272     return clone_die (die);
10273
10274   /* If the DIE is a specification, just clone its declaration DIE.  */
10275   decl = get_AT_ref (die, DW_AT_specification);
10276   if (decl != NULL)
10277     return clone_die (decl);
10278
10279   clone = ggc_alloc_cleared_die_node ();
10280   clone->die_tag = die->die_tag;
10281
10282   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10283     {
10284       /* We don't want to copy over all attributes.
10285          For example we don't want DW_AT_byte_size because otherwise we will no
10286          longer have a declaration and GDB will treat it as a definition.  */
10287
10288       switch (a->dw_attr)
10289         {
10290         case DW_AT_artificial:
10291         case DW_AT_containing_type:
10292         case DW_AT_external:
10293         case DW_AT_name:
10294         case DW_AT_type:
10295         case DW_AT_virtuality:
10296         case DW_AT_linkage_name:
10297         case DW_AT_MIPS_linkage_name:
10298           add_dwarf_attr (clone, a);
10299           break;
10300         case DW_AT_byte_size:
10301         default:
10302           break;
10303         }
10304     }
10305
10306   if (die->die_id.die_type_node)
10307     add_AT_die_ref (clone, DW_AT_signature, die);
10308
10309   add_AT_flag (clone, DW_AT_declaration, 1);
10310   return clone;
10311 }
10312
10313 /* Copy the declaration context to the new compile unit DIE.  This includes
10314    any surrounding namespace or type declarations.  If the DIE has an
10315    AT_specification attribute, it also includes attributes and children
10316    attached to the specification.  */
10317
10318 static void
10319 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
10320 {
10321   dw_die_ref decl;
10322   dw_die_ref new_decl;
10323
10324   decl = get_AT_ref (die, DW_AT_specification);
10325   if (decl == NULL)
10326     decl = die;
10327   else
10328     {
10329       unsigned ix;
10330       dw_die_ref c;
10331       dw_attr_ref a;
10332
10333       /* Copy the type node pointer from the new DIE to the original
10334          declaration DIE so we can forward references later.  */
10335       decl->die_id.die_type_node = die->die_id.die_type_node;
10336
10337       remove_AT (die, DW_AT_specification);
10338
10339       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
10340         {
10341           if (a->dw_attr != DW_AT_name
10342               && a->dw_attr != DW_AT_declaration
10343               && a->dw_attr != DW_AT_external)
10344             add_dwarf_attr (die, a);
10345         }
10346
10347       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
10348     }
10349
10350   if (decl->die_parent != NULL
10351       && decl->die_parent->die_tag != DW_TAG_compile_unit
10352       && decl->die_parent->die_tag != DW_TAG_type_unit)
10353     {
10354       new_decl = copy_ancestor_tree (unit, decl, NULL);
10355       if (new_decl != NULL)
10356         {
10357           remove_AT (new_decl, DW_AT_signature);
10358           add_AT_specification (die, new_decl);
10359         }
10360     }
10361 }
10362
10363 /* Generate the skeleton ancestor tree for the given NODE, then clone
10364    the DIE and add the clone into the tree.  */
10365
10366 static void
10367 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
10368 {
10369   if (node->new_die != NULL)
10370     return;
10371
10372   node->new_die = clone_as_declaration (node->old_die);
10373
10374   if (node->parent != NULL)
10375     {
10376       generate_skeleton_ancestor_tree (node->parent);
10377       add_child_die (node->parent->new_die, node->new_die);
10378     }
10379 }
10380
10381 /* Generate a skeleton tree of DIEs containing any declarations that are
10382    found in the original tree.  We traverse the tree looking for declaration
10383    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
10384
10385 static void
10386 generate_skeleton_bottom_up (skeleton_chain_node *parent)
10387 {
10388   skeleton_chain_node node;
10389   dw_die_ref c;
10390   dw_die_ref first;
10391   dw_die_ref prev = NULL;
10392   dw_die_ref next = NULL;
10393
10394   node.parent = parent;
10395
10396   first = c = parent->old_die->die_child;
10397   if (c)
10398     next = c->die_sib;
10399   if (c) do {
10400     if (prev == NULL || prev->die_sib == c)
10401       prev = c;
10402     c = next;
10403     next = (c == first ? NULL : c->die_sib);
10404     node.old_die = c;
10405     node.new_die = NULL;
10406     if (is_declaration_die (c))
10407       {
10408         /* Clone the existing DIE, move the original to the skeleton
10409            tree (which is in the main CU), and put the clone, with
10410            all the original's children, where the original came from.  */
10411         dw_die_ref clone = clone_die (c);
10412         move_all_children (c, clone);
10413
10414         replace_child (c, clone, prev);
10415         generate_skeleton_ancestor_tree (parent);
10416         add_child_die (parent->new_die, c);
10417         node.new_die = c;
10418         c = clone;
10419       }
10420     generate_skeleton_bottom_up (&node);
10421   } while (next != NULL);
10422 }
10423
10424 /* Wrapper function for generate_skeleton_bottom_up.  */
10425
10426 static dw_die_ref
10427 generate_skeleton (dw_die_ref die)
10428 {
10429   skeleton_chain_node node;
10430
10431   node.old_die = die;
10432   node.new_die = NULL;
10433   node.parent = NULL;
10434
10435   /* If this type definition is nested inside another type,
10436      always leave at least a declaration in its place.  */
10437   if (die->die_parent != NULL && is_type_die (die->die_parent))
10438     node.new_die = clone_as_declaration (die);
10439
10440   generate_skeleton_bottom_up (&node);
10441   return node.new_die;
10442 }
10443
10444 /* Remove the DIE from its parent, possibly replacing it with a cloned
10445    declaration.  The original DIE will be moved to a new compile unit
10446    so that existing references to it follow it to the new location.  If
10447    any of the original DIE's descendants is a declaration, we need to
10448    replace the original DIE with a skeleton tree and move the
10449    declarations back into the skeleton tree.  */
10450
10451 static dw_die_ref
10452 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
10453 {
10454   dw_die_ref skeleton;
10455
10456   skeleton = generate_skeleton (child);
10457   if (skeleton == NULL)
10458     remove_child_with_prev (child, prev);
10459   else
10460     {
10461       skeleton->die_id.die_type_node = child->die_id.die_type_node;
10462       replace_child (child, skeleton, prev);
10463     }
10464
10465   return skeleton;
10466 }
10467
10468 /* Traverse the DIE and set up additional .debug_types sections for each
10469    type worthy of being placed in a COMDAT section.  */
10470
10471 static void
10472 break_out_comdat_types (dw_die_ref die)
10473 {
10474   dw_die_ref c;
10475   dw_die_ref first;
10476   dw_die_ref prev = NULL;
10477   dw_die_ref next = NULL;
10478   dw_die_ref unit = NULL;
10479
10480   first = c = die->die_child;
10481   if (c)
10482     next = c->die_sib;
10483   if (c) do {
10484     if (prev == NULL || prev->die_sib == c)
10485       prev = c;
10486     c = next;
10487     next = (c == first ? NULL : c->die_sib);
10488     if (should_move_die_to_comdat (c))
10489       {
10490         dw_die_ref replacement;
10491         comdat_type_node_ref type_node;
10492
10493         /* Create a new type unit DIE as the root for the new tree, and
10494            add it to the list of comdat types.  */
10495         unit = new_die (DW_TAG_type_unit, NULL, NULL);
10496         add_AT_unsigned (unit, DW_AT_language,
10497                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
10498         type_node = ggc_alloc_cleared_comdat_type_node ();
10499         type_node->root_die = unit;
10500         type_node->next = comdat_type_list;
10501         comdat_type_list = type_node;
10502
10503         /* Generate the type signature.  */
10504         generate_type_signature (c, type_node);
10505
10506         /* Copy the declaration context, attributes, and children of the
10507            declaration into the new compile unit DIE.  */
10508         copy_declaration_context (unit, c);
10509
10510         /* Remove this DIE from the main CU.  */
10511         replacement = remove_child_or_replace_with_skeleton (c, prev);
10512
10513         /* Break out nested types into their own type units.  */
10514         break_out_comdat_types (c);
10515
10516         /* Add the DIE to the new compunit.  */
10517         add_child_die (unit, c);
10518
10519         if (replacement != NULL)
10520           c = replacement;
10521       }
10522     else if (c->die_tag == DW_TAG_namespace
10523              || c->die_tag == DW_TAG_class_type
10524              || c->die_tag == DW_TAG_structure_type
10525              || c->die_tag == DW_TAG_union_type)
10526       {
10527         /* Look for nested types that can be broken out.  */
10528         break_out_comdat_types (c);
10529       }
10530   } while (next != NULL);
10531 }
10532
10533 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10534
10535 struct decl_table_entry
10536 {
10537   dw_die_ref orig;
10538   dw_die_ref copy;
10539 };
10540
10541 /* Routines to manipulate hash table of copied declarations.  */
10542
10543 static hashval_t
10544 htab_decl_hash (const void *of)
10545 {
10546   const struct decl_table_entry *const entry =
10547     (const struct decl_table_entry *) of;
10548
10549   return htab_hash_pointer (entry->orig);
10550 }
10551
10552 static int
10553 htab_decl_eq (const void *of1, const void *of2)
10554 {
10555   const struct decl_table_entry *const entry1 =
10556     (const struct decl_table_entry *) of1;
10557   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10558
10559   return entry1->orig == entry2;
10560 }
10561
10562 static void
10563 htab_decl_del (void *what)
10564 {
10565   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10566
10567   free (entry);
10568 }
10569
10570 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10571    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10572    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10573    to check if the ancestor has already been copied into UNIT.  */
10574
10575 static dw_die_ref
10576 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10577 {
10578   dw_die_ref parent = die->die_parent;
10579   dw_die_ref new_parent = unit;
10580   dw_die_ref copy;
10581   void **slot = NULL;
10582   struct decl_table_entry *entry = NULL;
10583
10584   if (decl_table)
10585     {
10586       /* Check if the entry has already been copied to UNIT.  */
10587       slot = htab_find_slot_with_hash (decl_table, die,
10588                                        htab_hash_pointer (die), INSERT);
10589       if (*slot != HTAB_EMPTY_ENTRY)
10590         {
10591           entry = (struct decl_table_entry *) *slot;
10592           return entry->copy;
10593         }
10594
10595       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10596       entry = XCNEW (struct decl_table_entry);
10597       entry->orig = die;
10598       entry->copy = NULL;
10599       *slot = entry;
10600     }
10601
10602   if (parent != NULL)
10603     {
10604       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10605       if (spec != NULL)
10606         parent = spec;
10607       if (parent->die_tag != DW_TAG_compile_unit
10608           && parent->die_tag != DW_TAG_type_unit)
10609         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10610     }
10611
10612   copy = clone_as_declaration (die);
10613   add_child_die (new_parent, copy);
10614
10615   if (decl_table != NULL)
10616     {
10617       /* Record the pointer to the copy.  */
10618       entry->copy = copy;
10619     }
10620
10621   return copy;
10622 }
10623
10624 /* Walk the DIE and its children, looking for references to incomplete
10625    or trivial types that are unmarked (i.e., that are not in the current
10626    type_unit).  */
10627
10628 static void
10629 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10630 {
10631   dw_die_ref c;
10632   dw_attr_ref a;
10633   unsigned ix;
10634
10635   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10636     {
10637       if (AT_class (a) == dw_val_class_die_ref)
10638         {
10639           dw_die_ref targ = AT_ref (a);
10640           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10641           void **slot;
10642           struct decl_table_entry *entry;
10643
10644           if (targ->die_mark != 0 || type_node != NULL)
10645             continue;
10646
10647           slot = htab_find_slot_with_hash (decl_table, targ,
10648                                            htab_hash_pointer (targ), INSERT);
10649
10650           if (*slot != HTAB_EMPTY_ENTRY)
10651             {
10652               /* TARG has already been copied, so we just need to
10653                  modify the reference to point to the copy.  */
10654               entry = (struct decl_table_entry *) *slot;
10655               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10656             }
10657           else
10658             {
10659               dw_die_ref parent = unit;
10660               dw_die_ref copy = clone_tree (targ);
10661
10662               /* Make sure the cloned tree is marked as part of the
10663                  type unit.  */
10664               mark_dies (copy);
10665
10666               /* Record in DECL_TABLE that TARG has been copied.
10667                  Need to do this now, before the recursive call,
10668                  because DECL_TABLE may be expanded and SLOT
10669                  would no longer be a valid pointer.  */
10670               entry = XCNEW (struct decl_table_entry);
10671               entry->orig = targ;
10672               entry->copy = copy;
10673               *slot = entry;
10674
10675               /* If TARG has surrounding context, copy its ancestor tree
10676                  into the new type unit.  */
10677               if (targ->die_parent != NULL
10678                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10679                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10680                 parent = copy_ancestor_tree (unit, targ->die_parent,
10681                                              decl_table);
10682
10683               add_child_die (parent, copy);
10684               a->dw_attr_val.v.val_die_ref.die = copy;
10685
10686               /* Make sure the newly-copied DIE is walked.  If it was
10687                  installed in a previously-added context, it won't
10688                  get visited otherwise.  */
10689               if (parent != unit)
10690                 {
10691                   /* Find the highest point of the newly-added tree,
10692                      mark each node along the way, and walk from there.  */
10693                   parent->die_mark = 1;
10694                   while (parent->die_parent
10695                          && parent->die_parent->die_mark == 0)
10696                     {
10697                       parent = parent->die_parent;
10698                       parent->die_mark = 1;
10699                     }
10700                   copy_decls_walk (unit, parent, decl_table);
10701                 }
10702             }
10703         }
10704     }
10705
10706   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10707 }
10708
10709 /* Copy declarations for "unworthy" types into the new comdat section.
10710    Incomplete types, modified types, and certain other types aren't broken
10711    out into comdat sections of their own, so they don't have a signature,
10712    and we need to copy the declaration into the same section so that we
10713    don't have an external reference.  */
10714
10715 static void
10716 copy_decls_for_unworthy_types (dw_die_ref unit)
10717 {
10718   htab_t decl_table;
10719
10720   mark_dies (unit);
10721   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10722   copy_decls_walk (unit, unit, decl_table);
10723   htab_delete (decl_table);
10724   unmark_dies (unit);
10725 }
10726
10727 /* Traverse the DIE and add a sibling attribute if it may have the
10728    effect of speeding up access to siblings.  To save some space,
10729    avoid generating sibling attributes for DIE's without children.  */
10730
10731 static void
10732 add_sibling_attributes (dw_die_ref die)
10733 {
10734   dw_die_ref c;
10735
10736   if (! die->die_child)
10737     return;
10738
10739   if (die->die_parent && die != die->die_parent->die_child)
10740     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10741
10742   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10743 }
10744
10745 /* Output all location lists for the DIE and its children.  */
10746
10747 static void
10748 output_location_lists (dw_die_ref die)
10749 {
10750   dw_die_ref c;
10751   dw_attr_ref a;
10752   unsigned ix;
10753
10754   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10755     if (AT_class (a) == dw_val_class_loc_list)
10756       output_loc_list (AT_loc_list (a));
10757
10758   FOR_EACH_CHILD (die, c, output_location_lists (c));
10759 }
10760
10761 /* The format of each DIE (and its attribute value pairs) is encoded in an
10762    abbreviation table.  This routine builds the abbreviation table and assigns
10763    a unique abbreviation id for each abbreviation entry.  The children of each
10764    die are visited recursively.  */
10765
10766 static void
10767 build_abbrev_table (dw_die_ref die)
10768 {
10769   unsigned long abbrev_id;
10770   unsigned int n_alloc;
10771   dw_die_ref c;
10772   dw_attr_ref a;
10773   unsigned ix;
10774
10775   /* Scan the DIE references, and mark as external any that refer to
10776      DIEs from other CUs (i.e. those which are not marked).  */
10777   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10778     if (AT_class (a) == dw_val_class_die_ref
10779         && AT_ref (a)->die_mark == 0)
10780       {
10781         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
10782         set_AT_ref_external (a, 1);
10783       }
10784
10785   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10786     {
10787       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10788       dw_attr_ref die_a, abbrev_a;
10789       unsigned ix;
10790       bool ok = true;
10791
10792       if (abbrev->die_tag != die->die_tag)
10793         continue;
10794       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10795         continue;
10796
10797       if (VEC_length (dw_attr_node, abbrev->die_attr)
10798           != VEC_length (dw_attr_node, die->die_attr))
10799         continue;
10800
10801       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
10802         {
10803           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10804           if ((abbrev_a->dw_attr != die_a->dw_attr)
10805               || (value_format (abbrev_a) != value_format (die_a)))
10806             {
10807               ok = false;
10808               break;
10809             }
10810         }
10811       if (ok)
10812         break;
10813     }
10814
10815   if (abbrev_id >= abbrev_die_table_in_use)
10816     {
10817       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10818         {
10819           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10820           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10821                                             n_alloc);
10822
10823           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10824                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10825           abbrev_die_table_allocated = n_alloc;
10826         }
10827
10828       ++abbrev_die_table_in_use;
10829       abbrev_die_table[abbrev_id] = die;
10830     }
10831
10832   die->die_abbrev = abbrev_id;
10833   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10834 }
10835 \f
10836 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10837
10838 static int
10839 constant_size (unsigned HOST_WIDE_INT value)
10840 {
10841   int log;
10842
10843   if (value == 0)
10844     log = 0;
10845   else
10846     log = floor_log2 (value);
10847
10848   log = log / 8;
10849   log = 1 << (floor_log2 (log) + 1);
10850
10851   return log;
10852 }
10853
10854 /* Return the size of a DIE as it is represented in the
10855    .debug_info section.  */
10856
10857 static unsigned long
10858 size_of_die (dw_die_ref die)
10859 {
10860   unsigned long size = 0;
10861   dw_attr_ref a;
10862   unsigned ix;
10863
10864   size += size_of_uleb128 (die->die_abbrev);
10865   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
10866     {
10867       switch (AT_class (a))
10868         {
10869         case dw_val_class_addr:
10870           size += DWARF2_ADDR_SIZE;
10871           break;
10872         case dw_val_class_offset:
10873           size += DWARF_OFFSET_SIZE;
10874           break;
10875         case dw_val_class_loc:
10876           {
10877             unsigned long lsize = size_of_locs (AT_loc (a));
10878
10879             /* Block length.  */
10880             if (dwarf_version >= 4)
10881               size += size_of_uleb128 (lsize);
10882             else
10883               size += constant_size (lsize);
10884             size += lsize;
10885           }
10886           break;
10887         case dw_val_class_loc_list:
10888           size += DWARF_OFFSET_SIZE;
10889           break;
10890         case dw_val_class_range_list:
10891           size += DWARF_OFFSET_SIZE;
10892           break;
10893         case dw_val_class_const:
10894           size += size_of_sleb128 (AT_int (a));
10895           break;
10896         case dw_val_class_unsigned_const:
10897           size += constant_size (AT_unsigned (a));
10898           break;
10899         case dw_val_class_const_double:
10900           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10901           if (HOST_BITS_PER_WIDE_INT >= 64)
10902             size++; /* block */
10903           break;
10904         case dw_val_class_vec:
10905           size += constant_size (a->dw_attr_val.v.val_vec.length
10906                                  * a->dw_attr_val.v.val_vec.elt_size)
10907                   + a->dw_attr_val.v.val_vec.length
10908                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10909           break;
10910         case dw_val_class_flag:
10911           if (dwarf_version >= 4)
10912             /* Currently all add_AT_flag calls pass in 1 as last argument,
10913                so DW_FORM_flag_present can be used.  If that ever changes,
10914                we'll need to use DW_FORM_flag and have some optimization
10915                in build_abbrev_table that will change those to
10916                DW_FORM_flag_present if it is set to 1 in all DIEs using
10917                the same abbrev entry.  */
10918             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10919           else
10920             size += 1;
10921           break;
10922         case dw_val_class_die_ref:
10923           if (AT_ref_external (a))
10924             {
10925               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
10926                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10927                  is sized by target address length, whereas in DWARF3
10928                  it's always sized as an offset.  */
10929               if (use_debug_types)
10930                 size += DWARF_TYPE_SIGNATURE_SIZE;
10931               else if (dwarf_version == 2)
10932                 size += DWARF2_ADDR_SIZE;
10933               else
10934                 size += DWARF_OFFSET_SIZE;
10935             }
10936           else
10937             size += DWARF_OFFSET_SIZE;
10938           break;
10939         case dw_val_class_fde_ref:
10940           size += DWARF_OFFSET_SIZE;
10941           break;
10942         case dw_val_class_lbl_id:
10943           size += DWARF2_ADDR_SIZE;
10944           break;
10945         case dw_val_class_lineptr:
10946         case dw_val_class_macptr:
10947           size += DWARF_OFFSET_SIZE;
10948           break;
10949         case dw_val_class_str:
10950           if (AT_string_form (a) == DW_FORM_strp)
10951             size += DWARF_OFFSET_SIZE;
10952           else
10953             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10954           break;
10955         case dw_val_class_file:
10956           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10957           break;
10958         case dw_val_class_data8:
10959           size += 8;
10960           break;
10961         case dw_val_class_vms_delta:
10962           size += DWARF_OFFSET_SIZE;
10963           break;
10964         default:
10965           gcc_unreachable ();
10966         }
10967     }
10968
10969   return size;
10970 }
10971
10972 /* Size the debugging information associated with a given DIE.  Visits the
10973    DIE's children recursively.  Updates the global variable next_die_offset, on
10974    each time through.  Uses the current value of next_die_offset to update the
10975    die_offset field in each DIE.  */
10976
10977 static void
10978 calc_die_sizes (dw_die_ref die)
10979 {
10980   dw_die_ref c;
10981
10982   gcc_assert (die->die_offset == 0
10983               || (unsigned long int) die->die_offset == next_die_offset);
10984   die->die_offset = next_die_offset;
10985   next_die_offset += size_of_die (die);
10986
10987   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10988
10989   if (die->die_child != NULL)
10990     /* Count the null byte used to terminate sibling lists.  */
10991     next_die_offset += 1;
10992 }
10993
10994 /* Size just the base type children at the start of the CU.
10995    This is needed because build_abbrev needs to size locs
10996    and sizing of type based stack ops needs to know die_offset
10997    values for the base types.  */
10998
10999 static void
11000 calc_base_type_die_sizes (void)
11001 {
11002   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11003   unsigned int i;
11004   dw_die_ref base_type;
11005 #if ENABLE_ASSERT_CHECKING
11006   dw_die_ref prev = comp_unit_die ()->die_child;
11007 #endif
11008
11009   die_offset += size_of_die (comp_unit_die ());
11010   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
11011     {
11012 #if ENABLE_ASSERT_CHECKING
11013       gcc_assert (base_type->die_offset == 0
11014                   && prev->die_sib == base_type
11015                   && base_type->die_child == NULL
11016                   && base_type->die_abbrev);
11017       prev = base_type;
11018 #endif
11019       base_type->die_offset = die_offset;
11020       die_offset += size_of_die (base_type);
11021     }
11022 }
11023
11024 /* Set the marks for a die and its children.  We do this so
11025    that we know whether or not a reference needs to use FORM_ref_addr; only
11026    DIEs in the same CU will be marked.  We used to clear out the offset
11027    and use that as the flag, but ran into ordering problems.  */
11028
11029 static void
11030 mark_dies (dw_die_ref die)
11031 {
11032   dw_die_ref c;
11033
11034   gcc_assert (!die->die_mark);
11035
11036   die->die_mark = 1;
11037   FOR_EACH_CHILD (die, c, mark_dies (c));
11038 }
11039
11040 /* Clear the marks for a die and its children.  */
11041
11042 static void
11043 unmark_dies (dw_die_ref die)
11044 {
11045   dw_die_ref c;
11046
11047   if (! use_debug_types)
11048     gcc_assert (die->die_mark);
11049
11050   die->die_mark = 0;
11051   FOR_EACH_CHILD (die, c, unmark_dies (c));
11052 }
11053
11054 /* Clear the marks for a die, its children and referred dies.  */
11055
11056 static void
11057 unmark_all_dies (dw_die_ref die)
11058 {
11059   dw_die_ref c;
11060   dw_attr_ref a;
11061   unsigned ix;
11062
11063   if (!die->die_mark)
11064     return;
11065   die->die_mark = 0;
11066
11067   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
11068
11069   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11070     if (AT_class (a) == dw_val_class_die_ref)
11071       unmark_all_dies (AT_ref (a));
11072 }
11073
11074 /* Return the size of the .debug_pubnames or .debug_pubtypes table
11075    generated for the compilation unit.  */
11076
11077 static unsigned long
11078 size_of_pubnames (VEC (pubname_entry, gc) * names)
11079 {
11080   unsigned long size;
11081   unsigned i;
11082   pubname_ref p;
11083
11084   size = DWARF_PUBNAMES_HEADER_SIZE;
11085   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
11086     if (names != pubtype_table
11087         || p->die->die_offset != 0
11088         || !flag_eliminate_unused_debug_types)
11089       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
11090
11091   size += DWARF_OFFSET_SIZE;
11092   return size;
11093 }
11094
11095 /* Return the size of the information in the .debug_aranges section.  */
11096
11097 static unsigned long
11098 size_of_aranges (void)
11099 {
11100   unsigned long size;
11101
11102   size = DWARF_ARANGES_HEADER_SIZE;
11103
11104   /* Count the address/length pair for this compilation unit.  */
11105   if (text_section_used)
11106     size += 2 * DWARF2_ADDR_SIZE;
11107   if (cold_text_section_used)
11108     size += 2 * DWARF2_ADDR_SIZE;
11109   if (have_multiple_function_sections)
11110     {
11111       unsigned fde_idx = 0;
11112
11113       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
11114         {
11115           dw_fde_ref fde = &fde_table[fde_idx];
11116
11117           if (!fde->in_std_section)
11118             size += 2 * DWARF2_ADDR_SIZE;
11119           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
11120             size += 2 * DWARF2_ADDR_SIZE;
11121         }
11122     }
11123
11124   /* Count the two zero words used to terminated the address range table.  */
11125   size += 2 * DWARF2_ADDR_SIZE;
11126   return size;
11127 }
11128 \f
11129 /* Select the encoding of an attribute value.  */
11130
11131 static enum dwarf_form
11132 value_format (dw_attr_ref a)
11133 {
11134   switch (a->dw_attr_val.val_class)
11135     {
11136     case dw_val_class_addr:
11137       /* Only very few attributes allow DW_FORM_addr.  */
11138       switch (a->dw_attr)
11139         {
11140         case DW_AT_low_pc:
11141         case DW_AT_high_pc:
11142         case DW_AT_entry_pc:
11143         case DW_AT_trampoline:
11144           return DW_FORM_addr;
11145         default:
11146           break;
11147         }
11148       switch (DWARF2_ADDR_SIZE)
11149         {
11150         case 1:
11151           return DW_FORM_data1;
11152         case 2:
11153           return DW_FORM_data2;
11154         case 4:
11155           return DW_FORM_data4;
11156         case 8:
11157           return DW_FORM_data8;
11158         default:
11159           gcc_unreachable ();
11160         }
11161     case dw_val_class_range_list:
11162     case dw_val_class_loc_list:
11163       if (dwarf_version >= 4)
11164         return DW_FORM_sec_offset;
11165       /* FALLTHRU */
11166     case dw_val_class_vms_delta:
11167     case dw_val_class_offset:
11168       switch (DWARF_OFFSET_SIZE)
11169         {
11170         case 4:
11171           return DW_FORM_data4;
11172         case 8:
11173           return DW_FORM_data8;
11174         default:
11175           gcc_unreachable ();
11176         }
11177     case dw_val_class_loc:
11178       if (dwarf_version >= 4)
11179         return DW_FORM_exprloc;
11180       switch (constant_size (size_of_locs (AT_loc (a))))
11181         {
11182         case 1:
11183           return DW_FORM_block1;
11184         case 2:
11185           return DW_FORM_block2;
11186         default:
11187           gcc_unreachable ();
11188         }
11189     case dw_val_class_const:
11190       return DW_FORM_sdata;
11191     case dw_val_class_unsigned_const:
11192       switch (constant_size (AT_unsigned (a)))
11193         {
11194         case 1:
11195           return DW_FORM_data1;
11196         case 2:
11197           return DW_FORM_data2;
11198         case 4:
11199           return DW_FORM_data4;
11200         case 8:
11201           return DW_FORM_data8;
11202         default:
11203           gcc_unreachable ();
11204         }
11205     case dw_val_class_const_double:
11206       switch (HOST_BITS_PER_WIDE_INT)
11207         {
11208         case 8:
11209           return DW_FORM_data2;
11210         case 16:
11211           return DW_FORM_data4;
11212         case 32:
11213           return DW_FORM_data8;
11214         case 64:
11215         default:
11216           return DW_FORM_block1;
11217         }
11218     case dw_val_class_vec:
11219       switch (constant_size (a->dw_attr_val.v.val_vec.length
11220                              * a->dw_attr_val.v.val_vec.elt_size))
11221         {
11222         case 1:
11223           return DW_FORM_block1;
11224         case 2:
11225           return DW_FORM_block2;
11226         case 4:
11227           return DW_FORM_block4;
11228         default:
11229           gcc_unreachable ();
11230         }
11231     case dw_val_class_flag:
11232       if (dwarf_version >= 4)
11233         {
11234           /* Currently all add_AT_flag calls pass in 1 as last argument,
11235              so DW_FORM_flag_present can be used.  If that ever changes,
11236              we'll need to use DW_FORM_flag and have some optimization
11237              in build_abbrev_table that will change those to
11238              DW_FORM_flag_present if it is set to 1 in all DIEs using
11239              the same abbrev entry.  */
11240           gcc_assert (a->dw_attr_val.v.val_flag == 1);
11241           return DW_FORM_flag_present;
11242         }
11243       return DW_FORM_flag;
11244     case dw_val_class_die_ref:
11245       if (AT_ref_external (a))
11246         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
11247       else
11248         return DW_FORM_ref;
11249     case dw_val_class_fde_ref:
11250       return DW_FORM_data;
11251     case dw_val_class_lbl_id:
11252       return DW_FORM_addr;
11253     case dw_val_class_lineptr:
11254     case dw_val_class_macptr:
11255       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
11256     case dw_val_class_str:
11257       return AT_string_form (a);
11258     case dw_val_class_file:
11259       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
11260         {
11261         case 1:
11262           return DW_FORM_data1;
11263         case 2:
11264           return DW_FORM_data2;
11265         case 4:
11266           return DW_FORM_data4;
11267         default:
11268           gcc_unreachable ();
11269         }
11270
11271     case dw_val_class_data8:
11272       return DW_FORM_data8;
11273
11274     default:
11275       gcc_unreachable ();
11276     }
11277 }
11278
11279 /* Output the encoding of an attribute value.  */
11280
11281 static void
11282 output_value_format (dw_attr_ref a)
11283 {
11284   enum dwarf_form form = value_format (a);
11285
11286   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
11287 }
11288
11289 /* Output the .debug_abbrev section which defines the DIE abbreviation
11290    table.  */
11291
11292 static void
11293 output_abbrev_section (void)
11294 {
11295   unsigned long abbrev_id;
11296
11297   if (abbrev_die_table_in_use == 1)
11298     return;
11299
11300   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
11301     {
11302       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
11303       unsigned ix;
11304       dw_attr_ref a_attr;
11305
11306       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
11307       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
11308                                    dwarf_tag_name (abbrev->die_tag));
11309
11310       if (abbrev->die_child != NULL)
11311         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
11312       else
11313         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
11314
11315       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
11316            ix++)
11317         {
11318           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
11319                                        dwarf_attr_name (a_attr->dw_attr));
11320           output_value_format (a_attr);
11321         }
11322
11323       dw2_asm_output_data (1, 0, NULL);
11324       dw2_asm_output_data (1, 0, NULL);
11325     }
11326
11327   /* Terminate the table.  */
11328   dw2_asm_output_data (1, 0, NULL);
11329 }
11330
11331 /* Output a symbol we can use to refer to this DIE from another CU.  */
11332
11333 static inline void
11334 output_die_symbol (dw_die_ref die)
11335 {
11336   char *sym = die->die_id.die_symbol;
11337
11338   if (sym == 0)
11339     return;
11340
11341   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
11342     /* We make these global, not weak; if the target doesn't support
11343        .linkonce, it doesn't support combining the sections, so debugging
11344        will break.  */
11345     targetm.asm_out.globalize_label (asm_out_file, sym);
11346
11347   ASM_OUTPUT_LABEL (asm_out_file, sym);
11348 }
11349
11350 /* Return a new location list, given the begin and end range, and the
11351    expression.  */
11352
11353 static inline dw_loc_list_ref
11354 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
11355               const char *section)
11356 {
11357   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
11358
11359   retlist->begin = begin;
11360   retlist->end = end;
11361   retlist->expr = expr;
11362   retlist->section = section;
11363
11364   return retlist;
11365 }
11366
11367 /* Generate a new internal symbol for this location list node, if it
11368    hasn't got one yet.  */
11369
11370 static inline void
11371 gen_llsym (dw_loc_list_ref list)
11372 {
11373   gcc_assert (!list->ll_symbol);
11374   list->ll_symbol = gen_internal_sym ("LLST");
11375 }
11376
11377 /* Output the location list given to us.  */
11378
11379 static void
11380 output_loc_list (dw_loc_list_ref list_head)
11381 {
11382   dw_loc_list_ref curr = list_head;
11383
11384   if (list_head->emitted)
11385     return;
11386   list_head->emitted = true;
11387
11388   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
11389
11390   /* Walk the location list, and output each range + expression.  */
11391   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
11392     {
11393       unsigned long size;
11394       /* Don't output an entry that starts and ends at the same address.  */
11395       if (strcmp (curr->begin, curr->end) == 0)
11396         continue;
11397       if (!have_multiple_function_sections)
11398         {
11399           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
11400                                 "Location list begin address (%s)",
11401                                 list_head->ll_symbol);
11402           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
11403                                 "Location list end address (%s)",
11404                                 list_head->ll_symbol);
11405         }
11406       else
11407         {
11408           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
11409                                "Location list begin address (%s)",
11410                                list_head->ll_symbol);
11411           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
11412                                "Location list end address (%s)",
11413                                list_head->ll_symbol);
11414         }
11415       size = size_of_locs (curr->expr);
11416
11417       /* Output the block length for this list of location operations.  */
11418       gcc_assert (size <= 0xffff);
11419       dw2_asm_output_data (2, size, "%s", "Location expression size");
11420
11421       output_loc_sequence (curr->expr, -1);
11422     }
11423
11424   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11425                        "Location list terminator begin (%s)",
11426                        list_head->ll_symbol);
11427   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
11428                        "Location list terminator end (%s)",
11429                        list_head->ll_symbol);
11430 }
11431
11432 /* Output a type signature.  */
11433
11434 static inline void
11435 output_signature (const char *sig, const char *name)
11436 {
11437   int i;
11438
11439   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11440     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
11441 }
11442
11443 /* Output the DIE and its attributes.  Called recursively to generate
11444    the definitions of each child DIE.  */
11445
11446 static void
11447 output_die (dw_die_ref die)
11448 {
11449   dw_attr_ref a;
11450   dw_die_ref c;
11451   unsigned long size;
11452   unsigned ix;
11453
11454   /* If someone in another CU might refer to us, set up a symbol for
11455      them to point to.  */
11456   if (! use_debug_types && die->die_id.die_symbol)
11457     output_die_symbol (die);
11458
11459   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
11460                                (unsigned long)die->die_offset,
11461                                dwarf_tag_name (die->die_tag));
11462
11463   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
11464     {
11465       const char *name = dwarf_attr_name (a->dw_attr);
11466
11467       switch (AT_class (a))
11468         {
11469         case dw_val_class_addr:
11470           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
11471           break;
11472
11473         case dw_val_class_offset:
11474           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
11475                                "%s", name);
11476           break;
11477
11478         case dw_val_class_range_list:
11479           {
11480             char *p = strchr (ranges_section_label, '\0');
11481
11482             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
11483                      a->dw_attr_val.v.val_offset);
11484             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
11485                                    debug_ranges_section, "%s", name);
11486             *p = '\0';
11487           }
11488           break;
11489
11490         case dw_val_class_loc:
11491           size = size_of_locs (AT_loc (a));
11492
11493           /* Output the block length for this list of location operations.  */
11494           if (dwarf_version >= 4)
11495             dw2_asm_output_data_uleb128 (size, "%s", name);
11496           else
11497             dw2_asm_output_data (constant_size (size), size, "%s", name);
11498
11499           output_loc_sequence (AT_loc (a), -1);
11500           break;
11501
11502         case dw_val_class_const:
11503           /* ??? It would be slightly more efficient to use a scheme like is
11504              used for unsigned constants below, but gdb 4.x does not sign
11505              extend.  Gdb 5.x does sign extend.  */
11506           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
11507           break;
11508
11509         case dw_val_class_unsigned_const:
11510           dw2_asm_output_data (constant_size (AT_unsigned (a)),
11511                                AT_unsigned (a), "%s", name);
11512           break;
11513
11514         case dw_val_class_const_double:
11515           {
11516             unsigned HOST_WIDE_INT first, second;
11517
11518             if (HOST_BITS_PER_WIDE_INT >= 64)
11519               dw2_asm_output_data (1,
11520                                    2 * HOST_BITS_PER_WIDE_INT
11521                                    / HOST_BITS_PER_CHAR,
11522                                    NULL);
11523
11524             if (WORDS_BIG_ENDIAN)
11525               {
11526                 first = a->dw_attr_val.v.val_double.high;
11527                 second = a->dw_attr_val.v.val_double.low;
11528               }
11529             else
11530               {
11531                 first = a->dw_attr_val.v.val_double.low;
11532                 second = a->dw_attr_val.v.val_double.high;
11533               }
11534
11535             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11536                                  first, name);
11537             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
11538                                  second, NULL);
11539           }
11540           break;
11541
11542         case dw_val_class_vec:
11543           {
11544             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
11545             unsigned int len = a->dw_attr_val.v.val_vec.length;
11546             unsigned int i;
11547             unsigned char *p;
11548
11549             dw2_asm_output_data (constant_size (len * elt_size),
11550                                  len * elt_size, "%s", name);
11551             if (elt_size > sizeof (HOST_WIDE_INT))
11552               {
11553                 elt_size /= 2;
11554                 len *= 2;
11555               }
11556             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
11557                  i < len;
11558                  i++, p += elt_size)
11559               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
11560                                    "fp or vector constant word %u", i);
11561             break;
11562           }
11563
11564         case dw_val_class_flag:
11565           if (dwarf_version >= 4)
11566             {
11567               /* Currently all add_AT_flag calls pass in 1 as last argument,
11568                  so DW_FORM_flag_present can be used.  If that ever changes,
11569                  we'll need to use DW_FORM_flag and have some optimization
11570                  in build_abbrev_table that will change those to
11571                  DW_FORM_flag_present if it is set to 1 in all DIEs using
11572                  the same abbrev entry.  */
11573               gcc_assert (AT_flag (a) == 1);
11574               if (flag_debug_asm)
11575                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11576                          ASM_COMMENT_START, name);
11577               break;
11578             }
11579           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11580           break;
11581
11582         case dw_val_class_loc_list:
11583           {
11584             char *sym = AT_loc_list (a)->ll_symbol;
11585
11586             gcc_assert (sym);
11587             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11588                                    "%s", name);
11589           }
11590           break;
11591
11592         case dw_val_class_die_ref:
11593           if (AT_ref_external (a))
11594             {
11595               if (use_debug_types)
11596                 {
11597                   comdat_type_node_ref type_node =
11598                     AT_ref (a)->die_id.die_type_node;
11599
11600                   gcc_assert (type_node);
11601                   output_signature (type_node->signature, name);
11602                 }
11603               else
11604                 {
11605                   char *sym = AT_ref (a)->die_id.die_symbol;
11606                   int size;
11607
11608                   gcc_assert (sym);
11609                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11610                      length, whereas in DWARF3 it's always sized as an
11611                      offset.  */
11612                   if (dwarf_version == 2)
11613                     size = DWARF2_ADDR_SIZE;
11614                   else
11615                     size = DWARF_OFFSET_SIZE;
11616                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11617                                          name);
11618                 }
11619             }
11620           else
11621             {
11622               gcc_assert (AT_ref (a)->die_offset);
11623               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11624                                    "%s", name);
11625             }
11626           break;
11627
11628         case dw_val_class_fde_ref:
11629           {
11630             char l1[20];
11631
11632             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11633                                          a->dw_attr_val.v.val_fde_index * 2);
11634             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11635                                    "%s", name);
11636           }
11637           break;
11638
11639         case dw_val_class_vms_delta:
11640           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11641                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11642                                     "%s", name);
11643           break;
11644
11645         case dw_val_class_lbl_id:
11646           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11647           break;
11648
11649         case dw_val_class_lineptr:
11650           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11651                                  debug_line_section, "%s", name);
11652           break;
11653
11654         case dw_val_class_macptr:
11655           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11656                                  debug_macinfo_section, "%s", name);
11657           break;
11658
11659         case dw_val_class_str:
11660           if (AT_string_form (a) == DW_FORM_strp)
11661             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11662                                    a->dw_attr_val.v.val_str->label,
11663                                    debug_str_section,
11664                                    "%s: \"%s\"", name, AT_string (a));
11665           else
11666             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11667           break;
11668
11669         case dw_val_class_file:
11670           {
11671             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11672
11673             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11674                                  a->dw_attr_val.v.val_file->filename);
11675             break;
11676           }
11677
11678         case dw_val_class_data8:
11679           {
11680             int i;
11681
11682             for (i = 0; i < 8; i++)
11683               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11684                                    i == 0 ? "%s" : NULL, name);
11685             break;
11686           }
11687
11688         default:
11689           gcc_unreachable ();
11690         }
11691     }
11692
11693   FOR_EACH_CHILD (die, c, output_die (c));
11694
11695   /* Add null byte to terminate sibling list.  */
11696   if (die->die_child != NULL)
11697     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11698                          (unsigned long) die->die_offset);
11699 }
11700
11701 /* Output the compilation unit that appears at the beginning of the
11702    .debug_info section, and precedes the DIE descriptions.  */
11703
11704 static void
11705 output_compilation_unit_header (void)
11706 {
11707   int ver = dwarf_version;
11708
11709   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11710     dw2_asm_output_data (4, 0xffffffff,
11711       "Initial length escape value indicating 64-bit DWARF extension");
11712   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11713                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11714                        "Length of Compilation Unit Info");
11715   dw2_asm_output_data (2, ver, "DWARF version number");
11716   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11717                          debug_abbrev_section,
11718                          "Offset Into Abbrev. Section");
11719   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11720 }
11721
11722 /* Output the compilation unit DIE and its children.  */
11723
11724 static void
11725 output_comp_unit (dw_die_ref die, int output_if_empty)
11726 {
11727   const char *secname;
11728   char *oldsym, *tmp;
11729
11730   /* Unless we are outputting main CU, we may throw away empty ones.  */
11731   if (!output_if_empty && die->die_child == NULL)
11732     return;
11733
11734   /* Even if there are no children of this DIE, we must output the information
11735      about the compilation unit.  Otherwise, on an empty translation unit, we
11736      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11737      will then complain when examining the file.  First mark all the DIEs in
11738      this CU so we know which get local refs.  */
11739   mark_dies (die);
11740
11741   build_abbrev_table (die);
11742
11743   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11744   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11745   calc_die_sizes (die);
11746
11747   oldsym = die->die_id.die_symbol;
11748   if (oldsym)
11749     {
11750       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11751
11752       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11753       secname = tmp;
11754       die->die_id.die_symbol = NULL;
11755       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11756     }
11757   else
11758     {
11759       switch_to_section (debug_info_section);
11760       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
11761       info_section_emitted = true;
11762     }
11763
11764   /* Output debugging information.  */
11765   output_compilation_unit_header ();
11766   output_die (die);
11767
11768   /* Leave the marks on the main CU, so we can check them in
11769      output_pubnames.  */
11770   if (oldsym)
11771     {
11772       unmark_dies (die);
11773       die->die_id.die_symbol = oldsym;
11774     }
11775 }
11776
11777 /* Output a comdat type unit DIE and its children.  */
11778
11779 static void
11780 output_comdat_type_unit (comdat_type_node *node)
11781 {
11782   const char *secname;
11783   char *tmp;
11784   int i;
11785 #if defined (OBJECT_FORMAT_ELF)
11786   tree comdat_key;
11787 #endif
11788
11789   /* First mark all the DIEs in this CU so we know which get local refs.  */
11790   mark_dies (node->root_die);
11791
11792   build_abbrev_table (node->root_die);
11793
11794   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11795   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11796   calc_die_sizes (node->root_die);
11797
11798 #if defined (OBJECT_FORMAT_ELF)
11799   secname = ".debug_types";
11800   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11801   sprintf (tmp, "wt.");
11802   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11803     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11804   comdat_key = get_identifier (tmp);
11805   targetm.asm_out.named_section (secname,
11806                                  SECTION_DEBUG | SECTION_LINKONCE,
11807                                  comdat_key);
11808 #else
11809   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11810   sprintf (tmp, ".gnu.linkonce.wt.");
11811   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11812     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11813   secname = tmp;
11814   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11815 #endif
11816
11817   /* Output debugging information.  */
11818   output_compilation_unit_header ();
11819   output_signature (node->signature, "Type Signature");
11820   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11821                        "Offset to Type DIE");
11822   output_die (node->root_die);
11823
11824   unmark_dies (node->root_die);
11825 }
11826
11827 /* Return the DWARF2/3 pubname associated with a decl.  */
11828
11829 static const char *
11830 dwarf2_name (tree decl, int scope)
11831 {
11832   if (DECL_NAMELESS (decl))
11833     return NULL;
11834   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11835 }
11836
11837 /* Add a new entry to .debug_pubnames if appropriate.  */
11838
11839 static void
11840 add_pubname_string (const char *str, dw_die_ref die)
11841 {
11842   if (targetm.want_debug_pub_sections)
11843     {
11844       pubname_entry e;
11845
11846       e.die = die;
11847       e.name = xstrdup (str);
11848       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11849     }
11850 }
11851
11852 static void
11853 add_pubname (tree decl, dw_die_ref die)
11854 {
11855   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11856     {
11857       const char *name = dwarf2_name (decl, 1);
11858       if (name)
11859         add_pubname_string (name, die);
11860     }
11861 }
11862
11863 /* Add a new entry to .debug_pubtypes if appropriate.  */
11864
11865 static void
11866 add_pubtype (tree decl, dw_die_ref die)
11867 {
11868   pubname_entry e;
11869
11870   if (!targetm.want_debug_pub_sections)
11871     return;
11872
11873   e.name = NULL;
11874   if ((TREE_PUBLIC (decl)
11875        || is_cu_die (die->die_parent))
11876       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11877     {
11878       e.die = die;
11879       if (TYPE_P (decl))
11880         {
11881           if (TYPE_NAME (decl))
11882             {
11883               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11884                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11885               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11886                        && DECL_NAME (TYPE_NAME (decl)))
11887                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11888               else
11889                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11890             }
11891         }
11892       else
11893         {
11894           e.name = dwarf2_name (decl, 1);
11895           if (e.name)
11896             e.name = xstrdup (e.name);
11897         }
11898
11899       /* If we don't have a name for the type, there's no point in adding
11900          it to the table.  */
11901       if (e.name && e.name[0] != '\0')
11902         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11903     }
11904 }
11905
11906 /* Output the public names table used to speed up access to externally
11907    visible names; or the public types table used to find type definitions.  */
11908
11909 static void
11910 output_pubnames (VEC (pubname_entry, gc) * names)
11911 {
11912   unsigned i;
11913   unsigned long pubnames_length = size_of_pubnames (names);
11914   pubname_ref pub;
11915
11916   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11917     dw2_asm_output_data (4, 0xffffffff,
11918       "Initial length escape value indicating 64-bit DWARF extension");
11919   if (names == pubname_table)
11920     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11921                          "Length of Public Names Info");
11922   else
11923     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11924                          "Length of Public Type Names Info");
11925   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11926   dw2_asm_output_data (2, 2, "DWARF Version");
11927   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11928                          debug_info_section,
11929                          "Offset of Compilation Unit Info");
11930   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11931                        "Compilation Unit Length");
11932
11933   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
11934     {
11935       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11936       if (names == pubname_table)
11937         gcc_assert (pub->die->die_mark);
11938
11939       if (names != pubtype_table
11940           || pub->die->die_offset != 0
11941           || !flag_eliminate_unused_debug_types)
11942         {
11943           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11944                                "DIE offset");
11945
11946           dw2_asm_output_nstring (pub->name, -1, "external name");
11947         }
11948     }
11949
11950   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11951 }
11952
11953 /* Output the information that goes into the .debug_aranges table.
11954    Namely, define the beginning and ending address range of the
11955    text section generated for this compilation unit.  */
11956
11957 static void
11958 output_aranges (unsigned long aranges_length)
11959 {
11960   unsigned i;
11961
11962   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11963     dw2_asm_output_data (4, 0xffffffff,
11964       "Initial length escape value indicating 64-bit DWARF extension");
11965   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11966                        "Length of Address Ranges Info");
11967   /* Version number for aranges is still 2, even in DWARF3.  */
11968   dw2_asm_output_data (2, 2, "DWARF Version");
11969   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11970                          debug_info_section,
11971                          "Offset of Compilation Unit Info");
11972   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11973   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11974
11975   /* We need to align to twice the pointer size here.  */
11976   if (DWARF_ARANGES_PAD_SIZE)
11977     {
11978       /* Pad using a 2 byte words so that padding is correct for any
11979          pointer size.  */
11980       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11981                            2 * DWARF2_ADDR_SIZE);
11982       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11983         dw2_asm_output_data (2, 0, NULL);
11984     }
11985
11986   /* It is necessary not to output these entries if the sections were
11987      not used; if the sections were not used, the length will be 0 and
11988      the address may end up as 0 if the section is discarded by ld
11989      --gc-sections, leaving an invalid (0, 0) entry that can be
11990      confused with the terminator.  */
11991   if (text_section_used)
11992     {
11993       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11994       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11995                             text_section_label, "Length");
11996     }
11997   if (cold_text_section_used)
11998     {
11999       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
12000                            "Address");
12001       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
12002                             cold_text_section_label, "Length");
12003     }
12004
12005   if (have_multiple_function_sections)
12006     {
12007       unsigned fde_idx = 0;
12008
12009       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
12010         {
12011           dw_fde_ref fde = &fde_table[fde_idx];
12012
12013           if (!fde->in_std_section)
12014             {
12015               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
12016                                    "Address");
12017               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
12018                                     fde->dw_fde_begin, "Length");
12019             }
12020           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
12021             {
12022               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
12023                                    "Address");
12024               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
12025                                     fde->dw_fde_second_begin, "Length");
12026             }
12027         }
12028     }
12029
12030   /* Output the terminator words.  */
12031   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12032   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12033 }
12034
12035 /* Add a new entry to .debug_ranges.  Return the offset at which it
12036    was placed.  */
12037
12038 static unsigned int
12039 add_ranges_num (int num)
12040 {
12041   unsigned int in_use = ranges_table_in_use;
12042
12043   if (in_use == ranges_table_allocated)
12044     {
12045       ranges_table_allocated += RANGES_TABLE_INCREMENT;
12046       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
12047                                     ranges_table_allocated);
12048       memset (ranges_table + ranges_table_in_use, 0,
12049               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
12050     }
12051
12052   ranges_table[in_use].num = num;
12053   ranges_table_in_use = in_use + 1;
12054
12055   return in_use * 2 * DWARF2_ADDR_SIZE;
12056 }
12057
12058 /* Add a new entry to .debug_ranges corresponding to a block, or a
12059    range terminator if BLOCK is NULL.  */
12060
12061 static unsigned int
12062 add_ranges (const_tree block)
12063 {
12064   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
12065 }
12066
12067 /* Add a new entry to .debug_ranges corresponding to a pair of
12068    labels.  */
12069
12070 static void
12071 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
12072                       bool *added)
12073 {
12074   unsigned int in_use = ranges_by_label_in_use;
12075   unsigned int offset;
12076
12077   if (in_use == ranges_by_label_allocated)
12078     {
12079       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
12080       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
12081                                        ranges_by_label,
12082                                        ranges_by_label_allocated);
12083       memset (ranges_by_label + ranges_by_label_in_use, 0,
12084               RANGES_TABLE_INCREMENT
12085               * sizeof (struct dw_ranges_by_label_struct));
12086     }
12087
12088   ranges_by_label[in_use].begin = begin;
12089   ranges_by_label[in_use].end = end;
12090   ranges_by_label_in_use = in_use + 1;
12091
12092   offset = add_ranges_num (-(int)in_use - 1);
12093   if (!*added)
12094     {
12095       add_AT_range_list (die, DW_AT_ranges, offset);
12096       *added = true;
12097     }
12098 }
12099
12100 static void
12101 output_ranges (void)
12102 {
12103   unsigned i;
12104   static const char *const start_fmt = "Offset %#x";
12105   const char *fmt = start_fmt;
12106
12107   for (i = 0; i < ranges_table_in_use; i++)
12108     {
12109       int block_num = ranges_table[i].num;
12110
12111       if (block_num > 0)
12112         {
12113           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
12114           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
12115
12116           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
12117           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
12118
12119           /* If all code is in the text section, then the compilation
12120              unit base address defaults to DW_AT_low_pc, which is the
12121              base of the text section.  */
12122           if (!have_multiple_function_sections)
12123             {
12124               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
12125                                     text_section_label,
12126                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12127               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
12128                                     text_section_label, NULL);
12129             }
12130
12131           /* Otherwise, the compilation unit base address is zero,
12132              which allows us to use absolute addresses, and not worry
12133              about whether the target supports cross-section
12134              arithmetic.  */
12135           else
12136             {
12137               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
12138                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12139               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
12140             }
12141
12142           fmt = NULL;
12143         }
12144
12145       /* Negative block_num stands for an index into ranges_by_label.  */
12146       else if (block_num < 0)
12147         {
12148           int lab_idx = - block_num - 1;
12149
12150           if (!have_multiple_function_sections)
12151             {
12152               gcc_unreachable ();
12153 #if 0
12154               /* If we ever use add_ranges_by_labels () for a single
12155                  function section, all we have to do is to take out
12156                  the #if 0 above.  */
12157               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12158                                     ranges_by_label[lab_idx].begin,
12159                                     text_section_label,
12160                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
12161               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
12162                                     ranges_by_label[lab_idx].end,
12163                                     text_section_label, NULL);
12164 #endif
12165             }
12166           else
12167             {
12168               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12169                                    ranges_by_label[lab_idx].begin,
12170                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
12171               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
12172                                    ranges_by_label[lab_idx].end,
12173                                    NULL);
12174             }
12175         }
12176       else
12177         {
12178           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12179           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
12180           fmt = start_fmt;
12181         }
12182     }
12183 }
12184
12185 /* Data structure containing information about input files.  */
12186 struct file_info
12187 {
12188   const char *path;     /* Complete file name.  */
12189   const char *fname;    /* File name part.  */
12190   int length;           /* Length of entire string.  */
12191   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
12192   int dir_idx;          /* Index in directory table.  */
12193 };
12194
12195 /* Data structure containing information about directories with source
12196    files.  */
12197 struct dir_info
12198 {
12199   const char *path;     /* Path including directory name.  */
12200   int length;           /* Path length.  */
12201   int prefix;           /* Index of directory entry which is a prefix.  */
12202   int count;            /* Number of files in this directory.  */
12203   int dir_idx;          /* Index of directory used as base.  */
12204 };
12205
12206 /* Callback function for file_info comparison.  We sort by looking at
12207    the directories in the path.  */
12208
12209 static int
12210 file_info_cmp (const void *p1, const void *p2)
12211 {
12212   const struct file_info *const s1 = (const struct file_info *) p1;
12213   const struct file_info *const s2 = (const struct file_info *) p2;
12214   const unsigned char *cp1;
12215   const unsigned char *cp2;
12216
12217   /* Take care of file names without directories.  We need to make sure that
12218      we return consistent values to qsort since some will get confused if
12219      we return the same value when identical operands are passed in opposite
12220      orders.  So if neither has a directory, return 0 and otherwise return
12221      1 or -1 depending on which one has the directory.  */
12222   if ((s1->path == s1->fname || s2->path == s2->fname))
12223     return (s2->path == s2->fname) - (s1->path == s1->fname);
12224
12225   cp1 = (const unsigned char *) s1->path;
12226   cp2 = (const unsigned char *) s2->path;
12227
12228   while (1)
12229     {
12230       ++cp1;
12231       ++cp2;
12232       /* Reached the end of the first path?  If so, handle like above.  */
12233       if ((cp1 == (const unsigned char *) s1->fname)
12234           || (cp2 == (const unsigned char *) s2->fname))
12235         return ((cp2 == (const unsigned char *) s2->fname)
12236                 - (cp1 == (const unsigned char *) s1->fname));
12237
12238       /* Character of current path component the same?  */
12239       else if (*cp1 != *cp2)
12240         return *cp1 - *cp2;
12241     }
12242 }
12243
12244 struct file_name_acquire_data
12245 {
12246   struct file_info *files;
12247   int used_files;
12248   int max_files;
12249 };
12250
12251 /* Traversal function for the hash table.  */
12252
12253 static int
12254 file_name_acquire (void ** slot, void *data)
12255 {
12256   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
12257   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
12258   struct file_info *fi;
12259   const char *f;
12260
12261   gcc_assert (fnad->max_files >= d->emitted_number);
12262
12263   if (! d->emitted_number)
12264     return 1;
12265
12266   gcc_assert (fnad->max_files != fnad->used_files);
12267
12268   fi = fnad->files + fnad->used_files++;
12269
12270   /* Skip all leading "./".  */
12271   f = d->filename;
12272   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
12273     f += 2;
12274
12275   /* Create a new array entry.  */
12276   fi->path = f;
12277   fi->length = strlen (f);
12278   fi->file_idx = d;
12279
12280   /* Search for the file name part.  */
12281   f = strrchr (f, DIR_SEPARATOR);
12282 #if defined (DIR_SEPARATOR_2)
12283   {
12284     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
12285
12286     if (g != NULL)
12287       {
12288         if (f == NULL || f < g)
12289           f = g;
12290       }
12291   }
12292 #endif
12293
12294   fi->fname = f == NULL ? fi->path : f + 1;
12295   return 1;
12296 }
12297
12298 /* Output the directory table and the file name table.  We try to minimize
12299    the total amount of memory needed.  A heuristic is used to avoid large
12300    slowdowns with many input files.  */
12301
12302 static void
12303 output_file_names (void)
12304 {
12305   struct file_name_acquire_data fnad;
12306   int numfiles;
12307   struct file_info *files;
12308   struct dir_info *dirs;
12309   int *saved;
12310   int *savehere;
12311   int *backmap;
12312   int ndirs;
12313   int idx_offset;
12314   int i;
12315
12316   if (!last_emitted_file)
12317     {
12318       dw2_asm_output_data (1, 0, "End directory table");
12319       dw2_asm_output_data (1, 0, "End file name table");
12320       return;
12321     }
12322
12323   numfiles = last_emitted_file->emitted_number;
12324
12325   /* Allocate the various arrays we need.  */
12326   files = XALLOCAVEC (struct file_info, numfiles);
12327   dirs = XALLOCAVEC (struct dir_info, numfiles);
12328
12329   fnad.files = files;
12330   fnad.used_files = 0;
12331   fnad.max_files = numfiles;
12332   htab_traverse (file_table, file_name_acquire, &fnad);
12333   gcc_assert (fnad.used_files == fnad.max_files);
12334
12335   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
12336
12337   /* Find all the different directories used.  */
12338   dirs[0].path = files[0].path;
12339   dirs[0].length = files[0].fname - files[0].path;
12340   dirs[0].prefix = -1;
12341   dirs[0].count = 1;
12342   dirs[0].dir_idx = 0;
12343   files[0].dir_idx = 0;
12344   ndirs = 1;
12345
12346   for (i = 1; i < numfiles; i++)
12347     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
12348         && memcmp (dirs[ndirs - 1].path, files[i].path,
12349                    dirs[ndirs - 1].length) == 0)
12350       {
12351         /* Same directory as last entry.  */
12352         files[i].dir_idx = ndirs - 1;
12353         ++dirs[ndirs - 1].count;
12354       }
12355     else
12356       {
12357         int j;
12358
12359         /* This is a new directory.  */
12360         dirs[ndirs].path = files[i].path;
12361         dirs[ndirs].length = files[i].fname - files[i].path;
12362         dirs[ndirs].count = 1;
12363         dirs[ndirs].dir_idx = ndirs;
12364         files[i].dir_idx = ndirs;
12365
12366         /* Search for a prefix.  */
12367         dirs[ndirs].prefix = -1;
12368         for (j = 0; j < ndirs; j++)
12369           if (dirs[j].length < dirs[ndirs].length
12370               && dirs[j].length > 1
12371               && (dirs[ndirs].prefix == -1
12372                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
12373               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
12374             dirs[ndirs].prefix = j;
12375
12376         ++ndirs;
12377       }
12378
12379   /* Now to the actual work.  We have to find a subset of the directories which
12380      allow expressing the file name using references to the directory table
12381      with the least amount of characters.  We do not do an exhaustive search
12382      where we would have to check out every combination of every single
12383      possible prefix.  Instead we use a heuristic which provides nearly optimal
12384      results in most cases and never is much off.  */
12385   saved = XALLOCAVEC (int, ndirs);
12386   savehere = XALLOCAVEC (int, ndirs);
12387
12388   memset (saved, '\0', ndirs * sizeof (saved[0]));
12389   for (i = 0; i < ndirs; i++)
12390     {
12391       int j;
12392       int total;
12393
12394       /* We can always save some space for the current directory.  But this
12395          does not mean it will be enough to justify adding the directory.  */
12396       savehere[i] = dirs[i].length;
12397       total = (savehere[i] - saved[i]) * dirs[i].count;
12398
12399       for (j = i + 1; j < ndirs; j++)
12400         {
12401           savehere[j] = 0;
12402           if (saved[j] < dirs[i].length)
12403             {
12404               /* Determine whether the dirs[i] path is a prefix of the
12405                  dirs[j] path.  */
12406               int k;
12407
12408               k = dirs[j].prefix;
12409               while (k != -1 && k != (int) i)
12410                 k = dirs[k].prefix;
12411
12412               if (k == (int) i)
12413                 {
12414                   /* Yes it is.  We can possibly save some memory by
12415                      writing the filenames in dirs[j] relative to
12416                      dirs[i].  */
12417                   savehere[j] = dirs[i].length;
12418                   total += (savehere[j] - saved[j]) * dirs[j].count;
12419                 }
12420             }
12421         }
12422
12423       /* Check whether we can save enough to justify adding the dirs[i]
12424          directory.  */
12425       if (total > dirs[i].length + 1)
12426         {
12427           /* It's worthwhile adding.  */
12428           for (j = i; j < ndirs; j++)
12429             if (savehere[j] > 0)
12430               {
12431                 /* Remember how much we saved for this directory so far.  */
12432                 saved[j] = savehere[j];
12433
12434                 /* Remember the prefix directory.  */
12435                 dirs[j].dir_idx = i;
12436               }
12437         }
12438     }
12439
12440   /* Emit the directory name table.  */
12441   idx_offset = dirs[0].length > 0 ? 1 : 0;
12442   for (i = 1 - idx_offset; i < ndirs; i++)
12443     dw2_asm_output_nstring (dirs[i].path,
12444                             dirs[i].length
12445                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
12446                             "Directory Entry: %#x", i + idx_offset);
12447
12448   dw2_asm_output_data (1, 0, "End directory table");
12449
12450   /* We have to emit them in the order of emitted_number since that's
12451      used in the debug info generation.  To do this efficiently we
12452      generate a back-mapping of the indices first.  */
12453   backmap = XALLOCAVEC (int, numfiles);
12454   for (i = 0; i < numfiles; i++)
12455     backmap[files[i].file_idx->emitted_number - 1] = i;
12456
12457   /* Now write all the file names.  */
12458   for (i = 0; i < numfiles; i++)
12459     {
12460       int file_idx = backmap[i];
12461       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
12462
12463 #ifdef VMS_DEBUGGING_INFO
12464 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
12465
12466       /* Setting these fields can lead to debugger miscomparisons,
12467          but VMS Debug requires them to be set correctly.  */
12468
12469       int ver;
12470       long long cdt;
12471       long siz;
12472       int maxfilelen = strlen (files[file_idx].path)
12473                                + dirs[dir_idx].length
12474                                + MAX_VMS_VERSION_LEN + 1;
12475       char *filebuf = XALLOCAVEC (char, maxfilelen);
12476
12477       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
12478       snprintf (filebuf, maxfilelen, "%s;%d",
12479                 files[file_idx].path + dirs[dir_idx].length, ver);
12480
12481       dw2_asm_output_nstring
12482         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
12483
12484       /* Include directory index.  */
12485       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12486
12487       /* Modification time.  */
12488       dw2_asm_output_data_uleb128
12489         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
12490           ? cdt : 0,
12491          NULL);
12492
12493       /* File length in bytes.  */
12494       dw2_asm_output_data_uleb128
12495         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
12496           ? siz : 0,
12497          NULL);
12498 #else
12499       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
12500                               "File Entry: %#x", (unsigned) i + 1);
12501
12502       /* Include directory index.  */
12503       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
12504
12505       /* Modification time.  */
12506       dw2_asm_output_data_uleb128 (0, NULL);
12507
12508       /* File length in bytes.  */
12509       dw2_asm_output_data_uleb128 (0, NULL);
12510 #endif /* VMS_DEBUGGING_INFO */
12511     }
12512
12513   dw2_asm_output_data (1, 0, "End file name table");
12514 }
12515
12516
12517 /* Output one line number table into the .debug_line section.  */
12518
12519 static void
12520 output_one_line_info_table (dw_line_info_table *table)
12521 {
12522   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
12523   unsigned int current_line = 1;
12524   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
12525   dw_line_info_entry *ent;
12526   size_t i;
12527
12528   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
12529     {
12530       switch (ent->opcode)
12531         {
12532         case LI_set_address:
12533           /* ??? Unfortunately, we have little choice here currently, and
12534              must always use the most general form.  GCC does not know the
12535              address delta itself, so we can't use DW_LNS_advance_pc.  Many
12536              ports do have length attributes which will give an upper bound
12537              on the address range.  We could perhaps use length attributes
12538              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
12539           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
12540
12541           /* This can handle any delta.  This takes
12542              4+DWARF2_ADDR_SIZE bytes.  */
12543           dw2_asm_output_data (1, 0, "set address %s", line_label);
12544           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12545           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12546           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12547           break;
12548
12549         case LI_set_line:
12550           if (ent->val == current_line)
12551             {
12552               /* We still need to start a new row, so output a copy insn.  */
12553               dw2_asm_output_data (1, DW_LNS_copy,
12554                                    "copy line %u", current_line);
12555             }
12556           else
12557             {
12558               int line_offset = ent->val - current_line;
12559               int line_delta = line_offset - DWARF_LINE_BASE;
12560
12561               current_line = ent->val;
12562               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12563                 {
12564                   /* This can handle deltas from -10 to 234, using the current
12565                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
12566                      This takes 1 byte.  */
12567                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12568                                        "line %u", current_line);
12569                 }
12570               else
12571                 {
12572                   /* This can handle any delta.  This takes at least 4 bytes,
12573                      depending on the value being encoded.  */
12574                   dw2_asm_output_data (1, DW_LNS_advance_line,
12575                                        "advance to line %u", current_line);
12576                   dw2_asm_output_data_sleb128 (line_offset, NULL);
12577                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
12578                 }
12579             }
12580           break;
12581
12582         case LI_set_file:
12583           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
12584           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12585           break;
12586
12587         case LI_set_column:
12588           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
12589           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
12590           break;
12591
12592         case LI_negate_stmt:
12593           current_is_stmt = !current_is_stmt;
12594           dw2_asm_output_data (1, DW_LNS_negate_stmt,
12595                                "is_stmt %d", current_is_stmt);
12596           break;
12597
12598         case LI_set_prologue_end:
12599           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
12600                                "set prologue end");
12601           break;
12602           
12603         case LI_set_epilogue_begin:
12604           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
12605                                "set epilogue begin");
12606           break;
12607
12608         case LI_set_discriminator:
12609           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
12610           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
12611           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
12612           dw2_asm_output_data_uleb128 (ent->val, NULL);
12613           break;
12614         }
12615     }
12616
12617   /* Emit debug info for the address of the end of the table.  */
12618   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
12619   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12620   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12621   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
12622
12623   dw2_asm_output_data (1, 0, "end sequence");
12624   dw2_asm_output_data_uleb128 (1, NULL);
12625   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12626 }
12627
12628 /* Output the source line number correspondence information.  This
12629    information goes into the .debug_line section.  */
12630
12631 static void
12632 output_line_info (void)
12633 {
12634   char l1[20], l2[20], p1[20], p2[20];
12635   int ver = dwarf_version;
12636   bool saw_one = false;
12637   int opc;
12638
12639   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
12640   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
12641   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
12642   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
12643
12644   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12645     dw2_asm_output_data (4, 0xffffffff,
12646       "Initial length escape value indicating 64-bit DWARF extension");
12647   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
12648                         "Length of Source Line Info");
12649   ASM_OUTPUT_LABEL (asm_out_file, l1);
12650
12651   dw2_asm_output_data (2, ver, "DWARF Version");
12652   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12653   ASM_OUTPUT_LABEL (asm_out_file, p1);
12654
12655   /* Define the architecture-dependent minimum instruction length (in bytes).
12656      In this implementation of DWARF, this field is used for information
12657      purposes only.  Since GCC generates assembly language, we have no
12658      a priori knowledge of how many instruction bytes are generated for each
12659      source line, and therefore can use only the DW_LNE_set_address and
12660      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
12661      this as '1', which is "correct enough" for all architectures,
12662      and don't let the target override.  */
12663   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
12664
12665   if (ver >= 4)
12666     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12667                          "Maximum Operations Per Instruction");
12668   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12669                        "Default is_stmt_start flag");
12670   dw2_asm_output_data (1, DWARF_LINE_BASE,
12671                        "Line Base Value (Special Opcodes)");
12672   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12673                        "Line Range Value (Special Opcodes)");
12674   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12675                        "Special Opcode Base");
12676
12677   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12678     {
12679       int n_op_args;
12680       switch (opc)
12681         {
12682         case DW_LNS_advance_pc:
12683         case DW_LNS_advance_line:
12684         case DW_LNS_set_file:
12685         case DW_LNS_set_column:
12686         case DW_LNS_fixed_advance_pc:
12687         case DW_LNS_set_isa:
12688           n_op_args = 1;
12689           break;
12690         default:
12691           n_op_args = 0;
12692           break;
12693         }
12694
12695       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12696                            opc, n_op_args);
12697     }
12698
12699   /* Write out the information about the files we use.  */
12700   output_file_names ();
12701   ASM_OUTPUT_LABEL (asm_out_file, p2);
12702
12703   if (separate_line_info)
12704     {
12705       dw_line_info_table *table;
12706       size_t i;
12707
12708       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
12709         if (table->in_use)
12710           {
12711             output_one_line_info_table (table);
12712             saw_one = true;
12713           }
12714     }
12715   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
12716     {
12717       output_one_line_info_table (cold_text_section_line_info);
12718       saw_one = true;
12719     }
12720
12721   /* ??? Some Darwin linkers crash on a .debug_line section with no
12722      sequences.  Further, merely a DW_LNE_end_sequence entry is not
12723      sufficient -- the address column must also be initialized.
12724      Make sure to output at least one set_address/end_sequence pair,
12725      choosing .text since that section is always present.  */
12726   if (text_section_line_info->in_use || !saw_one)
12727     output_one_line_info_table (text_section_line_info);
12728
12729   /* Output the marker for the end of the line number info.  */
12730   ASM_OUTPUT_LABEL (asm_out_file, l2);
12731 }
12732 \f
12733 /* Given a pointer to a tree node for some base type, return a pointer to
12734    a DIE that describes the given type.
12735
12736    This routine must only be called for GCC type nodes that correspond to
12737    Dwarf base (fundamental) types.  */
12738
12739 static dw_die_ref
12740 base_type_die (tree type)
12741 {
12742   dw_die_ref base_type_result;
12743   enum dwarf_type encoding;
12744
12745   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12746     return 0;
12747
12748   /* If this is a subtype that should not be emitted as a subrange type,
12749      use the base type.  See subrange_type_for_debug_p.  */
12750   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12751     type = TREE_TYPE (type);
12752
12753   switch (TREE_CODE (type))
12754     {
12755     case INTEGER_TYPE:
12756       if ((dwarf_version >= 4 || !dwarf_strict)
12757           && TYPE_NAME (type)
12758           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12759           && DECL_IS_BUILTIN (TYPE_NAME (type))
12760           && DECL_NAME (TYPE_NAME (type)))
12761         {
12762           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12763           if (strcmp (name, "char16_t") == 0
12764               || strcmp (name, "char32_t") == 0)
12765             {
12766               encoding = DW_ATE_UTF;
12767               break;
12768             }
12769         }
12770       if (TYPE_STRING_FLAG (type))
12771         {
12772           if (TYPE_UNSIGNED (type))
12773             encoding = DW_ATE_unsigned_char;
12774           else
12775             encoding = DW_ATE_signed_char;
12776         }
12777       else if (TYPE_UNSIGNED (type))
12778         encoding = DW_ATE_unsigned;
12779       else
12780         encoding = DW_ATE_signed;
12781       break;
12782
12783     case REAL_TYPE:
12784       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12785         {
12786           if (dwarf_version >= 3 || !dwarf_strict)
12787             encoding = DW_ATE_decimal_float;
12788           else
12789             encoding = DW_ATE_lo_user;
12790         }
12791       else
12792         encoding = DW_ATE_float;
12793       break;
12794
12795     case FIXED_POINT_TYPE:
12796       if (!(dwarf_version >= 3 || !dwarf_strict))
12797         encoding = DW_ATE_lo_user;
12798       else if (TYPE_UNSIGNED (type))
12799         encoding = DW_ATE_unsigned_fixed;
12800       else
12801         encoding = DW_ATE_signed_fixed;
12802       break;
12803
12804       /* Dwarf2 doesn't know anything about complex ints, so use
12805          a user defined type for it.  */
12806     case COMPLEX_TYPE:
12807       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12808         encoding = DW_ATE_complex_float;
12809       else
12810         encoding = DW_ATE_lo_user;
12811       break;
12812
12813     case BOOLEAN_TYPE:
12814       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12815       encoding = DW_ATE_boolean;
12816       break;
12817
12818     default:
12819       /* No other TREE_CODEs are Dwarf fundamental types.  */
12820       gcc_unreachable ();
12821     }
12822
12823   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
12824
12825   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12826                    int_size_in_bytes (type));
12827   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12828
12829   return base_type_result;
12830 }
12831
12832 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12833    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12834
12835 static inline int
12836 is_base_type (tree type)
12837 {
12838   switch (TREE_CODE (type))
12839     {
12840     case ERROR_MARK:
12841     case VOID_TYPE:
12842     case INTEGER_TYPE:
12843     case REAL_TYPE:
12844     case FIXED_POINT_TYPE:
12845     case COMPLEX_TYPE:
12846     case BOOLEAN_TYPE:
12847       return 1;
12848
12849     case ARRAY_TYPE:
12850     case RECORD_TYPE:
12851     case UNION_TYPE:
12852     case QUAL_UNION_TYPE:
12853     case ENUMERAL_TYPE:
12854     case FUNCTION_TYPE:
12855     case METHOD_TYPE:
12856     case POINTER_TYPE:
12857     case REFERENCE_TYPE:
12858     case NULLPTR_TYPE:
12859     case OFFSET_TYPE:
12860     case LANG_TYPE:
12861     case VECTOR_TYPE:
12862       return 0;
12863
12864     default:
12865       gcc_unreachable ();
12866     }
12867
12868   return 0;
12869 }
12870
12871 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12872    node, return the size in bits for the type if it is a constant, or else
12873    return the alignment for the type if the type's size is not constant, or
12874    else return BITS_PER_WORD if the type actually turns out to be an
12875    ERROR_MARK node.  */
12876
12877 static inline unsigned HOST_WIDE_INT
12878 simple_type_size_in_bits (const_tree type)
12879 {
12880   if (TREE_CODE (type) == ERROR_MARK)
12881     return BITS_PER_WORD;
12882   else if (TYPE_SIZE (type) == NULL_TREE)
12883     return 0;
12884   else if (host_integerp (TYPE_SIZE (type), 1))
12885     return tree_low_cst (TYPE_SIZE (type), 1);
12886   else
12887     return TYPE_ALIGN (type);
12888 }
12889
12890 /* Similarly, but return a double_int instead of UHWI.  */
12891
12892 static inline double_int
12893 double_int_type_size_in_bits (const_tree type)
12894 {
12895   if (TREE_CODE (type) == ERROR_MARK)
12896     return uhwi_to_double_int (BITS_PER_WORD);
12897   else if (TYPE_SIZE (type) == NULL_TREE)
12898     return double_int_zero;
12899   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12900     return tree_to_double_int (TYPE_SIZE (type));
12901   else
12902     return uhwi_to_double_int (TYPE_ALIGN (type));
12903 }
12904
12905 /*  Given a pointer to a tree node for a subrange type, return a pointer
12906     to a DIE that describes the given type.  */
12907
12908 static dw_die_ref
12909 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12910 {
12911   dw_die_ref subrange_die;
12912   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12913
12914   if (context_die == NULL)
12915     context_die = comp_unit_die ();
12916
12917   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12918
12919   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12920     {
12921       /* The size of the subrange type and its base type do not match,
12922          so we need to generate a size attribute for the subrange type.  */
12923       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12924     }
12925
12926   if (low)
12927     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12928   if (high)
12929     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12930
12931   return subrange_die;
12932 }
12933
12934 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12935    entry that chains various modifiers in front of the given type.  */
12936
12937 static dw_die_ref
12938 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12939                    dw_die_ref context_die)
12940 {
12941   enum tree_code code = TREE_CODE (type);
12942   dw_die_ref mod_type_die;
12943   dw_die_ref sub_die = NULL;
12944   tree item_type = NULL;
12945   tree qualified_type;
12946   tree name, low, high;
12947
12948   if (code == ERROR_MARK)
12949     return NULL;
12950
12951   /* See if we already have the appropriately qualified variant of
12952      this type.  */
12953   qualified_type
12954     = get_qualified_type (type,
12955                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12956                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12957
12958   if (qualified_type == sizetype
12959       && TYPE_NAME (qualified_type)
12960       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12961     {
12962       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
12963
12964       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
12965                            && TYPE_PRECISION (t)
12966                            == TYPE_PRECISION (qualified_type)
12967                            && TYPE_UNSIGNED (t)
12968                            == TYPE_UNSIGNED (qualified_type));
12969       qualified_type = t;
12970     }
12971
12972   /* If we do, then we can just use its DIE, if it exists.  */
12973   if (qualified_type)
12974     {
12975       mod_type_die = lookup_type_die (qualified_type);
12976       if (mod_type_die)
12977         return mod_type_die;
12978     }
12979
12980   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12981
12982   /* Handle C typedef types.  */
12983   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12984       && !DECL_ARTIFICIAL (name))
12985     {
12986       tree dtype = TREE_TYPE (name);
12987
12988       if (qualified_type == dtype)
12989         {
12990           /* For a named type, use the typedef.  */
12991           gen_type_die (qualified_type, context_die);
12992           return lookup_type_die (qualified_type);
12993         }
12994       else if (is_const_type < TYPE_READONLY (dtype)
12995                || is_volatile_type < TYPE_VOLATILE (dtype)
12996                || (is_const_type <= TYPE_READONLY (dtype)
12997                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12998                    && DECL_ORIGINAL_TYPE (name) != type))
12999         /* cv-unqualified version of named type.  Just use the unnamed
13000            type to which it refers.  */
13001         return modified_type_die (DECL_ORIGINAL_TYPE (name),
13002                                   is_const_type, is_volatile_type,
13003                                   context_die);
13004       /* Else cv-qualified version of named type; fall through.  */
13005     }
13006
13007   if (is_const_type
13008       /* If both is_const_type and is_volatile_type, prefer the path
13009          which leads to a qualified type.  */
13010       && (!is_volatile_type
13011           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
13012           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
13013     {
13014       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
13015       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
13016     }
13017   else if (is_volatile_type)
13018     {
13019       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
13020       sub_die = modified_type_die (type, is_const_type, 0, context_die);
13021     }
13022   else if (code == POINTER_TYPE)
13023     {
13024       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
13025       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13026                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13027       item_type = TREE_TYPE (type);
13028       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13029         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13030                          TYPE_ADDR_SPACE (item_type));
13031     }
13032   else if (code == REFERENCE_TYPE)
13033     {
13034       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
13035         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
13036                                 type);
13037       else
13038         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
13039       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
13040                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
13041       item_type = TREE_TYPE (type);
13042       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
13043         add_AT_unsigned (mod_type_die, DW_AT_address_class,
13044                          TYPE_ADDR_SPACE (item_type));
13045     }
13046   else if (code == INTEGER_TYPE
13047            && TREE_TYPE (type) != NULL_TREE
13048            && subrange_type_for_debug_p (type, &low, &high))
13049     {
13050       mod_type_die = subrange_type_die (type, low, high, context_die);
13051       item_type = TREE_TYPE (type);
13052     }
13053   else if (is_base_type (type))
13054     mod_type_die = base_type_die (type);
13055   else
13056     {
13057       gen_type_die (type, context_die);
13058
13059       /* We have to get the type_main_variant here (and pass that to the
13060          `lookup_type_die' routine) because the ..._TYPE node we have
13061          might simply be a *copy* of some original type node (where the
13062          copy was created to help us keep track of typedef names) and
13063          that copy might have a different TYPE_UID from the original
13064          ..._TYPE node.  */
13065       if (TREE_CODE (type) != VECTOR_TYPE)
13066         return lookup_type_die (type_main_variant (type));
13067       else
13068         /* Vectors have the debugging information in the type,
13069            not the main variant.  */
13070         return lookup_type_die (type);
13071     }
13072
13073   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
13074      don't output a DW_TAG_typedef, since there isn't one in the
13075      user's program; just attach a DW_AT_name to the type.
13076      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
13077      if the base type already has the same name.  */
13078   if (name
13079       && ((TREE_CODE (name) != TYPE_DECL
13080            && (qualified_type == TYPE_MAIN_VARIANT (type)
13081                || (!is_const_type && !is_volatile_type)))
13082           || (TREE_CODE (name) == TYPE_DECL
13083               && TREE_TYPE (name) == qualified_type
13084               && DECL_NAME (name))))
13085     {
13086       if (TREE_CODE (name) == TYPE_DECL)
13087         /* Could just call add_name_and_src_coords_attributes here,
13088            but since this is a builtin type it doesn't have any
13089            useful source coordinates anyway.  */
13090         name = DECL_NAME (name);
13091       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
13092       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
13093     }
13094   /* This probably indicates a bug.  */
13095   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
13096     add_name_attribute (mod_type_die, "__unknown__");
13097
13098   if (qualified_type)
13099     equate_type_number_to_die (qualified_type, mod_type_die);
13100
13101   if (item_type)
13102     /* We must do this after the equate_type_number_to_die call, in case
13103        this is a recursive type.  This ensures that the modified_type_die
13104        recursion will terminate even if the type is recursive.  Recursive
13105        types are possible in Ada.  */
13106     sub_die = modified_type_die (item_type,
13107                                  TYPE_READONLY (item_type),
13108                                  TYPE_VOLATILE (item_type),
13109                                  context_die);
13110
13111   if (sub_die != NULL)
13112     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
13113
13114   return mod_type_die;
13115 }
13116
13117 /* Generate DIEs for the generic parameters of T.
13118    T must be either a generic type or a generic function.
13119    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
13120
13121 static void
13122 gen_generic_params_dies (tree t)
13123 {
13124   tree parms, args;
13125   int parms_num, i;
13126   dw_die_ref die = NULL;
13127
13128   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
13129     return;
13130
13131   if (TYPE_P (t))
13132     die = lookup_type_die (t);
13133   else if (DECL_P (t))
13134     die = lookup_decl_die (t);
13135
13136   gcc_assert (die);
13137
13138   parms = lang_hooks.get_innermost_generic_parms (t);
13139   if (!parms)
13140     /* T has no generic parameter. It means T is neither a generic type
13141        or function. End of story.  */
13142     return;
13143
13144   parms_num = TREE_VEC_LENGTH (parms);
13145   args = lang_hooks.get_innermost_generic_args (t);
13146   for (i = 0; i < parms_num; i++)
13147     {
13148       tree parm, arg, arg_pack_elems;
13149
13150       parm = TREE_VEC_ELT (parms, i);
13151       arg = TREE_VEC_ELT (args, i);
13152       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
13153       gcc_assert (parm && TREE_VALUE (parm) && arg);
13154
13155       if (parm && TREE_VALUE (parm) && arg)
13156         {
13157           /* If PARM represents a template parameter pack,
13158              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
13159              by DW_TAG_template_*_parameter DIEs for the argument
13160              pack elements of ARG. Note that ARG would then be
13161              an argument pack.  */
13162           if (arg_pack_elems)
13163             template_parameter_pack_die (TREE_VALUE (parm),
13164                                          arg_pack_elems,
13165                                          die);
13166           else
13167             generic_parameter_die (TREE_VALUE (parm), arg,
13168                                    true /* Emit DW_AT_name */, die);
13169         }
13170     }
13171 }
13172
13173 /* Create and return a DIE for PARM which should be
13174    the representation of a generic type parameter.
13175    For instance, in the C++ front end, PARM would be a template parameter.
13176    ARG is the argument to PARM.
13177    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
13178    name of the PARM.
13179    PARENT_DIE is the parent DIE which the new created DIE should be added to,
13180    as a child node.  */
13181
13182 static dw_die_ref
13183 generic_parameter_die (tree parm, tree arg,
13184                        bool emit_name_p,
13185                        dw_die_ref parent_die)
13186 {
13187   dw_die_ref tmpl_die = NULL;
13188   const char *name = NULL;
13189
13190   if (!parm || !DECL_NAME (parm) || !arg)
13191     return NULL;
13192
13193   /* We support non-type generic parameters and arguments,
13194      type generic parameters and arguments, as well as
13195      generic generic parameters (a.k.a. template template parameters in C++)
13196      and arguments.  */
13197   if (TREE_CODE (parm) == PARM_DECL)
13198     /* PARM is a nontype generic parameter  */
13199     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
13200   else if (TREE_CODE (parm) == TYPE_DECL)
13201     /* PARM is a type generic parameter.  */
13202     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
13203   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13204     /* PARM is a generic generic parameter.
13205        Its DIE is a GNU extension. It shall have a
13206        DW_AT_name attribute to represent the name of the template template
13207        parameter, and a DW_AT_GNU_template_name attribute to represent the
13208        name of the template template argument.  */
13209     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
13210                         parent_die, parm);
13211   else
13212     gcc_unreachable ();
13213
13214   if (tmpl_die)
13215     {
13216       tree tmpl_type;
13217
13218       /* If PARM is a generic parameter pack, it means we are
13219          emitting debug info for a template argument pack element.
13220          In other terms, ARG is a template argument pack element.
13221          In that case, we don't emit any DW_AT_name attribute for
13222          the die.  */
13223       if (emit_name_p)
13224         {
13225           name = IDENTIFIER_POINTER (DECL_NAME (parm));
13226           gcc_assert (name);
13227           add_AT_string (tmpl_die, DW_AT_name, name);
13228         }
13229
13230       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
13231         {
13232           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
13233              TMPL_DIE should have a child DW_AT_type attribute that is set
13234              to the type of the argument to PARM, which is ARG.
13235              If PARM is a type generic parameter, TMPL_DIE should have a
13236              child DW_AT_type that is set to ARG.  */
13237           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
13238           add_type_attribute (tmpl_die, tmpl_type, 0,
13239                               TREE_THIS_VOLATILE (tmpl_type),
13240                               parent_die);
13241         }
13242       else
13243         {
13244           /* So TMPL_DIE is a DIE representing a
13245              a generic generic template parameter, a.k.a template template
13246              parameter in C++ and arg is a template.  */
13247
13248           /* The DW_AT_GNU_template_name attribute of the DIE must be set
13249              to the name of the argument.  */
13250           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
13251           if (name)
13252             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
13253         }
13254
13255       if (TREE_CODE (parm) == PARM_DECL)
13256         /* So PARM is a non-type generic parameter.
13257            DWARF3 5.6.8 says we must set a DW_AT_const_value child
13258            attribute of TMPL_DIE which value represents the value
13259            of ARG.
13260            We must be careful here:
13261            The value of ARG might reference some function decls.
13262            We might currently be emitting debug info for a generic
13263            type and types are emitted before function decls, we don't
13264            know if the function decls referenced by ARG will actually be
13265            emitted after cgraph computations.
13266            So must defer the generation of the DW_AT_const_value to
13267            after cgraph is ready.  */
13268         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
13269     }
13270
13271   return tmpl_die;
13272 }
13273
13274 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
13275    PARM_PACK must be a template parameter pack. The returned DIE
13276    will be child DIE of PARENT_DIE.  */
13277
13278 static dw_die_ref
13279 template_parameter_pack_die (tree parm_pack,
13280                              tree parm_pack_args,
13281                              dw_die_ref parent_die)
13282 {
13283   dw_die_ref die;
13284   int j;
13285
13286   gcc_assert (parent_die && parm_pack);
13287
13288   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
13289   add_name_and_src_coords_attributes (die, parm_pack);
13290   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
13291     generic_parameter_die (parm_pack,
13292                            TREE_VEC_ELT (parm_pack_args, j),
13293                            false /* Don't emit DW_AT_name */,
13294                            die);
13295   return die;
13296 }
13297
13298 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
13299    an enumerated type.  */
13300
13301 static inline int
13302 type_is_enum (const_tree type)
13303 {
13304   return TREE_CODE (type) == ENUMERAL_TYPE;
13305 }
13306
13307 /* Return the DBX register number described by a given RTL node.  */
13308
13309 static unsigned int
13310 dbx_reg_number (const_rtx rtl)
13311 {
13312   unsigned regno = REGNO (rtl);
13313
13314   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
13315
13316 #ifdef LEAF_REG_REMAP
13317   if (current_function_uses_only_leaf_regs)
13318     {
13319       int leaf_reg = LEAF_REG_REMAP (regno);
13320       if (leaf_reg != -1)
13321         regno = (unsigned) leaf_reg;
13322     }
13323 #endif
13324
13325   return DBX_REGISTER_NUMBER (regno);
13326 }
13327
13328 /* Optionally add a DW_OP_piece term to a location description expression.
13329    DW_OP_piece is only added if the location description expression already
13330    doesn't end with DW_OP_piece.  */
13331
13332 static void
13333 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13334 {
13335   dw_loc_descr_ref loc;
13336
13337   if (*list_head != NULL)
13338     {
13339       /* Find the end of the chain.  */
13340       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13341         ;
13342
13343       if (loc->dw_loc_opc != DW_OP_piece)
13344         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13345     }
13346 }
13347
13348 /* Return a location descriptor that designates a machine register or
13349    zero if there is none.  */
13350
13351 static dw_loc_descr_ref
13352 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13353 {
13354   rtx regs;
13355
13356   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13357     return 0;
13358
13359   /* We only use "frame base" when we're sure we're talking about the
13360      post-prologue local stack frame.  We do this by *not* running
13361      register elimination until this point, and recognizing the special
13362      argument pointer and soft frame pointer rtx's.
13363      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13364   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13365       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13366     {
13367       dw_loc_descr_ref result = NULL;
13368
13369       if (dwarf_version >= 4 || !dwarf_strict)
13370         {
13371           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
13372                                        initialized);
13373           if (result)
13374             add_loc_descr (&result,
13375                            new_loc_descr (DW_OP_stack_value, 0, 0));
13376         }
13377       return result;
13378     }
13379
13380   regs = targetm.dwarf_register_span (rtl);
13381
13382   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13383     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13384   else
13385     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13386 }
13387
13388 /* Return a location descriptor that designates a machine register for
13389    a given hard register number.  */
13390
13391 static dw_loc_descr_ref
13392 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13393 {
13394   dw_loc_descr_ref reg_loc_descr;
13395
13396   if (regno <= 31)
13397     reg_loc_descr
13398       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13399   else
13400     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13401
13402   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13403     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13404
13405   return reg_loc_descr;
13406 }
13407
13408 /* Given an RTL of a register, return a location descriptor that
13409    designates a value that spans more than one register.  */
13410
13411 static dw_loc_descr_ref
13412 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13413                              enum var_init_status initialized)
13414 {
13415   int nregs, size, i;
13416   unsigned reg;
13417   dw_loc_descr_ref loc_result = NULL;
13418
13419   reg = REGNO (rtl);
13420 #ifdef LEAF_REG_REMAP
13421   if (current_function_uses_only_leaf_regs)
13422     {
13423       int leaf_reg = LEAF_REG_REMAP (reg);
13424       if (leaf_reg != -1)
13425         reg = (unsigned) leaf_reg;
13426     }
13427 #endif
13428   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13429   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13430
13431   /* Simple, contiguous registers.  */
13432   if (regs == NULL_RTX)
13433     {
13434       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13435
13436       loc_result = NULL;
13437       while (nregs--)
13438         {
13439           dw_loc_descr_ref t;
13440
13441           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13442                                       VAR_INIT_STATUS_INITIALIZED);
13443           add_loc_descr (&loc_result, t);
13444           add_loc_descr_op_piece (&loc_result, size);
13445           ++reg;
13446         }
13447       return loc_result;
13448     }
13449
13450   /* Now onto stupid register sets in non contiguous locations.  */
13451
13452   gcc_assert (GET_CODE (regs) == PARALLEL);
13453
13454   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13455   loc_result = NULL;
13456
13457   for (i = 0; i < XVECLEN (regs, 0); ++i)
13458     {
13459       dw_loc_descr_ref t;
13460
13461       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13462                                   VAR_INIT_STATUS_INITIALIZED);
13463       add_loc_descr (&loc_result, t);
13464       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13465       add_loc_descr_op_piece (&loc_result, size);
13466     }
13467
13468   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13469     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13470   return loc_result;
13471 }
13472
13473 /* Return a location descriptor that designates a constant.  */
13474
13475 static dw_loc_descr_ref
13476 int_loc_descriptor (HOST_WIDE_INT i)
13477 {
13478   enum dwarf_location_atom op;
13479
13480   /* Pick the smallest representation of a constant, rather than just
13481      defaulting to the LEB encoding.  */
13482   if (i >= 0)
13483     {
13484       if (i <= 31)
13485         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13486       else if (i <= 0xff)
13487         op = DW_OP_const1u;
13488       else if (i <= 0xffff)
13489         op = DW_OP_const2u;
13490       else if (HOST_BITS_PER_WIDE_INT == 32
13491                || i <= 0xffffffff)
13492         op = DW_OP_const4u;
13493       else
13494         op = DW_OP_constu;
13495     }
13496   else
13497     {
13498       if (i >= -0x80)
13499         op = DW_OP_const1s;
13500       else if (i >= -0x8000)
13501         op = DW_OP_const2s;
13502       else if (HOST_BITS_PER_WIDE_INT == 32
13503                || i >= -0x80000000)
13504         op = DW_OP_const4s;
13505       else
13506         op = DW_OP_consts;
13507     }
13508
13509   return new_loc_descr (op, i, 0);
13510 }
13511
13512 /* Return loc description representing "address" of integer value.
13513    This can appear only as toplevel expression.  */
13514
13515 static dw_loc_descr_ref
13516 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13517 {
13518   int litsize;
13519   dw_loc_descr_ref loc_result = NULL;
13520
13521   if (!(dwarf_version >= 4 || !dwarf_strict))
13522     return NULL;
13523
13524   if (i >= 0)
13525     {
13526       if (i <= 31)
13527         litsize = 1;
13528       else if (i <= 0xff)
13529         litsize = 2;
13530       else if (i <= 0xffff)
13531         litsize = 3;
13532       else if (HOST_BITS_PER_WIDE_INT == 32
13533                || i <= 0xffffffff)
13534         litsize = 5;
13535       else
13536         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13537     }
13538   else
13539     {
13540       if (i >= -0x80)
13541         litsize = 2;
13542       else if (i >= -0x8000)
13543         litsize = 3;
13544       else if (HOST_BITS_PER_WIDE_INT == 32
13545                || i >= -0x80000000)
13546         litsize = 5;
13547       else
13548         litsize = 1 + size_of_sleb128 (i);
13549     }
13550   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13551      is more compact.  For DW_OP_stack_value we need:
13552      litsize + 1 (DW_OP_stack_value)
13553      and for DW_OP_implicit_value:
13554      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13555   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13556     {
13557       loc_result = int_loc_descriptor (i);
13558       add_loc_descr (&loc_result,
13559                      new_loc_descr (DW_OP_stack_value, 0, 0));
13560       return loc_result;
13561     }
13562
13563   loc_result = new_loc_descr (DW_OP_implicit_value,
13564                               size, 0);
13565   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13566   loc_result->dw_loc_oprnd2.v.val_int = i;
13567   return loc_result;
13568 }
13569
13570 /* Return a location descriptor that designates a base+offset location.  */
13571
13572 static dw_loc_descr_ref
13573 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13574                  enum var_init_status initialized)
13575 {
13576   unsigned int regno;
13577   dw_loc_descr_ref result;
13578   dw_fde_ref fde = current_fde ();
13579
13580   /* We only use "frame base" when we're sure we're talking about the
13581      post-prologue local stack frame.  We do this by *not* running
13582      register elimination until this point, and recognizing the special
13583      argument pointer and soft frame pointer rtx's.  */
13584   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13585     {
13586       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13587
13588       if (elim != reg)
13589         {
13590           if (GET_CODE (elim) == PLUS)
13591             {
13592               offset += INTVAL (XEXP (elim, 1));
13593               elim = XEXP (elim, 0);
13594             }
13595           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13596                        && (elim == hard_frame_pointer_rtx
13597                            || elim == stack_pointer_rtx))
13598                       || elim == (frame_pointer_needed
13599                                   ? hard_frame_pointer_rtx
13600                                   : stack_pointer_rtx));
13601
13602           /* If drap register is used to align stack, use frame
13603              pointer + offset to access stack variables.  If stack
13604              is aligned without drap, use stack pointer + offset to
13605              access stack variables.  */
13606           if (crtl->stack_realign_tried
13607               && reg == frame_pointer_rtx)
13608             {
13609               int base_reg
13610                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13611                                       ? HARD_FRAME_POINTER_REGNUM
13612                                       : REGNO (elim));
13613               return new_reg_loc_descr (base_reg, offset);
13614             }
13615
13616           offset += frame_pointer_fb_offset;
13617           return new_loc_descr (DW_OP_fbreg, offset, 0);
13618         }
13619     }
13620   else if (!optimize
13621            && fde
13622            && (fde->drap_reg == REGNO (reg)
13623                || fde->vdrap_reg == REGNO (reg)))
13624     {
13625       /* Use cfa+offset to represent the location of arguments passed
13626          on the stack when drap is used to align stack.
13627          Only do this when not optimizing, for optimized code var-tracking
13628          is supposed to track where the arguments live and the register
13629          used as vdrap or drap in some spot might be used for something
13630          else in other part of the routine.  */
13631       return new_loc_descr (DW_OP_fbreg, offset, 0);
13632     }
13633
13634   regno = dbx_reg_number (reg);
13635   if (regno <= 31)
13636     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13637                             offset, 0);
13638   else
13639     result = new_loc_descr (DW_OP_bregx, regno, offset);
13640
13641   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13642     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13643
13644   return result;
13645 }
13646
13647 /* Return true if this RTL expression describes a base+offset calculation.  */
13648
13649 static inline int
13650 is_based_loc (const_rtx rtl)
13651 {
13652   return (GET_CODE (rtl) == PLUS
13653           && ((REG_P (XEXP (rtl, 0))
13654                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13655                && CONST_INT_P (XEXP (rtl, 1)))));
13656 }
13657
13658 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13659    failed.  */
13660
13661 static dw_loc_descr_ref
13662 tls_mem_loc_descriptor (rtx mem)
13663 {
13664   tree base;
13665   dw_loc_descr_ref loc_result;
13666
13667   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13668     return NULL;
13669
13670   base = get_base_address (MEM_EXPR (mem));
13671   if (base == NULL
13672       || TREE_CODE (base) != VAR_DECL
13673       || !DECL_THREAD_LOCAL_P (base))
13674     return NULL;
13675
13676   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13677   if (loc_result == NULL)
13678     return NULL;
13679
13680   if (INTVAL (MEM_OFFSET (mem)))
13681     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13682
13683   return loc_result;
13684 }
13685
13686 /* Output debug info about reason why we failed to expand expression as dwarf
13687    expression.  */
13688
13689 static void
13690 expansion_failed (tree expr, rtx rtl, char const *reason)
13691 {
13692   if (dump_file && (dump_flags & TDF_DETAILS))
13693     {
13694       fprintf (dump_file, "Failed to expand as dwarf: ");
13695       if (expr)
13696         print_generic_expr (dump_file, expr, dump_flags);
13697       if (rtl)
13698         {
13699           fprintf (dump_file, "\n");
13700           print_rtl (dump_file, rtl);
13701         }
13702       fprintf (dump_file, "\nReason: %s\n", reason);
13703     }
13704 }
13705
13706 /* Helper function for const_ok_for_output, called either directly
13707    or via for_each_rtx.  */
13708
13709 static int
13710 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13711 {
13712   rtx rtl = *rtlp;
13713
13714   if (GET_CODE (rtl) == UNSPEC)
13715     {
13716       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13717          we can't express it in the debug info.  */
13718 #ifdef ENABLE_CHECKING
13719       /* Don't complain about TLS UNSPECs, those are just too hard to
13720          delegitimize.  */
13721       if (XVECLEN (rtl, 0) != 1
13722           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
13723           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
13724           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
13725           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
13726         inform (current_function_decl
13727                 ? DECL_SOURCE_LOCATION (current_function_decl)
13728                 : UNKNOWN_LOCATION,
13729 #if NUM_UNSPEC_VALUES > 0
13730                 "non-delegitimized UNSPEC %s (%d) found in variable location",
13731                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
13732                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
13733                 XINT (rtl, 1));
13734 #else
13735                 "non-delegitimized UNSPEC %d found in variable location",
13736                 XINT (rtl, 1));
13737 #endif
13738 #endif
13739       expansion_failed (NULL_TREE, rtl,
13740                         "UNSPEC hasn't been delegitimized.\n");
13741       return 1;
13742     }
13743
13744   if (GET_CODE (rtl) != SYMBOL_REF)
13745     return 0;
13746
13747   if (CONSTANT_POOL_ADDRESS_P (rtl))
13748     {
13749       bool marked;
13750       get_pool_constant_mark (rtl, &marked);
13751       /* If all references to this pool constant were optimized away,
13752          it was not output and thus we can't represent it.  */
13753       if (!marked)
13754         {
13755           expansion_failed (NULL_TREE, rtl,
13756                             "Constant was removed from constant pool.\n");
13757           return 1;
13758         }
13759     }
13760
13761   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13762     return 1;
13763
13764   /* Avoid references to external symbols in debug info, on several targets
13765      the linker might even refuse to link when linking a shared library,
13766      and in many other cases the relocations for .debug_info/.debug_loc are
13767      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13768      to be defined within the same shared library or executable are fine.  */
13769   if (SYMBOL_REF_EXTERNAL_P (rtl))
13770     {
13771       tree decl = SYMBOL_REF_DECL (rtl);
13772
13773       if (decl == NULL || !targetm.binds_local_p (decl))
13774         {
13775           expansion_failed (NULL_TREE, rtl,
13776                             "Symbol not defined in current TU.\n");
13777           return 1;
13778         }
13779     }
13780
13781   return 0;
13782 }
13783
13784 /* Return true if constant RTL can be emitted in DW_OP_addr or
13785    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13786    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13787
13788 static bool
13789 const_ok_for_output (rtx rtl)
13790 {
13791   if (GET_CODE (rtl) == SYMBOL_REF)
13792     return const_ok_for_output_1 (&rtl, NULL) == 0;
13793
13794   if (GET_CODE (rtl) == CONST)
13795     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13796
13797   return true;
13798 }
13799
13800 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
13801    if possible, NULL otherwise.  */
13802
13803 static dw_die_ref
13804 base_type_for_mode (enum machine_mode mode, bool unsignedp)
13805 {
13806   dw_die_ref type_die;
13807   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
13808
13809   if (type == NULL)
13810     return NULL;
13811   switch (TREE_CODE (type))
13812     {
13813     case INTEGER_TYPE:
13814     case REAL_TYPE:
13815       break;
13816     default:
13817       return NULL;
13818     }
13819   type_die = lookup_type_die (type);
13820   if (!type_die)
13821     type_die = modified_type_die (type, false, false, comp_unit_die ());
13822   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
13823     return NULL;
13824   return type_die;
13825 }
13826
13827 /* The following routine converts the RTL for a variable or parameter
13828    (resident in memory) into an equivalent Dwarf representation of a
13829    mechanism for getting the address of that same variable onto the top of a
13830    hypothetical "address evaluation" stack.
13831
13832    When creating memory location descriptors, we are effectively transforming
13833    the RTL for a memory-resident object into its Dwarf postfix expression
13834    equivalent.  This routine recursively descends an RTL tree, turning
13835    it into Dwarf postfix code as it goes.
13836
13837    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
13838
13839    MEM_MODE is the mode of the memory reference, needed to handle some
13840    autoincrement addressing modes.
13841
13842    Return 0 if we can't represent the location.  */
13843
13844 static dw_loc_descr_ref
13845 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13846                     enum machine_mode mem_mode,
13847                     enum var_init_status initialized)
13848 {
13849   dw_loc_descr_ref mem_loc_result = NULL;
13850   enum dwarf_location_atom op;
13851   dw_loc_descr_ref op0, op1;
13852
13853   if (mode == VOIDmode)
13854     mode = GET_MODE (rtl);
13855
13856   /* Note that for a dynamically sized array, the location we will generate a
13857      description of here will be the lowest numbered location which is
13858      actually within the array.  That's *not* necessarily the same as the
13859      zeroth element of the array.  */
13860
13861   rtl = targetm.delegitimize_address (rtl);
13862
13863   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
13864     return NULL;
13865
13866   switch (GET_CODE (rtl))
13867     {
13868     case POST_INC:
13869     case POST_DEC:
13870     case POST_MODIFY:
13871       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
13872
13873     case SUBREG:
13874       /* The case of a subreg may arise when we have a local (register)
13875          variable or a formal (register) parameter which doesn't quite fill
13876          up an entire register.  For now, just assume that it is
13877          legitimate to make the Dwarf info refer to the whole register which
13878          contains the given subreg.  */
13879       if (!subreg_lowpart_p (rtl))
13880         break;
13881       if (GET_MODE_CLASS (mode) == MODE_INT
13882           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
13883           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
13884 #ifdef POINTERS_EXTEND_UNSIGNED
13885               || (mode == Pmode && mem_mode != VOIDmode)
13886 #endif
13887              )
13888           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
13889         {
13890           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
13891                                                GET_MODE (SUBREG_REG (rtl)),
13892                                                mem_mode, initialized);
13893           break;
13894         }
13895       if (dwarf_strict)
13896         break;
13897       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
13898         break;
13899       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
13900           && (GET_MODE_CLASS (mode) != MODE_INT
13901               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
13902         break;
13903       else
13904         {
13905           dw_die_ref type_die;
13906           dw_loc_descr_ref cvt;
13907
13908           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
13909                                                GET_MODE (SUBREG_REG (rtl)),
13910                                                mem_mode, initialized);
13911           if (mem_loc_result == NULL)
13912             break;
13913           type_die = base_type_for_mode (mode, 0);
13914           if (type_die == NULL)
13915             {
13916               mem_loc_result = NULL;
13917               break;
13918             }
13919           if (GET_MODE_SIZE (mode)
13920               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
13921             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
13922           else
13923             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
13924           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
13925           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
13926           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
13927           add_loc_descr (&mem_loc_result, cvt);
13928         }
13929       break;
13930
13931     case REG:
13932       if (GET_MODE_CLASS (mode) != MODE_INT
13933           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
13934 #ifdef POINTERS_EXTEND_UNSIGNED
13935               && (mode != Pmode || mem_mode == VOIDmode)
13936 #endif
13937               ))
13938         {
13939           dw_die_ref type_die;
13940
13941           if (dwarf_strict)
13942             break;
13943           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
13944             break;
13945           type_die = base_type_for_mode (mode, 0);
13946           if (type_die == NULL)
13947             break;
13948           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
13949                                           dbx_reg_number (rtl), 0);
13950           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
13951           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
13952           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
13953           break;
13954         }
13955       /* Whenever a register number forms a part of the description of the
13956          method for calculating the (dynamic) address of a memory resident
13957          object, DWARF rules require the register number be referred to as
13958          a "base register".  This distinction is not based in any way upon
13959          what category of register the hardware believes the given register
13960          belongs to.  This is strictly DWARF terminology we're dealing with
13961          here. Note that in cases where the location of a memory-resident
13962          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13963          OP_CONST (0)) the actual DWARF location descriptor that we generate
13964          may just be OP_BASEREG (basereg).  This may look deceptively like
13965          the object in question was allocated to a register (rather than in
13966          memory) so DWARF consumers need to be aware of the subtle
13967          distinction between OP_REG and OP_BASEREG.  */
13968       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13969         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13970       else if (stack_realign_drap
13971                && crtl->drap_reg
13972                && crtl->args.internal_arg_pointer == rtl
13973                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13974         {
13975           /* If RTL is internal_arg_pointer, which has been optimized
13976              out, use DRAP instead.  */
13977           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13978                                             VAR_INIT_STATUS_INITIALIZED);
13979         }
13980       break;
13981
13982     case SIGN_EXTEND:
13983     case ZERO_EXTEND:
13984       gcc_assert (GET_MODE_CLASS (mode) == MODE_INT);
13985       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
13986                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
13987       if (op0 == 0)
13988         break;
13989       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
13990         {
13991           int shift = DWARF2_ADDR_SIZE
13992                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13993           shift *= BITS_PER_UNIT;
13994           if (GET_CODE (rtl) == SIGN_EXTEND)
13995             op = DW_OP_shra;
13996           else
13997             op = DW_OP_shr;
13998           mem_loc_result = op0;
13999           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14000           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14001           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
14002           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14003         }
14004       else if (!dwarf_strict)
14005         {
14006           dw_die_ref type_die1, type_die2;
14007           dw_loc_descr_ref cvt;
14008
14009           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14010                                           GET_CODE (rtl) == ZERO_EXTEND);
14011           if (type_die1 == NULL)
14012             break;
14013           type_die2 = base_type_for_mode (mode, 0);
14014           if (type_die2 == NULL)
14015             break;
14016           mem_loc_result = op0;
14017           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14018           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14019           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
14020           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14021           add_loc_descr (&mem_loc_result, cvt);
14022           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14023           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14024           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
14025           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14026           add_loc_descr (&mem_loc_result, cvt);
14027         }
14028       break;
14029
14030     case MEM:
14031       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
14032                                            get_address_mode (rtl), mode,
14033                                            VAR_INIT_STATUS_INITIALIZED);
14034       if (mem_loc_result == NULL)
14035         mem_loc_result = tls_mem_loc_descriptor (rtl);
14036       if (mem_loc_result != 0)
14037         {
14038           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14039               || GET_MODE_CLASS (mode) != MODE_INT)
14040             {
14041               dw_die_ref type_die;
14042               dw_loc_descr_ref deref;
14043
14044               if (dwarf_strict)
14045                 return NULL;
14046               type_die = base_type_for_mode (mode, 0);
14047               if (type_die == NULL)
14048                 return NULL;
14049               deref = new_loc_descr (DW_OP_GNU_deref_type,
14050                                      GET_MODE_SIZE (mode), 0);
14051               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
14052               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
14053               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
14054               add_loc_descr (&mem_loc_result, deref);
14055             }
14056           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14057             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
14058           else
14059             add_loc_descr (&mem_loc_result,
14060                            new_loc_descr (DW_OP_deref_size,
14061                                           GET_MODE_SIZE (mode), 0));
14062         }
14063       else
14064         {
14065           rtx new_rtl = avoid_constant_pool_reference (rtl);
14066           if (new_rtl != rtl)
14067             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
14068         }
14069       break;
14070
14071     case LO_SUM:
14072       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
14073
14074     case LABEL_REF:
14075       /* Some ports can transform a symbol ref into a label ref, because
14076          the symbol ref is too far away and has to be dumped into a constant
14077          pool.  */
14078     case CONST:
14079     case SYMBOL_REF:
14080       if (GET_MODE_CLASS (mode) != MODE_INT
14081           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
14082 #ifdef POINTERS_EXTEND_UNSIGNED
14083               && (mode != Pmode || mem_mode == VOIDmode)
14084 #endif
14085               ))
14086         break;
14087       if (GET_CODE (rtl) == SYMBOL_REF
14088           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
14089         {
14090           dw_loc_descr_ref temp;
14091
14092           /* If this is not defined, we have no way to emit the data.  */
14093           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
14094             break;
14095
14096           /* We used to emit DW_OP_addr here, but that's wrong, since
14097              DW_OP_addr should be relocated by the debug info consumer,
14098              while DW_OP_GNU_push_tls_address operand should not.  */
14099           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
14100                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
14101           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
14102           temp->dw_loc_oprnd1.v.val_addr = rtl;
14103           temp->dtprel = true;
14104
14105           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
14106           add_loc_descr (&mem_loc_result, temp);
14107
14108           break;
14109         }
14110
14111       if (!const_ok_for_output (rtl))
14112         break;
14113
14114     symref:
14115       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14116       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14117       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14118       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14119       break;
14120
14121     case CONCAT:
14122     case CONCATN:
14123     case VAR_LOCATION:
14124     case DEBUG_IMPLICIT_PTR:
14125       expansion_failed (NULL_TREE, rtl,
14126                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
14127       return 0;
14128
14129     case ENTRY_VALUE:
14130       if (dwarf_strict)
14131         return NULL;
14132       if (REG_P (ENTRY_VALUE_EXP (rtl)))
14133         {
14134           if (GET_MODE_CLASS (mode) != MODE_INT
14135               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
14136             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14137                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14138           else
14139             op0
14140               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
14141                                         VAR_INIT_STATUS_INITIALIZED);
14142         }
14143       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
14144                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
14145         {
14146           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
14147                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14148           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
14149             return NULL;
14150         }
14151       else
14152         gcc_unreachable ();
14153       if (op0 == NULL)
14154         return NULL;
14155       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
14156       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
14157       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
14158       return mem_loc_result;
14159
14160     case PRE_MODIFY:
14161       /* Extract the PLUS expression nested inside and fall into
14162          PLUS code below.  */
14163       rtl = XEXP (rtl, 1);
14164       goto plus;
14165
14166     case PRE_INC:
14167     case PRE_DEC:
14168       /* Turn these into a PLUS expression and fall into the PLUS code
14169          below.  */
14170       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
14171                           GEN_INT (GET_CODE (rtl) == PRE_INC
14172                                    ? GET_MODE_UNIT_SIZE (mem_mode)
14173                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
14174
14175       /* ... fall through ...  */
14176
14177     case PLUS:
14178     plus:
14179       if (is_based_loc (rtl)
14180           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14181           && GET_MODE_CLASS (mode) == MODE_INT)
14182         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
14183                                           INTVAL (XEXP (rtl, 1)),
14184                                           VAR_INIT_STATUS_INITIALIZED);
14185       else
14186         {
14187           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14188                                                VAR_INIT_STATUS_INITIALIZED);
14189           if (mem_loc_result == 0)
14190             break;
14191
14192           if (CONST_INT_P (XEXP (rtl, 1))
14193               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
14194             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
14195           else
14196             {
14197               dw_loc_descr_ref mem_loc_result2
14198                 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14199                                       VAR_INIT_STATUS_INITIALIZED);
14200               if (mem_loc_result2 == 0)
14201                 break;
14202               add_loc_descr (&mem_loc_result, mem_loc_result2);
14203               add_loc_descr (&mem_loc_result,
14204                              new_loc_descr (DW_OP_plus, 0, 0));
14205             }
14206         }
14207       break;
14208
14209     /* If a pseudo-reg is optimized away, it is possible for it to
14210        be replaced with a MEM containing a multiply or shift.  */
14211     case MINUS:
14212       op = DW_OP_minus;
14213       goto do_binop;
14214
14215     case MULT:
14216       op = DW_OP_mul;
14217       goto do_binop;
14218
14219     case DIV:
14220       op = DW_OP_div;
14221       goto do_binop;
14222
14223     case UMOD:
14224       op = DW_OP_mod;
14225       goto do_binop;
14226
14227     case ASHIFT:
14228       op = DW_OP_shl;
14229       goto do_shift;
14230
14231     case ASHIFTRT:
14232       op = DW_OP_shra;
14233       goto do_shift;
14234
14235     case LSHIFTRT:
14236       op = DW_OP_shr;
14237       goto do_shift;
14238
14239     do_shift:
14240       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14241                                 VAR_INIT_STATUS_INITIALIZED);
14242       op1 = mem_loc_descriptor (XEXP (rtl, 1),
14243                                 GET_MODE (XEXP (rtl, 1)) == VOIDmode
14244                                 ? mode : GET_MODE (XEXP (rtl, 1)), mem_mode,
14245                                 VAR_INIT_STATUS_INITIALIZED);
14246
14247       if (op0 == 0 || op1 == 0)
14248         break;
14249
14250       mem_loc_result = op0;
14251       add_loc_descr (&mem_loc_result, op1);
14252       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14253       break;
14254
14255     case AND:
14256       op = DW_OP_and;
14257       goto do_binop;
14258
14259     case IOR:
14260       op = DW_OP_or;
14261       goto do_binop;
14262
14263     case XOR:
14264       op = DW_OP_xor;
14265       goto do_binop;
14266
14267     do_binop:
14268       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14269                                 VAR_INIT_STATUS_INITIALIZED);
14270       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14271                                 VAR_INIT_STATUS_INITIALIZED);
14272
14273       if (op0 == 0 || op1 == 0)
14274         break;
14275
14276       mem_loc_result = op0;
14277       add_loc_descr (&mem_loc_result, op1);
14278       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14279       break;
14280
14281     case MOD:
14282       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14283                                 VAR_INIT_STATUS_INITIALIZED);
14284       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14285                                 VAR_INIT_STATUS_INITIALIZED);
14286
14287       if (op0 == 0 || op1 == 0)
14288         break;
14289
14290       mem_loc_result = op0;
14291       add_loc_descr (&mem_loc_result, op1);
14292       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14293       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
14294       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
14295       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14296       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
14297       break;
14298
14299     case NOT:
14300       op = DW_OP_not;
14301       goto do_unop;
14302
14303     case ABS:
14304       op = DW_OP_abs;
14305       goto do_unop;
14306
14307     case NEG:
14308       op = DW_OP_neg;
14309       goto do_unop;
14310
14311     do_unop:
14312       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14313                                 VAR_INIT_STATUS_INITIALIZED);
14314
14315       if (op0 == 0)
14316         break;
14317
14318       mem_loc_result = op0;
14319       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14320       break;
14321
14322     case CONST_INT:
14323       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14324 #ifdef POINTERS_EXTEND_UNSIGNED
14325           || (mode == Pmode
14326               && mem_mode != VOIDmode
14327               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
14328 #endif
14329           )
14330         {
14331           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
14332           break;
14333         }
14334       if (!dwarf_strict
14335           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
14336               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
14337         {
14338           dw_die_ref type_die = base_type_for_mode (mode, 0);
14339           if (type_die == NULL)
14340             return NULL;
14341           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
14342                                           INTVAL (rtl));
14343           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14344           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14345           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14346           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
14347             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
14348           else
14349             {
14350               mem_loc_result->dw_loc_oprnd2.val_class
14351                 = dw_val_class_const_double;
14352               mem_loc_result->dw_loc_oprnd2.v.val_double
14353                 = shwi_to_double_int (INTVAL (rtl));
14354             }
14355         }
14356       break;
14357
14358     case CONST_DOUBLE:
14359       if (!dwarf_strict)
14360         {
14361           dw_die_ref type_die;
14362
14363           /* Note that a CONST_DOUBLE rtx could represent either an integer
14364              or a floating-point constant.  A CONST_DOUBLE is used whenever
14365              the constant requires more than one word in order to be
14366              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14367           if (mode == VOIDmode
14368               || (GET_MODE (rtl) == VOIDmode
14369                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
14370             break;
14371           type_die = base_type_for_mode (mode, 0);
14372           if (type_die == NULL)
14373             return NULL;
14374           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
14375           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14376           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14377           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
14378           if (SCALAR_FLOAT_MODE_P (mode))
14379             {
14380               unsigned int length = GET_MODE_SIZE (mode);
14381               unsigned char *array
14382                   = (unsigned char*) ggc_alloc_atomic (length);
14383
14384               insert_float (rtl, array);
14385               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14386               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14387               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14388               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14389             }
14390           else
14391             {
14392               mem_loc_result->dw_loc_oprnd2.val_class
14393                 = dw_val_class_const_double;
14394               mem_loc_result->dw_loc_oprnd2.v.val_double
14395                 = rtx_to_double_int (rtl);
14396             }
14397         }
14398       break;
14399
14400     case EQ:
14401       op = DW_OP_eq;
14402       goto do_scompare;
14403
14404     case GE:
14405       op = DW_OP_ge;
14406       goto do_scompare;
14407
14408     case GT:
14409       op = DW_OP_gt;
14410       goto do_scompare;
14411
14412     case LE:
14413       op = DW_OP_le;
14414       goto do_scompare;
14415
14416     case LT:
14417       op = DW_OP_lt;
14418       goto do_scompare;
14419
14420     case NE:
14421       op = DW_OP_ne;
14422       goto do_scompare;
14423
14424     do_scompare:
14425       {
14426         enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14427
14428         if (op_mode == VOIDmode)
14429           op_mode = GET_MODE (XEXP (rtl, 1));
14430         if (op_mode == VOIDmode)
14431           break;
14432
14433         if (dwarf_strict
14434             && (GET_MODE_CLASS (op_mode) != MODE_INT
14435                 || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
14436           break;
14437
14438         op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14439                                   VAR_INIT_STATUS_INITIALIZED);
14440         op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14441                                   VAR_INIT_STATUS_INITIALIZED);
14442
14443         if (op0 == 0 || op1 == 0)
14444           break;
14445
14446         if (GET_MODE_CLASS (op_mode) == MODE_INT
14447             && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14448           {
14449             int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
14450             shift *= BITS_PER_UNIT;
14451             /* For eq/ne, if the operands are known to be zero-extended,
14452                there is no need to do the fancy shifting up.  */
14453             if (op == DW_OP_eq || op == DW_OP_ne)
14454               {
14455                 dw_loc_descr_ref last0, last1;
14456                 for (last0 = op0;
14457                      last0->dw_loc_next != NULL;
14458                      last0 = last0->dw_loc_next)
14459                   ;
14460                 for (last1 = op1;
14461                      last1->dw_loc_next != NULL;
14462                      last1 = last1->dw_loc_next)
14463                   ;
14464                 /* deref_size zero extends, and for constants we can check
14465                    whether they are zero extended or not.  */
14466                 if (((last0->dw_loc_opc == DW_OP_deref_size
14467                       && last0->dw_loc_oprnd1.v.val_int
14468                          <= GET_MODE_SIZE (op_mode))
14469                      || (CONST_INT_P (XEXP (rtl, 0))
14470                          && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
14471                              == (INTVAL (XEXP (rtl, 0))
14472                                  & GET_MODE_MASK (op_mode))))
14473                     && ((last1->dw_loc_opc == DW_OP_deref_size
14474                          && last1->dw_loc_oprnd1.v.val_int
14475                             <= GET_MODE_SIZE (op_mode))
14476                         || (CONST_INT_P (XEXP (rtl, 1))
14477                             && (unsigned HOST_WIDE_INT)
14478                                INTVAL (XEXP (rtl, 1))
14479                                == (INTVAL (XEXP (rtl, 1))
14480                                    & GET_MODE_MASK (op_mode)))))
14481                   goto do_compare;
14482               }
14483             add_loc_descr (&op0, int_loc_descriptor (shift));
14484             add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14485             if (CONST_INT_P (XEXP (rtl, 1)))
14486               op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
14487             else
14488               {
14489                 add_loc_descr (&op1, int_loc_descriptor (shift));
14490                 add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14491               }
14492           }
14493       }
14494
14495     do_compare:
14496       mem_loc_result = op0;
14497       add_loc_descr (&mem_loc_result, op1);
14498       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14499       if (STORE_FLAG_VALUE != 1)
14500         {
14501           add_loc_descr (&mem_loc_result,
14502                          int_loc_descriptor (STORE_FLAG_VALUE));
14503           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
14504         }
14505       break;
14506
14507     case GEU:
14508       op = DW_OP_ge;
14509       goto do_ucompare;
14510
14511     case GTU:
14512       op = DW_OP_gt;
14513       goto do_ucompare;
14514
14515     case LEU:
14516       op = DW_OP_le;
14517       goto do_ucompare;
14518
14519     case LTU:
14520       op = DW_OP_lt;
14521       goto do_ucompare;
14522
14523     do_ucompare:
14524       {
14525         enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
14526
14527         if (op_mode == VOIDmode)
14528           op_mode = GET_MODE (XEXP (rtl, 1));
14529         if (op_mode == VOIDmode)
14530           break;
14531         if (GET_MODE_CLASS (op_mode) != MODE_INT)
14532           break;
14533
14534         if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
14535           break;
14536
14537         if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
14538             break;
14539
14540         op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
14541                                   VAR_INIT_STATUS_INITIALIZED);
14542         op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
14543                                   VAR_INIT_STATUS_INITIALIZED);
14544
14545         if (op0 == 0 || op1 == 0)
14546           break;
14547
14548         if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
14549           {
14550             HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
14551             dw_loc_descr_ref last0, last1;
14552             for (last0 = op0;
14553                  last0->dw_loc_next != NULL;
14554                  last0 = last0->dw_loc_next)
14555               ;
14556             for (last1 = op1;
14557                  last1->dw_loc_next != NULL;
14558                  last1 = last1->dw_loc_next)
14559               ;
14560             if (CONST_INT_P (XEXP (rtl, 0)))
14561               op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
14562             /* deref_size zero extends, so no need to mask it again.  */
14563             else if (last0->dw_loc_opc != DW_OP_deref_size
14564                      || last0->dw_loc_oprnd1.v.val_int
14565                         > GET_MODE_SIZE (op_mode))
14566               {
14567                 add_loc_descr (&op0, int_loc_descriptor (mask));
14568                 add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14569               }
14570             if (CONST_INT_P (XEXP (rtl, 1)))
14571               op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
14572             /* deref_size zero extends, so no need to mask it again.  */
14573             else if (last1->dw_loc_opc != DW_OP_deref_size
14574                      || last1->dw_loc_oprnd1.v.val_int
14575                         > GET_MODE_SIZE (op_mode))
14576               {
14577                 add_loc_descr (&op1, int_loc_descriptor (mask));
14578                 add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14579               }
14580           }
14581         else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
14582           {
14583             HOST_WIDE_INT bias = 1;
14584             bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14585             add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14586             if (CONST_INT_P (XEXP (rtl, 1)))
14587               op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
14588                                         + INTVAL (XEXP (rtl, 1)));
14589             else
14590               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
14591                                                   bias, 0));
14592           }
14593         else
14594           {
14595             dw_die_ref type_die = base_type_for_mode (mode, 1);
14596             dw_loc_descr_ref cvt;
14597
14598             if (type_die == NULL)
14599               break;
14600             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14601             cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14602             cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14603             cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14604             add_loc_descr (&op0, cvt);
14605             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14606             cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14607             cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14608             cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14609             add_loc_descr (&op1, cvt);
14610           }
14611       }
14612       goto do_compare;
14613
14614     case UMIN:
14615     case UMAX:
14616       if (GET_MODE_CLASS (mode) != MODE_INT)
14617         break;
14618       /* FALLTHRU */
14619     case SMIN:
14620     case SMAX:
14621       if (dwarf_strict
14622           && (GET_MODE_CLASS (mode) != MODE_INT
14623               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
14624         break;
14625
14626       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
14627                                 VAR_INIT_STATUS_INITIALIZED);
14628       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14629                                 VAR_INIT_STATUS_INITIALIZED);
14630
14631       if (op0 == 0 || op1 == 0)
14632         break;
14633
14634       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14635       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14636       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14637       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14638         {
14639           if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14640             {
14641               HOST_WIDE_INT mask = GET_MODE_MASK (mode);
14642               add_loc_descr (&op0, int_loc_descriptor (mask));
14643               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14644               add_loc_descr (&op1, int_loc_descriptor (mask));
14645               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14646             }
14647           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
14648             {
14649               HOST_WIDE_INT bias = 1;
14650               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14651               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14652               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14653             }
14654           else
14655             {
14656               dw_die_ref type_die = base_type_for_mode (mode, 1);
14657               dw_loc_descr_ref cvt;
14658
14659               if (type_die == NULL)
14660                 break;
14661               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14662               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14663               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14664               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14665               add_loc_descr (&op0, cvt);
14666               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14667               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14668               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14669               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14670               add_loc_descr (&op1, cvt);
14671             }
14672         }
14673       else if (GET_MODE_CLASS (mode) == MODE_INT
14674                && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14675         {
14676           int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode);
14677           shift *= BITS_PER_UNIT;
14678           add_loc_descr (&op0, int_loc_descriptor (shift));
14679           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14680           add_loc_descr (&op1, int_loc_descriptor (shift));
14681           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14682         }
14683
14684       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14685         op = DW_OP_lt;
14686       else
14687         op = DW_OP_gt;
14688       mem_loc_result = op0;
14689       add_loc_descr (&mem_loc_result, op1);
14690       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14691       {
14692         dw_loc_descr_ref bra_node, drop_node;
14693
14694         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14695         add_loc_descr (&mem_loc_result, bra_node);
14696         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14697         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14698         add_loc_descr (&mem_loc_result, drop_node);
14699         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14700         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14701       }
14702       break;
14703
14704     case ZERO_EXTRACT:
14705     case SIGN_EXTRACT:
14706       if (CONST_INT_P (XEXP (rtl, 1))
14707           && CONST_INT_P (XEXP (rtl, 2))
14708           && ((unsigned) INTVAL (XEXP (rtl, 1))
14709               + (unsigned) INTVAL (XEXP (rtl, 2))
14710               <= GET_MODE_BITSIZE (mode))
14711           && GET_MODE_CLASS (mode) == MODE_INT
14712           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
14713           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14714         {
14715           int shift, size;
14716           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14717                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14718           if (op0 == 0)
14719             break;
14720           if (GET_CODE (rtl) == SIGN_EXTRACT)
14721             op = DW_OP_shra;
14722           else
14723             op = DW_OP_shr;
14724           mem_loc_result = op0;
14725           size = INTVAL (XEXP (rtl, 1));
14726           shift = INTVAL (XEXP (rtl, 2));
14727           if (BITS_BIG_ENDIAN)
14728             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14729                     - shift - size;
14730           if (shift + size != (int) DWARF2_ADDR_SIZE)
14731             {
14732               add_loc_descr (&mem_loc_result,
14733                              int_loc_descriptor (DWARF2_ADDR_SIZE
14734                                                  - shift - size));
14735               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14736             }
14737           if (size != (int) DWARF2_ADDR_SIZE)
14738             {
14739               add_loc_descr (&mem_loc_result,
14740                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14741               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14742             }
14743         }
14744       break;
14745
14746     case IF_THEN_ELSE:
14747       {
14748         dw_loc_descr_ref op2, bra_node, drop_node;
14749         op0 = mem_loc_descriptor (XEXP (rtl, 0),
14750                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
14751                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
14752                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
14753         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
14754                                   VAR_INIT_STATUS_INITIALIZED);
14755         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
14756                                   VAR_INIT_STATUS_INITIALIZED);
14757         if (op0 == NULL || op1 == NULL || op2 == NULL)
14758           break;
14759
14760         mem_loc_result = op1;
14761         add_loc_descr (&mem_loc_result, op2);
14762         add_loc_descr (&mem_loc_result, op0);
14763         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14764         add_loc_descr (&mem_loc_result, bra_node);
14765         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14766         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14767         add_loc_descr (&mem_loc_result, drop_node);
14768         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14769         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14770       }
14771       break;
14772
14773     case FLOAT_EXTEND:
14774     case FLOAT_TRUNCATE:
14775     case FLOAT:
14776     case UNSIGNED_FLOAT:
14777     case FIX:
14778     case UNSIGNED_FIX:
14779       if (!dwarf_strict)
14780         {
14781           dw_die_ref type_die;
14782           dw_loc_descr_ref cvt;
14783
14784           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
14785                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
14786           if (op0 == NULL)
14787             break;
14788           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
14789               && (GET_CODE (rtl) == UNSIGNED_FLOAT
14790                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
14791                      <= DWARF2_ADDR_SIZE))
14792             {
14793               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
14794                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
14795               if (type_die == NULL)
14796                 break;
14797               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14798               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14799               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14800               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14801               add_loc_descr (&op0, cvt);
14802             }
14803           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
14804           if (type_die == NULL)
14805             break;
14806           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14807           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14808           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14809           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14810           add_loc_descr (&op0, cvt);
14811           if (GET_MODE_CLASS (mode) == MODE_INT
14812               && (GET_CODE (rtl) == UNSIGNED_FIX
14813                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
14814             {
14815               enum machine_mode outer_mode = mode;
14816               if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
14817                 {
14818                   outer_mode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
14819                                               MODE_INT, 0);
14820                   if (outer_mode == BLKmode
14821                       || GET_MODE_SIZE (outer_mode) != DWARF2_ADDR_SIZE)
14822                     break;
14823                 }
14824               type_die = base_type_for_mode (outer_mode, 0);
14825               if (type_die == NULL)
14826                 break;
14827               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
14828               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14829               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
14830               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
14831               add_loc_descr (&op0, cvt);
14832             }
14833           mem_loc_result = op0;
14834         }
14835       break;
14836
14837     case COMPARE:
14838     case ROTATE:
14839     case ROTATERT:
14840     case TRUNCATE:
14841       /* In theory, we could implement the above.  */
14842       /* DWARF cannot represent the unsigned compare operations
14843          natively.  */
14844     case SS_MULT:
14845     case US_MULT:
14846     case SS_DIV:
14847     case US_DIV:
14848     case SS_PLUS:
14849     case US_PLUS:
14850     case SS_MINUS:
14851     case US_MINUS:
14852     case SS_NEG:
14853     case US_NEG:
14854     case SS_ABS:
14855     case SS_ASHIFT:
14856     case US_ASHIFT:
14857     case SS_TRUNCATE:
14858     case US_TRUNCATE:
14859     case UDIV:
14860     case UNORDERED:
14861     case ORDERED:
14862     case UNEQ:
14863     case UNGE:
14864     case UNGT:
14865     case UNLE:
14866     case UNLT:
14867     case LTGT:
14868     case FRACT_CONVERT:
14869     case UNSIGNED_FRACT_CONVERT:
14870     case SAT_FRACT:
14871     case UNSIGNED_SAT_FRACT:
14872     case SQRT:
14873     case BSWAP:
14874     case FFS:
14875     case CLZ:
14876     case CTZ:
14877     case POPCOUNT:
14878     case PARITY:
14879     case ASM_OPERANDS:
14880     case VEC_MERGE:
14881     case VEC_SELECT:
14882     case VEC_CONCAT:
14883     case VEC_DUPLICATE:
14884     case UNSPEC:
14885     case HIGH:
14886     case FMA:
14887     case STRICT_LOW_PART:
14888     case CONST_VECTOR:
14889     case CONST_FIXED:
14890       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14891          can't express it in the debug info.  This can happen e.g. with some
14892          TLS UNSPECs.  */
14893       break;
14894
14895     case CONST_STRING:
14896       resolve_one_addr (&rtl, NULL);
14897       goto symref;
14898
14899     default:
14900 #ifdef ENABLE_CHECKING
14901       print_rtl (stderr, rtl);
14902       gcc_unreachable ();
14903 #else
14904       break;
14905 #endif
14906     }
14907
14908   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14909     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14910
14911   return mem_loc_result;
14912 }
14913
14914 /* Return a descriptor that describes the concatenation of two locations.
14915    This is typically a complex variable.  */
14916
14917 static dw_loc_descr_ref
14918 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14919 {
14920   dw_loc_descr_ref cc_loc_result = NULL;
14921   dw_loc_descr_ref x0_ref
14922     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14923   dw_loc_descr_ref x1_ref
14924     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14925
14926   if (x0_ref == 0 || x1_ref == 0)
14927     return 0;
14928
14929   cc_loc_result = x0_ref;
14930   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14931
14932   add_loc_descr (&cc_loc_result, x1_ref);
14933   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14934
14935   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14936     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14937
14938   return cc_loc_result;
14939 }
14940
14941 /* Return a descriptor that describes the concatenation of N
14942    locations.  */
14943
14944 static dw_loc_descr_ref
14945 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14946 {
14947   unsigned int i;
14948   dw_loc_descr_ref cc_loc_result = NULL;
14949   unsigned int n = XVECLEN (concatn, 0);
14950
14951   for (i = 0; i < n; ++i)
14952     {
14953       dw_loc_descr_ref ref;
14954       rtx x = XVECEXP (concatn, 0, i);
14955
14956       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14957       if (ref == NULL)
14958         return NULL;
14959
14960       add_loc_descr (&cc_loc_result, ref);
14961       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14962     }
14963
14964   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14965     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14966
14967   return cc_loc_result;
14968 }
14969
14970 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
14971    for DEBUG_IMPLICIT_PTR RTL.  */
14972
14973 static dw_loc_descr_ref
14974 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
14975 {
14976   dw_loc_descr_ref ret;
14977   dw_die_ref ref;
14978
14979   if (dwarf_strict)
14980     return NULL;
14981   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
14982               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
14983               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
14984   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
14985   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
14986   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
14987   if (ref)
14988     {
14989       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
14990       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
14991       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
14992     }
14993   else
14994     {
14995       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
14996       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
14997     }
14998   return ret;
14999 }
15000
15001 /* Output a proper Dwarf location descriptor for a variable or parameter
15002    which is either allocated in a register or in a memory location.  For a
15003    register, we just generate an OP_REG and the register number.  For a
15004    memory location we provide a Dwarf postfix expression describing how to
15005    generate the (dynamic) address of the object onto the address stack.
15006
15007    MODE is mode of the decl if this loc_descriptor is going to be used in
15008    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
15009    allowed, VOIDmode otherwise.
15010
15011    If we don't know how to describe it, return 0.  */
15012
15013 static dw_loc_descr_ref
15014 loc_descriptor (rtx rtl, enum machine_mode mode,
15015                 enum var_init_status initialized)
15016 {
15017   dw_loc_descr_ref loc_result = NULL;
15018
15019   switch (GET_CODE (rtl))
15020     {
15021     case SUBREG:
15022       /* The case of a subreg may arise when we have a local (register)
15023          variable or a formal (register) parameter which doesn't quite fill
15024          up an entire register.  For now, just assume that it is
15025          legitimate to make the Dwarf info refer to the whole register which
15026          contains the given subreg.  */
15027       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
15028         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
15029       else
15030         goto do_default;
15031       break;
15032
15033     case REG:
15034       loc_result = reg_loc_descriptor (rtl, initialized);
15035       break;
15036
15037     case MEM:
15038       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15039                                        GET_MODE (rtl), initialized);
15040       if (loc_result == NULL)
15041         loc_result = tls_mem_loc_descriptor (rtl);
15042       if (loc_result == NULL)
15043         {
15044           rtx new_rtl = avoid_constant_pool_reference (rtl);
15045           if (new_rtl != rtl)
15046             loc_result = loc_descriptor (new_rtl, mode, initialized);
15047         }
15048       break;
15049
15050     case CONCAT:
15051       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
15052                                           initialized);
15053       break;
15054
15055     case CONCATN:
15056       loc_result = concatn_loc_descriptor (rtl, initialized);
15057       break;
15058
15059     case VAR_LOCATION:
15060       /* Single part.  */
15061       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
15062         {
15063           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
15064           if (GET_CODE (loc) == EXPR_LIST)
15065             loc = XEXP (loc, 0);
15066           loc_result = loc_descriptor (loc, mode, initialized);
15067           break;
15068         }
15069
15070       rtl = XEXP (rtl, 1);
15071       /* FALLTHRU */
15072
15073     case PARALLEL:
15074       {
15075         rtvec par_elems = XVEC (rtl, 0);
15076         int num_elem = GET_NUM_ELEM (par_elems);
15077         enum machine_mode mode;
15078         int i;
15079
15080         /* Create the first one, so we have something to add to.  */
15081         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
15082                                      VOIDmode, initialized);
15083         if (loc_result == NULL)
15084           return NULL;
15085         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
15086         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15087         for (i = 1; i < num_elem; i++)
15088           {
15089             dw_loc_descr_ref temp;
15090
15091             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
15092                                    VOIDmode, initialized);
15093             if (temp == NULL)
15094               return NULL;
15095             add_loc_descr (&loc_result, temp);
15096             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
15097             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
15098           }
15099       }
15100       break;
15101
15102     case CONST_INT:
15103       if (mode != VOIDmode && mode != BLKmode)
15104         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
15105                                                     INTVAL (rtl));
15106       break;
15107
15108     case CONST_DOUBLE:
15109       if (mode == VOIDmode)
15110         mode = GET_MODE (rtl);
15111
15112       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15113         {
15114           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15115
15116           /* Note that a CONST_DOUBLE rtx could represent either an integer
15117              or a floating-point constant.  A CONST_DOUBLE is used whenever
15118              the constant requires more than one word in order to be
15119              adequately represented.  We output CONST_DOUBLEs as blocks.  */
15120           loc_result = new_loc_descr (DW_OP_implicit_value,
15121                                       GET_MODE_SIZE (mode), 0);
15122           if (SCALAR_FLOAT_MODE_P (mode))
15123             {
15124               unsigned int length = GET_MODE_SIZE (mode);
15125               unsigned char *array
15126                   = (unsigned char*) ggc_alloc_atomic (length);
15127
15128               insert_float (rtl, array);
15129               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15130               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
15131               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
15132               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15133             }
15134           else
15135             {
15136               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
15137               loc_result->dw_loc_oprnd2.v.val_double
15138                 = rtx_to_double_int (rtl);
15139             }
15140         }
15141       break;
15142
15143     case CONST_VECTOR:
15144       if (mode == VOIDmode)
15145         mode = GET_MODE (rtl);
15146
15147       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
15148         {
15149           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
15150           unsigned int length = CONST_VECTOR_NUNITS (rtl);
15151           unsigned char *array = (unsigned char *)
15152             ggc_alloc_atomic (length * elt_size);
15153           unsigned int i;
15154           unsigned char *p;
15155
15156           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
15157           switch (GET_MODE_CLASS (mode))
15158             {
15159             case MODE_VECTOR_INT:
15160               for (i = 0, p = array; i < length; i++, p += elt_size)
15161                 {
15162                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15163                   double_int val = rtx_to_double_int (elt);
15164
15165                   if (elt_size <= sizeof (HOST_WIDE_INT))
15166                     insert_int (double_int_to_shwi (val), elt_size, p);
15167                   else
15168                     {
15169                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
15170                       insert_double (val, p);
15171                     }
15172                 }
15173               break;
15174
15175             case MODE_VECTOR_FLOAT:
15176               for (i = 0, p = array; i < length; i++, p += elt_size)
15177                 {
15178                   rtx elt = CONST_VECTOR_ELT (rtl, i);
15179                   insert_float (elt, p);
15180                 }
15181               break;
15182
15183             default:
15184               gcc_unreachable ();
15185             }
15186
15187           loc_result = new_loc_descr (DW_OP_implicit_value,
15188                                       length * elt_size, 0);
15189           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
15190           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
15191           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
15192           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
15193         }
15194       break;
15195
15196     case CONST:
15197       if (mode == VOIDmode
15198           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
15199           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
15200           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
15201         {
15202           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
15203           break;
15204         }
15205       /* FALLTHROUGH */
15206     case SYMBOL_REF:
15207       if (!const_ok_for_output (rtl))
15208         break;
15209     case LABEL_REF:
15210       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
15211           && (dwarf_version >= 4 || !dwarf_strict))
15212         {
15213           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
15214           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
15215           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
15216           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
15217           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
15218         }
15219       break;
15220
15221     case DEBUG_IMPLICIT_PTR:
15222       loc_result = implicit_ptr_descriptor (rtl, 0);
15223       break;
15224
15225     case PLUS:
15226       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
15227           && CONST_INT_P (XEXP (rtl, 1)))
15228         {
15229           loc_result
15230             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
15231           break;
15232         }
15233       /* FALLTHRU */
15234     do_default:
15235     default:
15236       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
15237            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
15238            && dwarf_version >= 4)
15239           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
15240         {
15241           /* Value expression.  */
15242           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
15243           if (loc_result)
15244             add_loc_descr (&loc_result,
15245                            new_loc_descr (DW_OP_stack_value, 0, 0));
15246         }
15247       break;
15248     }
15249
15250   return loc_result;
15251 }
15252
15253 /* We need to figure out what section we should use as the base for the
15254    address ranges where a given location is valid.
15255    1. If this particular DECL has a section associated with it, use that.
15256    2. If this function has a section associated with it, use that.
15257    3. Otherwise, use the text section.
15258    XXX: If you split a variable across multiple sections, we won't notice.  */
15259
15260 static const char *
15261 secname_for_decl (const_tree decl)
15262 {
15263   const char *secname;
15264
15265   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
15266     {
15267       tree sectree = DECL_SECTION_NAME (decl);
15268       secname = TREE_STRING_POINTER (sectree);
15269     }
15270   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
15271     {
15272       tree sectree = DECL_SECTION_NAME (current_function_decl);
15273       secname = TREE_STRING_POINTER (sectree);
15274     }
15275   else if (cfun && in_cold_section_p)
15276     secname = crtl->subsections.cold_section_label;
15277   else
15278     secname = text_section_label;
15279
15280   return secname;
15281 }
15282
15283 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
15284
15285 static bool
15286 decl_by_reference_p (tree decl)
15287 {
15288   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
15289            || TREE_CODE (decl) == VAR_DECL)
15290           && DECL_BY_REFERENCE (decl));
15291 }
15292
15293 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15294    for VARLOC.  */
15295
15296 static dw_loc_descr_ref
15297 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
15298                enum var_init_status initialized)
15299 {
15300   int have_address = 0;
15301   dw_loc_descr_ref descr;
15302   enum machine_mode mode;
15303
15304   if (want_address != 2)
15305     {
15306       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
15307       /* Single part.  */
15308       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15309         {
15310           varloc = PAT_VAR_LOCATION_LOC (varloc);
15311           if (GET_CODE (varloc) == EXPR_LIST)
15312             varloc = XEXP (varloc, 0);
15313           mode = GET_MODE (varloc);
15314           if (MEM_P (varloc))
15315             {
15316               rtx addr = XEXP (varloc, 0);
15317               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
15318                                           mode, initialized);
15319               if (descr)
15320                 have_address = 1;
15321               else
15322                 {
15323                   rtx x = avoid_constant_pool_reference (varloc);
15324                   if (x != varloc)
15325                     descr = mem_loc_descriptor (x, mode, VOIDmode,
15326                                                 initialized);
15327                 }
15328             }
15329           else
15330             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
15331         }
15332       else
15333         return 0;
15334     }
15335   else
15336     {
15337       if (GET_CODE (varloc) == VAR_LOCATION)
15338         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
15339       else
15340         mode = DECL_MODE (loc);
15341       descr = loc_descriptor (varloc, mode, initialized);
15342       have_address = 1;
15343     }
15344
15345   if (!descr)
15346     return 0;
15347
15348   if (want_address == 2 && !have_address
15349       && (dwarf_version >= 4 || !dwarf_strict))
15350     {
15351       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15352         {
15353           expansion_failed (loc, NULL_RTX,
15354                             "DWARF address size mismatch");
15355           return 0;
15356         }
15357       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
15358       have_address = 1;
15359     }
15360   /* Show if we can't fill the request for an address.  */
15361   if (want_address && !have_address)
15362     {
15363       expansion_failed (loc, NULL_RTX,
15364                         "Want address and only have value");
15365       return 0;
15366     }
15367
15368   /* If we've got an address and don't want one, dereference.  */
15369   if (!want_address && have_address)
15370     {
15371       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15372       enum dwarf_location_atom op;
15373
15374       if (size > DWARF2_ADDR_SIZE || size == -1)
15375         {
15376           expansion_failed (loc, NULL_RTX,
15377                             "DWARF address size mismatch");
15378           return 0;
15379         }
15380       else if (size == DWARF2_ADDR_SIZE)
15381         op = DW_OP_deref;
15382       else
15383         op = DW_OP_deref_size;
15384
15385       add_loc_descr (&descr, new_loc_descr (op, size, 0));
15386     }
15387
15388   return descr;
15389 }
15390
15391 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
15392    if it is not possible.  */
15393
15394 static dw_loc_descr_ref
15395 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
15396 {
15397   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
15398     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
15399   else if (dwarf_version >= 3 || !dwarf_strict)
15400     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
15401   else
15402     return NULL;
15403 }
15404
15405 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
15406    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
15407
15408 static dw_loc_descr_ref
15409 dw_sra_loc_expr (tree decl, rtx loc)
15410 {
15411   rtx p;
15412   unsigned int padsize = 0;
15413   dw_loc_descr_ref descr, *descr_tail;
15414   unsigned HOST_WIDE_INT decl_size;
15415   rtx varloc;
15416   enum var_init_status initialized;
15417
15418   if (DECL_SIZE (decl) == NULL
15419       || !host_integerp (DECL_SIZE (decl), 1))
15420     return NULL;
15421
15422   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
15423   descr = NULL;
15424   descr_tail = &descr;
15425
15426   for (p = loc; p; p = XEXP (p, 1))
15427     {
15428       unsigned int bitsize = decl_piece_bitsize (p);
15429       rtx loc_note = *decl_piece_varloc_ptr (p);
15430       dw_loc_descr_ref cur_descr;
15431       dw_loc_descr_ref *tail, last = NULL;
15432       unsigned int opsize = 0;
15433
15434       if (loc_note == NULL_RTX
15435           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
15436         {
15437           padsize += bitsize;
15438           continue;
15439         }
15440       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
15441       varloc = NOTE_VAR_LOCATION (loc_note);
15442       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
15443       if (cur_descr == NULL)
15444         {
15445           padsize += bitsize;
15446           continue;
15447         }
15448
15449       /* Check that cur_descr either doesn't use
15450          DW_OP_*piece operations, or their sum is equal
15451          to bitsize.  Otherwise we can't embed it.  */
15452       for (tail = &cur_descr; *tail != NULL;
15453            tail = &(*tail)->dw_loc_next)
15454         if ((*tail)->dw_loc_opc == DW_OP_piece)
15455           {
15456             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
15457                       * BITS_PER_UNIT;
15458             last = *tail;
15459           }
15460         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
15461           {
15462             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
15463             last = *tail;
15464           }
15465
15466       if (last != NULL && opsize != bitsize)
15467         {
15468           padsize += bitsize;
15469           continue;
15470         }
15471
15472       /* If there is a hole, add DW_OP_*piece after empty DWARF
15473          expression, which means that those bits are optimized out.  */
15474       if (padsize)
15475         {
15476           if (padsize > decl_size)
15477             return NULL;
15478           decl_size -= padsize;
15479           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
15480           if (*descr_tail == NULL)
15481             return NULL;
15482           descr_tail = &(*descr_tail)->dw_loc_next;
15483           padsize = 0;
15484         }
15485       *descr_tail = cur_descr;
15486       descr_tail = tail;
15487       if (bitsize > decl_size)
15488         return NULL;
15489       decl_size -= bitsize;
15490       if (last == NULL)
15491         {
15492           HOST_WIDE_INT offset = 0;
15493           if (GET_CODE (varloc) == VAR_LOCATION
15494               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
15495             {
15496               varloc = PAT_VAR_LOCATION_LOC (varloc);
15497               if (GET_CODE (varloc) == EXPR_LIST)
15498                 varloc = XEXP (varloc, 0);
15499             }
15500           do 
15501             {
15502               if (GET_CODE (varloc) == CONST
15503                   || GET_CODE (varloc) == SIGN_EXTEND
15504                   || GET_CODE (varloc) == ZERO_EXTEND)
15505                 varloc = XEXP (varloc, 0);
15506               else if (GET_CODE (varloc) == SUBREG)
15507                 varloc = SUBREG_REG (varloc);
15508               else
15509                 break;
15510             }
15511           while (1);
15512           /* DW_OP_bit_size offset should be zero for register
15513              or implicit location descriptions and empty location
15514              descriptions, but for memory addresses needs big endian
15515              adjustment.  */
15516           if (MEM_P (varloc))
15517             {
15518               unsigned HOST_WIDE_INT memsize
15519                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
15520               if (memsize != bitsize)
15521                 {
15522                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
15523                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
15524                     return NULL;
15525                   if (memsize < bitsize)
15526                     return NULL;
15527                   if (BITS_BIG_ENDIAN)
15528                     offset = memsize - bitsize;
15529                 }
15530             }
15531
15532           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
15533           if (*descr_tail == NULL)
15534             return NULL;
15535           descr_tail = &(*descr_tail)->dw_loc_next;
15536         }
15537     }
15538
15539   /* If there were any non-empty expressions, add padding till the end of
15540      the decl.  */
15541   if (descr != NULL && decl_size != 0)
15542     {
15543       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
15544       if (*descr_tail == NULL)
15545         return NULL;
15546     }
15547   return descr;
15548 }
15549
15550 /* Return the dwarf representation of the location list LOC_LIST of
15551    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
15552    function.  */
15553
15554 static dw_loc_list_ref
15555 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
15556 {
15557   const char *endname, *secname;
15558   rtx varloc;
15559   enum var_init_status initialized;
15560   struct var_loc_node *node;
15561   dw_loc_descr_ref descr;
15562   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15563   dw_loc_list_ref list = NULL;
15564   dw_loc_list_ref *listp = &list;
15565
15566   /* Now that we know what section we are using for a base,
15567      actually construct the list of locations.
15568      The first location information is what is passed to the
15569      function that creates the location list, and the remaining
15570      locations just get added on to that list.
15571      Note that we only know the start address for a location
15572      (IE location changes), so to build the range, we use
15573      the range [current location start, next location start].
15574      This means we have to special case the last node, and generate
15575      a range of [last location start, end of function label].  */
15576
15577   secname = secname_for_decl (decl);
15578
15579   for (node = loc_list->first; node; node = node->next)
15580     if (GET_CODE (node->loc) == EXPR_LIST
15581         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
15582       {
15583         if (GET_CODE (node->loc) == EXPR_LIST)
15584           {
15585             /* This requires DW_OP_{,bit_}piece, which is not usable
15586                inside DWARF expressions.  */
15587             if (want_address != 2)
15588               continue;
15589             descr = dw_sra_loc_expr (decl, node->loc);
15590             if (descr == NULL)
15591               continue;
15592           }
15593         else
15594           {
15595             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15596             varloc = NOTE_VAR_LOCATION (node->loc);
15597             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
15598           }
15599         if (descr)
15600           {
15601             bool range_across_switch = false;
15602             /* If section switch happens in between node->label
15603                and node->next->label (or end of function) and
15604                we can't emit it as a single entry list,
15605                emit two ranges, first one ending at the end
15606                of first partition and second one starting at the
15607                beginning of second partition.  */
15608             if (node == loc_list->last_before_switch
15609                 && (node != loc_list->first || loc_list->first->next)
15610                 && current_function_decl)
15611               {
15612                 endname = current_fde ()->dw_fde_end;
15613                 range_across_switch = true;
15614               }
15615             /* The variable has a location between NODE->LABEL and
15616                NODE->NEXT->LABEL.  */
15617             else if (node->next)
15618               endname = node->next->label;
15619             /* If the variable has a location at the last label
15620                it keeps its location until the end of function.  */
15621             else if (!current_function_decl)
15622               endname = text_end_label;
15623             else
15624               {
15625                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15626                                              current_function_funcdef_no);
15627                 endname = ggc_strdup (label_id);
15628               }
15629
15630             *listp = new_loc_list (descr, node->label, endname, secname);
15631             listp = &(*listp)->dw_loc_next;
15632
15633             if (range_across_switch)
15634               {
15635                 if (GET_CODE (node->loc) == EXPR_LIST)
15636                   descr = dw_sra_loc_expr (decl, node->loc);
15637                 else
15638                   {
15639                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
15640                     varloc = NOTE_VAR_LOCATION (node->loc);
15641                     descr = dw_loc_list_1 (decl, varloc, want_address,
15642                                            initialized);
15643                   }
15644                 gcc_assert (descr);
15645                 /* The variable has a location between NODE->LABEL and
15646                    NODE->NEXT->LABEL.  */
15647                 if (node->next)
15648                   endname = node->next->label;
15649                 else
15650                   endname = current_fde ()->dw_fde_second_end;
15651                 *listp = new_loc_list (descr,
15652                                        current_fde ()->dw_fde_second_begin,
15653                                        endname, secname);
15654                 listp = &(*listp)->dw_loc_next;
15655               }
15656           }
15657       }
15658
15659   /* Try to avoid the overhead of a location list emitting a location
15660      expression instead, but only if we didn't have more than one
15661      location entry in the first place.  If some entries were not
15662      representable, we don't want to pretend a single entry that was
15663      applies to the entire scope in which the variable is
15664      available.  */
15665   if (list && loc_list->first->next)
15666     gen_llsym (list);
15667
15668   return list;
15669 }
15670
15671 /* Return if the loc_list has only single element and thus can be represented
15672    as location description.   */
15673
15674 static bool
15675 single_element_loc_list_p (dw_loc_list_ref list)
15676 {
15677   gcc_assert (!list->dw_loc_next || list->ll_symbol);
15678   return !list->ll_symbol;
15679 }
15680
15681 /* To each location in list LIST add loc descr REF.  */
15682
15683 static void
15684 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
15685 {
15686   dw_loc_descr_ref copy;
15687   add_loc_descr (&list->expr, ref);
15688   list = list->dw_loc_next;
15689   while (list)
15690     {
15691       copy = ggc_alloc_dw_loc_descr_node ();
15692       memcpy (copy, ref, sizeof (dw_loc_descr_node));
15693       add_loc_descr (&list->expr, copy);
15694       while (copy->dw_loc_next)
15695         {
15696           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
15697           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
15698           copy->dw_loc_next = new_copy;
15699           copy = new_copy;
15700         }
15701       list = list->dw_loc_next;
15702     }
15703 }
15704
15705 /* Given two lists RET and LIST
15706    produce location list that is result of adding expression in LIST
15707    to expression in RET on each possition in program.
15708    Might be destructive on both RET and LIST.
15709
15710    TODO: We handle only simple cases of RET or LIST having at most one
15711    element. General case would inolve sorting the lists in program order
15712    and merging them that will need some additional work.
15713    Adding that will improve quality of debug info especially for SRA-ed
15714    structures.  */
15715
15716 static void
15717 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
15718 {
15719   if (!list)
15720     return;
15721   if (!*ret)
15722     {
15723       *ret = list;
15724       return;
15725     }
15726   if (!list->dw_loc_next)
15727     {
15728       add_loc_descr_to_each (*ret, list->expr);
15729       return;
15730     }
15731   if (!(*ret)->dw_loc_next)
15732     {
15733       add_loc_descr_to_each (list, (*ret)->expr);
15734       *ret = list;
15735       return;
15736     }
15737   expansion_failed (NULL_TREE, NULL_RTX,
15738                     "Don't know how to merge two non-trivial"
15739                     " location lists.\n");
15740   *ret = NULL;
15741   return;
15742 }
15743
15744 /* LOC is constant expression.  Try a luck, look it up in constant
15745    pool and return its loc_descr of its address.  */
15746
15747 static dw_loc_descr_ref
15748 cst_pool_loc_descr (tree loc)
15749 {
15750   /* Get an RTL for this, if something has been emitted.  */
15751   rtx rtl = lookup_constant_def (loc);
15752
15753   if (!rtl || !MEM_P (rtl))
15754     {
15755       gcc_assert (!rtl);
15756       return 0;
15757     }
15758   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
15759
15760   /* TODO: We might get more coverage if we was actually delaying expansion
15761      of all expressions till end of compilation when constant pools are fully
15762      populated.  */
15763   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
15764     {
15765       expansion_failed (loc, NULL_RTX,
15766                         "CST value in contant pool but not marked.");
15767       return 0;
15768     }
15769   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
15770                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
15771 }
15772
15773 /* Return dw_loc_list representing address of addr_expr LOC
15774    by looking for innder INDIRECT_REF expression and turing it
15775    into simple arithmetics.  */
15776
15777 static dw_loc_list_ref
15778 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
15779 {
15780   tree obj, offset;
15781   HOST_WIDE_INT bitsize, bitpos, bytepos;
15782   enum machine_mode mode;
15783   int volatilep;
15784   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15785   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15786
15787   obj = get_inner_reference (TREE_OPERAND (loc, 0),
15788                              &bitsize, &bitpos, &offset, &mode,
15789                              &unsignedp, &volatilep, false);
15790   STRIP_NOPS (obj);
15791   if (bitpos % BITS_PER_UNIT)
15792     {
15793       expansion_failed (loc, NULL_RTX, "bitfield access");
15794       return 0;
15795     }
15796   if (!INDIRECT_REF_P (obj))
15797     {
15798       expansion_failed (obj,
15799                         NULL_RTX, "no indirect ref in inner refrence");
15800       return 0;
15801     }
15802   if (!offset && !bitpos)
15803     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
15804   else if (toplev
15805            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
15806            && (dwarf_version >= 4 || !dwarf_strict))
15807     {
15808       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
15809       if (!list_ret)
15810         return 0;
15811       if (offset)
15812         {
15813           /* Variable offset.  */
15814           list_ret1 = loc_list_from_tree (offset, 0);
15815           if (list_ret1 == 0)
15816             return 0;
15817           add_loc_list (&list_ret, list_ret1);
15818           if (!list_ret)
15819             return 0;
15820           add_loc_descr_to_each (list_ret,
15821                                  new_loc_descr (DW_OP_plus, 0, 0));
15822         }
15823       bytepos = bitpos / BITS_PER_UNIT;
15824       if (bytepos > 0)
15825         add_loc_descr_to_each (list_ret,
15826                                new_loc_descr (DW_OP_plus_uconst,
15827                                               bytepos, 0));
15828       else if (bytepos < 0)
15829         loc_list_plus_const (list_ret, bytepos);
15830       add_loc_descr_to_each (list_ret,
15831                              new_loc_descr (DW_OP_stack_value, 0, 0));
15832     }
15833   return list_ret;
15834 }
15835
15836
15837 /* Generate Dwarf location list representing LOC.
15838    If WANT_ADDRESS is false, expression computing LOC will be computed
15839    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15840    if WANT_ADDRESS is 2, expression computing address useable in location
15841      will be returned (i.e. DW_OP_reg can be used
15842      to refer to register values).  */
15843
15844 static dw_loc_list_ref
15845 loc_list_from_tree (tree loc, int want_address)
15846 {
15847   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15848   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15849   int have_address = 0;
15850   enum dwarf_location_atom op;
15851
15852   /* ??? Most of the time we do not take proper care for sign/zero
15853      extending the values properly.  Hopefully this won't be a real
15854      problem...  */
15855
15856   switch (TREE_CODE (loc))
15857     {
15858     case ERROR_MARK:
15859       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15860       return 0;
15861
15862     case PLACEHOLDER_EXPR:
15863       /* This case involves extracting fields from an object to determine the
15864          position of other fields.  We don't try to encode this here.  The
15865          only user of this is Ada, which encodes the needed information using
15866          the names of types.  */
15867       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15868       return 0;
15869
15870     case CALL_EXPR:
15871       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15872       /* There are no opcodes for these operations.  */
15873       return 0;
15874
15875     case PREINCREMENT_EXPR:
15876     case PREDECREMENT_EXPR:
15877     case POSTINCREMENT_EXPR:
15878     case POSTDECREMENT_EXPR:
15879       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15880       /* There are no opcodes for these operations.  */
15881       return 0;
15882
15883     case ADDR_EXPR:
15884       /* If we already want an address, see if there is INDIRECT_REF inside
15885          e.g. for &this->field.  */
15886       if (want_address)
15887         {
15888           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15889                        (loc, want_address == 2);
15890           if (list_ret)
15891             have_address = 1;
15892           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15893                    && (ret = cst_pool_loc_descr (loc)))
15894             have_address = 1;
15895         }
15896         /* Otherwise, process the argument and look for the address.  */
15897       if (!list_ret && !ret)
15898         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15899       else
15900         {
15901           if (want_address)
15902             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15903           return NULL;
15904         }
15905       break;
15906
15907     case VAR_DECL:
15908       if (DECL_THREAD_LOCAL_P (loc))
15909         {
15910           rtx rtl;
15911           enum dwarf_location_atom first_op;
15912           enum dwarf_location_atom second_op;
15913           bool dtprel = false;
15914
15915           if (targetm.have_tls)
15916             {
15917               /* If this is not defined, we have no way to emit the
15918                  data.  */
15919               if (!targetm.asm_out.output_dwarf_dtprel)
15920                 return 0;
15921
15922                /* The way DW_OP_GNU_push_tls_address is specified, we
15923                   can only look up addresses of objects in the current
15924                   module.  We used DW_OP_addr as first op, but that's
15925                   wrong, because DW_OP_addr is relocated by the debug
15926                   info consumer, while DW_OP_GNU_push_tls_address
15927                   operand shouldn't be.  */
15928               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15929                 return 0;
15930               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15931               dtprel = true;
15932               second_op = DW_OP_GNU_push_tls_address;
15933             }
15934           else
15935             {
15936               if (!targetm.emutls.debug_form_tls_address
15937                   || !(dwarf_version >= 3 || !dwarf_strict))
15938                 return 0;
15939               /* We stuffed the control variable into the DECL_VALUE_EXPR
15940                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15941                  no longer appear in gimple code.  We used the control
15942                  variable in specific so that we could pick it up here.  */
15943               loc = DECL_VALUE_EXPR (loc);
15944               first_op = DW_OP_addr;
15945               second_op = DW_OP_form_tls_address;
15946             }
15947
15948           rtl = rtl_for_decl_location (loc);
15949           if (rtl == NULL_RTX)
15950             return 0;
15951
15952           if (!MEM_P (rtl))
15953             return 0;
15954           rtl = XEXP (rtl, 0);
15955           if (! CONSTANT_P (rtl))
15956             return 0;
15957
15958           ret = new_loc_descr (first_op, 0, 0);
15959           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15960           ret->dw_loc_oprnd1.v.val_addr = rtl;
15961           ret->dtprel = dtprel;
15962
15963           ret1 = new_loc_descr (second_op, 0, 0);
15964           add_loc_descr (&ret, ret1);
15965
15966           have_address = 1;
15967           break;
15968         }
15969       /* FALLTHRU */
15970
15971     case PARM_DECL:
15972     case RESULT_DECL:
15973       if (DECL_HAS_VALUE_EXPR_P (loc))
15974         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15975                                    want_address);
15976       /* FALLTHRU */
15977
15978     case FUNCTION_DECL:
15979       {
15980         rtx rtl;
15981         var_loc_list *loc_list = lookup_decl_loc (loc);
15982
15983         if (loc_list && loc_list->first)
15984           {
15985             list_ret = dw_loc_list (loc_list, loc, want_address);
15986             have_address = want_address != 0;
15987             break;
15988           }
15989         rtl = rtl_for_decl_location (loc);
15990         if (rtl == NULL_RTX)
15991           {
15992             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15993             return 0;
15994           }
15995         else if (CONST_INT_P (rtl))
15996           {
15997             HOST_WIDE_INT val = INTVAL (rtl);
15998             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15999               val &= GET_MODE_MASK (DECL_MODE (loc));
16000             ret = int_loc_descriptor (val);
16001           }
16002         else if (GET_CODE (rtl) == CONST_STRING)
16003           {
16004             expansion_failed (loc, NULL_RTX, "CONST_STRING");
16005             return 0;
16006           }
16007         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
16008           {
16009             ret = new_loc_descr (DW_OP_addr, 0, 0);
16010             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
16011             ret->dw_loc_oprnd1.v.val_addr = rtl;
16012           }
16013         else
16014           {
16015             enum machine_mode mode, mem_mode;
16016
16017             /* Certain constructs can only be represented at top-level.  */
16018             if (want_address == 2)
16019               {
16020                 ret = loc_descriptor (rtl, VOIDmode,
16021                                       VAR_INIT_STATUS_INITIALIZED);
16022                 have_address = 1;
16023               }
16024             else
16025               {
16026                 mode = GET_MODE (rtl);
16027                 mem_mode = VOIDmode;
16028                 if (MEM_P (rtl))
16029                   {
16030                     mem_mode = mode;
16031                     mode = get_address_mode (rtl);
16032                     rtl = XEXP (rtl, 0);
16033                     have_address = 1;
16034                   }
16035                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
16036                                           VAR_INIT_STATUS_INITIALIZED);
16037               }
16038             if (!ret)
16039               expansion_failed (loc, rtl,
16040                                 "failed to produce loc descriptor for rtl");
16041           }
16042       }
16043       break;
16044
16045     case MEM_REF:
16046       /* ??? FIXME.  */
16047       if (!integer_zerop (TREE_OPERAND (loc, 1)))
16048         return 0;
16049       /* Fallthru.  */
16050     case INDIRECT_REF:
16051       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16052       have_address = 1;
16053       break;
16054
16055     case COMPOUND_EXPR:
16056       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
16057
16058     CASE_CONVERT:
16059     case VIEW_CONVERT_EXPR:
16060     case SAVE_EXPR:
16061     case MODIFY_EXPR:
16062       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
16063
16064     case COMPONENT_REF:
16065     case BIT_FIELD_REF:
16066     case ARRAY_REF:
16067     case ARRAY_RANGE_REF:
16068     case REALPART_EXPR:
16069     case IMAGPART_EXPR:
16070       {
16071         tree obj, offset;
16072         HOST_WIDE_INT bitsize, bitpos, bytepos;
16073         enum machine_mode mode;
16074         int volatilep;
16075         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
16076
16077         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
16078                                    &unsignedp, &volatilep, false);
16079
16080         gcc_assert (obj != loc);
16081
16082         list_ret = loc_list_from_tree (obj,
16083                                        want_address == 2
16084                                        && !bitpos && !offset ? 2 : 1);
16085         /* TODO: We can extract value of the small expression via shifting even
16086            for nonzero bitpos.  */
16087         if (list_ret == 0)
16088           return 0;
16089         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
16090           {
16091             expansion_failed (loc, NULL_RTX,
16092                               "bitfield access");
16093             return 0;
16094           }
16095
16096         if (offset != NULL_TREE)
16097           {
16098             /* Variable offset.  */
16099             list_ret1 = loc_list_from_tree (offset, 0);
16100             if (list_ret1 == 0)
16101               return 0;
16102             add_loc_list (&list_ret, list_ret1);
16103             if (!list_ret)
16104               return 0;
16105             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
16106           }
16107
16108         bytepos = bitpos / BITS_PER_UNIT;
16109         if (bytepos > 0)
16110           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
16111         else if (bytepos < 0)
16112           loc_list_plus_const (list_ret, bytepos);
16113
16114         have_address = 1;
16115         break;
16116       }
16117
16118     case INTEGER_CST:
16119       if ((want_address || !host_integerp (loc, 0))
16120           && (ret = cst_pool_loc_descr (loc)))
16121         have_address = 1;
16122       else if (want_address == 2
16123                && host_integerp (loc, 0)
16124                && (ret = address_of_int_loc_descriptor
16125                            (int_size_in_bytes (TREE_TYPE (loc)),
16126                             tree_low_cst (loc, 0))))
16127         have_address = 1;
16128       else if (host_integerp (loc, 0))
16129         ret = int_loc_descriptor (tree_low_cst (loc, 0));
16130       else
16131         {
16132           expansion_failed (loc, NULL_RTX,
16133                             "Integer operand is not host integer");
16134           return 0;
16135         }
16136       break;
16137
16138     case CONSTRUCTOR:
16139     case REAL_CST:
16140     case STRING_CST:
16141     case COMPLEX_CST:
16142       if ((ret = cst_pool_loc_descr (loc)))
16143         have_address = 1;
16144       else
16145       /* We can construct small constants here using int_loc_descriptor.  */
16146         expansion_failed (loc, NULL_RTX,
16147                           "constructor or constant not in constant pool");
16148       break;
16149
16150     case TRUTH_AND_EXPR:
16151     case TRUTH_ANDIF_EXPR:
16152     case BIT_AND_EXPR:
16153       op = DW_OP_and;
16154       goto do_binop;
16155
16156     case TRUTH_XOR_EXPR:
16157     case BIT_XOR_EXPR:
16158       op = DW_OP_xor;
16159       goto do_binop;
16160
16161     case TRUTH_OR_EXPR:
16162     case TRUTH_ORIF_EXPR:
16163     case BIT_IOR_EXPR:
16164       op = DW_OP_or;
16165       goto do_binop;
16166
16167     case FLOOR_DIV_EXPR:
16168     case CEIL_DIV_EXPR:
16169     case ROUND_DIV_EXPR:
16170     case TRUNC_DIV_EXPR:
16171       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16172         return 0;
16173       op = DW_OP_div;
16174       goto do_binop;
16175
16176     case MINUS_EXPR:
16177       op = DW_OP_minus;
16178       goto do_binop;
16179
16180     case FLOOR_MOD_EXPR:
16181     case CEIL_MOD_EXPR:
16182     case ROUND_MOD_EXPR:
16183     case TRUNC_MOD_EXPR:
16184       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
16185         {
16186           op = DW_OP_mod;
16187           goto do_binop;
16188         }
16189       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16190       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16191       if (list_ret == 0 || list_ret1 == 0)
16192         return 0;
16193
16194       add_loc_list (&list_ret, list_ret1);
16195       if (list_ret == 0)
16196         return 0;
16197       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16198       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
16199       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
16200       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
16201       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
16202       break;
16203
16204     case MULT_EXPR:
16205       op = DW_OP_mul;
16206       goto do_binop;
16207
16208     case LSHIFT_EXPR:
16209       op = DW_OP_shl;
16210       goto do_binop;
16211
16212     case RSHIFT_EXPR:
16213       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
16214       goto do_binop;
16215
16216     case POINTER_PLUS_EXPR:
16217     case PLUS_EXPR:
16218       if (host_integerp (TREE_OPERAND (loc, 1), 0))
16219         {
16220           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16221           if (list_ret == 0)
16222             return 0;
16223
16224           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
16225           break;
16226         }
16227
16228       op = DW_OP_plus;
16229       goto do_binop;
16230
16231     case LE_EXPR:
16232       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16233         return 0;
16234
16235       op = DW_OP_le;
16236       goto do_binop;
16237
16238     case GE_EXPR:
16239       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16240         return 0;
16241
16242       op = DW_OP_ge;
16243       goto do_binop;
16244
16245     case LT_EXPR:
16246       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16247         return 0;
16248
16249       op = DW_OP_lt;
16250       goto do_binop;
16251
16252     case GT_EXPR:
16253       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
16254         return 0;
16255
16256       op = DW_OP_gt;
16257       goto do_binop;
16258
16259     case EQ_EXPR:
16260       op = DW_OP_eq;
16261       goto do_binop;
16262
16263     case NE_EXPR:
16264       op = DW_OP_ne;
16265       goto do_binop;
16266
16267     do_binop:
16268       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16269       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
16270       if (list_ret == 0 || list_ret1 == 0)
16271         return 0;
16272
16273       add_loc_list (&list_ret, list_ret1);
16274       if (list_ret == 0)
16275         return 0;
16276       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16277       break;
16278
16279     case TRUTH_NOT_EXPR:
16280     case BIT_NOT_EXPR:
16281       op = DW_OP_not;
16282       goto do_unop;
16283
16284     case ABS_EXPR:
16285       op = DW_OP_abs;
16286       goto do_unop;
16287
16288     case NEGATE_EXPR:
16289       op = DW_OP_neg;
16290       goto do_unop;
16291
16292     do_unop:
16293       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16294       if (list_ret == 0)
16295         return 0;
16296
16297       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
16298       break;
16299
16300     case MIN_EXPR:
16301     case MAX_EXPR:
16302       {
16303         const enum tree_code code =
16304           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
16305
16306         loc = build3 (COND_EXPR, TREE_TYPE (loc),
16307                       build2 (code, integer_type_node,
16308                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
16309                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
16310       }
16311
16312       /* ... fall through ...  */
16313
16314     case COND_EXPR:
16315       {
16316         dw_loc_descr_ref lhs
16317           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
16318         dw_loc_list_ref rhs
16319           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
16320         dw_loc_descr_ref bra_node, jump_node, tmp;
16321
16322         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
16323         if (list_ret == 0 || lhs == 0 || rhs == 0)
16324           return 0;
16325
16326         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
16327         add_loc_descr_to_each (list_ret, bra_node);
16328
16329         add_loc_list (&list_ret, rhs);
16330         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
16331         add_loc_descr_to_each (list_ret, jump_node);
16332
16333         add_loc_descr_to_each (list_ret, lhs);
16334         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16335         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
16336
16337         /* ??? Need a node to point the skip at.  Use a nop.  */
16338         tmp = new_loc_descr (DW_OP_nop, 0, 0);
16339         add_loc_descr_to_each (list_ret, tmp);
16340         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
16341         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
16342       }
16343       break;
16344
16345     case FIX_TRUNC_EXPR:
16346       return 0;
16347
16348     default:
16349       /* Leave front-end specific codes as simply unknown.  This comes
16350          up, for instance, with the C STMT_EXPR.  */
16351       if ((unsigned int) TREE_CODE (loc)
16352           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
16353         {
16354           expansion_failed (loc, NULL_RTX,
16355                             "language specific tree node");
16356           return 0;
16357         }
16358
16359 #ifdef ENABLE_CHECKING
16360       /* Otherwise this is a generic code; we should just lists all of
16361          these explicitly.  We forgot one.  */
16362       gcc_unreachable ();
16363 #else
16364       /* In a release build, we want to degrade gracefully: better to
16365          generate incomplete debugging information than to crash.  */
16366       return NULL;
16367 #endif
16368     }
16369
16370   if (!ret && !list_ret)
16371     return 0;
16372
16373   if (want_address == 2 && !have_address
16374       && (dwarf_version >= 4 || !dwarf_strict))
16375     {
16376       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
16377         {
16378           expansion_failed (loc, NULL_RTX,
16379                             "DWARF address size mismatch");
16380           return 0;
16381         }
16382       if (ret)
16383         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
16384       else
16385         add_loc_descr_to_each (list_ret,
16386                                new_loc_descr (DW_OP_stack_value, 0, 0));
16387       have_address = 1;
16388     }
16389   /* Show if we can't fill the request for an address.  */
16390   if (want_address && !have_address)
16391     {
16392       expansion_failed (loc, NULL_RTX,
16393                         "Want address and only have value");
16394       return 0;
16395     }
16396
16397   gcc_assert (!ret || !list_ret);
16398
16399   /* If we've got an address and don't want one, dereference.  */
16400   if (!want_address && have_address)
16401     {
16402       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
16403
16404       if (size > DWARF2_ADDR_SIZE || size == -1)
16405         {
16406           expansion_failed (loc, NULL_RTX,
16407                             "DWARF address size mismatch");
16408           return 0;
16409         }
16410       else if (size == DWARF2_ADDR_SIZE)
16411         op = DW_OP_deref;
16412       else
16413         op = DW_OP_deref_size;
16414
16415       if (ret)
16416         add_loc_descr (&ret, new_loc_descr (op, size, 0));
16417       else
16418         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
16419     }
16420   if (ret)
16421     list_ret = new_loc_list (ret, NULL, NULL, NULL);
16422
16423   return list_ret;
16424 }
16425
16426 /* Same as above but return only single location expression.  */
16427 static dw_loc_descr_ref
16428 loc_descriptor_from_tree (tree loc, int want_address)
16429 {
16430   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
16431   if (!ret)
16432     return NULL;
16433   if (ret->dw_loc_next)
16434     {
16435       expansion_failed (loc, NULL_RTX,
16436                         "Location list where only loc descriptor needed");
16437       return NULL;
16438     }
16439   return ret->expr;
16440 }
16441
16442 /* Given a value, round it up to the lowest multiple of `boundary'
16443    which is not less than the value itself.  */
16444
16445 static inline HOST_WIDE_INT
16446 ceiling (HOST_WIDE_INT value, unsigned int boundary)
16447 {
16448   return (((value + boundary - 1) / boundary) * boundary);
16449 }
16450
16451 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
16452    pointer to the declared type for the relevant field variable, or return
16453    `integer_type_node' if the given node turns out to be an
16454    ERROR_MARK node.  */
16455
16456 static inline tree
16457 field_type (const_tree decl)
16458 {
16459   tree type;
16460
16461   if (TREE_CODE (decl) == ERROR_MARK)
16462     return integer_type_node;
16463
16464   type = DECL_BIT_FIELD_TYPE (decl);
16465   if (type == NULL_TREE)
16466     type = TREE_TYPE (decl);
16467
16468   return type;
16469 }
16470
16471 /* Given a pointer to a tree node, return the alignment in bits for
16472    it, or else return BITS_PER_WORD if the node actually turns out to
16473    be an ERROR_MARK node.  */
16474
16475 static inline unsigned
16476 simple_type_align_in_bits (const_tree type)
16477 {
16478   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
16479 }
16480
16481 static inline unsigned
16482 simple_decl_align_in_bits (const_tree decl)
16483 {
16484   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
16485 }
16486
16487 /* Return the result of rounding T up to ALIGN.  */
16488
16489 static inline double_int
16490 round_up_to_align (double_int t, unsigned int align)
16491 {
16492   double_int alignd = uhwi_to_double_int (align);
16493   t = double_int_add (t, alignd);
16494   t = double_int_add (t, double_int_minus_one);
16495   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
16496   t = double_int_mul (t, alignd);
16497   return t;
16498 }
16499
16500 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
16501    lowest addressed byte of the "containing object" for the given FIELD_DECL,
16502    or return 0 if we are unable to determine what that offset is, either
16503    because the argument turns out to be a pointer to an ERROR_MARK node, or
16504    because the offset is actually variable.  (We can't handle the latter case
16505    just yet).  */
16506
16507 static HOST_WIDE_INT
16508 field_byte_offset (const_tree decl)
16509 {
16510   double_int object_offset_in_bits;
16511   double_int object_offset_in_bytes;
16512   double_int bitpos_int;
16513
16514   if (TREE_CODE (decl) == ERROR_MARK)
16515     return 0;
16516
16517   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
16518
16519   /* We cannot yet cope with fields whose positions are variable, so
16520      for now, when we see such things, we simply return 0.  Someday, we may
16521      be able to handle such cases, but it will be damn difficult.  */
16522   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
16523     return 0;
16524
16525   bitpos_int = tree_to_double_int (bit_position (decl));
16526
16527 #ifdef PCC_BITFIELD_TYPE_MATTERS
16528   if (PCC_BITFIELD_TYPE_MATTERS)
16529     {
16530       tree type;
16531       tree field_size_tree;
16532       double_int deepest_bitpos;
16533       double_int field_size_in_bits;
16534       unsigned int type_align_in_bits;
16535       unsigned int decl_align_in_bits;
16536       double_int type_size_in_bits;
16537
16538       type = field_type (decl);
16539       type_size_in_bits = double_int_type_size_in_bits (type);
16540       type_align_in_bits = simple_type_align_in_bits (type);
16541
16542       field_size_tree = DECL_SIZE (decl);
16543
16544       /* The size could be unspecified if there was an error, or for
16545          a flexible array member.  */
16546       if (!field_size_tree)
16547         field_size_tree = bitsize_zero_node;
16548
16549       /* If the size of the field is not constant, use the type size.  */
16550       if (TREE_CODE (field_size_tree) == INTEGER_CST)
16551         field_size_in_bits = tree_to_double_int (field_size_tree);
16552       else
16553         field_size_in_bits = type_size_in_bits;
16554
16555       decl_align_in_bits = simple_decl_align_in_bits (decl);
16556
16557       /* The GCC front-end doesn't make any attempt to keep track of the
16558          starting bit offset (relative to the start of the containing
16559          structure type) of the hypothetical "containing object" for a
16560          bit-field.  Thus, when computing the byte offset value for the
16561          start of the "containing object" of a bit-field, we must deduce
16562          this information on our own. This can be rather tricky to do in
16563          some cases.  For example, handling the following structure type
16564          definition when compiling for an i386/i486 target (which only
16565          aligns long long's to 32-bit boundaries) can be very tricky:
16566
16567          struct S { int field1; long long field2:31; };
16568
16569          Fortunately, there is a simple rule-of-thumb which can be used
16570          in such cases.  When compiling for an i386/i486, GCC will
16571          allocate 8 bytes for the structure shown above.  It decides to
16572          do this based upon one simple rule for bit-field allocation.
16573          GCC allocates each "containing object" for each bit-field at
16574          the first (i.e. lowest addressed) legitimate alignment boundary
16575          (based upon the required minimum alignment for the declared
16576          type of the field) which it can possibly use, subject to the
16577          condition that there is still enough available space remaining
16578          in the containing object (when allocated at the selected point)
16579          to fully accommodate all of the bits of the bit-field itself.
16580
16581          This simple rule makes it obvious why GCC allocates 8 bytes for
16582          each object of the structure type shown above.  When looking
16583          for a place to allocate the "containing object" for `field2',
16584          the compiler simply tries to allocate a 64-bit "containing
16585          object" at each successive 32-bit boundary (starting at zero)
16586          until it finds a place to allocate that 64- bit field such that
16587          at least 31 contiguous (and previously unallocated) bits remain
16588          within that selected 64 bit field.  (As it turns out, for the
16589          example above, the compiler finds it is OK to allocate the
16590          "containing object" 64-bit field at bit-offset zero within the
16591          structure type.)
16592
16593          Here we attempt to work backwards from the limited set of facts
16594          we're given, and we try to deduce from those facts, where GCC
16595          must have believed that the containing object started (within
16596          the structure type). The value we deduce is then used (by the
16597          callers of this routine) to generate DW_AT_location and
16598          DW_AT_bit_offset attributes for fields (both bit-fields and, in
16599          the case of DW_AT_location, regular fields as well).  */
16600
16601       /* Figure out the bit-distance from the start of the structure to
16602          the "deepest" bit of the bit-field.  */
16603       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
16604
16605       /* This is the tricky part.  Use some fancy footwork to deduce
16606          where the lowest addressed bit of the containing object must
16607          be.  */
16608       object_offset_in_bits
16609         = double_int_sub (deepest_bitpos, type_size_in_bits);
16610
16611       /* Round up to type_align by default.  This works best for
16612          bitfields.  */
16613       object_offset_in_bits
16614         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
16615
16616       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
16617         {
16618           object_offset_in_bits
16619             = double_int_sub (deepest_bitpos, type_size_in_bits);
16620
16621           /* Round up to decl_align instead.  */
16622           object_offset_in_bits
16623             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
16624         }
16625     }
16626   else
16627 #endif /* PCC_BITFIELD_TYPE_MATTERS */
16628     object_offset_in_bits = bitpos_int;
16629
16630   object_offset_in_bytes
16631     = double_int_div (object_offset_in_bits,
16632                       uhwi_to_double_int (BITS_PER_UNIT), true,
16633                       TRUNC_DIV_EXPR);
16634   return double_int_to_shwi (object_offset_in_bytes);
16635 }
16636 \f
16637 /* The following routines define various Dwarf attributes and any data
16638    associated with them.  */
16639
16640 /* Add a location description attribute value to a DIE.
16641
16642    This emits location attributes suitable for whole variables and
16643    whole parameters.  Note that the location attributes for struct fields are
16644    generated by the routine `data_member_location_attribute' below.  */
16645
16646 static inline void
16647 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
16648                              dw_loc_list_ref descr)
16649 {
16650   if (descr == 0)
16651     return;
16652   if (single_element_loc_list_p (descr))
16653     add_AT_loc (die, attr_kind, descr->expr);
16654   else
16655     add_AT_loc_list (die, attr_kind, descr);
16656 }
16657
16658 /* Add DW_AT_accessibility attribute to DIE if needed.  */
16659
16660 static void
16661 add_accessibility_attribute (dw_die_ref die, tree decl)
16662 {
16663   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
16664      children, otherwise the default is DW_ACCESS_public.  In DWARF2
16665      the default has always been DW_ACCESS_public.  */
16666   if (TREE_PROTECTED (decl))
16667     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16668   else if (TREE_PRIVATE (decl))
16669     {
16670       if (dwarf_version == 2
16671           || die->die_parent == NULL
16672           || die->die_parent->die_tag != DW_TAG_class_type)
16673         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
16674     }
16675   else if (dwarf_version > 2
16676            && die->die_parent
16677            && die->die_parent->die_tag == DW_TAG_class_type)
16678     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16679 }
16680
16681 /* Attach the specialized form of location attribute used for data members of
16682    struct and union types.  In the special case of a FIELD_DECL node which
16683    represents a bit-field, the "offset" part of this special location
16684    descriptor must indicate the distance in bytes from the lowest-addressed
16685    byte of the containing struct or union type to the lowest-addressed byte of
16686    the "containing object" for the bit-field.  (See the `field_byte_offset'
16687    function above).
16688
16689    For any given bit-field, the "containing object" is a hypothetical object
16690    (of some integral or enum type) within which the given bit-field lives.  The
16691    type of this hypothetical "containing object" is always the same as the
16692    declared type of the individual bit-field itself (for GCC anyway... the
16693    DWARF spec doesn't actually mandate this).  Note that it is the size (in
16694    bytes) of the hypothetical "containing object" which will be given in the
16695    DW_AT_byte_size attribute for this bit-field.  (See the
16696    `byte_size_attribute' function below.)  It is also used when calculating the
16697    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
16698    function below.)  */
16699
16700 static void
16701 add_data_member_location_attribute (dw_die_ref die, tree decl)
16702 {
16703   HOST_WIDE_INT offset;
16704   dw_loc_descr_ref loc_descr = 0;
16705
16706   if (TREE_CODE (decl) == TREE_BINFO)
16707     {
16708       /* We're working on the TAG_inheritance for a base class.  */
16709       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
16710         {
16711           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
16712              aren't at a fixed offset from all (sub)objects of the same
16713              type.  We need to extract the appropriate offset from our
16714              vtable.  The following dwarf expression means
16715
16716                BaseAddr = ObAddr + *((*ObAddr) - Offset)
16717
16718              This is specific to the V3 ABI, of course.  */
16719
16720           dw_loc_descr_ref tmp;
16721
16722           /* Make a copy of the object address.  */
16723           tmp = new_loc_descr (DW_OP_dup, 0, 0);
16724           add_loc_descr (&loc_descr, tmp);
16725
16726           /* Extract the vtable address.  */
16727           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16728           add_loc_descr (&loc_descr, tmp);
16729
16730           /* Calculate the address of the offset.  */
16731           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
16732           gcc_assert (offset < 0);
16733
16734           tmp = int_loc_descriptor (-offset);
16735           add_loc_descr (&loc_descr, tmp);
16736           tmp = new_loc_descr (DW_OP_minus, 0, 0);
16737           add_loc_descr (&loc_descr, tmp);
16738
16739           /* Extract the offset.  */
16740           tmp = new_loc_descr (DW_OP_deref, 0, 0);
16741           add_loc_descr (&loc_descr, tmp);
16742
16743           /* Add it to the object address.  */
16744           tmp = new_loc_descr (DW_OP_plus, 0, 0);
16745           add_loc_descr (&loc_descr, tmp);
16746         }
16747       else
16748         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
16749     }
16750   else
16751     offset = field_byte_offset (decl);
16752
16753   if (! loc_descr)
16754     {
16755       if (dwarf_version > 2)
16756         {
16757           /* Don't need to output a location expression, just the constant. */
16758           if (offset < 0)
16759             add_AT_int (die, DW_AT_data_member_location, offset);
16760           else
16761             add_AT_unsigned (die, DW_AT_data_member_location, offset);
16762           return;
16763         }
16764       else
16765         {
16766           enum dwarf_location_atom op;
16767
16768           /* The DWARF2 standard says that we should assume that the structure
16769              address is already on the stack, so we can specify a structure
16770              field address by using DW_OP_plus_uconst.  */
16771
16772 #ifdef MIPS_DEBUGGING_INFO
16773           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
16774              operator correctly.  It works only if we leave the offset on the
16775              stack.  */
16776           op = DW_OP_constu;
16777 #else
16778           op = DW_OP_plus_uconst;
16779 #endif
16780
16781           loc_descr = new_loc_descr (op, offset, 0);
16782         }
16783     }
16784
16785   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
16786 }
16787
16788 /* Writes integer values to dw_vec_const array.  */
16789
16790 static void
16791 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
16792 {
16793   while (size != 0)
16794     {
16795       *dest++ = val & 0xff;
16796       val >>= 8;
16797       --size;
16798     }
16799 }
16800
16801 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
16802
16803 static HOST_WIDE_INT
16804 extract_int (const unsigned char *src, unsigned int size)
16805 {
16806   HOST_WIDE_INT val = 0;
16807
16808   src += size;
16809   while (size != 0)
16810     {
16811       val <<= 8;
16812       val |= *--src & 0xff;
16813       --size;
16814     }
16815   return val;
16816 }
16817
16818 /* Writes double_int values to dw_vec_const array.  */
16819
16820 static void
16821 insert_double (double_int val, unsigned char *dest)
16822 {
16823   unsigned char *p0 = dest;
16824   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
16825
16826   if (WORDS_BIG_ENDIAN)
16827     {
16828       p0 = p1;
16829       p1 = dest;
16830     }
16831
16832   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
16833   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
16834 }
16835
16836 /* Writes floating point values to dw_vec_const array.  */
16837
16838 static void
16839 insert_float (const_rtx rtl, unsigned char *array)
16840 {
16841   REAL_VALUE_TYPE rv;
16842   long val[4];
16843   int i;
16844
16845   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16846   real_to_target (val, &rv, GET_MODE (rtl));
16847
16848   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16849   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16850     {
16851       insert_int (val[i], 4, array);
16852       array += 4;
16853     }
16854 }
16855
16856 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16857    does not have a "location" either in memory or in a register.  These
16858    things can arise in GNU C when a constant is passed as an actual parameter
16859    to an inlined function.  They can also arise in C++ where declared
16860    constants do not necessarily get memory "homes".  */
16861
16862 static bool
16863 add_const_value_attribute (dw_die_ref die, rtx rtl)
16864 {
16865   switch (GET_CODE (rtl))
16866     {
16867     case CONST_INT:
16868       {
16869         HOST_WIDE_INT val = INTVAL (rtl);
16870
16871         if (val < 0)
16872           add_AT_int (die, DW_AT_const_value, val);
16873         else
16874           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16875       }
16876       return true;
16877
16878     case CONST_DOUBLE:
16879       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16880          floating-point constant.  A CONST_DOUBLE is used whenever the
16881          constant requires more than one word in order to be adequately
16882          represented.  */
16883       {
16884         enum machine_mode mode = GET_MODE (rtl);
16885
16886         if (SCALAR_FLOAT_MODE_P (mode))
16887           {
16888             unsigned int length = GET_MODE_SIZE (mode);
16889             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16890
16891             insert_float (rtl, array);
16892             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16893           }
16894         else
16895           add_AT_double (die, DW_AT_const_value,
16896                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16897       }
16898       return true;
16899
16900     case CONST_VECTOR:
16901       {
16902         enum machine_mode mode = GET_MODE (rtl);
16903         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16904         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16905         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16906           (length * elt_size);
16907         unsigned int i;
16908         unsigned char *p;
16909
16910         switch (GET_MODE_CLASS (mode))
16911           {
16912           case MODE_VECTOR_INT:
16913             for (i = 0, p = array; i < length; i++, p += elt_size)
16914               {
16915                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16916                 double_int val = rtx_to_double_int (elt);
16917
16918                 if (elt_size <= sizeof (HOST_WIDE_INT))
16919                   insert_int (double_int_to_shwi (val), elt_size, p);
16920                 else
16921                   {
16922                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16923                     insert_double (val, p);
16924                   }
16925               }
16926             break;
16927
16928           case MODE_VECTOR_FLOAT:
16929             for (i = 0, p = array; i < length; i++, p += elt_size)
16930               {
16931                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16932                 insert_float (elt, p);
16933               }
16934             break;
16935
16936           default:
16937             gcc_unreachable ();
16938           }
16939
16940         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16941       }
16942       return true;
16943
16944     case CONST_STRING:
16945       if (dwarf_version >= 4 || !dwarf_strict)
16946         {
16947           dw_loc_descr_ref loc_result;
16948           resolve_one_addr (&rtl, NULL);
16949         rtl_addr:
16950           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16951           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16952           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16953           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16954           add_AT_loc (die, DW_AT_location, loc_result);
16955           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16956           return true;
16957         }
16958       return false;
16959
16960     case CONST:
16961       if (CONSTANT_P (XEXP (rtl, 0)))
16962         return add_const_value_attribute (die, XEXP (rtl, 0));
16963       /* FALLTHROUGH */
16964     case SYMBOL_REF:
16965       if (!const_ok_for_output (rtl))
16966         return false;
16967     case LABEL_REF:
16968       if (dwarf_version >= 4 || !dwarf_strict)
16969         goto rtl_addr;
16970       return false;
16971
16972     case PLUS:
16973       /* In cases where an inlined instance of an inline function is passed
16974          the address of an `auto' variable (which is local to the caller) we
16975          can get a situation where the DECL_RTL of the artificial local
16976          variable (for the inlining) which acts as a stand-in for the
16977          corresponding formal parameter (of the inline function) will look
16978          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16979          exactly a compile-time constant expression, but it isn't the address
16980          of the (artificial) local variable either.  Rather, it represents the
16981          *value* which the artificial local variable always has during its
16982          lifetime.  We currently have no way to represent such quasi-constant
16983          values in Dwarf, so for now we just punt and generate nothing.  */
16984       return false;
16985
16986     case HIGH:
16987     case CONST_FIXED:
16988       return false;
16989
16990     case MEM:
16991       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16992           && MEM_READONLY_P (rtl)
16993           && GET_MODE (rtl) == BLKmode)
16994         {
16995           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16996           return true;
16997         }
16998       return false;
16999
17000     default:
17001       /* No other kinds of rtx should be possible here.  */
17002       gcc_unreachable ();
17003     }
17004   return false;
17005 }
17006
17007 /* Determine whether the evaluation of EXPR references any variables
17008    or functions which aren't otherwise used (and therefore may not be
17009    output).  */
17010 static tree
17011 reference_to_unused (tree * tp, int * walk_subtrees,
17012                      void * data ATTRIBUTE_UNUSED)
17013 {
17014   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
17015     *walk_subtrees = 0;
17016
17017   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
17018       && ! TREE_ASM_WRITTEN (*tp))
17019     return *tp;
17020   /* ???  The C++ FE emits debug information for using decls, so
17021      putting gcc_unreachable here falls over.  See PR31899.  For now
17022      be conservative.  */
17023   else if (!cgraph_global_info_ready
17024            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
17025     return *tp;
17026   else if (TREE_CODE (*tp) == VAR_DECL)
17027     {
17028       struct varpool_node *node = varpool_get_node (*tp);
17029       if (!node || !node->needed)
17030         return *tp;
17031     }
17032   else if (TREE_CODE (*tp) == FUNCTION_DECL
17033            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
17034     {
17035       /* The call graph machinery must have finished analyzing,
17036          optimizing and gimplifying the CU by now.
17037          So if *TP has no call graph node associated
17038          to it, it means *TP will not be emitted.  */
17039       if (!cgraph_get_node (*tp))
17040         return *tp;
17041     }
17042   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
17043     return *tp;
17044
17045   return NULL_TREE;
17046 }
17047
17048 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
17049    for use in a later add_const_value_attribute call.  */
17050
17051 static rtx
17052 rtl_for_decl_init (tree init, tree type)
17053 {
17054   rtx rtl = NULL_RTX;
17055
17056   STRIP_NOPS (init);
17057
17058   /* If a variable is initialized with a string constant without embedded
17059      zeros, build CONST_STRING.  */
17060   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
17061     {
17062       tree enttype = TREE_TYPE (type);
17063       tree domain = TYPE_DOMAIN (type);
17064       enum machine_mode mode = TYPE_MODE (enttype);
17065
17066       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
17067           && domain
17068           && integer_zerop (TYPE_MIN_VALUE (domain))
17069           && compare_tree_int (TYPE_MAX_VALUE (domain),
17070                                TREE_STRING_LENGTH (init) - 1) == 0
17071           && ((size_t) TREE_STRING_LENGTH (init)
17072               == strlen (TREE_STRING_POINTER (init)) + 1))
17073         {
17074           rtl = gen_rtx_CONST_STRING (VOIDmode,
17075                                       ggc_strdup (TREE_STRING_POINTER (init)));
17076           rtl = gen_rtx_MEM (BLKmode, rtl);
17077           MEM_READONLY_P (rtl) = 1;
17078         }
17079     }
17080   /* Other aggregates, and complex values, could be represented using
17081      CONCAT: FIXME!  */
17082   else if (AGGREGATE_TYPE_P (type)
17083            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
17084                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
17085            || TREE_CODE (type) == COMPLEX_TYPE)
17086     ;
17087   /* Vectors only work if their mode is supported by the target.
17088      FIXME: generic vectors ought to work too.  */
17089   else if (TREE_CODE (type) == VECTOR_TYPE
17090            && !VECTOR_MODE_P (TYPE_MODE (type)))
17091     ;
17092   /* If the initializer is something that we know will expand into an
17093      immediate RTL constant, expand it now.  We must be careful not to
17094      reference variables which won't be output.  */
17095   else if (initializer_constant_valid_p (init, type)
17096            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
17097     {
17098       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
17099          possible.  */
17100       if (TREE_CODE (type) == VECTOR_TYPE)
17101         switch (TREE_CODE (init))
17102           {
17103           case VECTOR_CST:
17104             break;
17105           case CONSTRUCTOR:
17106             if (TREE_CONSTANT (init))
17107               {
17108                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
17109                 bool constant_p = true;
17110                 tree value;
17111                 unsigned HOST_WIDE_INT ix;
17112
17113                 /* Even when ctor is constant, it might contain non-*_CST
17114                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
17115                    belong into VECTOR_CST nodes.  */
17116                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
17117                   if (!CONSTANT_CLASS_P (value))
17118                     {
17119                       constant_p = false;
17120                       break;
17121                     }
17122
17123                 if (constant_p)
17124                   {
17125                     init = build_vector_from_ctor (type, elts);
17126                     break;
17127                   }
17128               }
17129             /* FALLTHRU */
17130
17131           default:
17132             return NULL;
17133           }
17134
17135       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
17136
17137       /* If expand_expr returns a MEM, it wasn't immediate.  */
17138       gcc_assert (!rtl || !MEM_P (rtl));
17139     }
17140
17141   return rtl;
17142 }
17143
17144 /* Generate RTL for the variable DECL to represent its location.  */
17145
17146 static rtx
17147 rtl_for_decl_location (tree decl)
17148 {
17149   rtx rtl;
17150
17151   /* Here we have to decide where we are going to say the parameter "lives"
17152      (as far as the debugger is concerned).  We only have a couple of
17153      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
17154
17155      DECL_RTL normally indicates where the parameter lives during most of the
17156      activation of the function.  If optimization is enabled however, this
17157      could be either NULL or else a pseudo-reg.  Both of those cases indicate
17158      that the parameter doesn't really live anywhere (as far as the code
17159      generation parts of GCC are concerned) during most of the function's
17160      activation.  That will happen (for example) if the parameter is never
17161      referenced within the function.
17162
17163      We could just generate a location descriptor here for all non-NULL
17164      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
17165      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
17166      where DECL_RTL is NULL or is a pseudo-reg.
17167
17168      Note however that we can only get away with using DECL_INCOMING_RTL as
17169      a backup substitute for DECL_RTL in certain limited cases.  In cases
17170      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
17171      we can be sure that the parameter was passed using the same type as it is
17172      declared to have within the function, and that its DECL_INCOMING_RTL
17173      points us to a place where a value of that type is passed.
17174
17175      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
17176      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
17177      because in these cases DECL_INCOMING_RTL points us to a value of some
17178      type which is *different* from the type of the parameter itself.  Thus,
17179      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
17180      such cases, the debugger would end up (for example) trying to fetch a
17181      `float' from a place which actually contains the first part of a
17182      `double'.  That would lead to really incorrect and confusing
17183      output at debug-time.
17184
17185      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
17186      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
17187      are a couple of exceptions however.  On little-endian machines we can
17188      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
17189      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
17190      an integral type that is smaller than TREE_TYPE (decl). These cases arise
17191      when (on a little-endian machine) a non-prototyped function has a
17192      parameter declared to be of type `short' or `char'.  In such cases,
17193      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
17194      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
17195      passed `int' value.  If the debugger then uses that address to fetch
17196      a `short' or a `char' (on a little-endian machine) the result will be
17197      the correct data, so we allow for such exceptional cases below.
17198
17199      Note that our goal here is to describe the place where the given formal
17200      parameter lives during most of the function's activation (i.e. between the
17201      end of the prologue and the start of the epilogue).  We'll do that as best
17202      as we can. Note however that if the given formal parameter is modified
17203      sometime during the execution of the function, then a stack backtrace (at
17204      debug-time) will show the function as having been called with the *new*
17205      value rather than the value which was originally passed in.  This happens
17206      rarely enough that it is not a major problem, but it *is* a problem, and
17207      I'd like to fix it.
17208
17209      A future version of dwarf2out.c may generate two additional attributes for
17210      any given DW_TAG_formal_parameter DIE which will describe the "passed
17211      type" and the "passed location" for the given formal parameter in addition
17212      to the attributes we now generate to indicate the "declared type" and the
17213      "active location" for each parameter.  This additional set of attributes
17214      could be used by debuggers for stack backtraces. Separately, note that
17215      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
17216      This happens (for example) for inlined-instances of inline function formal
17217      parameters which are never referenced.  This really shouldn't be
17218      happening.  All PARM_DECL nodes should get valid non-NULL
17219      DECL_INCOMING_RTL values.  FIXME.  */
17220
17221   /* Use DECL_RTL as the "location" unless we find something better.  */
17222   rtl = DECL_RTL_IF_SET (decl);
17223
17224   /* When generating abstract instances, ignore everything except
17225      constants, symbols living in memory, and symbols living in
17226      fixed registers.  */
17227   if (! reload_completed)
17228     {
17229       if (rtl
17230           && (CONSTANT_P (rtl)
17231               || (MEM_P (rtl)
17232                   && CONSTANT_P (XEXP (rtl, 0)))
17233               || (REG_P (rtl)
17234                   && TREE_CODE (decl) == VAR_DECL
17235                   && TREE_STATIC (decl))))
17236         {
17237           rtl = targetm.delegitimize_address (rtl);
17238           return rtl;
17239         }
17240       rtl = NULL_RTX;
17241     }
17242   else if (TREE_CODE (decl) == PARM_DECL)
17243     {
17244       if (rtl == NULL_RTX
17245           || is_pseudo_reg (rtl)
17246           || (MEM_P (rtl)
17247               && is_pseudo_reg (XEXP (rtl, 0))
17248               && DECL_INCOMING_RTL (decl)
17249               && MEM_P (DECL_INCOMING_RTL (decl))
17250               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
17251         {
17252           tree declared_type = TREE_TYPE (decl);
17253           tree passed_type = DECL_ARG_TYPE (decl);
17254           enum machine_mode dmode = TYPE_MODE (declared_type);
17255           enum machine_mode pmode = TYPE_MODE (passed_type);
17256
17257           /* This decl represents a formal parameter which was optimized out.
17258              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
17259              all cases where (rtl == NULL_RTX) just below.  */
17260           if (dmode == pmode)
17261             rtl = DECL_INCOMING_RTL (decl);
17262           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
17263                    && SCALAR_INT_MODE_P (dmode)
17264                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
17265                    && DECL_INCOMING_RTL (decl))
17266             {
17267               rtx inc = DECL_INCOMING_RTL (decl);
17268               if (REG_P (inc))
17269                 rtl = inc;
17270               else if (MEM_P (inc))
17271                 {
17272                   if (BYTES_BIG_ENDIAN)
17273                     rtl = adjust_address_nv (inc, dmode,
17274                                              GET_MODE_SIZE (pmode)
17275                                              - GET_MODE_SIZE (dmode));
17276                   else
17277                     rtl = inc;
17278                 }
17279             }
17280         }
17281
17282       /* If the parm was passed in registers, but lives on the stack, then
17283          make a big endian correction if the mode of the type of the
17284          parameter is not the same as the mode of the rtl.  */
17285       /* ??? This is the same series of checks that are made in dbxout.c before
17286          we reach the big endian correction code there.  It isn't clear if all
17287          of these checks are necessary here, but keeping them all is the safe
17288          thing to do.  */
17289       else if (MEM_P (rtl)
17290                && XEXP (rtl, 0) != const0_rtx
17291                && ! CONSTANT_P (XEXP (rtl, 0))
17292                /* Not passed in memory.  */
17293                && !MEM_P (DECL_INCOMING_RTL (decl))
17294                /* Not passed by invisible reference.  */
17295                && (!REG_P (XEXP (rtl, 0))
17296                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
17297                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
17298 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
17299                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
17300 #endif
17301                      )
17302                /* Big endian correction check.  */
17303                && BYTES_BIG_ENDIAN
17304                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
17305                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
17306                    < UNITS_PER_WORD))
17307         {
17308           int offset = (UNITS_PER_WORD
17309                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
17310
17311           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17312                              plus_constant (XEXP (rtl, 0), offset));
17313         }
17314     }
17315   else if (TREE_CODE (decl) == VAR_DECL
17316            && rtl
17317            && MEM_P (rtl)
17318            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
17319            && BYTES_BIG_ENDIAN)
17320     {
17321       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
17322       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
17323
17324       /* If a variable is declared "register" yet is smaller than
17325          a register, then if we store the variable to memory, it
17326          looks like we're storing a register-sized value, when in
17327          fact we are not.  We need to adjust the offset of the
17328          storage location to reflect the actual value's bytes,
17329          else gdb will not be able to display it.  */
17330       if (rsize > dsize)
17331         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
17332                            plus_constant (XEXP (rtl, 0), rsize-dsize));
17333     }
17334
17335   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
17336      and will have been substituted directly into all expressions that use it.
17337      C does not have such a concept, but C++ and other languages do.  */
17338   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
17339     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
17340
17341   if (rtl)
17342     rtl = targetm.delegitimize_address (rtl);
17343
17344   /* If we don't look past the constant pool, we risk emitting a
17345      reference to a constant pool entry that isn't referenced from
17346      code, and thus is not emitted.  */
17347   if (rtl)
17348     rtl = avoid_constant_pool_reference (rtl);
17349
17350   /* Try harder to get a rtl.  If this symbol ends up not being emitted
17351      in the current CU, resolve_addr will remove the expression referencing
17352      it.  */
17353   if (rtl == NULL_RTX
17354       && TREE_CODE (decl) == VAR_DECL
17355       && !DECL_EXTERNAL (decl)
17356       && TREE_STATIC (decl)
17357       && DECL_NAME (decl)
17358       && !DECL_HARD_REGISTER (decl)
17359       && DECL_MODE (decl) != VOIDmode)
17360     {
17361       rtl = make_decl_rtl_for_debug (decl);
17362       if (!MEM_P (rtl)
17363           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
17364           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
17365         rtl = NULL_RTX;
17366     }
17367
17368   return rtl;
17369 }
17370
17371 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
17372    returned.  If so, the decl for the COMMON block is returned, and the
17373    value is the offset into the common block for the symbol.  */
17374
17375 static tree
17376 fortran_common (tree decl, HOST_WIDE_INT *value)
17377 {
17378   tree val_expr, cvar;
17379   enum machine_mode mode;
17380   HOST_WIDE_INT bitsize, bitpos;
17381   tree offset;
17382   int volatilep = 0, unsignedp = 0;
17383
17384   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
17385      it does not have a value (the offset into the common area), or if it
17386      is thread local (as opposed to global) then it isn't common, and shouldn't
17387      be handled as such.  */
17388   if (TREE_CODE (decl) != VAR_DECL
17389       || !TREE_STATIC (decl)
17390       || !DECL_HAS_VALUE_EXPR_P (decl)
17391       || !is_fortran ())
17392     return NULL_TREE;
17393
17394   val_expr = DECL_VALUE_EXPR (decl);
17395   if (TREE_CODE (val_expr) != COMPONENT_REF)
17396     return NULL_TREE;
17397
17398   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
17399                               &mode, &unsignedp, &volatilep, true);
17400
17401   if (cvar == NULL_TREE
17402       || TREE_CODE (cvar) != VAR_DECL
17403       || DECL_ARTIFICIAL (cvar)
17404       || !TREE_PUBLIC (cvar))
17405     return NULL_TREE;
17406
17407   *value = 0;
17408   if (offset != NULL)
17409     {
17410       if (!host_integerp (offset, 0))
17411         return NULL_TREE;
17412       *value = tree_low_cst (offset, 0);
17413     }
17414   if (bitpos != 0)
17415     *value += bitpos / BITS_PER_UNIT;
17416
17417   return cvar;
17418 }
17419
17420 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
17421    data attribute for a variable or a parameter.  We generate the
17422    DW_AT_const_value attribute only in those cases where the given variable
17423    or parameter does not have a true "location" either in memory or in a
17424    register.  This can happen (for example) when a constant is passed as an
17425    actual argument in a call to an inline function.  (It's possible that
17426    these things can crop up in other ways also.)  Note that one type of
17427    constant value which can be passed into an inlined function is a constant
17428    pointer.  This can happen for example if an actual argument in an inlined
17429    function call evaluates to a compile-time constant address.
17430
17431    CACHE_P is true if it is worth caching the location list for DECL,
17432    so that future calls can reuse it rather than regenerate it from scratch.
17433    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
17434    since we will need to refer to them each time the function is inlined.  */
17435
17436 static bool
17437 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
17438                                        enum dwarf_attribute attr)
17439 {
17440   rtx rtl;
17441   dw_loc_list_ref list;
17442   var_loc_list *loc_list;
17443   cached_dw_loc_list *cache;
17444   void **slot;
17445
17446   if (TREE_CODE (decl) == ERROR_MARK)
17447     return false;
17448
17449   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
17450               || TREE_CODE (decl) == RESULT_DECL);
17451
17452   /* Try to get some constant RTL for this decl, and use that as the value of
17453      the location.  */
17454
17455   rtl = rtl_for_decl_location (decl);
17456   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17457       && add_const_value_attribute (die, rtl))
17458     return true;
17459
17460   /* See if we have single element location list that is equivalent to
17461      a constant value.  That way we are better to use add_const_value_attribute
17462      rather than expanding constant value equivalent.  */
17463   loc_list = lookup_decl_loc (decl);
17464   if (loc_list
17465       && loc_list->first
17466       && loc_list->first->next == NULL
17467       && NOTE_P (loc_list->first->loc)
17468       && NOTE_VAR_LOCATION (loc_list->first->loc)
17469       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
17470     {
17471       struct var_loc_node *node;
17472
17473       node = loc_list->first;
17474       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
17475       if (GET_CODE (rtl) == EXPR_LIST)
17476         rtl = XEXP (rtl, 0);
17477       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
17478           && add_const_value_attribute (die, rtl))
17479          return true;
17480     }
17481   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
17482      list several times.  See if we've already cached the contents.  */
17483   list = NULL;
17484   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
17485     cache_p = false;
17486   if (cache_p)
17487     {
17488       cache = (cached_dw_loc_list *)
17489         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
17490       if (cache)
17491         list = cache->loc_list;
17492     }
17493   if (list == NULL)
17494     {
17495       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
17496       /* It is usually worth caching this result if the decl is from
17497          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
17498       if (cache_p && list && list->dw_loc_next)
17499         {
17500           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
17501                                            DECL_UID (decl), INSERT);
17502           cache = ggc_alloc_cleared_cached_dw_loc_list ();
17503           cache->decl_id = DECL_UID (decl);
17504           cache->loc_list = list;
17505           *slot = cache;
17506         }
17507     }
17508   if (list)
17509     {
17510       add_AT_location_description (die, attr, list);
17511       return true;
17512     }
17513   /* None of that worked, so it must not really have a location;
17514      try adding a constant value attribute from the DECL_INITIAL.  */
17515   return tree_add_const_value_attribute_for_decl (die, decl);
17516 }
17517
17518 /* Add VARIABLE and DIE into deferred locations list.  */
17519
17520 static void
17521 defer_location (tree variable, dw_die_ref die)
17522 {
17523   deferred_locations entry;
17524   entry.variable = variable;
17525   entry.die = die;
17526   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
17527 }
17528
17529 /* Helper function for tree_add_const_value_attribute.  Natively encode
17530    initializer INIT into an array.  Return true if successful.  */
17531
17532 static bool
17533 native_encode_initializer (tree init, unsigned char *array, int size)
17534 {
17535   tree type;
17536
17537   if (init == NULL_TREE)
17538     return false;
17539
17540   STRIP_NOPS (init);
17541   switch (TREE_CODE (init))
17542     {
17543     case STRING_CST:
17544       type = TREE_TYPE (init);
17545       if (TREE_CODE (type) == ARRAY_TYPE)
17546         {
17547           tree enttype = TREE_TYPE (type);
17548           enum machine_mode mode = TYPE_MODE (enttype);
17549
17550           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
17551             return false;
17552           if (int_size_in_bytes (type) != size)
17553             return false;
17554           if (size > TREE_STRING_LENGTH (init))
17555             {
17556               memcpy (array, TREE_STRING_POINTER (init),
17557                       TREE_STRING_LENGTH (init));
17558               memset (array + TREE_STRING_LENGTH (init),
17559                       '\0', size - TREE_STRING_LENGTH (init));
17560             }
17561           else
17562             memcpy (array, TREE_STRING_POINTER (init), size);
17563           return true;
17564         }
17565       return false;
17566     case CONSTRUCTOR:
17567       type = TREE_TYPE (init);
17568       if (int_size_in_bytes (type) != size)
17569         return false;
17570       if (TREE_CODE (type) == ARRAY_TYPE)
17571         {
17572           HOST_WIDE_INT min_index;
17573           unsigned HOST_WIDE_INT cnt;
17574           int curpos = 0, fieldsize;
17575           constructor_elt *ce;
17576
17577           if (TYPE_DOMAIN (type) == NULL_TREE
17578               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
17579             return false;
17580
17581           fieldsize = int_size_in_bytes (TREE_TYPE (type));
17582           if (fieldsize <= 0)
17583             return false;
17584
17585           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
17586           memset (array, '\0', size);
17587           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17588             {
17589               tree val = ce->value;
17590               tree index = ce->index;
17591               int pos = curpos;
17592               if (index && TREE_CODE (index) == RANGE_EXPR)
17593                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
17594                       * fieldsize;
17595               else if (index)
17596                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
17597
17598               if (val)
17599                 {
17600                   STRIP_NOPS (val);
17601                   if (!native_encode_initializer (val, array + pos, fieldsize))
17602                     return false;
17603                 }
17604               curpos = pos + fieldsize;
17605               if (index && TREE_CODE (index) == RANGE_EXPR)
17606                 {
17607                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
17608                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
17609                   while (count > 0)
17610                     {
17611                       if (val)
17612                         memcpy (array + curpos, array + pos, fieldsize);
17613                       curpos += fieldsize;
17614                     }
17615                 }
17616               gcc_assert (curpos <= size);
17617             }
17618           return true;
17619         }
17620       else if (TREE_CODE (type) == RECORD_TYPE
17621                || TREE_CODE (type) == UNION_TYPE)
17622         {
17623           tree field = NULL_TREE;
17624           unsigned HOST_WIDE_INT cnt;
17625           constructor_elt *ce;
17626
17627           if (int_size_in_bytes (type) != size)
17628             return false;
17629
17630           if (TREE_CODE (type) == RECORD_TYPE)
17631             field = TYPE_FIELDS (type);
17632
17633           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
17634             {
17635               tree val = ce->value;
17636               int pos, fieldsize;
17637
17638               if (ce->index != 0)
17639                 field = ce->index;
17640
17641               if (val)
17642                 STRIP_NOPS (val);
17643
17644               if (field == NULL_TREE || DECL_BIT_FIELD (field))
17645                 return false;
17646
17647               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
17648                   && TYPE_DOMAIN (TREE_TYPE (field))
17649                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
17650                 return false;
17651               else if (DECL_SIZE_UNIT (field) == NULL_TREE
17652                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
17653                 return false;
17654               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
17655               pos = int_byte_position (field);
17656               gcc_assert (pos + fieldsize <= size);
17657               if (val
17658                   && !native_encode_initializer (val, array + pos, fieldsize))
17659                 return false;
17660             }
17661           return true;
17662         }
17663       return false;
17664     case VIEW_CONVERT_EXPR:
17665     case NON_LVALUE_EXPR:
17666       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
17667     default:
17668       return native_encode_expr (init, array, size) == size;
17669     }
17670 }
17671
17672 /* Attach a DW_AT_const_value attribute to DIE. The value of the
17673    attribute is the const value T.  */
17674
17675 static bool
17676 tree_add_const_value_attribute (dw_die_ref die, tree t)
17677 {
17678   tree init;
17679   tree type = TREE_TYPE (t);
17680   rtx rtl;
17681
17682   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
17683     return false;
17684
17685   init = t;
17686   gcc_assert (!DECL_P (init));
17687
17688   rtl = rtl_for_decl_init (init, type);
17689   if (rtl)
17690     return add_const_value_attribute (die, rtl);
17691   /* If the host and target are sane, try harder.  */
17692   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
17693            && initializer_constant_valid_p (init, type))
17694     {
17695       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
17696       if (size > 0 && (int) size == size)
17697         {
17698           unsigned char *array = (unsigned char *)
17699             ggc_alloc_cleared_atomic (size);
17700
17701           if (native_encode_initializer (init, array, size))
17702             {
17703               add_AT_vec (die, DW_AT_const_value, size, 1, array);
17704               return true;
17705             }
17706         }
17707     }
17708   return false;
17709 }
17710
17711 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
17712    attribute is the const value of T, where T is an integral constant
17713    variable with static storage duration
17714    (so it can't be a PARM_DECL or a RESULT_DECL).  */
17715
17716 static bool
17717 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
17718 {
17719
17720   if (!decl
17721       || (TREE_CODE (decl) != VAR_DECL
17722           && TREE_CODE (decl) != CONST_DECL)
17723       || (TREE_CODE (decl) == VAR_DECL
17724           && !TREE_STATIC (decl)))
17725     return false;
17726
17727     if (TREE_READONLY (decl)
17728         && ! TREE_THIS_VOLATILE (decl)
17729         && DECL_INITIAL (decl))
17730       /* OK */;
17731     else
17732       return false;
17733
17734   /* Don't add DW_AT_const_value if abstract origin already has one.  */
17735   if (get_AT (var_die, DW_AT_const_value))
17736     return false;
17737
17738   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
17739 }
17740
17741 /* Convert the CFI instructions for the current function into a
17742    location list.  This is used for DW_AT_frame_base when we targeting
17743    a dwarf2 consumer that does not support the dwarf3
17744    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
17745    expressions.  */
17746
17747 static dw_loc_list_ref
17748 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
17749 {
17750   int ix;
17751   dw_fde_ref fde;
17752   dw_loc_list_ref list, *list_tail;
17753   dw_cfi_ref cfi;
17754   dw_cfa_location last_cfa, next_cfa;
17755   const char *start_label, *last_label, *section;
17756   dw_cfa_location remember;
17757
17758   fde = current_fde ();
17759   gcc_assert (fde != NULL);
17760
17761   section = secname_for_decl (current_function_decl);
17762   list_tail = &list;
17763   list = NULL;
17764
17765   memset (&next_cfa, 0, sizeof (next_cfa));
17766   next_cfa.reg = INVALID_REGNUM;
17767   remember = next_cfa;
17768
17769   start_label = fde->dw_fde_begin;
17770
17771   /* ??? Bald assumption that the CIE opcode list does not contain
17772      advance opcodes.  */
17773   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
17774     lookup_cfa_1 (cfi, &next_cfa, &remember);
17775
17776   last_cfa = next_cfa;
17777   last_label = start_label;
17778
17779   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
17780     {
17781       /* If the first partition contained no CFI adjustments, the
17782          CIE opcodes apply to the whole first partition.  */
17783       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17784                                  fde->dw_fde_begin, fde->dw_fde_end, section);
17785       list_tail =&(*list_tail)->dw_loc_next;
17786       start_label = last_label = fde->dw_fde_second_begin;
17787     }
17788
17789   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
17790     {
17791       switch (cfi->dw_cfi_opc)
17792         {
17793         case DW_CFA_set_loc:
17794         case DW_CFA_advance_loc1:
17795         case DW_CFA_advance_loc2:
17796         case DW_CFA_advance_loc4:
17797           if (!cfa_equal_p (&last_cfa, &next_cfa))
17798             {
17799               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17800                                          start_label, last_label, section);
17801
17802               list_tail = &(*list_tail)->dw_loc_next;
17803               last_cfa = next_cfa;
17804               start_label = last_label;
17805             }
17806           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
17807           break;
17808
17809         case DW_CFA_advance_loc:
17810           /* The encoding is complex enough that we should never emit this.  */
17811           gcc_unreachable ();
17812
17813         default:
17814           lookup_cfa_1 (cfi, &next_cfa, &remember);
17815           break;
17816         }
17817       if (ix + 1 == fde->dw_fde_switch_cfi_index)
17818         {
17819           if (!cfa_equal_p (&last_cfa, &next_cfa))
17820             {
17821               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17822                                          start_label, last_label, section);
17823
17824               list_tail = &(*list_tail)->dw_loc_next;
17825               last_cfa = next_cfa;
17826               start_label = last_label;
17827             }
17828           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17829                                      start_label, fde->dw_fde_end, section);
17830           list_tail = &(*list_tail)->dw_loc_next;
17831           start_label = last_label = fde->dw_fde_second_begin;
17832         }
17833     }
17834
17835   if (!cfa_equal_p (&last_cfa, &next_cfa))
17836     {
17837       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
17838                                  start_label, last_label, section);
17839       list_tail = &(*list_tail)->dw_loc_next;
17840       start_label = last_label;
17841     }
17842
17843   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
17844                              start_label,
17845                              fde->dw_fde_second_begin
17846                              ? fde->dw_fde_second_end : fde->dw_fde_end,
17847                              section);
17848
17849   if (list && list->dw_loc_next)
17850     gen_llsym (list);
17851
17852   return list;
17853 }
17854
17855 /* Compute a displacement from the "steady-state frame pointer" to the
17856    frame base (often the same as the CFA), and store it in
17857    frame_pointer_fb_offset.  OFFSET is added to the displacement
17858    before the latter is negated.  */
17859
17860 static void
17861 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
17862 {
17863   rtx reg, elim;
17864
17865 #ifdef FRAME_POINTER_CFA_OFFSET
17866   reg = frame_pointer_rtx;
17867   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
17868 #else
17869   reg = arg_pointer_rtx;
17870   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
17871 #endif
17872
17873   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
17874   if (GET_CODE (elim) == PLUS)
17875     {
17876       offset += INTVAL (XEXP (elim, 1));
17877       elim = XEXP (elim, 0);
17878     }
17879
17880   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
17881                && (elim == hard_frame_pointer_rtx
17882                    || elim == stack_pointer_rtx))
17883               || elim == (frame_pointer_needed
17884                           ? hard_frame_pointer_rtx
17885                           : stack_pointer_rtx));
17886
17887   frame_pointer_fb_offset = -offset;
17888 }
17889
17890 /* Generate a DW_AT_name attribute given some string value to be included as
17891    the value of the attribute.  */
17892
17893 static void
17894 add_name_attribute (dw_die_ref die, const char *name_string)
17895 {
17896   if (name_string != NULL && *name_string != 0)
17897     {
17898       if (demangle_name_func)
17899         name_string = (*demangle_name_func) (name_string);
17900
17901       add_AT_string (die, DW_AT_name, name_string);
17902     }
17903 }
17904
17905 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
17906    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
17907    of TYPE accordingly.
17908
17909    ??? This is a temporary measure until after we're able to generate
17910    regular DWARF for the complex Ada type system.  */
17911
17912 static void 
17913 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
17914                                      dw_die_ref context_die)
17915 {
17916   tree dtype;
17917   dw_die_ref dtype_die;
17918
17919   if (!lang_hooks.types.descriptive_type)
17920     return;
17921
17922   dtype = lang_hooks.types.descriptive_type (type);
17923   if (!dtype)
17924     return;
17925
17926   dtype_die = lookup_type_die (dtype);
17927   if (!dtype_die)
17928     {
17929       gen_type_die (dtype, context_die);
17930       dtype_die = lookup_type_die (dtype);
17931       gcc_assert (dtype_die);
17932     }
17933
17934   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
17935 }
17936
17937 /* Generate a DW_AT_comp_dir attribute for DIE.  */
17938
17939 static void
17940 add_comp_dir_attribute (dw_die_ref die)
17941 {
17942   const char *wd = get_src_pwd ();
17943   char *wd1;
17944
17945   if (wd == NULL)
17946     return;
17947
17948   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17949     {
17950       int wdlen;
17951
17952       wdlen = strlen (wd);
17953       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17954       strcpy (wd1, wd);
17955       wd1 [wdlen] = DIR_SEPARATOR;
17956       wd1 [wdlen + 1] = 0;
17957       wd = wd1;
17958     }
17959
17960     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17961 }
17962
17963 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17964    default.  */
17965
17966 static int
17967 lower_bound_default (void)
17968 {
17969   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
17970     {
17971     case DW_LANG_C:
17972     case DW_LANG_C89:
17973     case DW_LANG_C99:
17974     case DW_LANG_C_plus_plus:
17975     case DW_LANG_ObjC:
17976     case DW_LANG_ObjC_plus_plus:
17977     case DW_LANG_Java:
17978       return 0;
17979     case DW_LANG_Fortran77:
17980     case DW_LANG_Fortran90:
17981     case DW_LANG_Fortran95:
17982       return 1;
17983     case DW_LANG_UPC:
17984     case DW_LANG_D:
17985     case DW_LANG_Python:
17986       return dwarf_version >= 4 ? 0 : -1;
17987     case DW_LANG_Ada95:
17988     case DW_LANG_Ada83:
17989     case DW_LANG_Cobol74:
17990     case DW_LANG_Cobol85:
17991     case DW_LANG_Pascal83:
17992     case DW_LANG_Modula2:
17993     case DW_LANG_PLI:
17994       return dwarf_version >= 4 ? 1 : -1;
17995     default:
17996       return -1;
17997     }
17998 }
17999
18000 /* Given a tree node describing an array bound (either lower or upper) output
18001    a representation for that bound.  */
18002
18003 static void
18004 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
18005 {
18006   switch (TREE_CODE (bound))
18007     {
18008     case ERROR_MARK:
18009       return;
18010
18011     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
18012     case INTEGER_CST:
18013       {
18014         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
18015         int dflt;
18016
18017         /* Use the default if possible.  */
18018         if (bound_attr == DW_AT_lower_bound
18019             && host_integerp (bound, 0)
18020             && (dflt = lower_bound_default ()) != -1
18021             && tree_low_cst (bound, 0) == dflt)
18022           ;
18023
18024         /* Otherwise represent the bound as an unsigned value with the
18025            precision of its type.  The precision and signedness of the
18026            type will be necessary to re-interpret it unambiguously.  */
18027         else if (prec < HOST_BITS_PER_WIDE_INT)
18028           {
18029             unsigned HOST_WIDE_INT mask
18030               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
18031             add_AT_unsigned (subrange_die, bound_attr,
18032                              TREE_INT_CST_LOW (bound) & mask);
18033           }
18034         else if (prec == HOST_BITS_PER_WIDE_INT
18035                  || TREE_INT_CST_HIGH (bound) == 0)
18036           add_AT_unsigned (subrange_die, bound_attr,
18037                            TREE_INT_CST_LOW (bound));
18038         else
18039           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
18040                          TREE_INT_CST_LOW (bound));
18041       }
18042       break;
18043
18044     CASE_CONVERT:
18045     case VIEW_CONVERT_EXPR:
18046       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
18047       break;
18048
18049     case SAVE_EXPR:
18050       break;
18051
18052     case VAR_DECL:
18053     case PARM_DECL:
18054     case RESULT_DECL:
18055       {
18056         dw_die_ref decl_die = lookup_decl_die (bound);
18057
18058         /* ??? Can this happen, or should the variable have been bound
18059            first?  Probably it can, since I imagine that we try to create
18060            the types of parameters in the order in which they exist in
18061            the list, and won't have created a forward reference to a
18062            later parameter.  */
18063         if (decl_die != NULL)
18064           {
18065             add_AT_die_ref (subrange_die, bound_attr, decl_die);
18066             break;
18067           }
18068       }
18069       /* FALLTHRU */
18070
18071     default:
18072       {
18073         /* Otherwise try to create a stack operation procedure to
18074            evaluate the value of the array bound.  */
18075
18076         dw_die_ref ctx, decl_die;
18077         dw_loc_list_ref list;
18078
18079         list = loc_list_from_tree (bound, 2);
18080         if (list == NULL || single_element_loc_list_p (list))
18081           {
18082             /* If DW_AT_*bound is not a reference nor constant, it is
18083                a DWARF expression rather than location description.
18084                For that loc_list_from_tree (bound, 0) is needed.
18085                If that fails to give a single element list,
18086                fall back to outputting this as a reference anyway.  */
18087             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
18088             if (list2 && single_element_loc_list_p (list2))
18089               {
18090                 add_AT_loc (subrange_die, bound_attr, list2->expr);
18091                 break;
18092               }
18093           }
18094         if (list == NULL)
18095           break;
18096
18097         if (current_function_decl == 0)
18098           ctx = comp_unit_die ();
18099         else
18100           ctx = lookup_decl_die (current_function_decl);
18101
18102         decl_die = new_die (DW_TAG_variable, ctx, bound);
18103         add_AT_flag (decl_die, DW_AT_artificial, 1);
18104         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
18105         add_AT_location_description (decl_die, DW_AT_location, list);
18106         add_AT_die_ref (subrange_die, bound_attr, decl_die);
18107         break;
18108       }
18109     }
18110 }
18111
18112 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
18113    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
18114    Note that the block of subscript information for an array type also
18115    includes information about the element type of the given array type.  */
18116
18117 static void
18118 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
18119 {
18120   unsigned dimension_number;
18121   tree lower, upper;
18122   dw_die_ref subrange_die;
18123
18124   for (dimension_number = 0;
18125        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
18126        type = TREE_TYPE (type), dimension_number++)
18127     {
18128       tree domain = TYPE_DOMAIN (type);
18129
18130       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
18131         break;
18132
18133       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
18134          and (in GNU C only) variable bounds.  Handle all three forms
18135          here.  */
18136       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
18137       if (domain)
18138         {
18139           /* We have an array type with specified bounds.  */
18140           lower = TYPE_MIN_VALUE (domain);
18141           upper = TYPE_MAX_VALUE (domain);
18142
18143           /* Define the index type.  */
18144           if (TREE_TYPE (domain))
18145             {
18146               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
18147                  TREE_TYPE field.  We can't emit debug info for this
18148                  because it is an unnamed integral type.  */
18149               if (TREE_CODE (domain) == INTEGER_TYPE
18150                   && TYPE_NAME (domain) == NULL_TREE
18151                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
18152                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
18153                 ;
18154               else
18155                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
18156                                     type_die);
18157             }
18158
18159           /* ??? If upper is NULL, the array has unspecified length,
18160              but it does have a lower bound.  This happens with Fortran
18161                dimension arr(N:*)
18162              Since the debugger is definitely going to need to know N
18163              to produce useful results, go ahead and output the lower
18164              bound solo, and hope the debugger can cope.  */
18165
18166           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
18167           if (upper)
18168             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
18169         }
18170
18171       /* Otherwise we have an array type with an unspecified length.  The
18172          DWARF-2 spec does not say how to handle this; let's just leave out the
18173          bounds.  */
18174     }
18175 }
18176
18177 static void
18178 add_byte_size_attribute (dw_die_ref die, tree tree_node)
18179 {
18180   unsigned size;
18181
18182   switch (TREE_CODE (tree_node))
18183     {
18184     case ERROR_MARK:
18185       size = 0;
18186       break;
18187     case ENUMERAL_TYPE:
18188     case RECORD_TYPE:
18189     case UNION_TYPE:
18190     case QUAL_UNION_TYPE:
18191       size = int_size_in_bytes (tree_node);
18192       break;
18193     case FIELD_DECL:
18194       /* For a data member of a struct or union, the DW_AT_byte_size is
18195          generally given as the number of bytes normally allocated for an
18196          object of the *declared* type of the member itself.  This is true
18197          even for bit-fields.  */
18198       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
18199       break;
18200     default:
18201       gcc_unreachable ();
18202     }
18203
18204   /* Note that `size' might be -1 when we get to this point.  If it is, that
18205      indicates that the byte size of the entity in question is variable.  We
18206      have no good way of expressing this fact in Dwarf at the present time,
18207      so just let the -1 pass on through.  */
18208   add_AT_unsigned (die, DW_AT_byte_size, size);
18209 }
18210
18211 /* For a FIELD_DECL node which represents a bit-field, output an attribute
18212    which specifies the distance in bits from the highest order bit of the
18213    "containing object" for the bit-field to the highest order bit of the
18214    bit-field itself.
18215
18216    For any given bit-field, the "containing object" is a hypothetical object
18217    (of some integral or enum type) within which the given bit-field lives.  The
18218    type of this hypothetical "containing object" is always the same as the
18219    declared type of the individual bit-field itself.  The determination of the
18220    exact location of the "containing object" for a bit-field is rather
18221    complicated.  It's handled by the `field_byte_offset' function (above).
18222
18223    Note that it is the size (in bytes) of the hypothetical "containing object"
18224    which will be given in the DW_AT_byte_size attribute for this bit-field.
18225    (See `byte_size_attribute' above).  */
18226
18227 static inline void
18228 add_bit_offset_attribute (dw_die_ref die, tree decl)
18229 {
18230   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
18231   tree type = DECL_BIT_FIELD_TYPE (decl);
18232   HOST_WIDE_INT bitpos_int;
18233   HOST_WIDE_INT highest_order_object_bit_offset;
18234   HOST_WIDE_INT highest_order_field_bit_offset;
18235   HOST_WIDE_INT bit_offset;
18236
18237   /* Must be a field and a bit field.  */
18238   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
18239
18240   /* We can't yet handle bit-fields whose offsets are variable, so if we
18241      encounter such things, just return without generating any attribute
18242      whatsoever.  Likewise for variable or too large size.  */
18243   if (! host_integerp (bit_position (decl), 0)
18244       || ! host_integerp (DECL_SIZE (decl), 1))
18245     return;
18246
18247   bitpos_int = int_bit_position (decl);
18248
18249   /* Note that the bit offset is always the distance (in bits) from the
18250      highest-order bit of the "containing object" to the highest-order bit of
18251      the bit-field itself.  Since the "high-order end" of any object or field
18252      is different on big-endian and little-endian machines, the computation
18253      below must take account of these differences.  */
18254   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
18255   highest_order_field_bit_offset = bitpos_int;
18256
18257   if (! BYTES_BIG_ENDIAN)
18258     {
18259       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
18260       highest_order_object_bit_offset += simple_type_size_in_bits (type);
18261     }
18262
18263   bit_offset
18264     = (! BYTES_BIG_ENDIAN
18265        ? highest_order_object_bit_offset - highest_order_field_bit_offset
18266        : highest_order_field_bit_offset - highest_order_object_bit_offset);
18267
18268   if (bit_offset < 0)
18269     add_AT_int (die, DW_AT_bit_offset, bit_offset);
18270   else
18271     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
18272 }
18273
18274 /* For a FIELD_DECL node which represents a bit field, output an attribute
18275    which specifies the length in bits of the given field.  */
18276
18277 static inline void
18278 add_bit_size_attribute (dw_die_ref die, tree decl)
18279 {
18280   /* Must be a field and a bit field.  */
18281   gcc_assert (TREE_CODE (decl) == FIELD_DECL
18282               && DECL_BIT_FIELD_TYPE (decl));
18283
18284   if (host_integerp (DECL_SIZE (decl), 1))
18285     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
18286 }
18287
18288 /* If the compiled language is ANSI C, then add a 'prototyped'
18289    attribute, if arg types are given for the parameters of a function.  */
18290
18291 static inline void
18292 add_prototyped_attribute (dw_die_ref die, tree func_type)
18293 {
18294   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
18295       && prototype_p (func_type))
18296     add_AT_flag (die, DW_AT_prototyped, 1);
18297 }
18298
18299 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
18300    by looking in either the type declaration or object declaration
18301    equate table.  */
18302
18303 static inline dw_die_ref
18304 add_abstract_origin_attribute (dw_die_ref die, tree origin)
18305 {
18306   dw_die_ref origin_die = NULL;
18307
18308   if (TREE_CODE (origin) != FUNCTION_DECL)
18309     {
18310       /* We may have gotten separated from the block for the inlined
18311          function, if we're in an exception handler or some such; make
18312          sure that the abstract function has been written out.
18313
18314          Doing this for nested functions is wrong, however; functions are
18315          distinct units, and our context might not even be inline.  */
18316       tree fn = origin;
18317
18318       if (TYPE_P (fn))
18319         fn = TYPE_STUB_DECL (fn);
18320
18321       fn = decl_function_context (fn);
18322       if (fn)
18323         dwarf2out_abstract_function (fn);
18324     }
18325
18326   if (DECL_P (origin))
18327     origin_die = lookup_decl_die (origin);
18328   else if (TYPE_P (origin))
18329     origin_die = lookup_type_die (origin);
18330
18331   /* XXX: Functions that are never lowered don't always have correct block
18332      trees (in the case of java, they simply have no block tree, in some other
18333      languages).  For these functions, there is nothing we can really do to
18334      output correct debug info for inlined functions in all cases.  Rather
18335      than die, we'll just produce deficient debug info now, in that we will
18336      have variables without a proper abstract origin.  In the future, when all
18337      functions are lowered, we should re-add a gcc_assert (origin_die)
18338      here.  */
18339
18340   if (origin_die)
18341     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
18342   return origin_die;
18343 }
18344
18345 /* We do not currently support the pure_virtual attribute.  */
18346
18347 static inline void
18348 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
18349 {
18350   if (DECL_VINDEX (func_decl))
18351     {
18352       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18353
18354       if (host_integerp (DECL_VINDEX (func_decl), 0))
18355         add_AT_loc (die, DW_AT_vtable_elem_location,
18356                     new_loc_descr (DW_OP_constu,
18357                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
18358                                    0));
18359
18360       /* GNU extension: Record what type this method came from originally.  */
18361       if (debug_info_level > DINFO_LEVEL_TERSE
18362           && DECL_CONTEXT (func_decl))
18363         add_AT_die_ref (die, DW_AT_containing_type,
18364                         lookup_type_die (DECL_CONTEXT (func_decl)));
18365     }
18366 }
18367 \f
18368 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
18369    given decl.  This used to be a vendor extension until after DWARF 4
18370    standardized it.  */
18371
18372 static void
18373 add_linkage_attr (dw_die_ref die, tree decl)
18374 {
18375   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
18376
18377   /* Mimic what assemble_name_raw does with a leading '*'.  */
18378   if (name[0] == '*')
18379     name = &name[1];
18380
18381   if (dwarf_version >= 4)
18382     add_AT_string (die, DW_AT_linkage_name, name);
18383   else
18384     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
18385 }
18386
18387 /* Add source coordinate attributes for the given decl.  */
18388
18389 static void
18390 add_src_coords_attributes (dw_die_ref die, tree decl)
18391 {
18392   expanded_location s;
18393
18394   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
18395     return;
18396   s = expand_location (DECL_SOURCE_LOCATION (decl));
18397   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
18398   add_AT_unsigned (die, DW_AT_decl_line, s.line);
18399 }
18400
18401 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
18402
18403 static void
18404 add_linkage_name (dw_die_ref die, tree decl)
18405 {
18406   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
18407        && TREE_PUBLIC (decl)
18408        && !DECL_ABSTRACT (decl)
18409        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
18410        && die->die_tag != DW_TAG_member)
18411     {
18412       /* Defer until we have an assembler name set.  */
18413       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
18414         {
18415           limbo_die_node *asm_name;
18416
18417           asm_name = ggc_alloc_cleared_limbo_die_node ();
18418           asm_name->die = die;
18419           asm_name->created_for = decl;
18420           asm_name->next = deferred_asm_name;
18421           deferred_asm_name = asm_name;
18422         }
18423       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18424         add_linkage_attr (die, decl);
18425     }
18426 }
18427
18428 /* Add a DW_AT_name attribute and source coordinate attribute for the
18429    given decl, but only if it actually has a name.  */
18430
18431 static void
18432 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
18433 {
18434   tree decl_name;
18435
18436   decl_name = DECL_NAME (decl);
18437   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
18438     {
18439       const char *name = dwarf2_name (decl, 0);
18440       if (name)
18441         add_name_attribute (die, name);
18442       if (! DECL_ARTIFICIAL (decl))
18443         add_src_coords_attributes (die, decl);
18444
18445       add_linkage_name (die, decl);
18446     }
18447
18448 #ifdef VMS_DEBUGGING_INFO
18449   /* Get the function's name, as described by its RTL.  This may be different
18450      from the DECL_NAME name used in the source file.  */
18451   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
18452     {
18453       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
18454                    XEXP (DECL_RTL (decl), 0));
18455       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
18456     }
18457 #endif /* VMS_DEBUGGING_INFO */
18458 }
18459
18460 #ifdef VMS_DEBUGGING_INFO
18461 /* Output the debug main pointer die for VMS */
18462
18463 void
18464 dwarf2out_vms_debug_main_pointer (void)
18465 {
18466   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18467   dw_die_ref die;
18468
18469   /* Allocate the VMS debug main subprogram die.  */
18470   die = ggc_alloc_cleared_die_node ();
18471   die->die_tag = DW_TAG_subprogram;
18472   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
18473   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
18474                                current_function_funcdef_no);
18475   add_AT_lbl_id (die, DW_AT_entry_pc, label);
18476
18477   /* Make it the first child of comp_unit_die ().  */
18478   die->die_parent = comp_unit_die ();
18479   if (comp_unit_die ()->die_child)
18480     {
18481       die->die_sib = comp_unit_die ()->die_child->die_sib;
18482       comp_unit_die ()->die_child->die_sib = die;
18483     }
18484   else
18485     {
18486       die->die_sib = die;
18487       comp_unit_die ()->die_child = die;
18488     }
18489 }
18490 #endif /* VMS_DEBUGGING_INFO */
18491
18492 /* Push a new declaration scope.  */
18493
18494 static void
18495 push_decl_scope (tree scope)
18496 {
18497   VEC_safe_push (tree, gc, decl_scope_table, scope);
18498 }
18499
18500 /* Pop a declaration scope.  */
18501
18502 static inline void
18503 pop_decl_scope (void)
18504 {
18505   VEC_pop (tree, decl_scope_table);
18506 }
18507
18508 /* Return the DIE for the scope that immediately contains this type.
18509    Non-named types get global scope.  Named types nested in other
18510    types get their containing scope if it's open, or global scope
18511    otherwise.  All other types (i.e. function-local named types) get
18512    the current active scope.  */
18513
18514 static dw_die_ref
18515 scope_die_for (tree t, dw_die_ref context_die)
18516 {
18517   dw_die_ref scope_die = NULL;
18518   tree containing_scope;
18519   int i;
18520
18521   /* Non-types always go in the current scope.  */
18522   gcc_assert (TYPE_P (t));
18523
18524   containing_scope = TYPE_CONTEXT (t);
18525
18526   /* Use the containing namespace if it was passed in (for a declaration).  */
18527   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
18528     {
18529       if (context_die == lookup_decl_die (containing_scope))
18530         /* OK */;
18531       else
18532         containing_scope = NULL_TREE;
18533     }
18534
18535   /* Ignore function type "scopes" from the C frontend.  They mean that
18536      a tagged type is local to a parmlist of a function declarator, but
18537      that isn't useful to DWARF.  */
18538   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
18539     containing_scope = NULL_TREE;
18540
18541   if (SCOPE_FILE_SCOPE_P (containing_scope))
18542     scope_die = comp_unit_die ();
18543   else if (TYPE_P (containing_scope))
18544     {
18545       /* For types, we can just look up the appropriate DIE.  But
18546          first we check to see if we're in the middle of emitting it
18547          so we know where the new DIE should go.  */
18548       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
18549         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
18550           break;
18551
18552       if (i < 0)
18553         {
18554           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
18555                       || TREE_ASM_WRITTEN (containing_scope));
18556           /*We are not in the middle of emitting the type
18557             CONTAINING_SCOPE. Let's see if it's emitted already.  */
18558           scope_die = lookup_type_die (containing_scope);
18559
18560           /* If none of the current dies are suitable, we get file scope.  */
18561           if (scope_die == NULL)
18562             scope_die = comp_unit_die ();
18563         }
18564       else
18565         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
18566     }
18567   else
18568     scope_die = context_die;
18569
18570   return scope_die;
18571 }
18572
18573 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
18574
18575 static inline int
18576 local_scope_p (dw_die_ref context_die)
18577 {
18578   for (; context_die; context_die = context_die->die_parent)
18579     if (context_die->die_tag == DW_TAG_inlined_subroutine
18580         || context_die->die_tag == DW_TAG_subprogram)
18581       return 1;
18582
18583   return 0;
18584 }
18585
18586 /* Returns nonzero if CONTEXT_DIE is a class.  */
18587
18588 static inline int
18589 class_scope_p (dw_die_ref context_die)
18590 {
18591   return (context_die
18592           && (context_die->die_tag == DW_TAG_structure_type
18593               || context_die->die_tag == DW_TAG_class_type
18594               || context_die->die_tag == DW_TAG_interface_type
18595               || context_die->die_tag == DW_TAG_union_type));
18596 }
18597
18598 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
18599    whether or not to treat a DIE in this context as a declaration.  */
18600
18601 static inline int
18602 class_or_namespace_scope_p (dw_die_ref context_die)
18603 {
18604   return (class_scope_p (context_die)
18605           || (context_die && context_die->die_tag == DW_TAG_namespace));
18606 }
18607
18608 /* Many forms of DIEs require a "type description" attribute.  This
18609    routine locates the proper "type descriptor" die for the type given
18610    by 'type', and adds a DW_AT_type attribute below the given die.  */
18611
18612 static void
18613 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
18614                     int decl_volatile, dw_die_ref context_die)
18615 {
18616   enum tree_code code  = TREE_CODE (type);
18617   dw_die_ref type_die  = NULL;
18618
18619   /* ??? If this type is an unnamed subrange type of an integral, floating-point
18620      or fixed-point type, use the inner type.  This is because we have no
18621      support for unnamed types in base_type_die.  This can happen if this is
18622      an Ada subrange type.  Correct solution is emit a subrange type die.  */
18623   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
18624       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
18625     type = TREE_TYPE (type), code = TREE_CODE (type);
18626
18627   if (code == ERROR_MARK
18628       /* Handle a special case.  For functions whose return type is void, we
18629          generate *no* type attribute.  (Note that no object may have type
18630          `void', so this only applies to function return types).  */
18631       || code == VOID_TYPE)
18632     return;
18633
18634   type_die = modified_type_die (type,
18635                                 decl_const || TYPE_READONLY (type),
18636                                 decl_volatile || TYPE_VOLATILE (type),
18637                                 context_die);
18638
18639   if (type_die != NULL)
18640     add_AT_die_ref (object_die, DW_AT_type, type_die);
18641 }
18642
18643 /* Given an object die, add the calling convention attribute for the
18644    function call type.  */
18645 static void
18646 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
18647 {
18648   enum dwarf_calling_convention value = DW_CC_normal;
18649
18650   value = ((enum dwarf_calling_convention)
18651            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
18652
18653   if (is_fortran ()
18654       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
18655     {
18656       /* DWARF 2 doesn't provide a way to identify a program's source-level
18657         entry point.  DW_AT_calling_convention attributes are only meant
18658         to describe functions' calling conventions.  However, lacking a
18659         better way to signal the Fortran main program, we used this for 
18660         a long time, following existing custom.  Now, DWARF 4 has 
18661         DW_AT_main_subprogram, which we add below, but some tools still
18662         rely on the old way, which we thus keep.  */
18663       value = DW_CC_program;
18664
18665       if (dwarf_version >= 4 || !dwarf_strict)
18666         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
18667     }
18668
18669   /* Only add the attribute if the backend requests it, and
18670      is not DW_CC_normal.  */
18671   if (value && (value != DW_CC_normal))
18672     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
18673 }
18674
18675 /* Given a tree pointer to a struct, class, union, or enum type node, return
18676    a pointer to the (string) tag name for the given type, or zero if the type
18677    was declared without a tag.  */
18678
18679 static const char *
18680 type_tag (const_tree type)
18681 {
18682   const char *name = 0;
18683
18684   if (TYPE_NAME (type) != 0)
18685     {
18686       tree t = 0;
18687
18688       /* Find the IDENTIFIER_NODE for the type name.  */
18689       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
18690           && !TYPE_NAMELESS (type))
18691         t = TYPE_NAME (type);
18692
18693       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
18694          a TYPE_DECL node, regardless of whether or not a `typedef' was
18695          involved.  */
18696       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18697                && ! DECL_IGNORED_P (TYPE_NAME (type)))
18698         {
18699           /* We want to be extra verbose.  Don't call dwarf_name if
18700              DECL_NAME isn't set.  The default hook for decl_printable_name
18701              doesn't like that, and in this context it's correct to return
18702              0, instead of "<anonymous>" or the like.  */
18703           if (DECL_NAME (TYPE_NAME (type))
18704               && !DECL_NAMELESS (TYPE_NAME (type)))
18705             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
18706         }
18707
18708       /* Now get the name as a string, or invent one.  */
18709       if (!name && t != 0)
18710         name = IDENTIFIER_POINTER (t);
18711     }
18712
18713   return (name == 0 || *name == '\0') ? 0 : name;
18714 }
18715
18716 /* Return the type associated with a data member, make a special check
18717    for bit field types.  */
18718
18719 static inline tree
18720 member_declared_type (const_tree member)
18721 {
18722   return (DECL_BIT_FIELD_TYPE (member)
18723           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
18724 }
18725
18726 /* Get the decl's label, as described by its RTL. This may be different
18727    from the DECL_NAME name used in the source file.  */
18728
18729 #if 0
18730 static const char *
18731 decl_start_label (tree decl)
18732 {
18733   rtx x;
18734   const char *fnname;
18735
18736   x = DECL_RTL (decl);
18737   gcc_assert (MEM_P (x));
18738
18739   x = XEXP (x, 0);
18740   gcc_assert (GET_CODE (x) == SYMBOL_REF);
18741
18742   fnname = XSTR (x, 0);
18743   return fnname;
18744 }
18745 #endif
18746 \f
18747 /* These routines generate the internal representation of the DIE's for
18748    the compilation unit.  Debugging information is collected by walking
18749    the declaration trees passed in from dwarf2out_decl().  */
18750
18751 static void
18752 gen_array_type_die (tree type, dw_die_ref context_die)
18753 {
18754   dw_die_ref scope_die = scope_die_for (type, context_die);
18755   dw_die_ref array_die;
18756
18757   /* GNU compilers represent multidimensional array types as sequences of one
18758      dimensional array types whose element types are themselves array types.
18759      We sometimes squish that down to a single array_type DIE with multiple
18760      subscripts in the Dwarf debugging info.  The draft Dwarf specification
18761      say that we are allowed to do this kind of compression in C, because
18762      there is no difference between an array of arrays and a multidimensional
18763      array.  We don't do this for Ada to remain as close as possible to the
18764      actual representation, which is especially important against the language
18765      flexibilty wrt arrays of variable size.  */
18766
18767   bool collapse_nested_arrays = !is_ada ();
18768   tree element_type;
18769
18770   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
18771      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
18772   if (TYPE_STRING_FLAG (type)
18773       && TREE_CODE (type) == ARRAY_TYPE
18774       && is_fortran ()
18775       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
18776     {
18777       HOST_WIDE_INT size;
18778
18779       array_die = new_die (DW_TAG_string_type, scope_die, type);
18780       add_name_attribute (array_die, type_tag (type));
18781       equate_type_number_to_die (type, array_die);
18782       size = int_size_in_bytes (type);
18783       if (size >= 0)
18784         add_AT_unsigned (array_die, DW_AT_byte_size, size);
18785       else if (TYPE_DOMAIN (type) != NULL_TREE
18786                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
18787                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
18788         {
18789           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
18790           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
18791
18792           size = int_size_in_bytes (TREE_TYPE (szdecl));
18793           if (loc && size > 0)
18794             {
18795               add_AT_location_description (array_die, DW_AT_string_length, loc);
18796               if (size != DWARF2_ADDR_SIZE)
18797                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
18798             }
18799         }
18800       return;
18801     }
18802
18803   /* ??? The SGI dwarf reader fails for array of array of enum types
18804      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
18805      array type comes before the outer array type.  We thus call gen_type_die
18806      before we new_die and must prevent nested array types collapsing for this
18807      target.  */
18808
18809 #ifdef MIPS_DEBUGGING_INFO
18810   gen_type_die (TREE_TYPE (type), context_die);
18811   collapse_nested_arrays = false;
18812 #endif
18813
18814   array_die = new_die (DW_TAG_array_type, scope_die, type);
18815   add_name_attribute (array_die, type_tag (type));
18816   add_gnat_descriptive_type_attribute (array_die, type, context_die);
18817   equate_type_number_to_die (type, array_die);
18818
18819   if (TREE_CODE (type) == VECTOR_TYPE)
18820     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
18821
18822   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18823   if (is_fortran ()
18824       && TREE_CODE (type) == ARRAY_TYPE
18825       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
18826       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
18827     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18828
18829 #if 0
18830   /* We default the array ordering.  SDB will probably do
18831      the right things even if DW_AT_ordering is not present.  It's not even
18832      an issue until we start to get into multidimensional arrays anyway.  If
18833      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
18834      then we'll have to put the DW_AT_ordering attribute back in.  (But if
18835      and when we find out that we need to put these in, we will only do so
18836      for multidimensional arrays.  */
18837   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
18838 #endif
18839
18840 #ifdef MIPS_DEBUGGING_INFO
18841   /* The SGI compilers handle arrays of unknown bound by setting
18842      AT_declaration and not emitting any subrange DIEs.  */
18843   if (TREE_CODE (type) == ARRAY_TYPE
18844       && ! TYPE_DOMAIN (type))
18845     add_AT_flag (array_die, DW_AT_declaration, 1);
18846   else
18847 #endif
18848   if (TREE_CODE (type) == VECTOR_TYPE)
18849     {
18850       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
18851       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
18852       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
18853       add_bound_info (subrange_die, DW_AT_upper_bound,
18854                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
18855     }
18856   else
18857     add_subscript_info (array_die, type, collapse_nested_arrays);
18858
18859   /* Add representation of the type of the elements of this array type and
18860      emit the corresponding DIE if we haven't done it already.  */
18861   element_type = TREE_TYPE (type);
18862   if (collapse_nested_arrays)
18863     while (TREE_CODE (element_type) == ARRAY_TYPE)
18864       {
18865         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
18866           break;
18867         element_type = TREE_TYPE (element_type);
18868       }
18869
18870 #ifndef MIPS_DEBUGGING_INFO
18871   gen_type_die (element_type, context_die);
18872 #endif
18873
18874   add_type_attribute (array_die, element_type, 0, 0, context_die);
18875
18876   if (get_AT (array_die, DW_AT_name))
18877     add_pubtype (type, array_die);
18878 }
18879
18880 static dw_loc_descr_ref
18881 descr_info_loc (tree val, tree base_decl)
18882 {
18883   HOST_WIDE_INT size;
18884   dw_loc_descr_ref loc, loc2;
18885   enum dwarf_location_atom op;
18886
18887   if (val == base_decl)
18888     return new_loc_descr (DW_OP_push_object_address, 0, 0);
18889
18890   switch (TREE_CODE (val))
18891     {
18892     CASE_CONVERT:
18893       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18894     case VAR_DECL:
18895       return loc_descriptor_from_tree (val, 0);
18896     case INTEGER_CST:
18897       if (host_integerp (val, 0))
18898         return int_loc_descriptor (tree_low_cst (val, 0));
18899       break;
18900     case INDIRECT_REF:
18901       size = int_size_in_bytes (TREE_TYPE (val));
18902       if (size < 0)
18903         break;
18904       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18905       if (!loc)
18906         break;
18907       if (size == DWARF2_ADDR_SIZE)
18908         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
18909       else
18910         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
18911       return loc;
18912     case POINTER_PLUS_EXPR:
18913     case PLUS_EXPR:
18914       if (host_integerp (TREE_OPERAND (val, 1), 1)
18915           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
18916              < 16384)
18917         {
18918           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18919           if (!loc)
18920             break;
18921           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
18922         }
18923       else
18924         {
18925           op = DW_OP_plus;
18926         do_binop:
18927           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
18928           if (!loc)
18929             break;
18930           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
18931           if (!loc2)
18932             break;
18933           add_loc_descr (&loc, loc2);
18934           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
18935         }
18936       return loc;
18937     case MINUS_EXPR:
18938       op = DW_OP_minus;
18939       goto do_binop;
18940     case MULT_EXPR:
18941       op = DW_OP_mul;
18942       goto do_binop;
18943     case EQ_EXPR:
18944       op = DW_OP_eq;
18945       goto do_binop;
18946     case NE_EXPR:
18947       op = DW_OP_ne;
18948       goto do_binop;
18949     default:
18950       break;
18951     }
18952   return NULL;
18953 }
18954
18955 static void
18956 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
18957                       tree val, tree base_decl)
18958 {
18959   dw_loc_descr_ref loc;
18960
18961   if (host_integerp (val, 0))
18962     {
18963       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18964       return;
18965     }
18966
18967   loc = descr_info_loc (val, base_decl);
18968   if (!loc)
18969     return;
18970
18971   add_AT_loc (die, attr, loc);
18972 }
18973
18974 /* This routine generates DIE for array with hidden descriptor, details
18975    are filled into *info by a langhook.  */
18976
18977 static void
18978 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18979                           dw_die_ref context_die)
18980 {
18981   dw_die_ref scope_die = scope_die_for (type, context_die);
18982   dw_die_ref array_die;
18983   int dim;
18984
18985   array_die = new_die (DW_TAG_array_type, scope_die, type);
18986   add_name_attribute (array_die, type_tag (type));
18987   equate_type_number_to_die (type, array_die);
18988
18989   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18990   if (is_fortran ()
18991       && info->ndimensions >= 2)
18992     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18993
18994   if (info->data_location)
18995     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18996                           info->base_decl);
18997   if (info->associated)
18998     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18999                           info->base_decl);
19000   if (info->allocated)
19001     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
19002                           info->base_decl);
19003
19004   for (dim = 0; dim < info->ndimensions; dim++)
19005     {
19006       dw_die_ref subrange_die
19007         = new_die (DW_TAG_subrange_type, array_die, NULL);
19008
19009       if (info->dimen[dim].lower_bound)
19010         {
19011           /* If it is the default value, omit it.  */
19012           int dflt;
19013
19014           if (host_integerp (info->dimen[dim].lower_bound, 0)
19015               && (dflt = lower_bound_default ()) != -1
19016               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
19017             ;
19018           else
19019             add_descr_info_field (subrange_die, DW_AT_lower_bound,
19020                                   info->dimen[dim].lower_bound,
19021                                   info->base_decl);
19022         }
19023       if (info->dimen[dim].upper_bound)
19024         add_descr_info_field (subrange_die, DW_AT_upper_bound,
19025                               info->dimen[dim].upper_bound,
19026                               info->base_decl);
19027       if (info->dimen[dim].stride)
19028         add_descr_info_field (subrange_die, DW_AT_byte_stride,
19029                               info->dimen[dim].stride,
19030                               info->base_decl);
19031     }
19032
19033   gen_type_die (info->element_type, context_die);
19034   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
19035
19036   if (get_AT (array_die, DW_AT_name))
19037     add_pubtype (type, array_die);
19038 }
19039
19040 #if 0
19041 static void
19042 gen_entry_point_die (tree decl, dw_die_ref context_die)
19043 {
19044   tree origin = decl_ultimate_origin (decl);
19045   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
19046
19047   if (origin != NULL)
19048     add_abstract_origin_attribute (decl_die, origin);
19049   else
19050     {
19051       add_name_and_src_coords_attributes (decl_die, decl);
19052       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
19053                           0, 0, context_die);
19054     }
19055
19056   if (DECL_ABSTRACT (decl))
19057     equate_decl_number_to_die (decl, decl_die);
19058   else
19059     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
19060 }
19061 #endif
19062
19063 /* Walk through the list of incomplete types again, trying once more to
19064    emit full debugging info for them.  */
19065
19066 static void
19067 retry_incomplete_types (void)
19068 {
19069   int i;
19070
19071   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
19072     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
19073                                   DINFO_USAGE_DIR_USE))
19074       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
19075 }
19076
19077 /* Determine what tag to use for a record type.  */
19078
19079 static enum dwarf_tag
19080 record_type_tag (tree type)
19081 {
19082   if (! lang_hooks.types.classify_record)
19083     return DW_TAG_structure_type;
19084
19085   switch (lang_hooks.types.classify_record (type))
19086     {
19087     case RECORD_IS_STRUCT:
19088       return DW_TAG_structure_type;
19089
19090     case RECORD_IS_CLASS:
19091       return DW_TAG_class_type;
19092
19093     case RECORD_IS_INTERFACE:
19094       if (dwarf_version >= 3 || !dwarf_strict)
19095         return DW_TAG_interface_type;
19096       return DW_TAG_structure_type;
19097
19098     default:
19099       gcc_unreachable ();
19100     }
19101 }
19102
19103 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
19104    include all of the information about the enumeration values also. Each
19105    enumerated type name/value is listed as a child of the enumerated type
19106    DIE.  */
19107
19108 static dw_die_ref
19109 gen_enumeration_type_die (tree type, dw_die_ref context_die)
19110 {
19111   dw_die_ref type_die = lookup_type_die (type);
19112
19113   if (type_die == NULL)
19114     {
19115       type_die = new_die (DW_TAG_enumeration_type,
19116                           scope_die_for (type, context_die), type);
19117       equate_type_number_to_die (type, type_die);
19118       add_name_attribute (type_die, type_tag (type));
19119       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19120       if (dwarf_version >= 4 || !dwarf_strict)
19121         {
19122           if (ENUM_IS_SCOPED (type))
19123             add_AT_flag (type_die, DW_AT_enum_class, 1);
19124           if (ENUM_IS_OPAQUE (type))
19125             add_AT_flag (type_die, DW_AT_declaration, 1);
19126         }
19127     }
19128   else if (! TYPE_SIZE (type))
19129     return type_die;
19130   else
19131     remove_AT (type_die, DW_AT_declaration);
19132
19133   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
19134      given enum type is incomplete, do not generate the DW_AT_byte_size
19135      attribute or the DW_AT_element_list attribute.  */
19136   if (TYPE_SIZE (type))
19137     {
19138       tree link;
19139
19140       TREE_ASM_WRITTEN (type) = 1;
19141       add_byte_size_attribute (type_die, type);
19142       if (TYPE_STUB_DECL (type) != NULL_TREE)
19143         {
19144           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19145           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19146         }
19147
19148       /* If the first reference to this type was as the return type of an
19149          inline function, then it may not have a parent.  Fix this now.  */
19150       if (type_die->die_parent == NULL)
19151         add_child_die (scope_die_for (type, context_die), type_die);
19152
19153       for (link = TYPE_VALUES (type);
19154            link != NULL; link = TREE_CHAIN (link))
19155         {
19156           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
19157           tree value = TREE_VALUE (link);
19158
19159           add_name_attribute (enum_die,
19160                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
19161
19162           if (TREE_CODE (value) == CONST_DECL)
19163             value = DECL_INITIAL (value);
19164
19165           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
19166             /* DWARF2 does not provide a way of indicating whether or
19167                not enumeration constants are signed or unsigned.  GDB
19168                always assumes the values are signed, so we output all
19169                values as if they were signed.  That means that
19170                enumeration constants with very large unsigned values
19171                will appear to have negative values in the debugger.  */
19172             add_AT_int (enum_die, DW_AT_const_value,
19173                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
19174         }
19175     }
19176   else
19177     add_AT_flag (type_die, DW_AT_declaration, 1);
19178
19179   if (get_AT (type_die, DW_AT_name))
19180     add_pubtype (type, type_die);
19181
19182   return type_die;
19183 }
19184
19185 /* Generate a DIE to represent either a real live formal parameter decl or to
19186    represent just the type of some formal parameter position in some function
19187    type.
19188
19189    Note that this routine is a bit unusual because its argument may be a
19190    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
19191    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
19192    node.  If it's the former then this function is being called to output a
19193    DIE to represent a formal parameter object (or some inlining thereof).  If
19194    it's the latter, then this function is only being called to output a
19195    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
19196    argument type of some subprogram type.
19197    If EMIT_NAME_P is true, name and source coordinate attributes
19198    are emitted.  */
19199
19200 static dw_die_ref
19201 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
19202                           dw_die_ref context_die)
19203 {
19204   tree node_or_origin = node ? node : origin;
19205   tree ultimate_origin;
19206   dw_die_ref parm_die
19207     = new_die (DW_TAG_formal_parameter, context_die, node);
19208
19209   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
19210     {
19211     case tcc_declaration:
19212       ultimate_origin = decl_ultimate_origin (node_or_origin);
19213       if (node || ultimate_origin)
19214         origin = ultimate_origin;
19215       if (origin != NULL)
19216         add_abstract_origin_attribute (parm_die, origin);
19217       else if (emit_name_p)
19218         add_name_and_src_coords_attributes (parm_die, node);
19219       if (origin == NULL
19220           || (! DECL_ABSTRACT (node_or_origin)
19221               && variably_modified_type_p (TREE_TYPE (node_or_origin),
19222                                            decl_function_context
19223                                                             (node_or_origin))))
19224         {
19225           tree type = TREE_TYPE (node_or_origin);
19226           if (decl_by_reference_p (node_or_origin))
19227             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
19228                                 context_die);
19229           else
19230             add_type_attribute (parm_die, type,
19231                                 TREE_READONLY (node_or_origin),
19232                                 TREE_THIS_VOLATILE (node_or_origin),
19233                                 context_die);
19234         }
19235       if (origin == NULL && DECL_ARTIFICIAL (node))
19236         add_AT_flag (parm_die, DW_AT_artificial, 1);
19237
19238       if (node && node != origin)
19239         equate_decl_number_to_die (node, parm_die);
19240       if (! DECL_ABSTRACT (node_or_origin))
19241         add_location_or_const_value_attribute (parm_die, node_or_origin,
19242                                                node == NULL, DW_AT_location);
19243
19244       break;
19245
19246     case tcc_type:
19247       /* We were called with some kind of a ..._TYPE node.  */
19248       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
19249       break;
19250
19251     default:
19252       gcc_unreachable ();
19253     }
19254
19255   return parm_die;
19256 }
19257
19258 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
19259    children DW_TAG_formal_parameter DIEs representing the arguments of the
19260    parameter pack.
19261
19262    PARM_PACK must be a function parameter pack.
19263    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
19264    must point to the subsequent arguments of the function PACK_ARG belongs to.
19265    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
19266    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
19267    following the last one for which a DIE was generated.  */
19268
19269 static dw_die_ref
19270 gen_formal_parameter_pack_die  (tree parm_pack,
19271                                 tree pack_arg,
19272                                 dw_die_ref subr_die,
19273                                 tree *next_arg)
19274 {
19275   tree arg;
19276   dw_die_ref parm_pack_die;
19277
19278   gcc_assert (parm_pack
19279               && lang_hooks.function_parameter_pack_p (parm_pack)
19280               && subr_die);
19281
19282   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
19283   add_src_coords_attributes (parm_pack_die, parm_pack);
19284
19285   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
19286     {
19287       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
19288                                                                  parm_pack))
19289         break;
19290       gen_formal_parameter_die (arg, NULL,
19291                                 false /* Don't emit name attribute.  */,
19292                                 parm_pack_die);
19293     }
19294   if (next_arg)
19295     *next_arg = arg;
19296   return parm_pack_die;
19297 }
19298
19299 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
19300    at the end of an (ANSI prototyped) formal parameters list.  */
19301
19302 static void
19303 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
19304 {
19305   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
19306 }
19307
19308 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
19309    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
19310    parameters as specified in some function type specification (except for
19311    those which appear as part of a function *definition*).  */
19312
19313 static void
19314 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
19315 {
19316   tree link;
19317   tree formal_type = NULL;
19318   tree first_parm_type;
19319   tree arg;
19320
19321   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
19322     {
19323       arg = DECL_ARGUMENTS (function_or_method_type);
19324       function_or_method_type = TREE_TYPE (function_or_method_type);
19325     }
19326   else
19327     arg = NULL_TREE;
19328
19329   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
19330
19331   /* Make our first pass over the list of formal parameter types and output a
19332      DW_TAG_formal_parameter DIE for each one.  */
19333   for (link = first_parm_type; link; )
19334     {
19335       dw_die_ref parm_die;
19336
19337       formal_type = TREE_VALUE (link);
19338       if (formal_type == void_type_node)
19339         break;
19340
19341       /* Output a (nameless) DIE to represent the formal parameter itself.  */
19342       parm_die = gen_formal_parameter_die (formal_type, NULL,
19343                                            true /* Emit name attribute.  */,
19344                                            context_die);
19345       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
19346           && link == first_parm_type)
19347         {
19348           add_AT_flag (parm_die, DW_AT_artificial, 1);
19349           if (dwarf_version >= 3 || !dwarf_strict)
19350             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
19351         }
19352       else if (arg && DECL_ARTIFICIAL (arg))
19353         add_AT_flag (parm_die, DW_AT_artificial, 1);
19354
19355       link = TREE_CHAIN (link);
19356       if (arg)
19357         arg = DECL_CHAIN (arg);
19358     }
19359
19360   /* If this function type has an ellipsis, add a
19361      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
19362   if (formal_type != void_type_node)
19363     gen_unspecified_parameters_die (function_or_method_type, context_die);
19364
19365   /* Make our second (and final) pass over the list of formal parameter types
19366      and output DIEs to represent those types (as necessary).  */
19367   for (link = TYPE_ARG_TYPES (function_or_method_type);
19368        link && TREE_VALUE (link);
19369        link = TREE_CHAIN (link))
19370     gen_type_die (TREE_VALUE (link), context_die);
19371 }
19372
19373 /* We want to generate the DIE for TYPE so that we can generate the
19374    die for MEMBER, which has been defined; we will need to refer back
19375    to the member declaration nested within TYPE.  If we're trying to
19376    generate minimal debug info for TYPE, processing TYPE won't do the
19377    trick; we need to attach the member declaration by hand.  */
19378
19379 static void
19380 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
19381 {
19382   gen_type_die (type, context_die);
19383
19384   /* If we're trying to avoid duplicate debug info, we may not have
19385      emitted the member decl for this function.  Emit it now.  */
19386   if (TYPE_STUB_DECL (type)
19387       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
19388       && ! lookup_decl_die (member))
19389     {
19390       dw_die_ref type_die;
19391       gcc_assert (!decl_ultimate_origin (member));
19392
19393       push_decl_scope (type);
19394       type_die = lookup_type_die_strip_naming_typedef (type);
19395       if (TREE_CODE (member) == FUNCTION_DECL)
19396         gen_subprogram_die (member, type_die);
19397       else if (TREE_CODE (member) == FIELD_DECL)
19398         {
19399           /* Ignore the nameless fields that are used to skip bits but handle
19400              C++ anonymous unions and structs.  */
19401           if (DECL_NAME (member) != NULL_TREE
19402               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
19403               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
19404             {
19405               gen_type_die (member_declared_type (member), type_die);
19406               gen_field_die (member, type_die);
19407             }
19408         }
19409       else
19410         gen_variable_die (member, NULL_TREE, type_die);
19411
19412       pop_decl_scope ();
19413     }
19414 }
19415
19416 /* Generate the DWARF2 info for the "abstract" instance of a function which we
19417    may later generate inlined and/or out-of-line instances of.  */
19418
19419 static void
19420 dwarf2out_abstract_function (tree decl)
19421 {
19422   dw_die_ref old_die;
19423   tree save_fn;
19424   tree context;
19425   int was_abstract;
19426   htab_t old_decl_loc_table;
19427   htab_t old_cached_dw_loc_list_table;
19428   int old_call_site_count, old_tail_call_site_count;
19429   struct call_arg_loc_node *old_call_arg_locations;
19430
19431   /* Make sure we have the actual abstract inline, not a clone.  */
19432   decl = DECL_ORIGIN (decl);
19433
19434   old_die = lookup_decl_die (decl);
19435   if (old_die && get_AT (old_die, DW_AT_inline))
19436     /* We've already generated the abstract instance.  */
19437     return;
19438
19439   /* We can be called while recursively when seeing block defining inlined subroutine
19440      DIE.  Be sure to not clobber the outer location table nor use it or we would
19441      get locations in abstract instantces.  */
19442   old_decl_loc_table = decl_loc_table;
19443   decl_loc_table = NULL;
19444   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
19445   cached_dw_loc_list_table = NULL;
19446   old_call_arg_locations = call_arg_locations;
19447   call_arg_locations = NULL;
19448   old_call_site_count = call_site_count;
19449   call_site_count = -1;
19450   old_tail_call_site_count = tail_call_site_count;
19451   tail_call_site_count = -1;
19452
19453   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
19454      we don't get confused by DECL_ABSTRACT.  */
19455   if (debug_info_level > DINFO_LEVEL_TERSE)
19456     {
19457       context = decl_class_context (decl);
19458       if (context)
19459         gen_type_die_for_member
19460           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
19461     }
19462
19463   /* Pretend we've just finished compiling this function.  */
19464   save_fn = current_function_decl;
19465   current_function_decl = decl;
19466   push_cfun (DECL_STRUCT_FUNCTION (decl));
19467
19468   was_abstract = DECL_ABSTRACT (decl);
19469   set_decl_abstract_flags (decl, 1);
19470   dwarf2out_decl (decl);
19471   if (! was_abstract)
19472     set_decl_abstract_flags (decl, 0);
19473
19474   current_function_decl = save_fn;
19475   decl_loc_table = old_decl_loc_table;
19476   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
19477   call_arg_locations = old_call_arg_locations;
19478   call_site_count = old_call_site_count;
19479   tail_call_site_count = old_tail_call_site_count;
19480   pop_cfun ();
19481 }
19482
19483 /* Helper function of premark_used_types() which gets called through
19484    htab_traverse.
19485
19486    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19487    marked as unused by prune_unused_types.  */
19488
19489 static int
19490 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
19491 {
19492   tree type;
19493   dw_die_ref die;
19494
19495   type = (tree) *slot;
19496   die = lookup_type_die (type);
19497   if (die != NULL)
19498     die->die_perennial_p = 1;
19499   return 1;
19500 }
19501
19502 /* Helper function of premark_types_used_by_global_vars which gets called
19503    through htab_traverse.
19504
19505    Marks the DIE of a given type in *SLOT as perennial, so it never gets
19506    marked as unused by prune_unused_types. The DIE of the type is marked
19507    only if the global variable using the type will actually be emitted.  */
19508
19509 static int
19510 premark_types_used_by_global_vars_helper (void **slot,
19511                                           void *data ATTRIBUTE_UNUSED)
19512 {
19513   struct types_used_by_vars_entry *entry;
19514   dw_die_ref die;
19515
19516   entry = (struct types_used_by_vars_entry *) *slot;
19517   gcc_assert (entry->type != NULL
19518               && entry->var_decl != NULL);
19519   die = lookup_type_die (entry->type);
19520   if (die)
19521     {
19522       /* Ask cgraph if the global variable really is to be emitted.
19523          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
19524       struct varpool_node *node = varpool_get_node (entry->var_decl);
19525       if (node && node->needed)
19526         {
19527           die->die_perennial_p = 1;
19528           /* Keep the parent DIEs as well.  */
19529           while ((die = die->die_parent) && die->die_perennial_p == 0)
19530             die->die_perennial_p = 1;
19531         }
19532     }
19533   return 1;
19534 }
19535
19536 /* Mark all members of used_types_hash as perennial.  */
19537
19538 static void
19539 premark_used_types (void)
19540 {
19541   if (cfun && cfun->used_types_hash)
19542     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
19543 }
19544
19545 /* Mark all members of types_used_by_vars_entry as perennial.  */
19546
19547 static void
19548 premark_types_used_by_global_vars (void)
19549 {
19550   if (types_used_by_vars_hash)
19551     htab_traverse (types_used_by_vars_hash,
19552                    premark_types_used_by_global_vars_helper, NULL);
19553 }
19554
19555 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
19556    for CA_LOC call arg loc node.  */
19557
19558 static dw_die_ref
19559 gen_call_site_die (tree decl, dw_die_ref subr_die,
19560                    struct call_arg_loc_node *ca_loc)
19561 {
19562   dw_die_ref stmt_die = NULL, die;
19563   tree block = ca_loc->block;
19564
19565   while (block
19566          && block != DECL_INITIAL (decl)
19567          && TREE_CODE (block) == BLOCK)
19568     {
19569       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
19570         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
19571       if (stmt_die)
19572         break;
19573       block = BLOCK_SUPERCONTEXT (block);
19574     }
19575   if (stmt_die == NULL)
19576     stmt_die = subr_die;
19577   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
19578   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
19579   if (ca_loc->tail_call_p)
19580     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
19581   if (ca_loc->symbol_ref)
19582     {
19583       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
19584       if (tdie)
19585         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
19586       else
19587         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
19588     }
19589   return die;
19590 }
19591
19592 /* Generate a DIE to represent a declared function (either file-scope or
19593    block-local).  */
19594
19595 static void
19596 gen_subprogram_die (tree decl, dw_die_ref context_die)
19597 {
19598   tree origin = decl_ultimate_origin (decl);
19599   dw_die_ref subr_die;
19600   tree outer_scope;
19601   dw_die_ref old_die = lookup_decl_die (decl);
19602   int declaration = (current_function_decl != decl
19603                      || class_or_namespace_scope_p (context_die));
19604
19605   premark_used_types ();
19606
19607   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
19608      started to generate the abstract instance of an inline, decided to output
19609      its containing class, and proceeded to emit the declaration of the inline
19610      from the member list for the class.  If so, DECLARATION takes priority;
19611      we'll get back to the abstract instance when done with the class.  */
19612
19613   /* The class-scope declaration DIE must be the primary DIE.  */
19614   if (origin && declaration && class_or_namespace_scope_p (context_die))
19615     {
19616       origin = NULL;
19617       gcc_assert (!old_die);
19618     }
19619
19620   /* Now that the C++ front end lazily declares artificial member fns, we
19621      might need to retrofit the declaration into its class.  */
19622   if (!declaration && !origin && !old_die
19623       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
19624       && !class_or_namespace_scope_p (context_die)
19625       && debug_info_level > DINFO_LEVEL_TERSE)
19626     old_die = force_decl_die (decl);
19627
19628   if (origin != NULL)
19629     {
19630       gcc_assert (!declaration || local_scope_p (context_die));
19631
19632       /* Fixup die_parent for the abstract instance of a nested
19633          inline function.  */
19634       if (old_die && old_die->die_parent == NULL)
19635         add_child_die (context_die, old_die);
19636
19637       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19638       add_abstract_origin_attribute (subr_die, origin);
19639     }
19640   else if (old_die)
19641     {
19642       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19643       struct dwarf_file_data * file_index = lookup_filename (s.file);
19644
19645       if (!get_AT_flag (old_die, DW_AT_declaration)
19646           /* We can have a normal definition following an inline one in the
19647              case of redefinition of GNU C extern inlines.
19648              It seems reasonable to use AT_specification in this case.  */
19649           && !get_AT (old_die, DW_AT_inline))
19650         {
19651           /* Detect and ignore this case, where we are trying to output
19652              something we have already output.  */
19653           return;
19654         }
19655
19656       /* If the definition comes from the same place as the declaration,
19657          maybe use the old DIE.  We always want the DIE for this function
19658          that has the *_pc attributes to be under comp_unit_die so the
19659          debugger can find it.  We also need to do this for abstract
19660          instances of inlines, since the spec requires the out-of-line copy
19661          to have the same parent.  For local class methods, this doesn't
19662          apply; we just use the old DIE.  */
19663       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
19664           && (DECL_ARTIFICIAL (decl)
19665               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
19666                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
19667                       == (unsigned) s.line))))
19668         {
19669           subr_die = old_die;
19670
19671           /* Clear out the declaration attribute and the formal parameters.
19672              Do not remove all children, because it is possible that this
19673              declaration die was forced using force_decl_die(). In such
19674              cases die that forced declaration die (e.g. TAG_imported_module)
19675              is one of the children that we do not want to remove.  */
19676           remove_AT (subr_die, DW_AT_declaration);
19677           remove_AT (subr_die, DW_AT_object_pointer);
19678           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
19679         }
19680       else
19681         {
19682           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19683           add_AT_specification (subr_die, old_die);
19684           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19685             add_AT_file (subr_die, DW_AT_decl_file, file_index);
19686           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19687             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
19688         }
19689     }
19690   else
19691     {
19692       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
19693
19694       if (TREE_PUBLIC (decl))
19695         add_AT_flag (subr_die, DW_AT_external, 1);
19696
19697       add_name_and_src_coords_attributes (subr_die, decl);
19698       if (debug_info_level > DINFO_LEVEL_TERSE)
19699         {
19700           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
19701           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
19702                               0, 0, context_die);
19703         }
19704
19705       add_pure_or_virtual_attribute (subr_die, decl);
19706       if (DECL_ARTIFICIAL (decl))
19707         add_AT_flag (subr_die, DW_AT_artificial, 1);
19708
19709       add_accessibility_attribute (subr_die, decl);
19710     }
19711
19712   if (declaration)
19713     {
19714       if (!old_die || !get_AT (old_die, DW_AT_inline))
19715         {
19716           add_AT_flag (subr_die, DW_AT_declaration, 1);
19717
19718           /* If this is an explicit function declaration then generate
19719              a DW_AT_explicit attribute.  */
19720           if (lang_hooks.decls.function_decl_explicit_p (decl)
19721               && (dwarf_version >= 3 || !dwarf_strict))
19722             add_AT_flag (subr_die, DW_AT_explicit, 1);
19723
19724           /* The first time we see a member function, it is in the context of
19725              the class to which it belongs.  We make sure of this by emitting
19726              the class first.  The next time is the definition, which is
19727              handled above.  The two may come from the same source text.
19728
19729              Note that force_decl_die() forces function declaration die. It is
19730              later reused to represent definition.  */
19731           equate_decl_number_to_die (decl, subr_die);
19732         }
19733     }
19734   else if (DECL_ABSTRACT (decl))
19735     {
19736       if (DECL_DECLARED_INLINE_P (decl))
19737         {
19738           if (cgraph_function_possibly_inlined_p (decl))
19739             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
19740           else
19741             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
19742         }
19743       else
19744         {
19745           if (cgraph_function_possibly_inlined_p (decl))
19746             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
19747           else
19748             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
19749         }
19750
19751       if (DECL_DECLARED_INLINE_P (decl)
19752           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
19753         add_AT_flag (subr_die, DW_AT_artificial, 1);
19754
19755       equate_decl_number_to_die (decl, subr_die);
19756     }
19757   else if (!DECL_EXTERNAL (decl))
19758     {
19759       HOST_WIDE_INT cfa_fb_offset;
19760
19761       if (!old_die || !get_AT (old_die, DW_AT_inline))
19762         equate_decl_number_to_die (decl, subr_die);
19763
19764       if (!flag_reorder_blocks_and_partition)
19765         {
19766           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19767           if (fde->dw_fde_begin)
19768             {
19769               /* We have already generated the labels.  */
19770               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19771               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19772             }
19773           else
19774             {
19775               /* Create start/end labels and add the range.  */
19776               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
19777               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
19778                                            current_function_funcdef_no);
19779               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
19780               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
19781                                            current_function_funcdef_no);
19782               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
19783             }
19784
19785 #if VMS_DEBUGGING_INFO
19786       /* HP OpenVMS Industry Standard 64: DWARF Extensions
19787          Section 2.3 Prologue and Epilogue Attributes:
19788          When a breakpoint is set on entry to a function, it is generally
19789          desirable for execution to be suspended, not on the very first
19790          instruction of the function, but rather at a point after the
19791          function's frame has been set up, after any language defined local
19792          declaration processing has been completed, and before execution of
19793          the first statement of the function begins. Debuggers generally
19794          cannot properly determine where this point is.  Similarly for a
19795          breakpoint set on exit from a function. The prologue and epilogue
19796          attributes allow a compiler to communicate the location(s) to use.  */
19797
19798       {
19799         if (fde->dw_fde_vms_end_prologue)
19800           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
19801             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
19802
19803         if (fde->dw_fde_vms_begin_epilogue)
19804           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
19805             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
19806       }
19807 #endif
19808
19809           add_pubname (decl, subr_die);
19810         }
19811       else
19812         {  /* Generate pubnames entries for the split function code
19813               ranges.  */
19814           dw_fde_ref fde = &fde_table[current_funcdef_fde];
19815
19816           if (fde->dw_fde_second_begin)
19817             {
19818               if (dwarf_version >= 3 || !dwarf_strict)
19819                 {
19820                   /* We should use ranges for non-contiguous code section 
19821                      addresses.  Use the actual code range for the initial
19822                      section, since the HOT/COLD labels might precede an 
19823                      alignment offset.  */
19824                   bool range_list_added = false;
19825                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
19826                                         fde->dw_fde_end, &range_list_added);
19827                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
19828                                         fde->dw_fde_second_end,
19829                                         &range_list_added);
19830                   add_pubname (decl, subr_die);
19831                   if (range_list_added)
19832                     add_ranges (NULL);
19833                 }
19834               else
19835                 {
19836                   /* There is no real support in DW2 for this .. so we make
19837                      a work-around.  First, emit the pub name for the segment
19838                      containing the function label.  Then make and emit a
19839                      simplified subprogram DIE for the second segment with the
19840                      name pre-fixed by __hot/cold_sect_of_.  We use the same
19841                      linkage name for the second die so that gdb will find both
19842                      sections when given "b foo".  */
19843                   const char *name = NULL;
19844                   tree decl_name = DECL_NAME (decl);
19845                   dw_die_ref seg_die;
19846
19847                   /* Do the 'primary' section.   */
19848                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
19849                                  fde->dw_fde_begin);
19850                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
19851                                  fde->dw_fde_end);
19852                   /* Add it.   */
19853                   add_pubname (decl, subr_die);
19854
19855                   /* Build a minimal DIE for the secondary section.  */
19856                   seg_die = new_die (DW_TAG_subprogram,
19857                                      subr_die->die_parent, decl);
19858
19859                   if (TREE_PUBLIC (decl))
19860                     add_AT_flag (seg_die, DW_AT_external, 1);
19861
19862                   if (decl_name != NULL 
19863                       && IDENTIFIER_POINTER (decl_name) != NULL)
19864                     {
19865                       name = dwarf2_name (decl, 1);
19866                       if (! DECL_ARTIFICIAL (decl))
19867                         add_src_coords_attributes (seg_die, decl);
19868
19869                       add_linkage_name (seg_die, decl);
19870                     }
19871                   gcc_assert (name != NULL);
19872                   add_pure_or_virtual_attribute (seg_die, decl);
19873                   if (DECL_ARTIFICIAL (decl))
19874                     add_AT_flag (seg_die, DW_AT_artificial, 1);
19875
19876                   name = concat ("__second_sect_of_", name, NULL); 
19877                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
19878                                  fde->dw_fde_second_begin);
19879                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
19880                                  fde->dw_fde_second_end);
19881                   add_name_attribute (seg_die, name);
19882                   add_pubname_string (name, seg_die);
19883                 }
19884             }
19885           else
19886             {
19887               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
19888               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
19889               add_pubname (decl, subr_die);
19890             }
19891         }
19892
19893 #ifdef MIPS_DEBUGGING_INFO
19894       /* Add a reference to the FDE for this routine.  */
19895       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
19896 #endif
19897
19898       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
19899
19900       /* We define the "frame base" as the function's CFA.  This is more
19901          convenient for several reasons: (1) It's stable across the prologue
19902          and epilogue, which makes it better than just a frame pointer,
19903          (2) With dwarf3, there exists a one-byte encoding that allows us
19904          to reference the .debug_frame data by proxy, but failing that,
19905          (3) We can at least reuse the code inspection and interpretation
19906          code that determines the CFA position at various points in the
19907          function.  */
19908       if (dwarf_version >= 3)
19909         {
19910           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
19911           add_AT_loc (subr_die, DW_AT_frame_base, op);
19912         }
19913       else
19914         {
19915           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
19916           if (list->dw_loc_next)
19917             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
19918           else
19919             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
19920         }
19921
19922       /* Compute a displacement from the "steady-state frame pointer" to
19923          the CFA.  The former is what all stack slots and argument slots
19924          will reference in the rtl; the later is what we've told the
19925          debugger about.  We'll need to adjust all frame_base references
19926          by this displacement.  */
19927       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
19928
19929       if (cfun->static_chain_decl)
19930         add_AT_location_description (subr_die, DW_AT_static_link,
19931                  loc_list_from_tree (cfun->static_chain_decl, 2));
19932     }
19933
19934   /* Generate child dies for template paramaters.  */
19935   if (debug_info_level > DINFO_LEVEL_TERSE)
19936     gen_generic_params_dies (decl);
19937
19938   /* Now output descriptions of the arguments for this function. This gets
19939      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
19940      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
19941      `...' at the end of the formal parameter list.  In order to find out if
19942      there was a trailing ellipsis or not, we must instead look at the type
19943      associated with the FUNCTION_DECL.  This will be a node of type
19944      FUNCTION_TYPE. If the chain of type nodes hanging off of this
19945      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
19946      an ellipsis at the end.  */
19947
19948   /* In the case where we are describing a mere function declaration, all we
19949      need to do here (and all we *can* do here) is to describe the *types* of
19950      its formal parameters.  */
19951   if (debug_info_level <= DINFO_LEVEL_TERSE)
19952     ;
19953   else if (declaration)
19954     gen_formal_types_die (decl, subr_die);
19955   else
19956     {
19957       /* Generate DIEs to represent all known formal parameters.  */
19958       tree parm = DECL_ARGUMENTS (decl);
19959       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
19960       tree generic_decl_parm = generic_decl
19961                                 ? DECL_ARGUMENTS (generic_decl)
19962                                 : NULL;
19963
19964       /* Now we want to walk the list of parameters of the function and
19965          emit their relevant DIEs.
19966
19967          We consider the case of DECL being an instance of a generic function
19968          as well as it being a normal function.
19969
19970          If DECL is an instance of a generic function we walk the
19971          parameters of the generic function declaration _and_ the parameters of
19972          DECL itself. This is useful because we want to emit specific DIEs for
19973          function parameter packs and those are declared as part of the
19974          generic function declaration. In that particular case,
19975          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
19976          That DIE has children DIEs representing the set of arguments
19977          of the pack. Note that the set of pack arguments can be empty.
19978          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
19979          children DIE.
19980
19981          Otherwise, we just consider the parameters of DECL.  */
19982       while (generic_decl_parm || parm)
19983         {
19984           if (generic_decl_parm
19985               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
19986             gen_formal_parameter_pack_die (generic_decl_parm,
19987                                            parm, subr_die,
19988                                            &parm);
19989           else if (parm)
19990             {
19991               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
19992
19993               if (parm == DECL_ARGUMENTS (decl)
19994                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
19995                   && parm_die
19996                   && (dwarf_version >= 3 || !dwarf_strict))
19997                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
19998
19999               parm = DECL_CHAIN (parm);
20000             }
20001
20002           if (generic_decl_parm)
20003             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
20004         }
20005
20006       /* Decide whether we need an unspecified_parameters DIE at the end.
20007          There are 2 more cases to do this for: 1) the ansi ... declaration -
20008          this is detectable when the end of the arg list is not a
20009          void_type_node 2) an unprototyped function declaration (not a
20010          definition).  This just means that we have no info about the
20011          parameters at all.  */
20012       if (prototype_p (TREE_TYPE (decl)))
20013         {
20014           /* This is the prototyped case, check for....  */
20015           if (stdarg_p (TREE_TYPE (decl)))
20016             gen_unspecified_parameters_die (decl, subr_die);
20017         }
20018       else if (DECL_INITIAL (decl) == NULL_TREE)
20019         gen_unspecified_parameters_die (decl, subr_die);
20020     }
20021
20022   /* Output Dwarf info for all of the stuff within the body of the function
20023      (if it has one - it may be just a declaration).  */
20024   outer_scope = DECL_INITIAL (decl);
20025
20026   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
20027      a function.  This BLOCK actually represents the outermost binding contour
20028      for the function, i.e. the contour in which the function's formal
20029      parameters and labels get declared. Curiously, it appears that the front
20030      end doesn't actually put the PARM_DECL nodes for the current function onto
20031      the BLOCK_VARS list for this outer scope, but are strung off of the
20032      DECL_ARGUMENTS list for the function instead.
20033
20034      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
20035      the LABEL_DECL nodes for the function however, and we output DWARF info
20036      for those in decls_for_scope.  Just within the `outer_scope' there will be
20037      a BLOCK node representing the function's outermost pair of curly braces,
20038      and any blocks used for the base and member initializers of a C++
20039      constructor function.  */
20040   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
20041     {
20042       int call_site_note_count = 0;
20043       int tail_call_site_note_count = 0;
20044
20045       /* Emit a DW_TAG_variable DIE for a named return value.  */
20046       if (DECL_NAME (DECL_RESULT (decl)))
20047         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
20048
20049       current_function_has_inlines = 0;
20050       decls_for_scope (outer_scope, subr_die, 0);
20051
20052       if (call_arg_locations && !dwarf_strict)
20053         {
20054           struct call_arg_loc_node *ca_loc;
20055           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
20056             {
20057               dw_die_ref die = NULL;
20058               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
20059               rtx arg, next_arg;
20060
20061               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
20062                    arg; arg = next_arg)
20063                 {
20064                   dw_loc_descr_ref reg, val;
20065                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
20066                   dw_die_ref cdie;
20067
20068                   next_arg = XEXP (arg, 1);
20069                   if (REG_P (XEXP (XEXP (arg, 0), 0))
20070                       && next_arg
20071                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
20072                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
20073                       && REGNO (XEXP (XEXP (arg, 0), 0))
20074                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
20075                     next_arg = XEXP (next_arg, 1);
20076                   if (mode == VOIDmode)
20077                     {
20078                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
20079                       if (mode == VOIDmode)
20080                         mode = GET_MODE (XEXP (arg, 0));
20081                     }
20082                   if (mode == VOIDmode || mode == BLKmode)
20083                     continue;
20084                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
20085                     {
20086                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20087                       tloc = XEXP (XEXP (arg, 0), 1);
20088                       continue;
20089                     }
20090                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
20091                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
20092                     {
20093                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
20094                       tlocc = XEXP (XEXP (arg, 0), 1);
20095                       continue;
20096                     }
20097                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
20098                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
20099                                               VAR_INIT_STATUS_INITIALIZED);
20100                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
20101                     {
20102                       rtx mem = XEXP (XEXP (arg, 0), 0);
20103                       reg = mem_loc_descriptor (XEXP (mem, 0),
20104                                                 get_address_mode (mem),
20105                                                 GET_MODE (mem),
20106                                                 VAR_INIT_STATUS_INITIALIZED);
20107                     }
20108                   else
20109                     continue;
20110                   if (reg == NULL)
20111                     continue;
20112                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
20113                                             VOIDmode,
20114                                             VAR_INIT_STATUS_INITIALIZED);
20115                   if (val == NULL)
20116                     continue;
20117                   if (die == NULL)
20118                     die = gen_call_site_die (decl, subr_die, ca_loc);
20119                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
20120                                   NULL_TREE);           
20121                   add_AT_loc (cdie, DW_AT_location, reg);
20122                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
20123                   if (next_arg != XEXP (arg, 1))
20124                     {
20125                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
20126                       if (mode == VOIDmode)
20127                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
20128                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
20129                                                             0), 1),
20130                                                 mode, VOIDmode,
20131                                                 VAR_INIT_STATUS_INITIALIZED);
20132                       if (val != NULL)
20133                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
20134                     }
20135                 }
20136               if (die == NULL
20137                   && (ca_loc->symbol_ref || tloc))
20138                 die = gen_call_site_die (decl, subr_die, ca_loc);
20139               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
20140                 {
20141                   dw_loc_descr_ref tval = NULL;
20142
20143                   if (tloc != NULL_RTX)
20144                     tval = mem_loc_descriptor (tloc,
20145                                                GET_MODE (tloc) == VOIDmode
20146                                                ? Pmode : GET_MODE (tloc),
20147                                                VOIDmode,
20148                                                VAR_INIT_STATUS_INITIALIZED);
20149                   if (tval)
20150                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
20151                   else if (tlocc != NULL_RTX)
20152                     {
20153                       tval = mem_loc_descriptor (tlocc,
20154                                                  GET_MODE (tlocc) == VOIDmode
20155                                                  ? Pmode : GET_MODE (tlocc),
20156                                                  VOIDmode,
20157                                                  VAR_INIT_STATUS_INITIALIZED);
20158                       if (tval)
20159                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
20160                                     tval);
20161                     }
20162                 }
20163               if (die != NULL)
20164                 {
20165                   call_site_note_count++;
20166                   if (ca_loc->tail_call_p)
20167                     tail_call_site_note_count++;
20168                 }
20169             }
20170         }
20171       call_arg_locations = NULL;
20172       call_arg_loc_last = NULL;
20173       if (tail_call_site_count >= 0
20174           && tail_call_site_count == tail_call_site_note_count
20175           && !dwarf_strict)
20176         {
20177           if (call_site_count >= 0
20178               && call_site_count == call_site_note_count)
20179             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
20180           else
20181             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
20182         }
20183       call_site_count = -1;
20184       tail_call_site_count = -1;
20185     }
20186   /* Add the calling convention attribute if requested.  */
20187   add_calling_convention_attribute (subr_die, decl);
20188
20189 }
20190
20191 /* Returns a hash value for X (which really is a die_struct).  */
20192
20193 static hashval_t
20194 common_block_die_table_hash (const void *x)
20195 {
20196   const_dw_die_ref d = (const_dw_die_ref) x;
20197   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
20198 }
20199
20200 /* Return nonzero if decl_id and die_parent of die_struct X is the same
20201    as decl_id and die_parent of die_struct Y.  */
20202
20203 static int
20204 common_block_die_table_eq (const void *x, const void *y)
20205 {
20206   const_dw_die_ref d = (const_dw_die_ref) x;
20207   const_dw_die_ref e = (const_dw_die_ref) y;
20208   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
20209 }
20210
20211 /* Generate a DIE to represent a declared data object.
20212    Either DECL or ORIGIN must be non-null.  */
20213
20214 static void
20215 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
20216 {
20217   HOST_WIDE_INT off;
20218   tree com_decl;
20219   tree decl_or_origin = decl ? decl : origin;
20220   tree ultimate_origin;
20221   dw_die_ref var_die;
20222   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
20223   dw_die_ref origin_die;
20224   bool declaration = (DECL_EXTERNAL (decl_or_origin)
20225                       || class_or_namespace_scope_p (context_die));
20226   bool specialization_p = false;
20227
20228   ultimate_origin = decl_ultimate_origin (decl_or_origin);
20229   if (decl || ultimate_origin)
20230     origin = ultimate_origin;
20231   com_decl = fortran_common (decl_or_origin, &off);
20232
20233   /* Symbol in common gets emitted as a child of the common block, in the form
20234      of a data member.  */
20235   if (com_decl)
20236     {
20237       dw_die_ref com_die;
20238       dw_loc_list_ref loc;
20239       die_node com_die_arg;
20240
20241       var_die = lookup_decl_die (decl_or_origin);
20242       if (var_die)
20243         {
20244           if (get_AT (var_die, DW_AT_location) == NULL)
20245             {
20246               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
20247               if (loc)
20248                 {
20249                   if (off)
20250                     {
20251                       /* Optimize the common case.  */
20252                       if (single_element_loc_list_p (loc)
20253                           && loc->expr->dw_loc_opc == DW_OP_addr
20254                           && loc->expr->dw_loc_next == NULL
20255                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
20256                              == SYMBOL_REF)
20257                         loc->expr->dw_loc_oprnd1.v.val_addr
20258                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20259                         else
20260                           loc_list_plus_const (loc, off);
20261                     }
20262                   add_AT_location_description (var_die, DW_AT_location, loc);
20263                   remove_AT (var_die, DW_AT_declaration);
20264                 }
20265             }
20266           return;
20267         }
20268
20269       if (common_block_die_table == NULL)
20270         common_block_die_table
20271           = htab_create_ggc (10, common_block_die_table_hash,
20272                              common_block_die_table_eq, NULL);
20273
20274       com_die_arg.decl_id = DECL_UID (com_decl);
20275       com_die_arg.die_parent = context_die;
20276       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
20277       loc = loc_list_from_tree (com_decl, 2);
20278       if (com_die == NULL)
20279         {
20280           const char *cnam
20281             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
20282           void **slot;
20283
20284           com_die = new_die (DW_TAG_common_block, context_die, decl);
20285           add_name_and_src_coords_attributes (com_die, com_decl);
20286           if (loc)
20287             {
20288               add_AT_location_description (com_die, DW_AT_location, loc);
20289               /* Avoid sharing the same loc descriptor between
20290                  DW_TAG_common_block and DW_TAG_variable.  */
20291               loc = loc_list_from_tree (com_decl, 2);
20292             }
20293           else if (DECL_EXTERNAL (decl))
20294             add_AT_flag (com_die, DW_AT_declaration, 1);
20295           add_pubname_string (cnam, com_die); /* ??? needed? */
20296           com_die->decl_id = DECL_UID (com_decl);
20297           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
20298           *slot = (void *) com_die;
20299         }
20300       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
20301         {
20302           add_AT_location_description (com_die, DW_AT_location, loc);
20303           loc = loc_list_from_tree (com_decl, 2);
20304           remove_AT (com_die, DW_AT_declaration);
20305         }
20306       var_die = new_die (DW_TAG_variable, com_die, decl);
20307       add_name_and_src_coords_attributes (var_die, decl);
20308       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
20309                           TREE_THIS_VOLATILE (decl), context_die);
20310       add_AT_flag (var_die, DW_AT_external, 1);
20311       if (loc)
20312         {
20313           if (off)
20314             {
20315               /* Optimize the common case.  */
20316               if (single_element_loc_list_p (loc)
20317                   && loc->expr->dw_loc_opc == DW_OP_addr
20318                   && loc->expr->dw_loc_next == NULL
20319                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
20320                 loc->expr->dw_loc_oprnd1.v.val_addr
20321                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
20322               else
20323                 loc_list_plus_const (loc, off);
20324             }
20325           add_AT_location_description (var_die, DW_AT_location, loc);
20326         }
20327       else if (DECL_EXTERNAL (decl))
20328         add_AT_flag (var_die, DW_AT_declaration, 1);
20329       equate_decl_number_to_die (decl, var_die);
20330       return;
20331     }
20332
20333   /* If the compiler emitted a definition for the DECL declaration
20334      and if we already emitted a DIE for it, don't emit a second
20335      DIE for it again. Allow re-declarations of DECLs that are
20336      inside functions, though.  */
20337   if (old_die && declaration && !local_scope_p (context_die))
20338     return;
20339
20340   /* For static data members, the declaration in the class is supposed
20341      to have DW_TAG_member tag; the specification should still be
20342      DW_TAG_variable referencing the DW_TAG_member DIE.  */
20343   if (declaration && class_scope_p (context_die))
20344     var_die = new_die (DW_TAG_member, context_die, decl);
20345   else
20346     var_die = new_die (DW_TAG_variable, context_die, decl);
20347
20348   origin_die = NULL;
20349   if (origin != NULL)
20350     origin_die = add_abstract_origin_attribute (var_die, origin);
20351
20352   /* Loop unrolling can create multiple blocks that refer to the same
20353      static variable, so we must test for the DW_AT_declaration flag.
20354
20355      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
20356      copy decls and set the DECL_ABSTRACT flag on them instead of
20357      sharing them.
20358
20359      ??? Duplicated blocks have been rewritten to use .debug_ranges.
20360
20361      ??? The declare_in_namespace support causes us to get two DIEs for one
20362      variable, both of which are declarations.  We want to avoid considering
20363      one to be a specification, so we must test that this DIE is not a
20364      declaration.  */
20365   else if (old_die && TREE_STATIC (decl) && ! declaration
20366            && get_AT_flag (old_die, DW_AT_declaration) == 1)
20367     {
20368       /* This is a definition of a C++ class level static.  */
20369       add_AT_specification (var_die, old_die);
20370       specialization_p = true;
20371       if (DECL_NAME (decl))
20372         {
20373           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
20374           struct dwarf_file_data * file_index = lookup_filename (s.file);
20375
20376           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
20377             add_AT_file (var_die, DW_AT_decl_file, file_index);
20378
20379           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
20380             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
20381
20382           if (old_die->die_tag == DW_TAG_member)
20383             add_linkage_name (var_die, decl);
20384         }
20385     }
20386   else
20387     add_name_and_src_coords_attributes (var_die, decl);
20388
20389   if ((origin == NULL && !specialization_p)
20390       || (origin != NULL
20391           && !DECL_ABSTRACT (decl_or_origin)
20392           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
20393                                        decl_function_context
20394                                                         (decl_or_origin))))
20395     {
20396       tree type = TREE_TYPE (decl_or_origin);
20397
20398       if (decl_by_reference_p (decl_or_origin))
20399         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
20400       else
20401         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
20402                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
20403     }
20404
20405   if (origin == NULL && !specialization_p)
20406     {
20407       if (TREE_PUBLIC (decl))
20408         add_AT_flag (var_die, DW_AT_external, 1);
20409
20410       if (DECL_ARTIFICIAL (decl))
20411         add_AT_flag (var_die, DW_AT_artificial, 1);
20412
20413       add_accessibility_attribute (var_die, decl);
20414     }
20415
20416   if (declaration)
20417     add_AT_flag (var_die, DW_AT_declaration, 1);
20418
20419   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
20420     equate_decl_number_to_die (decl, var_die);
20421
20422   if (! declaration
20423       && (! DECL_ABSTRACT (decl_or_origin)
20424           /* Local static vars are shared between all clones/inlines,
20425              so emit DW_AT_location on the abstract DIE if DECL_RTL is
20426              already set.  */
20427           || (TREE_CODE (decl_or_origin) == VAR_DECL
20428               && TREE_STATIC (decl_or_origin)
20429               && DECL_RTL_SET_P (decl_or_origin)))
20430       /* When abstract origin already has DW_AT_location attribute, no need
20431          to add it again.  */
20432       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
20433     {
20434       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
20435           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
20436         defer_location (decl_or_origin, var_die);
20437       else
20438         add_location_or_const_value_attribute (var_die, decl_or_origin,
20439                                                decl == NULL, DW_AT_location);
20440       add_pubname (decl_or_origin, var_die);
20441     }
20442   else
20443     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
20444 }
20445
20446 /* Generate a DIE to represent a named constant.  */
20447
20448 static void
20449 gen_const_die (tree decl, dw_die_ref context_die)
20450 {
20451   dw_die_ref const_die;
20452   tree type = TREE_TYPE (decl);
20453
20454   const_die = new_die (DW_TAG_constant, context_die, decl);
20455   add_name_and_src_coords_attributes (const_die, decl);
20456   add_type_attribute (const_die, type, 1, 0, context_die);
20457   if (TREE_PUBLIC (decl))
20458     add_AT_flag (const_die, DW_AT_external, 1);
20459   if (DECL_ARTIFICIAL (decl))
20460     add_AT_flag (const_die, DW_AT_artificial, 1);
20461   tree_add_const_value_attribute_for_decl (const_die, decl);
20462 }
20463
20464 /* Generate a DIE to represent a label identifier.  */
20465
20466 static void
20467 gen_label_die (tree decl, dw_die_ref context_die)
20468 {
20469   tree origin = decl_ultimate_origin (decl);
20470   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
20471   rtx insn;
20472   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20473
20474   if (origin != NULL)
20475     add_abstract_origin_attribute (lbl_die, origin);
20476   else
20477     add_name_and_src_coords_attributes (lbl_die, decl);
20478
20479   if (DECL_ABSTRACT (decl))
20480     equate_decl_number_to_die (decl, lbl_die);
20481   else
20482     {
20483       insn = DECL_RTL_IF_SET (decl);
20484
20485       /* Deleted labels are programmer specified labels which have been
20486          eliminated because of various optimizations.  We still emit them
20487          here so that it is possible to put breakpoints on them.  */
20488       if (insn
20489           && (LABEL_P (insn)
20490               || ((NOTE_P (insn)
20491                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
20492         {
20493           /* When optimization is enabled (via -O) some parts of the compiler
20494              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
20495              represent source-level labels which were explicitly declared by
20496              the user.  This really shouldn't be happening though, so catch
20497              it if it ever does happen.  */
20498           gcc_assert (!INSN_DELETED_P (insn));
20499
20500           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
20501           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
20502         }
20503     }
20504 }
20505
20506 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
20507    attributes to the DIE for a block STMT, to describe where the inlined
20508    function was called from.  This is similar to add_src_coords_attributes.  */
20509
20510 static inline void
20511 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
20512 {
20513   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
20514
20515   if (dwarf_version >= 3 || !dwarf_strict)
20516     {
20517       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
20518       add_AT_unsigned (die, DW_AT_call_line, s.line);
20519     }
20520 }
20521
20522
20523 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
20524    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
20525
20526 static inline void
20527 add_high_low_attributes (tree stmt, dw_die_ref die)
20528 {
20529   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20530
20531   if (BLOCK_FRAGMENT_CHAIN (stmt)
20532       && (dwarf_version >= 3 || !dwarf_strict))
20533     {
20534       tree chain;
20535
20536       if (inlined_function_outer_scope_p (stmt))
20537         {
20538           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20539                                        BLOCK_NUMBER (stmt));
20540           add_AT_lbl_id (die, DW_AT_entry_pc, label);
20541         }
20542
20543       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
20544
20545       chain = BLOCK_FRAGMENT_CHAIN (stmt);
20546       do
20547         {
20548           add_ranges (chain);
20549           chain = BLOCK_FRAGMENT_CHAIN (chain);
20550         }
20551       while (chain);
20552       add_ranges (NULL);
20553     }
20554   else
20555     {
20556       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
20557                                    BLOCK_NUMBER (stmt));
20558       add_AT_lbl_id (die, DW_AT_low_pc, label);
20559       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
20560                                    BLOCK_NUMBER (stmt));
20561       add_AT_lbl_id (die, DW_AT_high_pc, label);
20562     }
20563 }
20564
20565 /* Generate a DIE for a lexical block.  */
20566
20567 static void
20568 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
20569 {
20570   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
20571
20572   if (call_arg_locations)
20573     {
20574       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20575         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20576                                BLOCK_NUMBER (stmt) + 1);
20577       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
20578     }
20579
20580   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
20581     add_high_low_attributes (stmt, stmt_die);
20582
20583   decls_for_scope (stmt, stmt_die, depth);
20584 }
20585
20586 /* Generate a DIE for an inlined subprogram.  */
20587
20588 static void
20589 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
20590 {
20591   tree decl;
20592
20593   /* The instance of function that is effectively being inlined shall not
20594      be abstract.  */
20595   gcc_assert (! BLOCK_ABSTRACT (stmt));
20596
20597   decl = block_ultimate_origin (stmt);
20598
20599   /* Emit info for the abstract instance first, if we haven't yet.  We
20600      must emit this even if the block is abstract, otherwise when we
20601      emit the block below (or elsewhere), we may end up trying to emit
20602      a die whose origin die hasn't been emitted, and crashing.  */
20603   dwarf2out_abstract_function (decl);
20604
20605   if (! BLOCK_ABSTRACT (stmt))
20606     {
20607       dw_die_ref subr_die
20608         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
20609
20610       if (call_arg_locations)
20611         {
20612           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
20613             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
20614                                    BLOCK_NUMBER (stmt) + 1);
20615           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
20616         }
20617       add_abstract_origin_attribute (subr_die, decl);
20618       if (TREE_ASM_WRITTEN (stmt))
20619         add_high_low_attributes (stmt, subr_die);
20620       add_call_src_coords_attributes (stmt, subr_die);
20621
20622       decls_for_scope (stmt, subr_die, depth);
20623       current_function_has_inlines = 1;
20624     }
20625 }
20626
20627 /* Generate a DIE for a field in a record, or structure.  */
20628
20629 static void
20630 gen_field_die (tree decl, dw_die_ref context_die)
20631 {
20632   dw_die_ref decl_die;
20633
20634   if (TREE_TYPE (decl) == error_mark_node)
20635     return;
20636
20637   decl_die = new_die (DW_TAG_member, context_die, decl);
20638   add_name_and_src_coords_attributes (decl_die, decl);
20639   add_type_attribute (decl_die, member_declared_type (decl),
20640                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
20641                       context_die);
20642
20643   if (DECL_BIT_FIELD_TYPE (decl))
20644     {
20645       add_byte_size_attribute (decl_die, decl);
20646       add_bit_size_attribute (decl_die, decl);
20647       add_bit_offset_attribute (decl_die, decl);
20648     }
20649
20650   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
20651     add_data_member_location_attribute (decl_die, decl);
20652
20653   if (DECL_ARTIFICIAL (decl))
20654     add_AT_flag (decl_die, DW_AT_artificial, 1);
20655
20656   add_accessibility_attribute (decl_die, decl);
20657
20658   /* Equate decl number to die, so that we can look up this decl later on.  */
20659   equate_decl_number_to_die (decl, decl_die);
20660 }
20661
20662 #if 0
20663 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20664    Use modified_type_die instead.
20665    We keep this code here just in case these types of DIEs may be needed to
20666    represent certain things in other languages (e.g. Pascal) someday.  */
20667
20668 static void
20669 gen_pointer_type_die (tree type, dw_die_ref context_die)
20670 {
20671   dw_die_ref ptr_die
20672     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
20673
20674   equate_type_number_to_die (type, ptr_die);
20675   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20676   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20677 }
20678
20679 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
20680    Use modified_type_die instead.
20681    We keep this code here just in case these types of DIEs may be needed to
20682    represent certain things in other languages (e.g. Pascal) someday.  */
20683
20684 static void
20685 gen_reference_type_die (tree type, dw_die_ref context_die)
20686 {
20687   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
20688
20689   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
20690     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
20691   else
20692     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
20693
20694   equate_type_number_to_die (type, ref_die);
20695   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
20696   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
20697 }
20698 #endif
20699
20700 /* Generate a DIE for a pointer to a member type.  */
20701
20702 static void
20703 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
20704 {
20705   dw_die_ref ptr_die
20706     = new_die (DW_TAG_ptr_to_member_type,
20707                scope_die_for (type, context_die), type);
20708
20709   equate_type_number_to_die (type, ptr_die);
20710   add_AT_die_ref (ptr_die, DW_AT_containing_type,
20711                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
20712   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
20713 }
20714
20715 /* Generate the DIE for the compilation unit.  */
20716
20717 static dw_die_ref
20718 gen_compile_unit_die (const char *filename)
20719 {
20720   dw_die_ref die;
20721   char producer[250];
20722   const char *language_string = lang_hooks.name;
20723   int language;
20724
20725   die = new_die (DW_TAG_compile_unit, NULL, NULL);
20726
20727   if (filename)
20728     {
20729       add_name_attribute (die, filename);
20730       /* Don't add cwd for <built-in>.  */
20731       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
20732         add_comp_dir_attribute (die);
20733     }
20734
20735   sprintf (producer, "%s %s", language_string, version_string);
20736
20737 #ifdef MIPS_DEBUGGING_INFO
20738   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
20739      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
20740      not appear in the producer string, the debugger reaches the conclusion
20741      that the object file is stripped and has no debugging information.
20742      To get the MIPS/SGI debugger to believe that there is debugging
20743      information in the object file, we add a -g to the producer string.  */
20744   if (debug_info_level > DINFO_LEVEL_TERSE)
20745     strcat (producer, " -g");
20746 #endif
20747
20748   add_AT_string (die, DW_AT_producer, producer);
20749
20750   /* If our producer is LTO try to figure out a common language to use
20751      from the global list of translation units.  */
20752   if (strcmp (language_string, "GNU GIMPLE") == 0)
20753     {
20754       unsigned i;
20755       tree t;
20756       const char *common_lang = NULL;
20757
20758       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
20759         {
20760           if (!TRANSLATION_UNIT_LANGUAGE (t))
20761             continue;
20762           if (!common_lang)
20763             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
20764           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
20765             ;
20766           else if (strncmp (common_lang, "GNU C", 5) == 0
20767                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
20768             /* Mixing C and C++ is ok, use C++ in that case.  */
20769             common_lang = "GNU C++";
20770           else
20771             {
20772               /* Fall back to C.  */
20773               common_lang = NULL;
20774               break;
20775             }
20776         }
20777
20778       if (common_lang)
20779         language_string = common_lang;
20780     }
20781
20782   language = DW_LANG_C89;
20783   if (strcmp (language_string, "GNU C++") == 0)
20784     language = DW_LANG_C_plus_plus;
20785   else if (strcmp (language_string, "GNU F77") == 0)
20786     language = DW_LANG_Fortran77;
20787   else if (strcmp (language_string, "GNU Pascal") == 0)
20788     language = DW_LANG_Pascal83;
20789   else if (dwarf_version >= 3 || !dwarf_strict)
20790     {
20791       if (strcmp (language_string, "GNU Ada") == 0)
20792         language = DW_LANG_Ada95;
20793       else if (strcmp (language_string, "GNU Fortran") == 0)
20794         language = DW_LANG_Fortran95;
20795       else if (strcmp (language_string, "GNU Java") == 0)
20796         language = DW_LANG_Java;
20797       else if (strcmp (language_string, "GNU Objective-C") == 0)
20798         language = DW_LANG_ObjC;
20799       else if (strcmp (language_string, "GNU Objective-C++") == 0)
20800         language = DW_LANG_ObjC_plus_plus;
20801     }
20802
20803   add_AT_unsigned (die, DW_AT_language, language);
20804
20805   switch (language)
20806     {
20807     case DW_LANG_Fortran77:
20808     case DW_LANG_Fortran90:
20809     case DW_LANG_Fortran95:
20810       /* Fortran has case insensitive identifiers and the front-end
20811          lowercases everything.  */
20812       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
20813       break;
20814     default:
20815       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
20816       break;
20817     }
20818   return die;
20819 }
20820
20821 /* Generate the DIE for a base class.  */
20822
20823 static void
20824 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
20825 {
20826   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
20827
20828   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
20829   add_data_member_location_attribute (die, binfo);
20830
20831   if (BINFO_VIRTUAL_P (binfo))
20832     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
20833
20834   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
20835      children, otherwise the default is DW_ACCESS_public.  In DWARF2
20836      the default has always been DW_ACCESS_private.  */
20837   if (access == access_public_node)
20838     {
20839       if (dwarf_version == 2
20840           || context_die->die_tag == DW_TAG_class_type)
20841       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
20842     }
20843   else if (access == access_protected_node)
20844     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
20845   else if (dwarf_version > 2
20846            && context_die->die_tag != DW_TAG_class_type)
20847     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
20848 }
20849
20850 /* Generate a DIE for a class member.  */
20851
20852 static void
20853 gen_member_die (tree type, dw_die_ref context_die)
20854 {
20855   tree member;
20856   tree binfo = TYPE_BINFO (type);
20857   dw_die_ref child;
20858
20859   /* If this is not an incomplete type, output descriptions of each of its
20860      members. Note that as we output the DIEs necessary to represent the
20861      members of this record or union type, we will also be trying to output
20862      DIEs to represent the *types* of those members. However the `type'
20863      function (above) will specifically avoid generating type DIEs for member
20864      types *within* the list of member DIEs for this (containing) type except
20865      for those types (of members) which are explicitly marked as also being
20866      members of this (containing) type themselves.  The g++ front- end can
20867      force any given type to be treated as a member of some other (containing)
20868      type by setting the TYPE_CONTEXT of the given (member) type to point to
20869      the TREE node representing the appropriate (containing) type.  */
20870
20871   /* First output info about the base classes.  */
20872   if (binfo)
20873     {
20874       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
20875       int i;
20876       tree base;
20877
20878       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
20879         gen_inheritance_die (base,
20880                              (accesses ? VEC_index (tree, accesses, i)
20881                               : access_public_node), context_die);
20882     }
20883
20884   /* Now output info about the data members and type members.  */
20885   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
20886     {
20887       /* If we thought we were generating minimal debug info for TYPE
20888          and then changed our minds, some of the member declarations
20889          may have already been defined.  Don't define them again, but
20890          do put them in the right order.  */
20891
20892       child = lookup_decl_die (member);
20893       if (child)
20894         splice_child_die (context_die, child);
20895       else
20896         gen_decl_die (member, NULL, context_die);
20897     }
20898
20899   /* Now output info about the function members (if any).  */
20900   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
20901     {
20902       /* Don't include clones in the member list.  */
20903       if (DECL_ABSTRACT_ORIGIN (member))
20904         continue;
20905
20906       child = lookup_decl_die (member);
20907       if (child)
20908         splice_child_die (context_die, child);
20909       else
20910         gen_decl_die (member, NULL, context_die);
20911     }
20912 }
20913
20914 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
20915    is set, we pretend that the type was never defined, so we only get the
20916    member DIEs needed by later specification DIEs.  */
20917
20918 static void
20919 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
20920                                 enum debug_info_usage usage)
20921 {
20922   dw_die_ref type_die = lookup_type_die (type);
20923   dw_die_ref scope_die = 0;
20924   int nested = 0;
20925   int complete = (TYPE_SIZE (type)
20926                   && (! TYPE_STUB_DECL (type)
20927                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
20928   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
20929   complete = complete && should_emit_struct_debug (type, usage);
20930
20931   if (type_die && ! complete)
20932     return;
20933
20934   if (TYPE_CONTEXT (type) != NULL_TREE
20935       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
20936           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
20937     nested = 1;
20938
20939   scope_die = scope_die_for (type, context_die);
20940
20941   if (! type_die || (nested && is_cu_die (scope_die)))
20942     /* First occurrence of type or toplevel definition of nested class.  */
20943     {
20944       dw_die_ref old_die = type_die;
20945
20946       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
20947                           ? record_type_tag (type) : DW_TAG_union_type,
20948                           scope_die, type);
20949       equate_type_number_to_die (type, type_die);
20950       if (old_die)
20951         add_AT_specification (type_die, old_die);
20952       else
20953         {
20954           add_name_attribute (type_die, type_tag (type));
20955           add_gnat_descriptive_type_attribute (type_die, type, context_die);
20956         }
20957     }
20958   else
20959     remove_AT (type_die, DW_AT_declaration);
20960
20961   /* Generate child dies for template paramaters.  */
20962   if (debug_info_level > DINFO_LEVEL_TERSE
20963       && COMPLETE_TYPE_P (type))
20964     schedule_generic_params_dies_gen (type);
20965
20966   /* If this type has been completed, then give it a byte_size attribute and
20967      then give a list of members.  */
20968   if (complete && !ns_decl)
20969     {
20970       /* Prevent infinite recursion in cases where the type of some member of
20971          this type is expressed in terms of this type itself.  */
20972       TREE_ASM_WRITTEN (type) = 1;
20973       add_byte_size_attribute (type_die, type);
20974       if (TYPE_STUB_DECL (type) != NULL_TREE)
20975         {
20976           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
20977           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
20978         }
20979
20980       /* If the first reference to this type was as the return type of an
20981          inline function, then it may not have a parent.  Fix this now.  */
20982       if (type_die->die_parent == NULL)
20983         add_child_die (scope_die, type_die);
20984
20985       push_decl_scope (type);
20986       gen_member_die (type, type_die);
20987       pop_decl_scope ();
20988
20989       /* GNU extension: Record what type our vtable lives in.  */
20990       if (TYPE_VFIELD (type))
20991         {
20992           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
20993
20994           gen_type_die (vtype, context_die);
20995           add_AT_die_ref (type_die, DW_AT_containing_type,
20996                           lookup_type_die (vtype));
20997         }
20998     }
20999   else
21000     {
21001       add_AT_flag (type_die, DW_AT_declaration, 1);
21002
21003       /* We don't need to do this for function-local types.  */
21004       if (TYPE_STUB_DECL (type)
21005           && ! decl_function_context (TYPE_STUB_DECL (type)))
21006         VEC_safe_push (tree, gc, incomplete_types, type);
21007     }
21008
21009   if (get_AT (type_die, DW_AT_name))
21010     add_pubtype (type, type_die);
21011 }
21012
21013 /* Generate a DIE for a subroutine _type_.  */
21014
21015 static void
21016 gen_subroutine_type_die (tree type, dw_die_ref context_die)
21017 {
21018   tree return_type = TREE_TYPE (type);
21019   dw_die_ref subr_die
21020     = new_die (DW_TAG_subroutine_type,
21021                scope_die_for (type, context_die), type);
21022
21023   equate_type_number_to_die (type, subr_die);
21024   add_prototyped_attribute (subr_die, type);
21025   add_type_attribute (subr_die, return_type, 0, 0, context_die);
21026   gen_formal_types_die (type, subr_die);
21027
21028   if (get_AT (subr_die, DW_AT_name))
21029     add_pubtype (type, subr_die);
21030 }
21031
21032 /* Generate a DIE for a type definition.  */
21033
21034 static void
21035 gen_typedef_die (tree decl, dw_die_ref context_die)
21036 {
21037   dw_die_ref type_die;
21038   tree origin;
21039
21040   if (TREE_ASM_WRITTEN (decl))
21041     return;
21042
21043   TREE_ASM_WRITTEN (decl) = 1;
21044   type_die = new_die (DW_TAG_typedef, context_die, decl);
21045   origin = decl_ultimate_origin (decl);
21046   if (origin != NULL)
21047     add_abstract_origin_attribute (type_die, origin);
21048   else
21049     {
21050       tree type;
21051
21052       add_name_and_src_coords_attributes (type_die, decl);
21053       if (DECL_ORIGINAL_TYPE (decl))
21054         {
21055           type = DECL_ORIGINAL_TYPE (decl);
21056
21057           gcc_assert (type != TREE_TYPE (decl));
21058           equate_type_number_to_die (TREE_TYPE (decl), type_die);
21059         }
21060       else
21061         {
21062           type = TREE_TYPE (decl);
21063
21064           if (is_naming_typedef_decl (TYPE_NAME (type)))
21065             {
21066               /* Here, we are in the case of decl being a typedef naming
21067                  an anonymous type, e.g:
21068                      typedef struct {...} foo;
21069                  In that case TREE_TYPE (decl) is not a typedef variant
21070                  type and TYPE_NAME of the anonymous type is set to the
21071                  TYPE_DECL of the typedef. This construct is emitted by
21072                  the C++ FE.
21073
21074                  TYPE is the anonymous struct named by the typedef
21075                  DECL. As we need the DW_AT_type attribute of the
21076                  DW_TAG_typedef to point to the DIE of TYPE, let's
21077                  generate that DIE right away. add_type_attribute
21078                  called below will then pick (via lookup_type_die) that
21079                  anonymous struct DIE.  */
21080               if (!TREE_ASM_WRITTEN (type))
21081                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
21082
21083               /* This is a GNU Extension.  We are adding a
21084                  DW_AT_linkage_name attribute to the DIE of the
21085                  anonymous struct TYPE.  The value of that attribute
21086                  is the name of the typedef decl naming the anonymous
21087                  struct.  This greatly eases the work of consumers of
21088                  this debug info.  */
21089               add_linkage_attr (lookup_type_die (type), decl);
21090             }
21091         }
21092
21093       add_type_attribute (type_die, type, TREE_READONLY (decl),
21094                           TREE_THIS_VOLATILE (decl), context_die);
21095
21096       if (is_naming_typedef_decl (decl))
21097         /* We want that all subsequent calls to lookup_type_die with
21098            TYPE in argument yield the DW_TAG_typedef we have just
21099            created.  */
21100         equate_type_number_to_die (type, type_die);
21101
21102       add_accessibility_attribute (type_die, decl);
21103     }
21104
21105   if (DECL_ABSTRACT (decl))
21106     equate_decl_number_to_die (decl, type_die);
21107
21108   if (get_AT (type_die, DW_AT_name))
21109     add_pubtype (decl, type_die);
21110 }
21111
21112 /* Generate a DIE for a struct, class, enum or union type.  */
21113
21114 static void
21115 gen_tagged_type_die (tree type,
21116                      dw_die_ref context_die,
21117                      enum debug_info_usage usage)
21118 {
21119   int need_pop;
21120
21121   if (type == NULL_TREE
21122       || !is_tagged_type (type))
21123     return;
21124
21125   /* If this is a nested type whose containing class hasn't been written
21126      out yet, writing it out will cover this one, too.  This does not apply
21127      to instantiations of member class templates; they need to be added to
21128      the containing class as they are generated.  FIXME: This hurts the
21129      idea of combining type decls from multiple TUs, since we can't predict
21130      what set of template instantiations we'll get.  */
21131   if (TYPE_CONTEXT (type)
21132       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
21133       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
21134     {
21135       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
21136
21137       if (TREE_ASM_WRITTEN (type))
21138         return;
21139
21140       /* If that failed, attach ourselves to the stub.  */
21141       push_decl_scope (TYPE_CONTEXT (type));
21142       context_die = lookup_type_die (TYPE_CONTEXT (type));
21143       need_pop = 1;
21144     }
21145   else if (TYPE_CONTEXT (type) != NULL_TREE
21146            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
21147     {
21148       /* If this type is local to a function that hasn't been written
21149          out yet, use a NULL context for now; it will be fixed up in
21150          decls_for_scope.  */
21151       context_die = lookup_decl_die (TYPE_CONTEXT (type));
21152       /* A declaration DIE doesn't count; nested types need to go in the
21153          specification.  */
21154       if (context_die && is_declaration_die (context_die))
21155         context_die = NULL;
21156       need_pop = 0;
21157     }
21158   else
21159     {
21160       context_die = declare_in_namespace (type, context_die);
21161       need_pop = 0;
21162     }
21163
21164   if (TREE_CODE (type) == ENUMERAL_TYPE)
21165     {
21166       /* This might have been written out by the call to
21167          declare_in_namespace.  */
21168       if (!TREE_ASM_WRITTEN (type))
21169         gen_enumeration_type_die (type, context_die);
21170     }
21171   else
21172     gen_struct_or_union_type_die (type, context_die, usage);
21173
21174   if (need_pop)
21175     pop_decl_scope ();
21176
21177   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
21178      it up if it is ever completed.  gen_*_type_die will set it for us
21179      when appropriate.  */
21180 }
21181
21182 /* Generate a type description DIE.  */
21183
21184 static void
21185 gen_type_die_with_usage (tree type, dw_die_ref context_die,
21186                          enum debug_info_usage usage)
21187 {
21188   struct array_descr_info info;
21189
21190   if (type == NULL_TREE || type == error_mark_node)
21191     return;
21192
21193   if (TYPE_NAME (type) != NULL_TREE
21194       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
21195       && is_redundant_typedef (TYPE_NAME (type))
21196       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
21197     /* The DECL of this type is a typedef we don't want to emit debug
21198        info for but we want debug info for its underlying typedef.
21199        This can happen for e.g, the injected-class-name of a C++
21200        type.  */
21201     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
21202
21203   /* If TYPE is a typedef type variant, let's generate debug info
21204      for the parent typedef which TYPE is a type of.  */
21205   if (typedef_variant_p (type))
21206     {
21207       if (TREE_ASM_WRITTEN (type))
21208         return;
21209
21210       /* Prevent broken recursion; we can't hand off to the same type.  */
21211       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
21212
21213       /* Use the DIE of the containing namespace as the parent DIE of
21214          the type description DIE we want to generate.  */
21215       if (DECL_CONTEXT (TYPE_NAME (type))
21216           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21217         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21218
21219       TREE_ASM_WRITTEN (type) = 1;
21220
21221       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21222       return;
21223     }
21224
21225   /* If type is an anonymous tagged type named by a typedef, let's
21226      generate debug info for the typedef.  */
21227   if (is_naming_typedef_decl (TYPE_NAME (type)))
21228     {
21229       /* Use the DIE of the containing namespace as the parent DIE of
21230          the type description DIE we want to generate.  */
21231       if (DECL_CONTEXT (TYPE_NAME (type))
21232           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
21233         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
21234       
21235       gen_decl_die (TYPE_NAME (type), NULL, context_die);
21236       return;
21237     }
21238
21239   /* If this is an array type with hidden descriptor, handle it first.  */
21240   if (!TREE_ASM_WRITTEN (type)
21241       && lang_hooks.types.get_array_descr_info
21242       && lang_hooks.types.get_array_descr_info (type, &info)
21243       && (dwarf_version >= 3 || !dwarf_strict))
21244     {
21245       gen_descr_array_type_die (type, &info, context_die);
21246       TREE_ASM_WRITTEN (type) = 1;
21247       return;
21248     }
21249
21250   /* We are going to output a DIE to represent the unqualified version
21251      of this type (i.e. without any const or volatile qualifiers) so
21252      get the main variant (i.e. the unqualified version) of this type
21253      now.  (Vectors are special because the debugging info is in the
21254      cloned type itself).  */
21255   if (TREE_CODE (type) != VECTOR_TYPE)
21256     type = type_main_variant (type);
21257
21258   if (TREE_ASM_WRITTEN (type))
21259     return;
21260
21261   switch (TREE_CODE (type))
21262     {
21263     case ERROR_MARK:
21264       break;
21265
21266     case POINTER_TYPE:
21267     case REFERENCE_TYPE:
21268       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
21269          ensures that the gen_type_die recursion will terminate even if the
21270          type is recursive.  Recursive types are possible in Ada.  */
21271       /* ??? We could perhaps do this for all types before the switch
21272          statement.  */
21273       TREE_ASM_WRITTEN (type) = 1;
21274
21275       /* For these types, all that is required is that we output a DIE (or a
21276          set of DIEs) to represent the "basis" type.  */
21277       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21278                                 DINFO_USAGE_IND_USE);
21279       break;
21280
21281     case OFFSET_TYPE:
21282       /* This code is used for C++ pointer-to-data-member types.
21283          Output a description of the relevant class type.  */
21284       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
21285                                         DINFO_USAGE_IND_USE);
21286
21287       /* Output a description of the type of the object pointed to.  */
21288       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21289                                         DINFO_USAGE_IND_USE);
21290
21291       /* Now output a DIE to represent this pointer-to-data-member type
21292          itself.  */
21293       gen_ptr_to_mbr_type_die (type, context_die);
21294       break;
21295
21296     case FUNCTION_TYPE:
21297       /* Force out return type (in case it wasn't forced out already).  */
21298       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21299                                         DINFO_USAGE_DIR_USE);
21300       gen_subroutine_type_die (type, context_die);
21301       break;
21302
21303     case METHOD_TYPE:
21304       /* Force out return type (in case it wasn't forced out already).  */
21305       gen_type_die_with_usage (TREE_TYPE (type), context_die,
21306                                         DINFO_USAGE_DIR_USE);
21307       gen_subroutine_type_die (type, context_die);
21308       break;
21309
21310     case ARRAY_TYPE:
21311       gen_array_type_die (type, context_die);
21312       break;
21313
21314     case VECTOR_TYPE:
21315       gen_array_type_die (type, context_die);
21316       break;
21317
21318     case ENUMERAL_TYPE:
21319     case RECORD_TYPE:
21320     case UNION_TYPE:
21321     case QUAL_UNION_TYPE:
21322       gen_tagged_type_die (type, context_die, usage);
21323       return;
21324
21325     case VOID_TYPE:
21326     case INTEGER_TYPE:
21327     case REAL_TYPE:
21328     case FIXED_POINT_TYPE:
21329     case COMPLEX_TYPE:
21330     case BOOLEAN_TYPE:
21331       /* No DIEs needed for fundamental types.  */
21332       break;
21333
21334     case NULLPTR_TYPE:
21335     case LANG_TYPE:
21336       /* Just use DW_TAG_unspecified_type.  */
21337       {
21338         dw_die_ref type_die = lookup_type_die (type);
21339         if (type_die == NULL)
21340           {
21341             tree name = TYPE_NAME (type);
21342             if (TREE_CODE (name) == TYPE_DECL)
21343               name = DECL_NAME (name);
21344             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
21345             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
21346             equate_type_number_to_die (type, type_die);
21347           }
21348       }
21349       break;
21350
21351     default:
21352       gcc_unreachable ();
21353     }
21354
21355   TREE_ASM_WRITTEN (type) = 1;
21356 }
21357
21358 static void
21359 gen_type_die (tree type, dw_die_ref context_die)
21360 {
21361   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
21362 }
21363
21364 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
21365    things which are local to the given block.  */
21366
21367 static void
21368 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
21369 {
21370   int must_output_die = 0;
21371   bool inlined_func;
21372
21373   /* Ignore blocks that are NULL.  */
21374   if (stmt == NULL_TREE)
21375     return;
21376
21377   inlined_func = inlined_function_outer_scope_p (stmt);
21378
21379   /* If the block is one fragment of a non-contiguous block, do not
21380      process the variables, since they will have been done by the
21381      origin block.  Do process subblocks.  */
21382   if (BLOCK_FRAGMENT_ORIGIN (stmt))
21383     {
21384       tree sub;
21385
21386       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
21387         gen_block_die (sub, context_die, depth + 1);
21388
21389       return;
21390     }
21391
21392   /* Determine if we need to output any Dwarf DIEs at all to represent this
21393      block.  */
21394   if (inlined_func)
21395     /* The outer scopes for inlinings *must* always be represented.  We
21396        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
21397     must_output_die = 1;
21398   else
21399     {
21400       /* Determine if this block directly contains any "significant"
21401          local declarations which we will need to output DIEs for.  */
21402       if (debug_info_level > DINFO_LEVEL_TERSE)
21403         /* We are not in terse mode so *any* local declaration counts
21404            as being a "significant" one.  */
21405         must_output_die = ((BLOCK_VARS (stmt) != NULL
21406                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
21407                            && (TREE_USED (stmt)
21408                                || TREE_ASM_WRITTEN (stmt)
21409                                || BLOCK_ABSTRACT (stmt)));
21410       else if ((TREE_USED (stmt)
21411                 || TREE_ASM_WRITTEN (stmt)
21412                 || BLOCK_ABSTRACT (stmt))
21413                && !dwarf2out_ignore_block (stmt))
21414         must_output_die = 1;
21415     }
21416
21417   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
21418      DIE for any block which contains no significant local declarations at
21419      all.  Rather, in such cases we just call `decls_for_scope' so that any
21420      needed Dwarf info for any sub-blocks will get properly generated. Note
21421      that in terse mode, our definition of what constitutes a "significant"
21422      local declaration gets restricted to include only inlined function
21423      instances and local (nested) function definitions.  */
21424   if (must_output_die)
21425     {
21426       if (inlined_func)
21427         {
21428           /* If STMT block is abstract, that means we have been called
21429              indirectly from dwarf2out_abstract_function.
21430              That function rightfully marks the descendent blocks (of
21431              the abstract function it is dealing with) as being abstract,
21432              precisely to prevent us from emitting any
21433              DW_TAG_inlined_subroutine DIE as a descendent
21434              of an abstract function instance. So in that case, we should
21435              not call gen_inlined_subroutine_die.
21436
21437              Later though, when cgraph asks dwarf2out to emit info
21438              for the concrete instance of the function decl into which
21439              the concrete instance of STMT got inlined, the later will lead
21440              to the generation of a DW_TAG_inlined_subroutine DIE.  */
21441           if (! BLOCK_ABSTRACT (stmt))
21442             gen_inlined_subroutine_die (stmt, context_die, depth);
21443         }
21444       else
21445         gen_lexical_block_die (stmt, context_die, depth);
21446     }
21447   else
21448     decls_for_scope (stmt, context_die, depth);
21449 }
21450
21451 /* Process variable DECL (or variable with origin ORIGIN) within
21452    block STMT and add it to CONTEXT_DIE.  */
21453 static void
21454 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
21455 {
21456   dw_die_ref die;
21457   tree decl_or_origin = decl ? decl : origin;
21458
21459   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
21460     die = lookup_decl_die (decl_or_origin);
21461   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
21462            && TYPE_DECL_IS_STUB (decl_or_origin))
21463     die = lookup_type_die (TREE_TYPE (decl_or_origin));
21464   else
21465     die = NULL;
21466
21467   if (die != NULL && die->die_parent == NULL)
21468     add_child_die (context_die, die);
21469   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
21470     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
21471                                          stmt, context_die);
21472   else
21473     gen_decl_die (decl, origin, context_die);
21474 }
21475
21476 /* Generate all of the decls declared within a given scope and (recursively)
21477    all of its sub-blocks.  */
21478
21479 static void
21480 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
21481 {
21482   tree decl;
21483   unsigned int i;
21484   tree subblocks;
21485
21486   /* Ignore NULL blocks.  */
21487   if (stmt == NULL_TREE)
21488     return;
21489
21490   /* Output the DIEs to represent all of the data objects and typedefs
21491      declared directly within this block but not within any nested
21492      sub-blocks.  Also, nested function and tag DIEs have been
21493      generated with a parent of NULL; fix that up now.  */
21494   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
21495     process_scope_var (stmt, decl, NULL_TREE, context_die);
21496   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
21497     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
21498                        context_die);
21499
21500   /* If we're at -g1, we're not interested in subblocks.  */
21501   if (debug_info_level <= DINFO_LEVEL_TERSE)
21502     return;
21503
21504   /* Output the DIEs to represent all sub-blocks (and the items declared
21505      therein) of this block.  */
21506   for (subblocks = BLOCK_SUBBLOCKS (stmt);
21507        subblocks != NULL;
21508        subblocks = BLOCK_CHAIN (subblocks))
21509     gen_block_die (subblocks, context_die, depth + 1);
21510 }
21511
21512 /* Is this a typedef we can avoid emitting?  */
21513
21514 static inline int
21515 is_redundant_typedef (const_tree decl)
21516 {
21517   if (TYPE_DECL_IS_STUB (decl))
21518     return 1;
21519
21520   if (DECL_ARTIFICIAL (decl)
21521       && DECL_CONTEXT (decl)
21522       && is_tagged_type (DECL_CONTEXT (decl))
21523       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
21524       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
21525     /* Also ignore the artificial member typedef for the class name.  */
21526     return 1;
21527
21528   return 0;
21529 }
21530
21531 /* Return TRUE if TYPE is a typedef that names a type for linkage
21532    purposes. This kind of typedefs is produced by the C++ FE for
21533    constructs like:
21534
21535    typedef struct {...} foo;
21536
21537    In that case, there is no typedef variant type produced for foo.
21538    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
21539    struct type.  */
21540
21541 static bool
21542 is_naming_typedef_decl (const_tree decl)
21543 {
21544   if (decl == NULL_TREE
21545       || TREE_CODE (decl) != TYPE_DECL
21546       || !is_tagged_type (TREE_TYPE (decl))
21547       || DECL_IS_BUILTIN (decl)
21548       || is_redundant_typedef (decl)
21549       /* It looks like Ada produces TYPE_DECLs that are very similar
21550          to C++ naming typedefs but that have different
21551          semantics. Let's be specific to c++ for now.  */
21552       || !is_cxx ())
21553     return FALSE;
21554
21555   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
21556           && TYPE_NAME (TREE_TYPE (decl)) == decl
21557           && (TYPE_STUB_DECL (TREE_TYPE (decl))
21558               != TYPE_NAME (TREE_TYPE (decl))));
21559 }
21560
21561 /* Returns the DIE for a context.  */
21562
21563 static inline dw_die_ref
21564 get_context_die (tree context)
21565 {
21566   if (context)
21567     {
21568       /* Find die that represents this context.  */
21569       if (TYPE_P (context))
21570         {
21571           context = TYPE_MAIN_VARIANT (context);
21572           return strip_naming_typedef (context, force_type_die (context));
21573         }
21574       else
21575         return force_decl_die (context);
21576     }
21577   return comp_unit_die ();
21578 }
21579
21580 /* Returns the DIE for decl.  A DIE will always be returned.  */
21581
21582 static dw_die_ref
21583 force_decl_die (tree decl)
21584 {
21585   dw_die_ref decl_die;
21586   unsigned saved_external_flag;
21587   tree save_fn = NULL_TREE;
21588   decl_die = lookup_decl_die (decl);
21589   if (!decl_die)
21590     {
21591       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
21592
21593       decl_die = lookup_decl_die (decl);
21594       if (decl_die)
21595         return decl_die;
21596
21597       switch (TREE_CODE (decl))
21598         {
21599         case FUNCTION_DECL:
21600           /* Clear current_function_decl, so that gen_subprogram_die thinks
21601              that this is a declaration. At this point, we just want to force
21602              declaration die.  */
21603           save_fn = current_function_decl;
21604           current_function_decl = NULL_TREE;
21605           gen_subprogram_die (decl, context_die);
21606           current_function_decl = save_fn;
21607           break;
21608
21609         case VAR_DECL:
21610           /* Set external flag to force declaration die. Restore it after
21611            gen_decl_die() call.  */
21612           saved_external_flag = DECL_EXTERNAL (decl);
21613           DECL_EXTERNAL (decl) = 1;
21614           gen_decl_die (decl, NULL, context_die);
21615           DECL_EXTERNAL (decl) = saved_external_flag;
21616           break;
21617
21618         case NAMESPACE_DECL:
21619           if (dwarf_version >= 3 || !dwarf_strict)
21620             dwarf2out_decl (decl);
21621           else
21622             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
21623             decl_die = comp_unit_die ();
21624           break;
21625
21626         case TRANSLATION_UNIT_DECL:
21627           decl_die = comp_unit_die ();
21628           break;
21629
21630         default:
21631           gcc_unreachable ();
21632         }
21633
21634       /* We should be able to find the DIE now.  */
21635       if (!decl_die)
21636         decl_die = lookup_decl_die (decl);
21637       gcc_assert (decl_die);
21638     }
21639
21640   return decl_die;
21641 }
21642
21643 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
21644    always returned.  */
21645
21646 static dw_die_ref
21647 force_type_die (tree type)
21648 {
21649   dw_die_ref type_die;
21650
21651   type_die = lookup_type_die (type);
21652   if (!type_die)
21653     {
21654       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
21655
21656       type_die = modified_type_die (type, TYPE_READONLY (type),
21657                                     TYPE_VOLATILE (type), context_die);
21658       gcc_assert (type_die);
21659     }
21660   return type_die;
21661 }
21662
21663 /* Force out any required namespaces to be able to output DECL,
21664    and return the new context_die for it, if it's changed.  */
21665
21666 static dw_die_ref
21667 setup_namespace_context (tree thing, dw_die_ref context_die)
21668 {
21669   tree context = (DECL_P (thing)
21670                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
21671   if (context && TREE_CODE (context) == NAMESPACE_DECL)
21672     /* Force out the namespace.  */
21673     context_die = force_decl_die (context);
21674
21675   return context_die;
21676 }
21677
21678 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
21679    type) within its namespace, if appropriate.
21680
21681    For compatibility with older debuggers, namespace DIEs only contain
21682    declarations; all definitions are emitted at CU scope.  */
21683
21684 static dw_die_ref
21685 declare_in_namespace (tree thing, dw_die_ref context_die)
21686 {
21687   dw_die_ref ns_context;
21688
21689   if (debug_info_level <= DINFO_LEVEL_TERSE)
21690     return context_die;
21691
21692   /* If this decl is from an inlined function, then don't try to emit it in its
21693      namespace, as we will get confused.  It would have already been emitted
21694      when the abstract instance of the inline function was emitted anyways.  */
21695   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
21696     return context_die;
21697
21698   ns_context = setup_namespace_context (thing, context_die);
21699
21700   if (ns_context != context_die)
21701     {
21702       if (is_fortran ())
21703         return ns_context;
21704       if (DECL_P (thing))
21705         gen_decl_die (thing, NULL, ns_context);
21706       else
21707         gen_type_die (thing, ns_context);
21708     }
21709   return context_die;
21710 }
21711
21712 /* Generate a DIE for a namespace or namespace alias.  */
21713
21714 static void
21715 gen_namespace_die (tree decl, dw_die_ref context_die)
21716 {
21717   dw_die_ref namespace_die;
21718
21719   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
21720      they are an alias of.  */
21721   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
21722     {
21723       /* Output a real namespace or module.  */
21724       context_die = setup_namespace_context (decl, comp_unit_die ());
21725       namespace_die = new_die (is_fortran ()
21726                                ? DW_TAG_module : DW_TAG_namespace,
21727                                context_die, decl);
21728       /* For Fortran modules defined in different CU don't add src coords.  */
21729       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
21730         {
21731           const char *name = dwarf2_name (decl, 0);
21732           if (name)
21733             add_name_attribute (namespace_die, name);
21734         }
21735       else
21736         add_name_and_src_coords_attributes (namespace_die, decl);
21737       if (DECL_EXTERNAL (decl))
21738         add_AT_flag (namespace_die, DW_AT_declaration, 1);
21739       equate_decl_number_to_die (decl, namespace_die);
21740     }
21741   else
21742     {
21743       /* Output a namespace alias.  */
21744
21745       /* Force out the namespace we are an alias of, if necessary.  */
21746       dw_die_ref origin_die
21747         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
21748
21749       if (DECL_FILE_SCOPE_P (decl)
21750           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
21751         context_die = setup_namespace_context (decl, comp_unit_die ());
21752       /* Now create the namespace alias DIE.  */
21753       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
21754       add_name_and_src_coords_attributes (namespace_die, decl);
21755       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
21756       equate_decl_number_to_die (decl, namespace_die);
21757     }
21758 }
21759
21760 /* Generate Dwarf debug information for a decl described by DECL.
21761    The return value is currently only meaningful for PARM_DECLs,
21762    for all other decls it returns NULL.  */
21763
21764 static dw_die_ref
21765 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
21766 {
21767   tree decl_or_origin = decl ? decl : origin;
21768   tree class_origin = NULL, ultimate_origin;
21769
21770   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
21771     return NULL;
21772
21773   switch (TREE_CODE (decl_or_origin))
21774     {
21775     case ERROR_MARK:
21776       break;
21777
21778     case CONST_DECL:
21779       if (!is_fortran () && !is_ada ())
21780         {
21781           /* The individual enumerators of an enum type get output when we output
21782              the Dwarf representation of the relevant enum type itself.  */
21783           break;
21784         }
21785
21786       /* Emit its type.  */
21787       gen_type_die (TREE_TYPE (decl), context_die);
21788
21789       /* And its containing namespace.  */
21790       context_die = declare_in_namespace (decl, context_die);
21791
21792       gen_const_die (decl, context_die);
21793       break;
21794
21795     case FUNCTION_DECL:
21796       /* Don't output any DIEs to represent mere function declarations,
21797          unless they are class members or explicit block externs.  */
21798       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
21799           && DECL_FILE_SCOPE_P (decl_or_origin)
21800           && (current_function_decl == NULL_TREE
21801               || DECL_ARTIFICIAL (decl_or_origin)))
21802         break;
21803
21804 #if 0
21805       /* FIXME */
21806       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
21807          on local redeclarations of global functions.  That seems broken.  */
21808       if (current_function_decl != decl)
21809         /* This is only a declaration.  */;
21810 #endif
21811
21812       /* If we're emitting a clone, emit info for the abstract instance.  */
21813       if (origin || DECL_ORIGIN (decl) != decl)
21814         dwarf2out_abstract_function (origin
21815                                      ? DECL_ORIGIN (origin)
21816                                      : DECL_ABSTRACT_ORIGIN (decl));
21817
21818       /* If we're emitting an out-of-line copy of an inline function,
21819          emit info for the abstract instance and set up to refer to it.  */
21820       else if (cgraph_function_possibly_inlined_p (decl)
21821                && ! DECL_ABSTRACT (decl)
21822                && ! class_or_namespace_scope_p (context_die)
21823                /* dwarf2out_abstract_function won't emit a die if this is just
21824                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
21825                   that case, because that works only if we have a die.  */
21826                && DECL_INITIAL (decl) != NULL_TREE)
21827         {
21828           dwarf2out_abstract_function (decl);
21829           set_decl_origin_self (decl);
21830         }
21831
21832       /* Otherwise we're emitting the primary DIE for this decl.  */
21833       else if (debug_info_level > DINFO_LEVEL_TERSE)
21834         {
21835           /* Before we describe the FUNCTION_DECL itself, make sure that we
21836              have its containing type.  */
21837           if (!origin)
21838             origin = decl_class_context (decl);
21839           if (origin != NULL_TREE)
21840             gen_type_die (origin, context_die);
21841
21842           /* And its return type.  */
21843           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
21844
21845           /* And its virtual context.  */
21846           if (DECL_VINDEX (decl) != NULL_TREE)
21847             gen_type_die (DECL_CONTEXT (decl), context_die);
21848
21849           /* Make sure we have a member DIE for decl.  */
21850           if (origin != NULL_TREE)
21851             gen_type_die_for_member (origin, decl, context_die);
21852
21853           /* And its containing namespace.  */
21854           context_die = declare_in_namespace (decl, context_die);
21855         }
21856
21857       /* Now output a DIE to represent the function itself.  */
21858       if (decl)
21859         gen_subprogram_die (decl, context_die);
21860       break;
21861
21862     case TYPE_DECL:
21863       /* If we are in terse mode, don't generate any DIEs to represent any
21864          actual typedefs.  */
21865       if (debug_info_level <= DINFO_LEVEL_TERSE)
21866         break;
21867
21868       /* In the special case of a TYPE_DECL node representing the declaration
21869          of some type tag, if the given TYPE_DECL is marked as having been
21870          instantiated from some other (original) TYPE_DECL node (e.g. one which
21871          was generated within the original definition of an inline function) we
21872          used to generate a special (abbreviated) DW_TAG_structure_type,
21873          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
21874          should be actually referencing those DIEs, as variable DIEs with that
21875          type would be emitted already in the abstract origin, so it was always
21876          removed during unused type prunning.  Don't add anything in this
21877          case.  */
21878       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
21879         break;
21880
21881       if (is_redundant_typedef (decl))
21882         gen_type_die (TREE_TYPE (decl), context_die);
21883       else
21884         /* Output a DIE to represent the typedef itself.  */
21885         gen_typedef_die (decl, context_die);
21886       break;
21887
21888     case LABEL_DECL:
21889       if (debug_info_level >= DINFO_LEVEL_NORMAL)
21890         gen_label_die (decl, context_die);
21891       break;
21892
21893     case VAR_DECL:
21894     case RESULT_DECL:
21895       /* If we are in terse mode, don't generate any DIEs to represent any
21896          variable declarations or definitions.  */
21897       if (debug_info_level <= DINFO_LEVEL_TERSE)
21898         break;
21899
21900       /* Output any DIEs that are needed to specify the type of this data
21901          object.  */
21902       if (decl_by_reference_p (decl_or_origin))
21903         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21904       else
21905         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21906
21907       /* And its containing type.  */
21908       class_origin = decl_class_context (decl_or_origin);
21909       if (class_origin != NULL_TREE)
21910         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
21911
21912       /* And its containing namespace.  */
21913       context_die = declare_in_namespace (decl_or_origin, context_die);
21914
21915       /* Now output the DIE to represent the data object itself.  This gets
21916          complicated because of the possibility that the VAR_DECL really
21917          represents an inlined instance of a formal parameter for an inline
21918          function.  */
21919       ultimate_origin = decl_ultimate_origin (decl_or_origin);
21920       if (ultimate_origin != NULL_TREE
21921           && TREE_CODE (ultimate_origin) == PARM_DECL)
21922         gen_formal_parameter_die (decl, origin,
21923                                   true /* Emit name attribute.  */,
21924                                   context_die);
21925       else
21926         gen_variable_die (decl, origin, context_die);
21927       break;
21928
21929     case FIELD_DECL:
21930       /* Ignore the nameless fields that are used to skip bits but handle C++
21931          anonymous unions and structs.  */
21932       if (DECL_NAME (decl) != NULL_TREE
21933           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
21934           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
21935         {
21936           gen_type_die (member_declared_type (decl), context_die);
21937           gen_field_die (decl, context_die);
21938         }
21939       break;
21940
21941     case PARM_DECL:
21942       if (DECL_BY_REFERENCE (decl_or_origin))
21943         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
21944       else
21945         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
21946       return gen_formal_parameter_die (decl, origin,
21947                                        true /* Emit name attribute.  */,
21948                                        context_die);
21949
21950     case NAMESPACE_DECL:
21951     case IMPORTED_DECL:
21952       if (dwarf_version >= 3 || !dwarf_strict)
21953         gen_namespace_die (decl, context_die);
21954       break;
21955
21956     default:
21957       /* Probably some frontend-internal decl.  Assume we don't care.  */
21958       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
21959       break;
21960     }
21961
21962   return NULL;
21963 }
21964 \f
21965 /* Output debug information for global decl DECL.  Called from toplev.c after
21966    compilation proper has finished.  */
21967
21968 static void
21969 dwarf2out_global_decl (tree decl)
21970 {
21971   /* Output DWARF2 information for file-scope tentative data object
21972      declarations, file-scope (extern) function declarations (which
21973      had no corresponding body) and file-scope tagged type declarations
21974      and definitions which have not yet been forced out.  */
21975   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
21976     dwarf2out_decl (decl);
21977 }
21978
21979 /* Output debug information for type decl DECL.  Called from toplev.c
21980    and from language front ends (to record built-in types).  */
21981 static void
21982 dwarf2out_type_decl (tree decl, int local)
21983 {
21984   if (!local)
21985     dwarf2out_decl (decl);
21986 }
21987
21988 /* Output debug information for imported module or decl DECL.
21989    NAME is non-NULL name in the lexical block if the decl has been renamed.
21990    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
21991    that DECL belongs to.
21992    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
21993 static void
21994 dwarf2out_imported_module_or_decl_1 (tree decl,
21995                                      tree name,
21996                                      tree lexical_block,
21997                                      dw_die_ref lexical_block_die)
21998 {
21999   expanded_location xloc;
22000   dw_die_ref imported_die = NULL;
22001   dw_die_ref at_import_die;
22002
22003   if (TREE_CODE (decl) == IMPORTED_DECL)
22004     {
22005       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
22006       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
22007       gcc_assert (decl);
22008     }
22009   else
22010     xloc = expand_location (input_location);
22011
22012   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
22013     {
22014       at_import_die = force_type_die (TREE_TYPE (decl));
22015       /* For namespace N { typedef void T; } using N::T; base_type_die
22016          returns NULL, but DW_TAG_imported_declaration requires
22017          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
22018       if (!at_import_die)
22019         {
22020           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
22021           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
22022           at_import_die = lookup_type_die (TREE_TYPE (decl));
22023           gcc_assert (at_import_die);
22024         }
22025     }
22026   else
22027     {
22028       at_import_die = lookup_decl_die (decl);
22029       if (!at_import_die)
22030         {
22031           /* If we're trying to avoid duplicate debug info, we may not have
22032              emitted the member decl for this field.  Emit it now.  */
22033           if (TREE_CODE (decl) == FIELD_DECL)
22034             {
22035               tree type = DECL_CONTEXT (decl);
22036
22037               if (TYPE_CONTEXT (type)
22038                   && TYPE_P (TYPE_CONTEXT (type))
22039                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
22040                                                 DINFO_USAGE_DIR_USE))
22041                 return;
22042               gen_type_die_for_member (type, decl,
22043                                        get_context_die (TYPE_CONTEXT (type)));
22044             }
22045           at_import_die = force_decl_die (decl);
22046         }
22047     }
22048
22049   if (TREE_CODE (decl) == NAMESPACE_DECL)
22050     {
22051       if (dwarf_version >= 3 || !dwarf_strict)
22052         imported_die = new_die (DW_TAG_imported_module,
22053                                 lexical_block_die,
22054                                 lexical_block);
22055       else
22056         return;
22057     }
22058   else
22059     imported_die = new_die (DW_TAG_imported_declaration,
22060                             lexical_block_die,
22061                             lexical_block);
22062
22063   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
22064   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
22065   if (name)
22066     add_AT_string (imported_die, DW_AT_name,
22067                    IDENTIFIER_POINTER (name));
22068   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
22069 }
22070
22071 /* Output debug information for imported module or decl DECL.
22072    NAME is non-NULL name in context if the decl has been renamed.
22073    CHILD is true if decl is one of the renamed decls as part of
22074    importing whole module.  */
22075
22076 static void
22077 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
22078                                    bool child)
22079 {
22080   /* dw_die_ref at_import_die;  */
22081   dw_die_ref scope_die;
22082
22083   if (debug_info_level <= DINFO_LEVEL_TERSE)
22084     return;
22085
22086   gcc_assert (decl);
22087
22088   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
22089      We need decl DIE for reference and scope die. First, get DIE for the decl
22090      itself.  */
22091
22092   /* Get the scope die for decl context. Use comp_unit_die for global module
22093      or decl. If die is not found for non globals, force new die.  */
22094   if (context
22095       && TYPE_P (context)
22096       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
22097     return;
22098
22099   if (!(dwarf_version >= 3 || !dwarf_strict))
22100     return;
22101
22102   scope_die = get_context_die (context);
22103
22104   if (child)
22105     {
22106       gcc_assert (scope_die->die_child);
22107       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
22108       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
22109       scope_die = scope_die->die_child;
22110     }
22111
22112   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
22113   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
22114
22115 }
22116
22117 /* Write the debugging output for DECL.  */
22118
22119 void
22120 dwarf2out_decl (tree decl)
22121 {
22122   dw_die_ref context_die = comp_unit_die ();
22123
22124   switch (TREE_CODE (decl))
22125     {
22126     case ERROR_MARK:
22127       return;
22128
22129     case FUNCTION_DECL:
22130       /* What we would really like to do here is to filter out all mere
22131          file-scope declarations of file-scope functions which are never
22132          referenced later within this translation unit (and keep all of ones
22133          that *are* referenced later on) but we aren't clairvoyant, so we have
22134          no idea which functions will be referenced in the future (i.e. later
22135          on within the current translation unit). So here we just ignore all
22136          file-scope function declarations which are not also definitions.  If
22137          and when the debugger needs to know something about these functions,
22138          it will have to hunt around and find the DWARF information associated
22139          with the definition of the function.
22140
22141          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
22142          nodes represent definitions and which ones represent mere
22143          declarations.  We have to check DECL_INITIAL instead. That's because
22144          the C front-end supports some weird semantics for "extern inline"
22145          function definitions.  These can get inlined within the current
22146          translation unit (and thus, we need to generate Dwarf info for their
22147          abstract instances so that the Dwarf info for the concrete inlined
22148          instances can have something to refer to) but the compiler never
22149          generates any out-of-lines instances of such things (despite the fact
22150          that they *are* definitions).
22151
22152          The important point is that the C front-end marks these "extern
22153          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
22154          them anyway. Note that the C++ front-end also plays some similar games
22155          for inline function definitions appearing within include files which
22156          also contain `#pragma interface' pragmas.  */
22157       if (DECL_INITIAL (decl) == NULL_TREE)
22158         return;
22159
22160       /* If we're a nested function, initially use a parent of NULL; if we're
22161          a plain function, this will be fixed up in decls_for_scope.  If
22162          we're a method, it will be ignored, since we already have a DIE.  */
22163       if (decl_function_context (decl)
22164           /* But if we're in terse mode, we don't care about scope.  */
22165           && debug_info_level > DINFO_LEVEL_TERSE)
22166         context_die = NULL;
22167       break;
22168
22169     case VAR_DECL:
22170       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
22171          declaration and if the declaration was never even referenced from
22172          within this entire compilation unit.  We suppress these DIEs in
22173          order to save space in the .debug section (by eliminating entries
22174          which are probably useless).  Note that we must not suppress
22175          block-local extern declarations (whether used or not) because that
22176          would screw-up the debugger's name lookup mechanism and cause it to
22177          miss things which really ought to be in scope at a given point.  */
22178       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
22179         return;
22180
22181       /* For local statics lookup proper context die.  */
22182       if (TREE_STATIC (decl) && decl_function_context (decl))
22183         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22184
22185       /* If we are in terse mode, don't generate any DIEs to represent any
22186          variable declarations or definitions.  */
22187       if (debug_info_level <= DINFO_LEVEL_TERSE)
22188         return;
22189       break;
22190
22191     case CONST_DECL:
22192       if (debug_info_level <= DINFO_LEVEL_TERSE)
22193         return;
22194       if (!is_fortran () && !is_ada ())
22195         return;
22196       if (TREE_STATIC (decl) && decl_function_context (decl))
22197         context_die = lookup_decl_die (DECL_CONTEXT (decl));
22198       break;
22199
22200     case NAMESPACE_DECL:
22201     case IMPORTED_DECL:
22202       if (debug_info_level <= DINFO_LEVEL_TERSE)
22203         return;
22204       if (lookup_decl_die (decl) != NULL)
22205         return;
22206       break;
22207
22208     case TYPE_DECL:
22209       /* Don't emit stubs for types unless they are needed by other DIEs.  */
22210       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
22211         return;
22212
22213       /* Don't bother trying to generate any DIEs to represent any of the
22214          normal built-in types for the language we are compiling.  */
22215       if (DECL_IS_BUILTIN (decl))
22216         return;
22217
22218       /* If we are in terse mode, don't generate any DIEs for types.  */
22219       if (debug_info_level <= DINFO_LEVEL_TERSE)
22220         return;
22221
22222       /* If we're a function-scope tag, initially use a parent of NULL;
22223          this will be fixed up in decls_for_scope.  */
22224       if (decl_function_context (decl))
22225         context_die = NULL;
22226
22227       break;
22228
22229     default:
22230       return;
22231     }
22232
22233   gen_decl_die (decl, NULL, context_die);
22234 }
22235
22236 /* Write the debugging output for DECL.  */
22237
22238 static void
22239 dwarf2out_function_decl (tree decl)
22240 {
22241   dwarf2out_decl (decl);
22242   call_arg_locations = NULL;
22243   call_arg_loc_last = NULL;
22244   call_site_count = -1;
22245   tail_call_site_count = -1;
22246   VEC_free (dw_die_ref, heap, block_map);
22247   htab_empty (decl_loc_table);
22248   htab_empty (cached_dw_loc_list_table);
22249 }
22250
22251 /* Output a marker (i.e. a label) for the beginning of the generated code for
22252    a lexical block.  */
22253
22254 static void
22255 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
22256                        unsigned int blocknum)
22257 {
22258   switch_to_section (current_function_section ());
22259   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
22260 }
22261
22262 /* Output a marker (i.e. a label) for the end of the generated code for a
22263    lexical block.  */
22264
22265 static void
22266 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
22267 {
22268   switch_to_section (current_function_section ());
22269   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
22270 }
22271
22272 /* Returns nonzero if it is appropriate not to emit any debugging
22273    information for BLOCK, because it doesn't contain any instructions.
22274
22275    Don't allow this for blocks with nested functions or local classes
22276    as we would end up with orphans, and in the presence of scheduling
22277    we may end up calling them anyway.  */
22278
22279 static bool
22280 dwarf2out_ignore_block (const_tree block)
22281 {
22282   tree decl;
22283   unsigned int i;
22284
22285   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
22286     if (TREE_CODE (decl) == FUNCTION_DECL
22287         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22288       return 0;
22289   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
22290     {
22291       decl = BLOCK_NONLOCALIZED_VAR (block, i);
22292       if (TREE_CODE (decl) == FUNCTION_DECL
22293           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
22294       return 0;
22295     }
22296
22297   return 1;
22298 }
22299
22300 /* Hash table routines for file_hash.  */
22301
22302 static int
22303 file_table_eq (const void *p1_p, const void *p2_p)
22304 {
22305   const struct dwarf_file_data *const p1 =
22306     (const struct dwarf_file_data *) p1_p;
22307   const char *const p2 = (const char *) p2_p;
22308   return filename_cmp (p1->filename, p2) == 0;
22309 }
22310
22311 static hashval_t
22312 file_table_hash (const void *p_p)
22313 {
22314   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
22315   return htab_hash_string (p->filename);
22316 }
22317
22318 /* Lookup FILE_NAME (in the list of filenames that we know about here in
22319    dwarf2out.c) and return its "index".  The index of each (known) filename is
22320    just a unique number which is associated with only that one filename.  We
22321    need such numbers for the sake of generating labels (in the .debug_sfnames
22322    section) and references to those files numbers (in the .debug_srcinfo
22323    and.debug_macinfo sections).  If the filename given as an argument is not
22324    found in our current list, add it to the list and assign it the next
22325    available unique index number.  In order to speed up searches, we remember
22326    the index of the filename was looked up last.  This handles the majority of
22327    all searches.  */
22328
22329 static struct dwarf_file_data *
22330 lookup_filename (const char *file_name)
22331 {
22332   void ** slot;
22333   struct dwarf_file_data * created;
22334
22335   /* Check to see if the file name that was searched on the previous
22336      call matches this file name.  If so, return the index.  */
22337   if (file_table_last_lookup
22338       && (file_name == file_table_last_lookup->filename
22339           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
22340     return file_table_last_lookup;
22341
22342   /* Didn't match the previous lookup, search the table.  */
22343   slot = htab_find_slot_with_hash (file_table, file_name,
22344                                    htab_hash_string (file_name), INSERT);
22345   if (*slot)
22346     return (struct dwarf_file_data *) *slot;
22347
22348   created = ggc_alloc_dwarf_file_data ();
22349   created->filename = file_name;
22350   created->emitted_number = 0;
22351   *slot = created;
22352   return created;
22353 }
22354
22355 /* If the assembler will construct the file table, then translate the compiler
22356    internal file table number into the assembler file table number, and emit
22357    a .file directive if we haven't already emitted one yet.  The file table
22358    numbers are different because we prune debug info for unused variables and
22359    types, which may include filenames.  */
22360
22361 static int
22362 maybe_emit_file (struct dwarf_file_data * fd)
22363 {
22364   if (! fd->emitted_number)
22365     {
22366       if (last_emitted_file)
22367         fd->emitted_number = last_emitted_file->emitted_number + 1;
22368       else
22369         fd->emitted_number = 1;
22370       last_emitted_file = fd;
22371
22372       if (DWARF2_ASM_LINE_DEBUG_INFO)
22373         {
22374           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
22375           output_quoted_string (asm_out_file,
22376                                 remap_debug_filename (fd->filename));
22377           fputc ('\n', asm_out_file);
22378         }
22379     }
22380
22381   return fd->emitted_number;
22382 }
22383
22384 /* Schedule generation of a DW_AT_const_value attribute to DIE.
22385    That generation should happen after function debug info has been
22386    generated. The value of the attribute is the constant value of ARG.  */
22387
22388 static void
22389 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
22390 {
22391   die_arg_entry entry;
22392
22393   if (!die || !arg)
22394     return;
22395
22396   if (!tmpl_value_parm_die_table)
22397     tmpl_value_parm_die_table
22398       = VEC_alloc (die_arg_entry, gc, 32);
22399
22400   entry.die = die;
22401   entry.arg = arg;
22402   VEC_safe_push (die_arg_entry, gc,
22403                  tmpl_value_parm_die_table,
22404                  &entry);
22405 }
22406
22407 /* Return TRUE if T is an instance of generic type, FALSE
22408    otherwise.  */
22409
22410 static bool
22411 generic_type_p (tree t)
22412 {
22413   if (t == NULL_TREE || !TYPE_P (t))
22414     return false;
22415   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
22416 }
22417
22418 /* Schedule the generation of the generic parameter dies for the
22419   instance of generic type T. The proper generation itself is later
22420   done by gen_scheduled_generic_parms_dies. */
22421
22422 static void
22423 schedule_generic_params_dies_gen (tree t)
22424 {
22425   if (!generic_type_p (t))
22426     return;
22427
22428   if (generic_type_instances == NULL)
22429     generic_type_instances = VEC_alloc (tree, gc, 256);
22430
22431   VEC_safe_push (tree, gc, generic_type_instances, t);
22432 }
22433
22434 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
22435    by append_entry_to_tmpl_value_parm_die_table. This function must
22436    be called after function DIEs have been generated.  */
22437
22438 static void
22439 gen_remaining_tmpl_value_param_die_attribute (void)
22440 {
22441   if (tmpl_value_parm_die_table)
22442     {
22443       unsigned i;
22444       die_arg_entry *e;
22445
22446       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
22447         tree_add_const_value_attribute (e->die, e->arg);
22448     }
22449 }
22450
22451 /* Generate generic parameters DIEs for instances of generic types
22452    that have been previously scheduled by
22453    schedule_generic_params_dies_gen. This function must be called
22454    after all the types of the CU have been laid out.  */
22455
22456 static void
22457 gen_scheduled_generic_parms_dies (void)
22458 {
22459   unsigned i;
22460   tree t;
22461
22462   if (generic_type_instances == NULL)
22463     return;
22464   
22465   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
22466     gen_generic_params_dies (t);
22467 }
22468
22469
22470 /* Replace DW_AT_name for the decl with name.  */
22471
22472 static void
22473 dwarf2out_set_name (tree decl, tree name)
22474 {
22475   dw_die_ref die;
22476   dw_attr_ref attr;
22477   const char *dname;
22478
22479   die = TYPE_SYMTAB_DIE (decl);
22480   if (!die)
22481     return;
22482
22483   dname = dwarf2_name (name, 0);
22484   if (!dname)
22485     return;
22486
22487   attr = get_AT (die, DW_AT_name);
22488   if (attr)
22489     {
22490       struct indirect_string_node *node;
22491
22492       node = find_AT_string (dname);
22493       /* replace the string.  */
22494       attr->dw_attr_val.v.val_str = node;
22495     }
22496
22497   else
22498     add_name_attribute (die, dname);
22499 }
22500
22501 /* Called by the final INSN scan whenever we see a var location.  We
22502    use it to drop labels in the right places, and throw the location in
22503    our lookup table.  */
22504
22505 static void
22506 dwarf2out_var_location (rtx loc_note)
22507 {
22508   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
22509   struct var_loc_node *newloc;
22510   rtx next_real;
22511   static const char *last_label;
22512   static const char *last_postcall_label;
22513   static bool last_in_cold_section_p;
22514   tree decl;
22515   bool var_loc_p;
22516
22517   if (!NOTE_P (loc_note))
22518     {
22519       if (CALL_P (loc_note))
22520         {
22521           call_site_count++;
22522           if (SIBLING_CALL_P (loc_note))
22523             tail_call_site_count++;
22524         }
22525       return;
22526     }
22527
22528   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
22529   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
22530     return;
22531
22532   next_real = next_real_insn (loc_note);
22533
22534   /* If there are no instructions which would be affected by this note,
22535      don't do anything.  */
22536   if (var_loc_p
22537       && next_real == NULL_RTX
22538       && !NOTE_DURING_CALL_P (loc_note))
22539     return;
22540
22541   if (next_real == NULL_RTX)
22542     next_real = get_last_insn ();
22543
22544   /* If there were any real insns between note we processed last time
22545      and this note (or if it is the first note), clear
22546      last_{,postcall_}label so that they are not reused this time.  */
22547   if (last_var_location_insn == NULL_RTX
22548       || last_var_location_insn != next_real
22549       || last_in_cold_section_p != in_cold_section_p)
22550     {
22551       last_label = NULL;
22552       last_postcall_label = NULL;
22553     }
22554
22555   if (var_loc_p)
22556     {
22557       decl = NOTE_VAR_LOCATION_DECL (loc_note);
22558       newloc = add_var_loc_to_decl (decl, loc_note,
22559                                     NOTE_DURING_CALL_P (loc_note)
22560                                     ? last_postcall_label : last_label);
22561       if (newloc == NULL)
22562         return;
22563     }
22564   else
22565     {
22566       decl = NULL_TREE;
22567       newloc = NULL;
22568     }
22569
22570   /* If there were no real insns between note we processed last time
22571      and this note, use the label we emitted last time.  Otherwise
22572      create a new label and emit it.  */
22573   if (last_label == NULL)
22574     {
22575       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
22576       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
22577       loclabel_num++;
22578       last_label = ggc_strdup (loclabel);
22579     }
22580
22581   if (!var_loc_p)
22582     {
22583       struct call_arg_loc_node *ca_loc
22584         = ggc_alloc_cleared_call_arg_loc_node ();
22585       rtx prev = prev_real_insn (loc_note), x;
22586       ca_loc->call_arg_loc_note = loc_note;
22587       ca_loc->next = NULL;
22588       ca_loc->label = last_label;
22589       gcc_assert (prev
22590                   && (CALL_P (prev)
22591                       || (NONJUMP_INSN_P (prev)
22592                           && GET_CODE (PATTERN (prev)) == SEQUENCE
22593                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
22594       if (!CALL_P (prev))
22595         prev = XVECEXP (PATTERN (prev), 0, 0);
22596       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
22597       x = PATTERN (prev);
22598       if (GET_CODE (x) == PARALLEL)
22599         x = XVECEXP (x, 0, 0);
22600       if (GET_CODE (x) == SET)
22601         x = SET_SRC (x);
22602       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
22603         {
22604           x = XEXP (XEXP (x, 0), 0);
22605           if (GET_CODE (x) == SYMBOL_REF
22606               && SYMBOL_REF_DECL (x)
22607               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
22608             ca_loc->symbol_ref = x;
22609         }
22610       ca_loc->block = insn_scope (prev);
22611       if (call_arg_locations)
22612         call_arg_loc_last->next = ca_loc;
22613       else
22614         call_arg_locations = ca_loc;
22615       call_arg_loc_last = ca_loc;
22616     }
22617   else if (!NOTE_DURING_CALL_P (loc_note))
22618     newloc->label = last_label;
22619   else
22620     {
22621       if (!last_postcall_label)
22622         {
22623           sprintf (loclabel, "%s-1", last_label);
22624           last_postcall_label = ggc_strdup (loclabel);
22625         }
22626       newloc->label = last_postcall_label;
22627     }
22628
22629   last_var_location_insn = next_real;
22630   last_in_cold_section_p = in_cold_section_p;
22631 }
22632
22633 /* Note in one location list that text section has changed.  */
22634
22635 static int
22636 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
22637 {
22638   var_loc_list *list = (var_loc_list *) *slot;
22639   if (list->first)
22640     list->last_before_switch
22641       = list->last->next ? list->last->next : list->last;
22642   return 1;
22643 }
22644
22645 /* Note in all location lists that text section has changed.  */
22646
22647 static void
22648 var_location_switch_text_section (void)
22649 {
22650   if (decl_loc_table == NULL)
22651     return;
22652
22653   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
22654 }
22655
22656 /* Create a new line number table.  */
22657
22658 static dw_line_info_table *
22659 new_line_info_table (void)
22660 {
22661   dw_line_info_table *table;
22662
22663   table = ggc_alloc_cleared_dw_line_info_table_struct ();
22664   table->file_num = 1;
22665   table->line_num = 1;
22666   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
22667
22668   return table;
22669 }
22670
22671 /* Lookup the "current" table into which we emit line info, so
22672    that we don't have to do it for every source line.  */
22673
22674 static void
22675 set_cur_line_info_table (section *sec)
22676 {
22677   dw_line_info_table *table;
22678
22679   if (sec == text_section)
22680     table = text_section_line_info;
22681   else if (sec == cold_text_section)
22682     {
22683       table = cold_text_section_line_info;
22684       if (!table)
22685         {
22686           cold_text_section_line_info = table = new_line_info_table ();
22687           table->end_label = cold_end_label;
22688         }
22689     }
22690   else
22691     {
22692       const char *end_label;
22693
22694       if (flag_reorder_blocks_and_partition)
22695         {
22696           if (in_cold_section_p)
22697             end_label = crtl->subsections.cold_section_end_label;
22698           else
22699             end_label = crtl->subsections.hot_section_end_label;
22700         }
22701       else
22702         {
22703           char label[MAX_ARTIFICIAL_LABEL_BYTES];
22704           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
22705                                        current_function_funcdef_no);
22706           end_label = ggc_strdup (label);
22707         }
22708
22709       table = new_line_info_table ();
22710       table->end_label = end_label;
22711
22712       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
22713     }
22714
22715   cur_line_info_table = table;
22716 }
22717
22718
22719 /* We need to reset the locations at the beginning of each
22720    function. We can't do this in the end_function hook, because the
22721    declarations that use the locations won't have been output when
22722    that hook is called.  Also compute have_multiple_function_sections here.  */
22723
22724 static void
22725 dwarf2out_begin_function (tree fun)
22726 {
22727   section *sec = function_section (fun);
22728
22729   if (sec != text_section)
22730     have_multiple_function_sections = true;
22731
22732   if (flag_reorder_blocks_and_partition && !cold_text_section)
22733     {
22734       gcc_assert (current_function_decl == fun);
22735       cold_text_section = unlikely_text_section ();
22736       switch_to_section (cold_text_section);
22737       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
22738       switch_to_section (sec);
22739     }
22740
22741   dwarf2out_note_section_used ();
22742   call_site_count = 0;
22743   tail_call_site_count = 0;
22744
22745   set_cur_line_info_table (sec);
22746 }
22747
22748 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
22749
22750 static void
22751 push_dw_line_info_entry (dw_line_info_table *table,
22752                          enum dw_line_info_opcode opcode, unsigned int val)
22753 {
22754   dw_line_info_entry e;
22755   e.opcode = opcode;
22756   e.val = val;
22757   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
22758 }
22759
22760 /* Output a label to mark the beginning of a source code line entry
22761    and record information relating to this source line, in
22762    'line_info_table' for later output of the .debug_line section.  */
22763 /* ??? The discriminator parameter ought to be unsigned.  */
22764
22765 static void
22766 dwarf2out_source_line (unsigned int line, const char *filename,
22767                        int discriminator, bool is_stmt)
22768 {
22769   unsigned int file_num;
22770   dw_line_info_table *table;
22771
22772   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
22773     return;
22774
22775   /* The discriminator column was added in dwarf4.  Simplify the below
22776      by simply removing it if we're not supposed to output it.  */
22777   if (dwarf_version < 4 && dwarf_strict)
22778     discriminator = 0;
22779
22780   table = cur_line_info_table;
22781   file_num = maybe_emit_file (lookup_filename (filename));
22782
22783   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
22784      the debugger has used the second (possibly duplicate) line number
22785      at the beginning of the function to mark the end of the prologue.
22786      We could eliminate any other duplicates within the function.  For
22787      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
22788      that second line number entry.  */
22789   /* Recall that this end-of-prologue indication is *not* the same thing
22790      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
22791      to which the hook corresponds, follows the last insn that was 
22792      emitted by gen_prologue.  What we need is to preceed the first insn
22793      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
22794      insn that corresponds to something the user wrote.  These may be
22795      very different locations once scheduling is enabled.  */
22796
22797   if (0 && file_num == table->file_num
22798       && line == table->line_num
22799       && discriminator == table->discrim_num
22800       && is_stmt == table->is_stmt)
22801     return;
22802
22803   switch_to_section (current_function_section ());
22804
22805   /* If requested, emit something human-readable.  */
22806   if (flag_debug_asm)
22807     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
22808
22809   if (DWARF2_ASM_LINE_DEBUG_INFO)
22810     {
22811       /* Emit the .loc directive understood by GNU as.  */
22812       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
22813       if (is_stmt != table->is_stmt)
22814         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
22815       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
22816         fprintf (asm_out_file, " discriminator %d", discriminator);
22817       fputc ('\n', asm_out_file);
22818     }
22819   else
22820     {
22821       unsigned int label_num = ++line_info_label_num;
22822
22823       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
22824
22825       push_dw_line_info_entry (table, LI_set_address, label_num);
22826       if (file_num != table->file_num)
22827         push_dw_line_info_entry (table, LI_set_file, file_num);
22828       if (discriminator != table->discrim_num)
22829         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
22830       if (is_stmt != table->is_stmt)
22831         push_dw_line_info_entry (table, LI_negate_stmt, 0);
22832       push_dw_line_info_entry (table, LI_set_line, line);
22833     }
22834
22835   table->file_num = file_num;
22836   table->line_num = line;
22837   table->discrim_num = discriminator;
22838   table->is_stmt = is_stmt;
22839   table->in_use = true;
22840 }
22841
22842 /* Record the beginning of a new source file.  */
22843
22844 static void
22845 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
22846 {
22847   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22848     {
22849       /* Record the beginning of the file for break_out_includes.  */
22850       dw_die_ref bincl_die;
22851
22852       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
22853       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
22854     }
22855
22856   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22857     {
22858       macinfo_entry e;
22859       e.code = DW_MACINFO_start_file;
22860       e.lineno = lineno;
22861       e.info = xstrdup (filename);
22862       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22863     }
22864 }
22865
22866 /* Record the end of a source file.  */
22867
22868 static void
22869 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
22870 {
22871   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22872     /* Record the end of the file for break_out_includes.  */
22873     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
22874
22875   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22876     {
22877       macinfo_entry e;
22878       e.code = DW_MACINFO_end_file;
22879       e.lineno = lineno;
22880       e.info = NULL;
22881       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22882     }
22883 }
22884
22885 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
22886    the tail part of the directive line, i.e. the part which is past the
22887    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22888
22889 static void
22890 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
22891                   const char *buffer ATTRIBUTE_UNUSED)
22892 {
22893   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22894     {
22895       macinfo_entry e;
22896       e.code = DW_MACINFO_define;
22897       e.lineno = lineno;
22898       e.info = xstrdup (buffer);;
22899       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22900     }
22901 }
22902
22903 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
22904    the tail part of the directive line, i.e. the part which is past the
22905    initial whitespace, #, whitespace, directive-name, whitespace part.  */
22906
22907 static void
22908 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
22909                  const char *buffer ATTRIBUTE_UNUSED)
22910 {
22911   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22912     {
22913       macinfo_entry e;
22914       e.code = DW_MACINFO_undef;
22915       e.lineno = lineno;
22916       e.info = xstrdup (buffer);;
22917       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
22918     }
22919 }
22920
22921 static void
22922 output_macinfo (void)
22923 {
22924   unsigned i;
22925   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
22926   macinfo_entry *ref;
22927
22928   if (! length)
22929     return;
22930
22931   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
22932     {
22933       switch (ref->code)
22934         {
22935           case DW_MACINFO_start_file:
22936             {
22937               int file_num = maybe_emit_file (lookup_filename (ref->info));
22938               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
22939               dw2_asm_output_data_uleb128 
22940                         (ref->lineno, "Included from line number %lu", 
22941                                                 (unsigned long)ref->lineno);
22942               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
22943             }
22944             break;
22945           case DW_MACINFO_end_file:
22946             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
22947             break;
22948           case DW_MACINFO_define:
22949             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
22950             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
22951                                                 (unsigned long)ref->lineno);
22952             dw2_asm_output_nstring (ref->info, -1, "The macro");
22953             break;
22954           case DW_MACINFO_undef:
22955             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
22956             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
22957                                                 (unsigned long)ref->lineno);
22958             dw2_asm_output_nstring (ref->info, -1, "The macro");
22959             break;
22960           default:
22961            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
22962              ASM_COMMENT_START, (unsigned long)ref->code);
22963           break;
22964         }
22965     }
22966 }
22967
22968 /* Set up for Dwarf output at the start of compilation.  */
22969
22970 static void
22971 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
22972 {
22973   /* Allocate the file_table.  */
22974   file_table = htab_create_ggc (50, file_table_hash,
22975                                 file_table_eq, NULL);
22976
22977   /* Allocate the decl_die_table.  */
22978   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
22979                                     decl_die_table_eq, NULL);
22980
22981   /* Allocate the decl_loc_table.  */
22982   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
22983                                     decl_loc_table_eq, NULL);
22984
22985   /* Allocate the cached_dw_loc_list_table.  */
22986   cached_dw_loc_list_table
22987     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
22988                        cached_dw_loc_list_table_eq, NULL);
22989
22990   /* Allocate the initial hunk of the decl_scope_table.  */
22991   decl_scope_table = VEC_alloc (tree, gc, 256);
22992
22993   /* Allocate the initial hunk of the abbrev_die_table.  */
22994   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
22995     (ABBREV_DIE_TABLE_INCREMENT);
22996   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
22997   /* Zero-th entry is allocated, but unused.  */
22998   abbrev_die_table_in_use = 1;
22999
23000   /* Allocate the pubtypes and pubnames vectors.  */
23001   pubname_table = VEC_alloc (pubname_entry, gc, 32);
23002   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
23003
23004   incomplete_types = VEC_alloc (tree, gc, 64);
23005
23006   used_rtx_array = VEC_alloc (rtx, gc, 32);
23007
23008   debug_info_section = get_section (DEBUG_INFO_SECTION,
23009                                     SECTION_DEBUG, NULL);
23010   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
23011                                       SECTION_DEBUG, NULL);
23012   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
23013                                        SECTION_DEBUG, NULL);
23014   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
23015                                        SECTION_DEBUG, NULL);
23016   debug_line_section = get_section (DEBUG_LINE_SECTION,
23017                                     SECTION_DEBUG, NULL);
23018   debug_loc_section = get_section (DEBUG_LOC_SECTION,
23019                                    SECTION_DEBUG, NULL);
23020   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
23021                                         SECTION_DEBUG, NULL);
23022   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
23023                                         SECTION_DEBUG, NULL);
23024   debug_str_section = get_section (DEBUG_STR_SECTION,
23025                                    DEBUG_STR_SECTION_FLAGS, NULL);
23026   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
23027                                       SECTION_DEBUG, NULL);
23028   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
23029                                      SECTION_DEBUG, NULL);
23030
23031   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
23032   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
23033                                DEBUG_ABBREV_SECTION_LABEL, 0);
23034   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
23035   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
23036                                COLD_TEXT_SECTION_LABEL, 0);
23037   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
23038
23039   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
23040                                DEBUG_INFO_SECTION_LABEL, 0);
23041   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
23042                                DEBUG_LINE_SECTION_LABEL, 0);
23043   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
23044                                DEBUG_RANGES_SECTION_LABEL, 0);
23045   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
23046                                DEBUG_MACINFO_SECTION_LABEL, 0);
23047
23048   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
23049     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
23050
23051   switch_to_section (text_section);
23052   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
23053
23054   /* Make sure the line number table for .text always exists.  */
23055   text_section_line_info = new_line_info_table ();
23056   text_section_line_info->end_label = text_end_label;
23057 }
23058
23059 /* Called before cgraph_optimize starts outputtting functions, variables
23060    and toplevel asms into assembly.  */
23061
23062 static void
23063 dwarf2out_assembly_start (void)
23064 {
23065   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
23066       && dwarf2out_do_cfi_asm ()
23067       && (!(flag_unwind_tables || flag_exceptions)
23068           || targetm.except_unwind_info (&global_options) != UI_DWARF2))
23069     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
23070 }
23071
23072 /* A helper function for dwarf2out_finish called through
23073    htab_traverse.  Emit one queued .debug_str string.  */
23074
23075 static int
23076 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
23077 {
23078   struct indirect_string_node *node = (struct indirect_string_node *) *h;
23079
23080   if (node->form == DW_FORM_strp)
23081     {
23082       switch_to_section (debug_str_section);
23083       ASM_OUTPUT_LABEL (asm_out_file, node->label);
23084       assemble_string (node->str, strlen (node->str) + 1);
23085     }
23086
23087   return 1;
23088 }
23089
23090 #if ENABLE_ASSERT_CHECKING
23091 /* Verify that all marks are clear.  */
23092
23093 static void
23094 verify_marks_clear (dw_die_ref die)
23095 {
23096   dw_die_ref c;
23097
23098   gcc_assert (! die->die_mark);
23099   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
23100 }
23101 #endif /* ENABLE_ASSERT_CHECKING */
23102
23103 /* Clear the marks for a die and its children.
23104    Be cool if the mark isn't set.  */
23105
23106 static void
23107 prune_unmark_dies (dw_die_ref die)
23108 {
23109   dw_die_ref c;
23110
23111   if (die->die_mark)
23112     die->die_mark = 0;
23113   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
23114 }
23115
23116 /* Given DIE that we're marking as used, find any other dies
23117    it references as attributes and mark them as used.  */
23118
23119 static void
23120 prune_unused_types_walk_attribs (dw_die_ref die)
23121 {
23122   dw_attr_ref a;
23123   unsigned ix;
23124
23125   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23126     {
23127       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
23128         {
23129           /* A reference to another DIE.
23130              Make sure that it will get emitted.
23131              If it was broken out into a comdat group, don't follow it.  */
23132           if (! use_debug_types
23133               || a->dw_attr == DW_AT_specification
23134               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
23135             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
23136         }
23137       /* Set the string's refcount to 0 so that prune_unused_types_mark
23138          accounts properly for it.  */
23139       if (AT_class (a) == dw_val_class_str)
23140         a->dw_attr_val.v.val_str->refcount = 0;
23141     }
23142 }
23143
23144 /* Mark the generic parameters and arguments children DIEs of DIE.  */
23145
23146 static void
23147 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
23148 {
23149   dw_die_ref c;
23150
23151   if (die == NULL || die->die_child == NULL)
23152     return;
23153   c = die->die_child;
23154   do
23155     {
23156       switch (c->die_tag)
23157         {
23158         case DW_TAG_template_type_param:
23159         case DW_TAG_template_value_param:
23160         case DW_TAG_GNU_template_template_param:
23161         case DW_TAG_GNU_template_parameter_pack:
23162           prune_unused_types_mark (c, 1);
23163           break;
23164         default:
23165           break;
23166         }
23167       c = c->die_sib;
23168     } while (c && c != die->die_child);
23169 }
23170
23171 /* Mark DIE as being used.  If DOKIDS is true, then walk down
23172    to DIE's children.  */
23173
23174 static void
23175 prune_unused_types_mark (dw_die_ref die, int dokids)
23176 {
23177   dw_die_ref c;
23178
23179   if (die->die_mark == 0)
23180     {
23181       /* We haven't done this node yet.  Mark it as used.  */
23182       die->die_mark = 1;
23183       /* If this is the DIE of a generic type instantiation,
23184          mark the children DIEs that describe its generic parms and
23185          args.  */
23186       prune_unused_types_mark_generic_parms_dies (die);
23187
23188       /* We also have to mark its parents as used.
23189          (But we don't want to mark our parents' kids due to this.)  */
23190       if (die->die_parent)
23191         prune_unused_types_mark (die->die_parent, 0);
23192
23193       /* Mark any referenced nodes.  */
23194       prune_unused_types_walk_attribs (die);
23195
23196       /* If this node is a specification,
23197          also mark the definition, if it exists.  */
23198       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
23199         prune_unused_types_mark (die->die_definition, 1);
23200     }
23201
23202   if (dokids && die->die_mark != 2)
23203     {
23204       /* We need to walk the children, but haven't done so yet.
23205          Remember that we've walked the kids.  */
23206       die->die_mark = 2;
23207
23208       /* If this is an array type, we need to make sure our
23209          kids get marked, even if they're types.  If we're
23210          breaking out types into comdat sections, do this
23211          for all type definitions.  */
23212       if (die->die_tag == DW_TAG_array_type
23213           || (use_debug_types
23214               && is_type_die (die) && ! is_declaration_die (die)))
23215         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
23216       else
23217         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23218     }
23219 }
23220
23221 /* For local classes, look if any static member functions were emitted
23222    and if so, mark them.  */
23223
23224 static void
23225 prune_unused_types_walk_local_classes (dw_die_ref die)
23226 {
23227   dw_die_ref c;
23228
23229   if (die->die_mark == 2)
23230     return;
23231
23232   switch (die->die_tag)
23233     {
23234     case DW_TAG_structure_type:
23235     case DW_TAG_union_type:
23236     case DW_TAG_class_type:
23237       break;
23238
23239     case DW_TAG_subprogram:
23240       if (!get_AT_flag (die, DW_AT_declaration)
23241           || die->die_definition != NULL)
23242         prune_unused_types_mark (die, 1);
23243       return;
23244
23245     default:
23246       return;
23247     }
23248
23249   /* Mark children.  */
23250   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
23251 }
23252
23253 /* Walk the tree DIE and mark types that we actually use.  */
23254
23255 static void
23256 prune_unused_types_walk (dw_die_ref die)
23257 {
23258   dw_die_ref c;
23259
23260   /* Don't do anything if this node is already marked and
23261      children have been marked as well.  */
23262   if (die->die_mark == 2)
23263     return;
23264
23265   switch (die->die_tag)
23266     {
23267     case DW_TAG_structure_type:
23268     case DW_TAG_union_type:
23269     case DW_TAG_class_type:
23270       if (die->die_perennial_p)
23271         break;
23272
23273       for (c = die->die_parent; c; c = c->die_parent)
23274         if (c->die_tag == DW_TAG_subprogram)
23275           break;
23276
23277       /* Finding used static member functions inside of classes
23278          is needed just for local classes, because for other classes
23279          static member function DIEs with DW_AT_specification
23280          are emitted outside of the DW_TAG_*_type.  If we ever change
23281          it, we'd need to call this even for non-local classes.  */
23282       if (c)
23283         prune_unused_types_walk_local_classes (die);
23284
23285       /* It's a type node --- don't mark it.  */
23286       return;
23287
23288     case DW_TAG_const_type:
23289     case DW_TAG_packed_type:
23290     case DW_TAG_pointer_type:
23291     case DW_TAG_reference_type:
23292     case DW_TAG_rvalue_reference_type:
23293     case DW_TAG_volatile_type:
23294     case DW_TAG_typedef:
23295     case DW_TAG_array_type:
23296     case DW_TAG_interface_type:
23297     case DW_TAG_friend:
23298     case DW_TAG_variant_part:
23299     case DW_TAG_enumeration_type:
23300     case DW_TAG_subroutine_type:
23301     case DW_TAG_string_type:
23302     case DW_TAG_set_type:
23303     case DW_TAG_subrange_type:
23304     case DW_TAG_ptr_to_member_type:
23305     case DW_TAG_file_type:
23306       if (die->die_perennial_p)
23307         break;
23308
23309       /* It's a type node --- don't mark it.  */
23310       return;
23311
23312     default:
23313       /* Mark everything else.  */
23314       break;
23315   }
23316
23317   if (die->die_mark == 0)
23318     {
23319       die->die_mark = 1;
23320
23321       /* Now, mark any dies referenced from here.  */
23322       prune_unused_types_walk_attribs (die);
23323     }
23324
23325   die->die_mark = 2;
23326
23327   /* Mark children.  */
23328   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
23329 }
23330
23331 /* Increment the string counts on strings referred to from DIE's
23332    attributes.  */
23333
23334 static void
23335 prune_unused_types_update_strings (dw_die_ref die)
23336 {
23337   dw_attr_ref a;
23338   unsigned ix;
23339
23340   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23341     if (AT_class (a) == dw_val_class_str)
23342       {
23343         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
23344         s->refcount++;
23345         /* Avoid unnecessarily putting strings that are used less than
23346            twice in the hash table.  */
23347         if (s->refcount
23348             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
23349           {
23350             void ** slot;
23351             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
23352                                              htab_hash_string (s->str),
23353                                              INSERT);
23354             gcc_assert (*slot == NULL);
23355             *slot = s;
23356           }
23357       }
23358 }
23359
23360 /* Remove from the tree DIE any dies that aren't marked.  */
23361
23362 static void
23363 prune_unused_types_prune (dw_die_ref die)
23364 {
23365   dw_die_ref c;
23366
23367   gcc_assert (die->die_mark);
23368   prune_unused_types_update_strings (die);
23369
23370   if (! die->die_child)
23371     return;
23372
23373   c = die->die_child;
23374   do {
23375     dw_die_ref prev = c;
23376     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
23377       if (c == die->die_child)
23378         {
23379           /* No marked children between 'prev' and the end of the list.  */
23380           if (prev == c)
23381             /* No marked children at all.  */
23382             die->die_child = NULL;
23383           else
23384             {
23385               prev->die_sib = c->die_sib;
23386               die->die_child = prev;
23387             }
23388           return;
23389         }
23390
23391     if (c != prev->die_sib)
23392       prev->die_sib = c;
23393     prune_unused_types_prune (c);
23394   } while (c != die->die_child);
23395 }
23396
23397 /* Remove dies representing declarations that we never use.  */
23398
23399 static void
23400 prune_unused_types (void)
23401 {
23402   unsigned int i;
23403   limbo_die_node *node;
23404   comdat_type_node *ctnode;
23405   pubname_ref pub;
23406   dw_die_ref base_type;
23407
23408 #if ENABLE_ASSERT_CHECKING
23409   /* All the marks should already be clear.  */
23410   verify_marks_clear (comp_unit_die ());
23411   for (node = limbo_die_list; node; node = node->next)
23412     verify_marks_clear (node->die);
23413   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23414     verify_marks_clear (ctnode->root_die);
23415 #endif /* ENABLE_ASSERT_CHECKING */
23416
23417   /* Mark types that are used in global variables.  */
23418   premark_types_used_by_global_vars ();
23419
23420   /* Set the mark on nodes that are actually used.  */
23421   prune_unused_types_walk (comp_unit_die ());
23422   for (node = limbo_die_list; node; node = node->next)
23423     prune_unused_types_walk (node->die);
23424   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23425     {
23426       prune_unused_types_walk (ctnode->root_die);
23427       prune_unused_types_mark (ctnode->type_die, 1);
23428     }
23429
23430   /* Also set the mark on nodes referenced from the
23431      pubname_table.  */
23432   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
23433     prune_unused_types_mark (pub->die, 1);
23434   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
23435     prune_unused_types_mark (base_type, 1);
23436
23437   if (debug_str_hash)
23438     htab_empty (debug_str_hash);
23439   prune_unused_types_prune (comp_unit_die ());
23440   for (node = limbo_die_list; node; node = node->next)
23441     prune_unused_types_prune (node->die);
23442   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23443     prune_unused_types_prune (ctnode->root_die);
23444
23445   /* Leave the marks clear.  */
23446   prune_unmark_dies (comp_unit_die ());
23447   for (node = limbo_die_list; node; node = node->next)
23448     prune_unmark_dies (node->die);
23449   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
23450     prune_unmark_dies (ctnode->root_die);
23451 }
23452
23453 /* Set the parameter to true if there are any relative pathnames in
23454    the file table.  */
23455 static int
23456 file_table_relative_p (void ** slot, void *param)
23457 {
23458   bool *p = (bool *) param;
23459   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
23460   if (!IS_ABSOLUTE_PATH (d->filename))
23461     {
23462       *p = true;
23463       return 0;
23464     }
23465   return 1;
23466 }
23467
23468 /* Routines to manipulate hash table of comdat type units.  */
23469
23470 static hashval_t
23471 htab_ct_hash (const void *of)
23472 {
23473   hashval_t h;
23474   const comdat_type_node *const type_node = (const comdat_type_node *) of;
23475
23476   memcpy (&h, type_node->signature, sizeof (h));
23477   return h;
23478 }
23479
23480 static int
23481 htab_ct_eq (const void *of1, const void *of2)
23482 {
23483   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
23484   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
23485
23486   return (! memcmp (type_node_1->signature, type_node_2->signature,
23487                     DWARF_TYPE_SIGNATURE_SIZE));
23488 }
23489
23490 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
23491    to the location it would have been added, should we know its
23492    DECL_ASSEMBLER_NAME when we added other attributes.  This will
23493    probably improve compactness of debug info, removing equivalent
23494    abbrevs, and hide any differences caused by deferring the
23495    computation of the assembler name, triggered by e.g. PCH.  */
23496
23497 static inline void
23498 move_linkage_attr (dw_die_ref die)
23499 {
23500   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
23501   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
23502
23503   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
23504               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
23505
23506   while (--ix > 0)
23507     {
23508       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
23509
23510       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
23511         break;
23512     }
23513
23514   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
23515     {
23516       VEC_pop (dw_attr_node, die->die_attr);
23517       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
23518     }
23519 }
23520
23521 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
23522    referenced from typed stack ops and count how often they are used.  */
23523
23524 static void
23525 mark_base_types (dw_loc_descr_ref loc)
23526 {
23527   dw_die_ref base_type = NULL;
23528
23529   for (; loc; loc = loc->dw_loc_next)
23530     {
23531       switch (loc->dw_loc_opc)
23532         {
23533         case DW_OP_GNU_regval_type:
23534         case DW_OP_GNU_deref_type:
23535           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
23536           break;
23537         case DW_OP_GNU_const_type:
23538         case DW_OP_GNU_convert:
23539         case DW_OP_GNU_reinterpret:
23540           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
23541           break;
23542         case DW_OP_GNU_entry_value:
23543           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
23544           continue;
23545         default:
23546           continue;
23547         }
23548       gcc_assert (base_type->die_parent == comp_unit_die ());
23549       if (base_type->die_mark)
23550         base_type->die_mark++;
23551       else
23552         {
23553           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
23554           base_type->die_mark = 1;
23555         }
23556     }
23557 }
23558
23559 /* Comparison function for sorting marked base types.  */
23560
23561 static int
23562 base_type_cmp (const void *x, const void *y)
23563 {
23564   dw_die_ref dx = *(const dw_die_ref *) x;
23565   dw_die_ref dy = *(const dw_die_ref *) y;
23566   unsigned int byte_size1, byte_size2;
23567   unsigned int encoding1, encoding2;
23568   if (dx->die_mark > dy->die_mark)
23569     return -1;
23570   if (dx->die_mark < dy->die_mark)
23571     return 1;
23572   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
23573   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
23574   if (byte_size1 < byte_size2)
23575     return 1;
23576   if (byte_size1 > byte_size2)
23577     return -1;
23578   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
23579   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
23580   if (encoding1 < encoding2)
23581     return 1;
23582   if (encoding1 > encoding2)
23583     return -1;
23584   return 0;
23585 }
23586
23587 /* Move base types marked by mark_base_types as early as possible
23588    in the CU, sorted by decreasing usage count both to make the
23589    uleb128 references as small as possible and to make sure they
23590    will have die_offset already computed by calc_die_sizes when
23591    sizes of typed stack loc ops is computed.  */
23592
23593 static void
23594 move_marked_base_types (void)
23595 {
23596   unsigned int i;
23597   dw_die_ref base_type, die, c;
23598
23599   if (VEC_empty (dw_die_ref, base_types))
23600     return;
23601
23602   /* Sort by decreasing usage count, they will be added again in that
23603      order later on.  */
23604   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
23605   die = comp_unit_die ();
23606   c = die->die_child;
23607   do
23608     {
23609       dw_die_ref prev = c;
23610       c = c->die_sib;
23611       while (c->die_mark)
23612         {
23613           remove_child_with_prev (c, prev);
23614           /* As base types got marked, there must be at least
23615              one node other than DW_TAG_base_type.  */
23616           gcc_assert (c != c->die_sib);
23617           c = c->die_sib;
23618         }
23619     }
23620   while (c != die->die_child);
23621   gcc_assert (die->die_child);
23622   c = die->die_child;
23623   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
23624     {
23625       base_type->die_mark = 0;
23626       base_type->die_sib = c->die_sib;
23627       c->die_sib = base_type;
23628       c = base_type;
23629     }
23630 }
23631
23632 /* Helper function for resolve_addr, attempt to resolve
23633    one CONST_STRING, return non-zero if not successful.  Similarly verify that
23634    SYMBOL_REFs refer to variables emitted in the current CU.  */
23635
23636 static int
23637 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
23638 {
23639   rtx rtl = *addr;
23640
23641   if (GET_CODE (rtl) == CONST_STRING)
23642     {
23643       size_t len = strlen (XSTR (rtl, 0)) + 1;
23644       tree t = build_string (len, XSTR (rtl, 0));
23645       tree tlen = size_int (len - 1);
23646       TREE_TYPE (t)
23647         = build_array_type (char_type_node, build_index_type (tlen));
23648       rtl = lookup_constant_def (t);
23649       if (!rtl || !MEM_P (rtl))
23650         return 1;
23651       rtl = XEXP (rtl, 0);
23652       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
23653       *addr = rtl;
23654       return 0;
23655     }
23656
23657   if (GET_CODE (rtl) == SYMBOL_REF
23658       && SYMBOL_REF_DECL (rtl))
23659     {
23660       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
23661         {
23662           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
23663             return 1;
23664         }
23665       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
23666         return 1;
23667     }
23668
23669   if (GET_CODE (rtl) == CONST
23670       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
23671     return 1;
23672
23673   return 0;
23674 }
23675
23676 /* Helper function for resolve_addr, handle one location
23677    expression, return false if at least one CONST_STRING or SYMBOL_REF in
23678    the location list couldn't be resolved.  */
23679
23680 static bool
23681 resolve_addr_in_expr (dw_loc_descr_ref loc)
23682 {
23683   for (; loc; loc = loc->dw_loc_next)
23684     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
23685          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
23686         || (loc->dw_loc_opc == DW_OP_implicit_value
23687             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
23688             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
23689       return false;
23690     else if (loc->dw_loc_opc == DW_OP_GNU_implicit_pointer
23691              && loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
23692       {
23693         dw_die_ref ref
23694           = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
23695         if (ref == NULL)
23696           return false;
23697         loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
23698         loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
23699         loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
23700       }
23701   return true;
23702 }
23703
23704 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
23705    an address in .rodata section if the string literal is emitted there,
23706    or remove the containing location list or replace DW_AT_const_value
23707    with DW_AT_location and empty location expression, if it isn't found
23708    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
23709    to something that has been emitted in the current CU.  */
23710
23711 static void
23712 resolve_addr (dw_die_ref die)
23713 {
23714   dw_die_ref c;
23715   dw_attr_ref a;
23716   dw_loc_list_ref *curr, *start, loc;
23717   unsigned ix;
23718
23719   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
23720     switch (AT_class (a))
23721       {
23722       case dw_val_class_loc_list:
23723         start = curr = AT_loc_list_ptr (a);
23724         loc = *curr;
23725         gcc_assert (loc);
23726         /* The same list can be referenced more than once.  See if we have
23727            already recorded the result from a previous pass.  */
23728         if (loc->replaced)
23729           *curr = loc->dw_loc_next;
23730         else if (!loc->resolved_addr)
23731           {
23732             /* As things stand, we do not expect or allow one die to
23733                reference a suffix of another die's location list chain.
23734                References must be identical or completely separate.
23735                There is therefore no need to cache the result of this
23736                pass on any list other than the first; doing so
23737                would lead to unnecessary writes.  */
23738             while (*curr)
23739               {
23740                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
23741                 if (!resolve_addr_in_expr ((*curr)->expr))
23742                   {
23743                     dw_loc_list_ref next = (*curr)->dw_loc_next;
23744                     if (next && (*curr)->ll_symbol)
23745                       {
23746                         gcc_assert (!next->ll_symbol);
23747                         next->ll_symbol = (*curr)->ll_symbol;
23748                       }
23749                     *curr = next;
23750                   }
23751                 else
23752                   {
23753                     mark_base_types ((*curr)->expr);
23754                     curr = &(*curr)->dw_loc_next;
23755                   }
23756               }
23757             if (loc == *start)
23758               loc->resolved_addr = 1;
23759             else
23760               {
23761                 loc->replaced = 1;
23762                 loc->dw_loc_next = *start;
23763               }
23764           }
23765         if (!*start)
23766           {
23767             remove_AT (die, a->dw_attr);
23768             ix--;
23769           }
23770         break;
23771       case dw_val_class_loc:
23772         if (!resolve_addr_in_expr (AT_loc (a)))
23773           {
23774             remove_AT (die, a->dw_attr);
23775             ix--;
23776           }
23777         else
23778           mark_base_types (AT_loc (a));
23779         break;
23780       case dw_val_class_addr:
23781         if (a->dw_attr == DW_AT_const_value
23782             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
23783           {
23784             remove_AT (die, a->dw_attr);
23785             ix--;
23786           }
23787         if (die->die_tag == DW_TAG_GNU_call_site
23788             && a->dw_attr == DW_AT_abstract_origin)
23789           {
23790             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
23791             dw_die_ref tdie = lookup_decl_die (tdecl);
23792             if (tdie == NULL
23793                 && DECL_EXTERNAL (tdecl)
23794                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
23795               {
23796                 force_decl_die (tdecl);
23797                 tdie = lookup_decl_die (tdecl);
23798               }
23799             if (tdie)
23800               {
23801                 a->dw_attr_val.val_class = dw_val_class_die_ref;
23802                 a->dw_attr_val.v.val_die_ref.die = tdie;
23803                 a->dw_attr_val.v.val_die_ref.external = 0;
23804               }
23805             else
23806               {
23807                 remove_AT (die, a->dw_attr);
23808                 ix--;
23809               }
23810           }
23811         break;
23812       default:
23813         break;
23814       }
23815
23816   FOR_EACH_CHILD (die, c, resolve_addr (c));
23817 }
23818 \f
23819 /* Helper routines for optimize_location_lists.
23820    This pass tries to share identical local lists in .debug_loc
23821    section.  */
23822
23823 /* Iteratively hash operands of LOC opcode.  */
23824
23825 static inline hashval_t
23826 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
23827 {
23828   dw_val_ref val1 = &loc->dw_loc_oprnd1;
23829   dw_val_ref val2 = &loc->dw_loc_oprnd2;
23830
23831   switch (loc->dw_loc_opc)
23832     {
23833     case DW_OP_const4u:
23834     case DW_OP_const8u:
23835       if (loc->dtprel)
23836         goto hash_addr;
23837       /* FALLTHRU */
23838     case DW_OP_const1u:
23839     case DW_OP_const1s:
23840     case DW_OP_const2u:
23841     case DW_OP_const2s:
23842     case DW_OP_const4s:
23843     case DW_OP_const8s:
23844     case DW_OP_constu:
23845     case DW_OP_consts:
23846     case DW_OP_pick:
23847     case DW_OP_plus_uconst:
23848     case DW_OP_breg0:
23849     case DW_OP_breg1:
23850     case DW_OP_breg2:
23851     case DW_OP_breg3:
23852     case DW_OP_breg4:
23853     case DW_OP_breg5:
23854     case DW_OP_breg6:
23855     case DW_OP_breg7:
23856     case DW_OP_breg8:
23857     case DW_OP_breg9:
23858     case DW_OP_breg10:
23859     case DW_OP_breg11:
23860     case DW_OP_breg12:
23861     case DW_OP_breg13:
23862     case DW_OP_breg14:
23863     case DW_OP_breg15:
23864     case DW_OP_breg16:
23865     case DW_OP_breg17:
23866     case DW_OP_breg18:
23867     case DW_OP_breg19:
23868     case DW_OP_breg20:
23869     case DW_OP_breg21:
23870     case DW_OP_breg22:
23871     case DW_OP_breg23:
23872     case DW_OP_breg24:
23873     case DW_OP_breg25:
23874     case DW_OP_breg26:
23875     case DW_OP_breg27:
23876     case DW_OP_breg28:
23877     case DW_OP_breg29:
23878     case DW_OP_breg30:
23879     case DW_OP_breg31:
23880     case DW_OP_regx:
23881     case DW_OP_fbreg:
23882     case DW_OP_piece:
23883     case DW_OP_deref_size:
23884     case DW_OP_xderef_size:
23885       hash = iterative_hash_object (val1->v.val_int, hash);
23886       break;
23887     case DW_OP_skip:
23888     case DW_OP_bra:
23889       {
23890         int offset;
23891
23892         gcc_assert (val1->val_class == dw_val_class_loc);
23893         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
23894         hash = iterative_hash_object (offset, hash);
23895       }
23896       break;
23897     case DW_OP_implicit_value:
23898       hash = iterative_hash_object (val1->v.val_unsigned, hash);
23899       switch (val2->val_class)
23900         {
23901         case dw_val_class_const:
23902           hash = iterative_hash_object (val2->v.val_int, hash);
23903           break;
23904         case dw_val_class_vec:
23905           {
23906             unsigned int elt_size = val2->v.val_vec.elt_size;
23907             unsigned int len = val2->v.val_vec.length;
23908
23909             hash = iterative_hash_object (elt_size, hash);
23910             hash = iterative_hash_object (len, hash);
23911             hash = iterative_hash (val2->v.val_vec.array,
23912                                    len * elt_size, hash);
23913           }
23914           break;
23915         case dw_val_class_const_double:
23916           hash = iterative_hash_object (val2->v.val_double.low, hash);
23917           hash = iterative_hash_object (val2->v.val_double.high, hash);
23918           break;
23919         case dw_val_class_addr:
23920           hash = iterative_hash_rtx (val2->v.val_addr, hash);
23921           break;
23922         default:
23923           gcc_unreachable ();
23924         }
23925       break;
23926     case DW_OP_bregx:
23927     case DW_OP_bit_piece:
23928       hash = iterative_hash_object (val1->v.val_int, hash);
23929       hash = iterative_hash_object (val2->v.val_int, hash);
23930       break;
23931     case DW_OP_addr:
23932     hash_addr:
23933       if (loc->dtprel)
23934         {
23935           unsigned char dtprel = 0xd1;
23936           hash = iterative_hash_object (dtprel, hash);
23937         }
23938       hash = iterative_hash_rtx (val1->v.val_addr, hash);
23939       break;
23940     case DW_OP_GNU_implicit_pointer:
23941       hash = iterative_hash_object (val2->v.val_int, hash);
23942       break;
23943     case DW_OP_GNU_entry_value:
23944       hash = hash_loc_operands (val1->v.val_loc, hash);
23945       break;
23946     case DW_OP_GNU_regval_type:
23947     case DW_OP_GNU_deref_type:
23948       {
23949         unsigned int byte_size
23950           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
23951         unsigned int encoding
23952           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
23953         hash = iterative_hash_object (val1->v.val_int, hash);
23954         hash = iterative_hash_object (byte_size, hash);
23955         hash = iterative_hash_object (encoding, hash);
23956       }
23957       break;
23958     case DW_OP_GNU_convert:
23959     case DW_OP_GNU_reinterpret:
23960     case DW_OP_GNU_const_type:
23961       {
23962         unsigned int byte_size
23963           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
23964         unsigned int encoding
23965           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
23966         hash = iterative_hash_object (byte_size, hash);
23967         hash = iterative_hash_object (encoding, hash);
23968         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
23969           break;
23970         hash = iterative_hash_object (val2->val_class, hash);
23971         switch (val2->val_class)
23972           {
23973           case dw_val_class_const:
23974             hash = iterative_hash_object (val2->v.val_int, hash);
23975             break;
23976           case dw_val_class_vec:
23977             {
23978               unsigned int elt_size = val2->v.val_vec.elt_size;
23979               unsigned int len = val2->v.val_vec.length;
23980
23981               hash = iterative_hash_object (elt_size, hash);
23982               hash = iterative_hash_object (len, hash);
23983               hash = iterative_hash (val2->v.val_vec.array,
23984                                      len * elt_size, hash);
23985             }
23986             break;
23987           case dw_val_class_const_double:
23988             hash = iterative_hash_object (val2->v.val_double.low, hash);
23989             hash = iterative_hash_object (val2->v.val_double.high, hash);
23990             break;
23991           default:
23992             gcc_unreachable ();
23993           }
23994       }
23995       break;
23996
23997     default:
23998       /* Other codes have no operands.  */
23999       break;
24000     }
24001   return hash;
24002 }
24003
24004 /* Iteratively hash the whole DWARF location expression LOC.  */
24005
24006 static inline hashval_t
24007 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
24008 {
24009   dw_loc_descr_ref l;
24010   bool sizes_computed = false;
24011   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
24012   size_of_locs (loc);
24013
24014   for (l = loc; l != NULL; l = l->dw_loc_next)
24015     {
24016       enum dwarf_location_atom opc = l->dw_loc_opc;
24017       hash = iterative_hash_object (opc, hash);
24018       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
24019         {
24020           size_of_locs (loc);
24021           sizes_computed = true;
24022         }
24023       hash = hash_loc_operands (l, hash);
24024     }
24025   return hash;
24026 }
24027
24028 /* Compute hash of the whole location list LIST_HEAD.  */
24029
24030 static inline void
24031 hash_loc_list (dw_loc_list_ref list_head)
24032 {
24033   dw_loc_list_ref curr = list_head;
24034   hashval_t hash = 0;
24035
24036   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
24037     {
24038       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
24039       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
24040       if (curr->section)
24041         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
24042                                hash);
24043       hash = hash_locs (curr->expr, hash);
24044     }
24045   list_head->hash = hash;
24046 }
24047
24048 /* Return true if X and Y opcodes have the same operands.  */
24049
24050 static inline bool
24051 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
24052 {
24053   dw_val_ref valx1 = &x->dw_loc_oprnd1;
24054   dw_val_ref valx2 = &x->dw_loc_oprnd2;
24055   dw_val_ref valy1 = &y->dw_loc_oprnd1;
24056   dw_val_ref valy2 = &y->dw_loc_oprnd2;
24057
24058   switch (x->dw_loc_opc)
24059     {
24060     case DW_OP_const4u:
24061     case DW_OP_const8u:
24062       if (x->dtprel)
24063         goto hash_addr;
24064       /* FALLTHRU */
24065     case DW_OP_const1u:
24066     case DW_OP_const1s:
24067     case DW_OP_const2u:
24068     case DW_OP_const2s:
24069     case DW_OP_const4s:
24070     case DW_OP_const8s:
24071     case DW_OP_constu:
24072     case DW_OP_consts:
24073     case DW_OP_pick:
24074     case DW_OP_plus_uconst:
24075     case DW_OP_breg0:
24076     case DW_OP_breg1:
24077     case DW_OP_breg2:
24078     case DW_OP_breg3:
24079     case DW_OP_breg4:
24080     case DW_OP_breg5:
24081     case DW_OP_breg6:
24082     case DW_OP_breg7:
24083     case DW_OP_breg8:
24084     case DW_OP_breg9:
24085     case DW_OP_breg10:
24086     case DW_OP_breg11:
24087     case DW_OP_breg12:
24088     case DW_OP_breg13:
24089     case DW_OP_breg14:
24090     case DW_OP_breg15:
24091     case DW_OP_breg16:
24092     case DW_OP_breg17:
24093     case DW_OP_breg18:
24094     case DW_OP_breg19:
24095     case DW_OP_breg20:
24096     case DW_OP_breg21:
24097     case DW_OP_breg22:
24098     case DW_OP_breg23:
24099     case DW_OP_breg24:
24100     case DW_OP_breg25:
24101     case DW_OP_breg26:
24102     case DW_OP_breg27:
24103     case DW_OP_breg28:
24104     case DW_OP_breg29:
24105     case DW_OP_breg30:
24106     case DW_OP_breg31:
24107     case DW_OP_regx:
24108     case DW_OP_fbreg:
24109     case DW_OP_piece:
24110     case DW_OP_deref_size:
24111     case DW_OP_xderef_size:
24112       return valx1->v.val_int == valy1->v.val_int;
24113     case DW_OP_skip:
24114     case DW_OP_bra:
24115       gcc_assert (valx1->val_class == dw_val_class_loc
24116                   && valy1->val_class == dw_val_class_loc
24117                   && x->dw_loc_addr == y->dw_loc_addr);
24118       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
24119     case DW_OP_implicit_value:
24120       if (valx1->v.val_unsigned != valy1->v.val_unsigned
24121           || valx2->val_class != valy2->val_class)
24122         return false;
24123       switch (valx2->val_class)
24124         {
24125         case dw_val_class_const:
24126           return valx2->v.val_int == valy2->v.val_int;
24127         case dw_val_class_vec:
24128           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24129                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24130                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24131                             valx2->v.val_vec.elt_size
24132                             * valx2->v.val_vec.length) == 0;
24133         case dw_val_class_const_double:
24134           return valx2->v.val_double.low == valy2->v.val_double.low
24135                  && valx2->v.val_double.high == valy2->v.val_double.high;
24136         case dw_val_class_addr:
24137           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
24138         default:
24139           gcc_unreachable ();
24140         }
24141     case DW_OP_bregx:
24142     case DW_OP_bit_piece:
24143       return valx1->v.val_int == valy1->v.val_int
24144              && valx2->v.val_int == valy2->v.val_int;
24145     case DW_OP_addr:
24146     hash_addr:
24147       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
24148     case DW_OP_GNU_implicit_pointer:
24149       return valx1->val_class == dw_val_class_die_ref
24150              && valx1->val_class == valy1->val_class
24151              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
24152              && valx2->v.val_int == valy2->v.val_int;
24153     case DW_OP_GNU_entry_value:
24154       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
24155     case DW_OP_GNU_const_type:
24156       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
24157           || valx2->val_class != valy2->val_class)
24158         return false;
24159       switch (valx2->val_class)
24160         {
24161         case dw_val_class_const:
24162           return valx2->v.val_int == valy2->v.val_int;
24163         case dw_val_class_vec:
24164           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
24165                  && valx2->v.val_vec.length == valy2->v.val_vec.length
24166                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
24167                             valx2->v.val_vec.elt_size
24168                             * valx2->v.val_vec.length) == 0;
24169         case dw_val_class_const_double:
24170           return valx2->v.val_double.low == valy2->v.val_double.low
24171                  && valx2->v.val_double.high == valy2->v.val_double.high;
24172         default:
24173           gcc_unreachable ();
24174         }
24175     case DW_OP_GNU_regval_type:
24176     case DW_OP_GNU_deref_type:
24177       return valx1->v.val_int == valy1->v.val_int
24178              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
24179     case DW_OP_GNU_convert:
24180     case DW_OP_GNU_reinterpret:
24181       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
24182     default:
24183       /* Other codes have no operands.  */
24184       return true;
24185     }
24186 }
24187
24188 /* Return true if DWARF location expressions X and Y are the same.  */
24189
24190 static inline bool
24191 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
24192 {
24193   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
24194     if (x->dw_loc_opc != y->dw_loc_opc
24195         || x->dtprel != y->dtprel
24196         || !compare_loc_operands (x, y))
24197       break;
24198   return x == NULL && y == NULL;
24199 }
24200
24201 /* Return precomputed hash of location list X.  */
24202
24203 static hashval_t
24204 loc_list_hash (const void *x)
24205 {
24206   return ((const struct dw_loc_list_struct *) x)->hash;
24207 }
24208
24209 /* Return 1 if location lists X and Y are the same.  */
24210
24211 static int
24212 loc_list_eq (const void *x, const void *y)
24213 {
24214   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
24215   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
24216   if (a == b)
24217     return 1;
24218   if (a->hash != b->hash)
24219     return 0;
24220   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
24221     if (strcmp (a->begin, b->begin) != 0
24222         || strcmp (a->end, b->end) != 0
24223         || (a->section == NULL) != (b->section == NULL)
24224         || (a->section && strcmp (a->section, b->section) != 0)
24225         || !compare_locs (a->expr, b->expr))
24226       break;
24227   return a == NULL && b == NULL;
24228 }
24229
24230 /* Recursively optimize location lists referenced from DIE
24231    children and share them whenever possible.  */
24232
24233 static void
24234 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
24235 {
24236   dw_die_ref c;
24237   dw_attr_ref a;
24238   unsigned ix;
24239   void **slot;
24240
24241   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
24242     if (AT_class (a) == dw_val_class_loc_list)
24243       {
24244         dw_loc_list_ref list = AT_loc_list (a);
24245         /* TODO: perform some optimizations here, before hashing
24246            it and storing into the hash table.  */
24247         hash_loc_list (list);
24248         slot = htab_find_slot_with_hash (htab, list, list->hash,
24249                                          INSERT);
24250         if (*slot == NULL)
24251           *slot = (void *) list;
24252         else
24253           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
24254       }
24255
24256   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
24257 }
24258
24259 /* Optimize location lists referenced from DIE
24260    children and share them whenever possible.  */
24261
24262 static void
24263 optimize_location_lists (dw_die_ref die)
24264 {
24265   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
24266   optimize_location_lists_1 (die, htab);
24267   htab_delete (htab);
24268 }
24269 \f
24270 /* Output stuff that dwarf requires at the end of every file,
24271    and generate the DWARF-2 debugging info.  */
24272
24273 static void
24274 dwarf2out_finish (const char *filename)
24275 {
24276   limbo_die_node *node, *next_node;
24277   comdat_type_node *ctnode;
24278   htab_t comdat_type_table;
24279   unsigned int i;
24280
24281   gen_scheduled_generic_parms_dies ();
24282   gen_remaining_tmpl_value_param_die_attribute ();
24283
24284   /* Add the name for the main input file now.  We delayed this from
24285      dwarf2out_init to avoid complications with PCH.  */
24286   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
24287   if (!IS_ABSOLUTE_PATH (filename))
24288     add_comp_dir_attribute (comp_unit_die ());
24289   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
24290     {
24291       bool p = false;
24292       htab_traverse (file_table, file_table_relative_p, &p);
24293       if (p)
24294         add_comp_dir_attribute (comp_unit_die ());
24295     }
24296
24297   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
24298     {
24299       add_location_or_const_value_attribute (
24300         VEC_index (deferred_locations, deferred_locations_list, i)->die,
24301         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
24302         false,
24303         DW_AT_location);
24304     }
24305
24306   /* Traverse the limbo die list, and add parent/child links.  The only
24307      dies without parents that should be here are concrete instances of
24308      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
24309      For concrete instances, we can get the parent die from the abstract
24310      instance.  */
24311   for (node = limbo_die_list; node; node = next_node)
24312     {
24313       dw_die_ref die = node->die;
24314       next_node = node->next;
24315
24316       if (die->die_parent == NULL)
24317         {
24318           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
24319
24320           if (origin)
24321             add_child_die (origin->die_parent, die);
24322           else if (is_cu_die (die))
24323             ;
24324           else if (seen_error ())
24325             /* It's OK to be confused by errors in the input.  */
24326             add_child_die (comp_unit_die (), die);
24327           else
24328             {
24329               /* In certain situations, the lexical block containing a
24330                  nested function can be optimized away, which results
24331                  in the nested function die being orphaned.  Likewise
24332                  with the return type of that nested function.  Force
24333                  this to be a child of the containing function.
24334
24335                  It may happen that even the containing function got fully
24336                  inlined and optimized out.  In that case we are lost and
24337                  assign the empty child.  This should not be big issue as
24338                  the function is likely unreachable too.  */
24339               tree context = NULL_TREE;
24340
24341               gcc_assert (node->created_for);
24342
24343               if (DECL_P (node->created_for))
24344                 context = DECL_CONTEXT (node->created_for);
24345               else if (TYPE_P (node->created_for))
24346                 context = TYPE_CONTEXT (node->created_for);
24347
24348               gcc_assert (context
24349                           && (TREE_CODE (context) == FUNCTION_DECL
24350                               || TREE_CODE (context) == NAMESPACE_DECL));
24351
24352               origin = lookup_decl_die (context);
24353               if (origin)
24354                 add_child_die (origin, die);
24355               else
24356                 add_child_die (comp_unit_die (), die);
24357             }
24358         }
24359     }
24360
24361   limbo_die_list = NULL;
24362
24363 #if ENABLE_ASSERT_CHECKING
24364   {
24365     dw_die_ref die = comp_unit_die (), c;
24366     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
24367   }
24368 #endif
24369   resolve_addr (comp_unit_die ());
24370   move_marked_base_types ();
24371
24372   for (node = deferred_asm_name; node; node = node->next)
24373     {
24374       tree decl = node->created_for;
24375       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
24376         {
24377           add_linkage_attr (node->die, decl);
24378           move_linkage_attr (node->die);
24379         }
24380     }
24381
24382   deferred_asm_name = NULL;
24383
24384   /* Walk through the list of incomplete types again, trying once more to
24385      emit full debugging info for them.  */
24386   retry_incomplete_types ();
24387
24388   if (flag_eliminate_unused_debug_types)
24389     prune_unused_types ();
24390
24391   /* Generate separate CUs for each of the include files we've seen.
24392      They will go into limbo_die_list.  */
24393   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
24394     break_out_includes (comp_unit_die ());
24395
24396   /* Generate separate COMDAT sections for type DIEs. */
24397   if (use_debug_types)
24398     {
24399       break_out_comdat_types (comp_unit_die ());
24400
24401       /* Each new type_unit DIE was added to the limbo die list when created.
24402          Since these have all been added to comdat_type_list, clear the
24403          limbo die list.  */
24404       limbo_die_list = NULL;
24405
24406       /* For each new comdat type unit, copy declarations for incomplete
24407          types to make the new unit self-contained (i.e., no direct
24408          references to the main compile unit).  */
24409       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24410         copy_decls_for_unworthy_types (ctnode->root_die);
24411       copy_decls_for_unworthy_types (comp_unit_die ());
24412
24413       /* In the process of copying declarations from one unit to another,
24414          we may have left some declarations behind that are no longer
24415          referenced.  Prune them.  */
24416       prune_unused_types ();
24417     }
24418
24419   /* Traverse the DIE's and add add sibling attributes to those DIE's
24420      that have children.  */
24421   add_sibling_attributes (comp_unit_die ());
24422   for (node = limbo_die_list; node; node = node->next)
24423     add_sibling_attributes (node->die);
24424   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24425     add_sibling_attributes (ctnode->root_die);
24426
24427   /* Output a terminator label for the .text section.  */
24428   switch_to_section (text_section);
24429   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
24430   if (cold_text_section)
24431     {
24432       switch_to_section (cold_text_section);
24433       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
24434     }
24435
24436   /* We can only use the low/high_pc attributes if all of the code was
24437      in .text.  */
24438   if (!have_multiple_function_sections 
24439       || (dwarf_version < 3 && dwarf_strict))
24440     {
24441       /* Don't add if the CU has no associated code.  */
24442       if (text_section_used)
24443         {
24444           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
24445           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
24446         }
24447     }
24448   else
24449     {
24450       unsigned fde_idx = 0;
24451       bool range_list_added = false;
24452
24453       if (text_section_used)
24454         add_ranges_by_labels (comp_unit_die (), text_section_label,
24455                               text_end_label, &range_list_added);
24456       if (cold_text_section_used)
24457         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
24458                               cold_end_label, &range_list_added);
24459
24460       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
24461         {
24462           dw_fde_ref fde = &fde_table[fde_idx];
24463
24464           if (!fde->in_std_section)
24465             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
24466                                   fde->dw_fde_end, &range_list_added);
24467           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
24468             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
24469                                   fde->dw_fde_second_end, &range_list_added);
24470         }
24471
24472       if (range_list_added)
24473         {
24474           /* We need to give .debug_loc and .debug_ranges an appropriate
24475              "base address".  Use zero so that these addresses become
24476              absolute.  Historically, we've emitted the unexpected
24477              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
24478              Emit both to give time for other tools to adapt.  */
24479           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
24480           if (! dwarf_strict && dwarf_version < 4)
24481             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
24482
24483           add_ranges (NULL);
24484         }
24485     }
24486
24487   if (debug_info_level >= DINFO_LEVEL_NORMAL)
24488     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
24489                     debug_line_section_label);
24490
24491   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24492     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
24493
24494   if (have_location_lists)
24495     optimize_location_lists (comp_unit_die ());
24496
24497   /* Output all of the compilation units.  We put the main one last so that
24498      the offsets are available to output_pubnames.  */
24499   for (node = limbo_die_list; node; node = node->next)
24500     output_comp_unit (node->die, 0);
24501
24502   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
24503   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
24504     {
24505       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
24506
24507       /* Don't output duplicate types.  */
24508       if (*slot != HTAB_EMPTY_ENTRY)
24509         continue;
24510
24511       /* Add a pointer to the line table for the main compilation unit
24512          so that the debugger can make sense of DW_AT_decl_file
24513          attributes.  */
24514       if (debug_info_level >= DINFO_LEVEL_NORMAL)
24515         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
24516                         debug_line_section_label);
24517
24518       output_comdat_type_unit (ctnode);
24519       *slot = ctnode;
24520     }
24521   htab_delete (comdat_type_table);
24522
24523   /* Output the main compilation unit if non-empty or if .debug_macinfo
24524      will be emitted.  */
24525   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
24526
24527   /* Output the abbreviation table.  */
24528   switch_to_section (debug_abbrev_section);
24529   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
24530   output_abbrev_section ();
24531
24532   /* Output location list section if necessary.  */
24533   if (have_location_lists)
24534     {
24535       /* Output the location lists info.  */
24536       switch_to_section (debug_loc_section);
24537       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
24538                                    DEBUG_LOC_SECTION_LABEL, 0);
24539       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
24540       output_location_lists (comp_unit_die ());
24541     }
24542
24543   /* Output public names table if necessary.  */
24544   if (!VEC_empty (pubname_entry, pubname_table))
24545     {
24546       gcc_assert (info_section_emitted);
24547       switch_to_section (debug_pubnames_section);
24548       output_pubnames (pubname_table);
24549     }
24550
24551   /* Output public types table if necessary.  */
24552   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
24553      It shouldn't hurt to emit it always, since pure DWARF2 consumers
24554      simply won't look for the section.  */
24555   if (!VEC_empty (pubname_entry, pubtype_table))
24556     {
24557       bool empty = false;
24558       
24559       if (flag_eliminate_unused_debug_types)
24560         {
24561           /* The pubtypes table might be emptied by pruning unused items.  */
24562           unsigned i;
24563           pubname_ref p;
24564           empty = true;
24565           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
24566             if (p->die->die_offset != 0)
24567               {
24568                 empty = false;
24569                 break;
24570               }
24571         }
24572       if (!empty)
24573         {
24574           gcc_assert (info_section_emitted);
24575           switch_to_section (debug_pubtypes_section);
24576           output_pubnames (pubtype_table);
24577         }
24578     }
24579
24580   /* Output the address range information if a CU (.debug_info section)
24581      was emitted.  We output an empty table even if we had no functions
24582      to put in it.  This because the consumer has no way to tell the
24583      difference between an empty table that we omitted and failure to
24584      generate a table that would have contained data.  */
24585   if (info_section_emitted)
24586     {
24587       unsigned long aranges_length = size_of_aranges ();
24588
24589       switch_to_section (debug_aranges_section);
24590       output_aranges (aranges_length);
24591     }
24592
24593   /* Output ranges section if necessary.  */
24594   if (ranges_table_in_use)
24595     {
24596       switch_to_section (debug_ranges_section);
24597       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
24598       output_ranges ();
24599     }
24600
24601   /* Output the source line correspondence table.  We must do this
24602      even if there is no line information.  Otherwise, on an empty
24603      translation unit, we will generate a present, but empty,
24604      .debug_info section.  IRIX 6.5 `nm' will then complain when
24605      examining the file.  This is done late so that any filenames
24606      used by the debug_info section are marked as 'used'.  */
24607   switch_to_section (debug_line_section);
24608   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
24609   if (! DWARF2_ASM_LINE_DEBUG_INFO)
24610     output_line_info ();
24611
24612   /* Have to end the macro section.  */
24613   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
24614     {
24615       switch_to_section (debug_macinfo_section);
24616       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
24617       if (!VEC_empty (macinfo_entry, macinfo_table))
24618         output_macinfo ();
24619       dw2_asm_output_data (1, 0, "End compilation unit");
24620     }
24621
24622   /* If we emitted any DW_FORM_strp form attribute, output the string
24623      table too.  */
24624   if (debug_str_hash)
24625     htab_traverse (debug_str_hash, output_indirect_string, NULL);
24626 }
24627
24628 #include "gt-dwarf2out.h"